add tests for using target_link_libraries() with imported managed targets

This commit is contained in:
Michael Stürmer
2018-03-20 12:22:02 +01:00
parent 43571073e0
commit 359544a907
18 changed files with 332 additions and 0 deletions

View File

@@ -435,4 +435,5 @@ endif()
if(${CMAKE_GENERATOR} MATCHES "Visual Studio ([^9]|9[0-9])")
add_RunCMake_test(CSharpCustomCommand)
add_RunCMake_test(CSharpReferenceImport)
endif()

View File

@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.3)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)

View File

@@ -0,0 +1,45 @@
enable_language(CXX CSharp)
if(NOT DEFINED exportFileName OR
NOT DEFINED exportNameSpace OR
NOT DEFINED exportTargetName)
message(FATAL_ERROR "export information missing")
endif()
add_library(${exportTargetName}CSharp SHARED
ImportLib.cs)
# native c++ dll
add_library(${exportTargetName}Native SHARED
ImportLibNative.h
ImportLibNative.cxx)
# mixed c++ dll
add_library(${exportTargetName}Mixed SHARED
ImportLibMixed.cxx
ImportLibMixedNative.h
ImportLibMixedNative.cxx)
set_target_properties(${exportTargetName}Mixed PROPERTIES
COMMON_LANGUAGE_RUNTIME "")
# pure c++ dll
add_library(${exportTargetName}Pure SHARED
ImportLibPure.cxx)
set_target_properties(${exportTargetName}Pure PROPERTIES
COMMON_LANGUAGE_RUNTIME "pure")
# safe c++ dll
add_library(${exportTargetName}Safe SHARED
ImportLibSafe.cxx)
set_target_properties(${exportTargetName}Safe PROPERTIES
COMMON_LANGUAGE_RUNTIME "safe")
# generate export file
export(TARGETS
${exportTargetName}CSharp
${exportTargetName}Native
${exportTargetName}Mixed
${exportTargetName}Pure
${exportTargetName}Safe
NAMESPACE "${exportNameSpace}:"
FILE "${exportFileName}")

View File

@@ -0,0 +1,8 @@
using System;
public
class ImportLibCSharp
{
public
static void Message() { Console.WriteLine("ImportLibCSharp"); }
}

View File

@@ -0,0 +1,8 @@
using namespace System;
public
ref class ImportLibMixed
{
public:
static void Message() { Console::WriteLine("ImportLibMixed"); }
};

View File

