From 359544a9074a26c3d5d7801932c809048763c341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20St=C3=BCrmer?= Date: Tue, 20 Mar 2018 12:22:02 +0100 Subject: add tests for using target_link_libraries() with imported managed targets --- Tests/RunCMake/CMakeLists.txt | 1 + .../RunCMake/CSharpReferenceImport/CMakeLists.txt | 3 + .../RunCMake/CSharpReferenceImport/ImportLib.cmake | 45 +++++++++++ Tests/RunCMake/CSharpReferenceImport/ImportLib.cs | 8 ++ .../CSharpReferenceImport/ImportLibMixed.cxx | 8 ++ .../CSharpReferenceImport/ImportLibMixedNative.cxx | 8 ++ .../CSharpReferenceImport/ImportLibMixedNative.h | 13 ++++ .../CSharpReferenceImport/ImportLibNative.cxx | 8 ++ .../CSharpReferenceImport/ImportLibNative.h | 13 ++++ .../CSharpReferenceImport/ImportLibPure.cxx | 8 ++ .../CSharpReferenceImport/ImportLibSafe.cxx | 8 ++ .../CSharpReferenceImport/ReferenceImport.cmake | 88 ++++++++++++++++++++++ .../CSharpReferenceImport/ReferenceImport.cs | 13 ++++ .../CSharpReferenceImport/ReferenceImportMixed.cxx | 20 +++++ .../ReferenceImportNative.cxx | 13 ++++ .../CSharpReferenceImport/ReferenceImportPure.cxx | 17 +++++ .../CSharpReferenceImport/ReferenceImportSafe.cxx | 17 +++++ .../CSharpReferenceImport/RunCMakeTest.cmake | 41 ++++++++++ 18 files changed, 332 insertions(+) create mode 100644 Tests/RunCMake/CSharpReferenceImport/CMakeLists.txt create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLib.cmake create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLib.cs create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibMixed.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.h create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibNative.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibNative.h create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibPure.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ImportLibSafe.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cmake create mode 100644 Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cs create mode 100644 Tests/RunCMake/CSharpReferenceImport/ReferenceImportMixed.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ReferenceImportNative.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ReferenceImportPure.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/ReferenceImportSafe.cxx create mode 100644 Tests/RunCMake/CSharpReferenceImport/RunCMakeTest.cmake diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 44a8c45..d557252 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -435,4 +435,5 @@ endif() if(${CMAKE_GENERATOR} MATCHES "Visual Studio ([^9]|9[0-9])") add_RunCMake_test(CSharpCustomCommand) + add_RunCMake_test(CSharpReferenceImport) endif() diff --git a/Tests/RunCMake/CSharpReferenceImport/CMakeLists.txt b/Tests/RunCMake/CSharpReferenceImport/CMakeLists.txt new file mode 100644 index 0000000..74b3ff8 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.3) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLib.cmake b/Tests/RunCMake/CSharpReferenceImport/ImportLib.cmake new file mode 100644 index 0000000..5ad6e76 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLib.cmake @@ -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}") diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLib.cs b/Tests/RunCMake/CSharpReferenceImport/ImportLib.cs new file mode 100644 index 0000000..4eb28af --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLib.cs @@ -0,0 +1,8 @@ +using System; + +public +class ImportLibCSharp +{ +public + static void Message() { Console.WriteLine("ImportLibCSharp"); } +} diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibMixed.cxx b/Tests/RunCMake/CSharpReferenceImport/ImportLibMixed.cxx new file mode 100644 index 0000000..d0b810e --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibMixed.cxx @@ -0,0 +1,8 @@ +using namespace System; + +public +ref class ImportLibMixed +{ +public: + static void Message() { Console::WriteLine("ImportLibMixed"); } +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.cxx b/Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.cxx new file mode 100644 index 0000000..c85a776 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.cxx @@ -0,0 +1,8 @@ +#include "ImportLibMixedNative.h" + +#include + +void ImportLibMixedNative::Message() +{ + std::cout << "ImportLibMixedNative" << std::endl; +} diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.h b/Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.h new file mode 100644 index 0000000..8d5eb62 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibMixedNative.h @@ -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(); +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibNative.cxx b/Tests/RunCMake/CSharpReferenceImport/ImportLibNative.cxx new file mode 100644 index 0000000..8e08b9a --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibNative.cxx @@ -0,0 +1,8 @@ +#include "ImportLibNative.h" + +#include + +void ImportLibNative::Message() +{ + std::cout << "ImportLibNative" << std::endl; +} diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibNative.h b/Tests/RunCMake/CSharpReferenceImport/ImportLibNative.h new file mode 100644 index 0000000..7c85626 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibNative.h @@ -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(); +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibPure.cxx b/Tests/RunCMake/CSharpReferenceImport/ImportLibPure.cxx new file mode 100644 index 0000000..271e375 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibPure.cxx @@ -0,0 +1,8 @@ +using namespace System; + +public +ref class ImportLibPure +{ +public: + static void Message() { Console::WriteLine("ImportLibPure"); } +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ImportLibSafe.cxx b/Tests/RunCMake/CSharpReferenceImport/ImportLibSafe.cxx new file mode 100644 index 0000000..13b40da --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ImportLibSafe.cxx @@ -0,0 +1,8 @@ +using namespace System; + +public +ref class ImportLibSafe +{ +public: + static void Message() { Console::WriteLine("ImportLibSafe"); } +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cmake b/Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cmake new file mode 100644 index 0000000..c65f623 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cmake @@ -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 + "$" + "${CMAKE_BINARY_DIR}/$" + ) + +# 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") diff --git a/Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cs b/Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cs new file mode 100644 index 0000000..24175f6 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ReferenceImport.cs @@ -0,0 +1,13 @@ +using System; + +public class ReferenceImport +{ + public static void Main() + { + Console.WriteLine("ReferenceImportCSharp"); + ImportLibMixed.Message(); + ImportLibPure.Message(); + ImportLibSafe.Message(); + ImportLibCSharp.Message(); + } +} diff --git a/Tests/RunCMake/CSharpReferenceImport/ReferenceImportMixed.cxx b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportMixed.cxx new file mode 100644 index 0000000..53ecd42 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportMixed.cxx @@ -0,0 +1,20 @@ +// clang-format off + +using namespace System; + +#using +#using +#using +#using + +#include "ImportLibNative.h" + +int main() +{ + Console::WriteLine("ReferenceImportMixed"); + ImportLibNative::Message(); + ImportLibMixed::Message(); + ImportLibPure::Message(); + ImportLibSafe::Message(); + ImportLibCSharp::Message(); +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ReferenceImportNative.cxx b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportNative.cxx new file mode 100644 index 0000000..831a2c4 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportNative.cxx @@ -0,0 +1,13 @@ +// clang-format off + +#include "ImportLibNative.h" +#include "ImportLibMixedNative.h" + +#include + +int main() +{ + std::cout << "ReferenceImportNative" << std::endl; + ImportLibNative::Message(); + ImportLibMixedNative::Message(); +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ReferenceImportPure.cxx b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportPure.cxx new file mode 100644 index 0000000..2c5dd66 --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportPure.cxx @@ -0,0 +1,17 @@ +// clang-format off + +using namespace System; + +#using +#using +#using +#using + +int main() +{ + Console::WriteLine("ReferenceImportPure"); + ImportLibMixed::Message(); + ImportLibPure::Message(); + ImportLibSafe::Message(); + ImportLibCSharp::Message(); +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/ReferenceImportSafe.cxx b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportSafe.cxx new file mode 100644 index 0000000..277c96f --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/ReferenceImportSafe.cxx @@ -0,0 +1,17 @@ +// clang-format off + +using namespace System; + +#using +#using +#using +#using + +int main() +{ + Console::WriteLine("ReferenceImportSafe"); + ImportLibMixed::Message(); + ImportLibPure::Message(); + ImportLibSafe::Message(); + ImportLibCSharp::Message(); +}; diff --git a/Tests/RunCMake/CSharpReferenceImport/RunCMakeTest.cmake b/Tests/RunCMake/CSharpReferenceImport/RunCMakeTest.cmake new file mode 100644 index 0000000..c44e59e --- /dev/null +++ b/Tests/RunCMake/CSharpReferenceImport/RunCMakeTest.cmake @@ -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) -- cgit v0.12