@@ -0,0 +1,8 @@
#include "ImportLibMixedNative.h"
#include <iostream>
void ImportLibMixedNative::Message()
{
std::cout << "ImportLibMixedNative" << std::endl;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#ifdef ImportLibMixed_EXPORTS
#define mixedAPI __declspec(dllexport)
#else
#define mixedAPI __declspec(dllimport)
#endif
class mixedAPI ImportLibMixedNative
{
public:
static void Message();
};

View File

@@ -0,0 +1,8 @@
#include "ImportLibNative.h"
#include <iostream>
void ImportLibNative::Message()
{
std::cout << "ImportLibNative" << std::endl;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#ifdef ImportLibNative_EXPORTS
#define nativeAPI __declspec(dllexport)
#else
#define nativeAPI __declspec(dllimport)
#endif
class nativeAPI ImportLibNative
{
public:
static void Message();
};

View File

@@ -0,0 +1,8 @@
using namespace System;
public
ref class ImportLibPure
{
public:
static void Message() { Console::WriteLine("ImportLibPure"); }
};

View File

@@ -0,0 +1,8 @@
using namespace System;
public
ref class ImportLibSafe
{
public:
static void Message() { Console::WriteLine("ImportLibSafe"); }
};

View File

@@ -0,0 +1,88 @@
enable_language(CXX CSharp)
if(NOT DEFINED exportFileName OR
NOT DEFINED exportNameSpace OR
NOT DEFINED exportTargetName)
message(FATAL_ERROR "export information missing")
endif()
# Include generated export file.
if(NOT EXISTS "${exportFileName}")
message(FATAL_ERROR "exportFileNameCSharp does not exist: ${exportFileName}")
endif()
include(${exportFileName})
# Verify expected targets are imported
set(linkNames linkNameCSharp linkNameNative linkNameMixed
linkNamePure linkNameSafe)
set(linkNameCSharp "${exportNameSpace}:${exportTargetName}CSharp")
set(linkNameNative "${exportNameSpace}:${exportTargetName}Native")
set(linkNameMixed "${exportNameSpace}:${exportTargetName}Mixed")
set(linkNamePure "${exportNameSpace}:${exportTargetName}Pure")
set(linkNameSafe "${exportNameSpace}:${exportTargetName}Safe")
foreach(l ${linkNames})
if(NOT TARGET ${${l}})
message(FATAL_ERROR "imported target not found (${${l}})")
endif()
endforeach()
# Test referencing managed assemblies from C# executable.
add_executable(ReferenceImportCSharp ReferenceImport.cs)
target_link_libraries(ReferenceImportCSharp
${linkNameCSharp}
${linkNameNative} # ignored
${linkNameMixed}
${linkNamePure}
${linkNameSafe}
)
# native C++ executable.
add_executable(ReferenceImportNative ReferenceImportNative.cxx)
target_link_libraries(ReferenceImportNative
${linkNameCSharp} # ignored
${linkNameNative}
${linkNameMixed}
${linkNamePure} # ignored
${linkNameSafe} # ignored
)
add_custom_command(TARGET ReferenceImportNative POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<TARGET_FILE:${linkNameNative}>"
"${CMAKE_BINARY_DIR}/$<CONFIG>"
)
# mixed C++ executable.
add_executable(ReferenceImportMixed ReferenceImportMixed.cxx)
target_link_libraries(ReferenceImportMixed
${linkNameCSharp}
${linkNameNative}
${linkNameMixed}
${linkNamePure}
${linkNameSafe}
)
set_target_properties(ReferenceImportMixed PROPERTIES
COMMON_LANGUAGE_RUNTIME "")
# pure C++ executable.
add_executable(ReferenceImportPure ReferenceImportPure.cxx)
target_link_libraries(ReferenceImportPure
${linkNameCSharp}
${linkNameNative} # ignored
${linkNameMixed}
${linkNamePure}
${linkNameSafe}
)
set_target_properties(ReferenceImportPure PROPERTIES
COMMON_LANGUAGE_RUNTIME "pure")
# native C++ executable.
add_executable(ReferenceImportSafe ReferenceImportSafe.cxx)
target_link_libraries(ReferenceImportSafe
${linkNameCSharp}
${linkNameNative} # ignored
${linkNameMixed}
${linkNamePure}
${linkNameSafe}
)
set_target_properties(ReferenceImportSafe PROPERTIES
COMMON_LANGUAGE_RUNTIME "safe")

View File

@@ -0,0 +1,13 @@
using System;
public class ReferenceImport
{
public static void Main()
{
Console.WriteLine("ReferenceImportCSharp");
ImportLibMixed.Message();
ImportLibPure.Message();
ImportLibSafe.Message();
ImportLibCSharp.Message();
}
}

View File

@@ -0,0 +1,20 @@
// clang-format off
using namespace System;
#using <ImportLibMixed.dll>
#using <ImportLibPure.dll>
#using <ImportLibSafe.dll>
#using <ImportLibCSharp.dll>
#include "ImportLibNative.h"
int main()
{
Console::WriteLine("ReferenceImportMixed");
ImportLibNative::Message();
ImportLibMixed::Message();
ImportLibPure::Message();
ImportLibSafe::Message();
ImportLibCSharp::Message();
};

View File

@@ -0,0 +1,13 @@
// clang-format off
#include "ImportLibNative.h"
#include "ImportLibMixedNative.h"
#include <iostream>
int main()
{
std::cout << "ReferenceImportNative" << std::endl;
ImportLibNative::Message();
ImportLibMixedNative::Message();
};

View File

@@ -0,0 +1,17 @@
// clang-format off
using namespace System;
#using <ImportLibMixed.dll>
#using <ImportLibPure.dll>
#using <ImportLibSafe.dll>
#using <ImportLibCSharp.dll>
int main()
{
Console::WriteLine("ReferenceImportPure");
ImportLibMixed::Message();
ImportLibPure::Message();
ImportLibSafe::Message();
ImportLibCSharp::Message();
};

View File

@@ -0,0 +1,17 @@
// clang-format off
using namespace System;
#using <ImportLibMixed.dll>
#using <ImportLibPure.dll>
#using <ImportLibSafe.dll>
#using <ImportLibCSharp.dll>
int main()
{
Console::WriteLine("ReferenceImportSafe");
ImportLibMixed::Message();
ImportLibPure::Message();
ImportLibSafe::Message();
ImportLibCSharp::Message();
};

View File

@@ -0,0 +1,41 @@
include(RunCMake)
set(RunCMake_TEST_NO_CLEAN 1)
set(exportFileName "${RunCMake_BINARY_DIR}/module.cmake")
set(exportNameSpace "ex")
set(exportTargetName "ImportLib")
set(RunCMake_TEST_OPTIONS_BASE ${RunCMake_TEST_OPTIONS}
"-DexportNameSpace:INTERNAL=${exportNameSpace}"
"-DexportTargetName:INTERNAL=${exportTargetName}")
file(REMOVE "${exportFileName}")
# generate C# & C++ assemblies for use as imported target
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ImportLib-build)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
set(RunCMake_TEST_OPTIONS ${RunCMake_TEST_OPTIONS_BASE}
"-DexportFileName:INTERNAL=${exportFileName}"
# make sure we know the RunCMake_TEST if configuring the project again
# with cmake-gui for debugging.
"-DRunCMake_TEST:INTERNAL=ImportLib")
run_cmake(ImportLib)
run_cmake_command(ImportLib-build ${CMAKE_COMMAND} --build . --config Debug)
# generate C# & managed C++ programs which reference imported managed assemblies.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/ReferenceImport-build)
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
set(RunCMake_TEST_OPTIONS ${RunCMake_TEST_OPTIONS_BASE}
"-DexportFileName:INTERNAL=${exportFileName}"
# make sure we know the RunCMake_TEST if configuring the project again
# with cmake-gui for debugging.
"-DRunCMake_TEST:INTERNAL=ReferenceImport")
run_cmake(ReferenceImport)
run_cmake_command(ReferenceImport-build ${CMAKE_COMMAND} --build . --config Debug)