diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2018-11-19 16:23:45 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2018-12-01 16:56:23 (GMT) |
commit | 29f9db5c63dbfa53acdb449fad78d716a4113a88 (patch) | |
tree | 6b6ba4b75a7e906a455024292fe9f5f25299c8a1 /Tests | |
parent | 78c2edb129594b198157799b3eb327f4a3915908 (diff) | |
download | CMake-29f9db5c63dbfa53acdb449fad78d716a4113a88.zip CMake-29f9db5c63dbfa53acdb449fad78d716a4113a88.tar.gz CMake-29f9db5c63dbfa53acdb449fad78d716a4113a88.tar.bz2 |
try_compile/try_run: Add support for LINK_OPTIONS option.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/RunCMake/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/LinkOptions.cmake | 38 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/RunCMakeTest.cmake | 7 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/lib.c | 4 | ||||
-rw-r--r-- | Tests/RunCMake/try_compile/main.c | 8 | ||||
-rw-r--r-- | Tests/RunCMake/try_run/LinkOptions.cmake | 42 | ||||
-rw-r--r-- | Tests/RunCMake/try_run/RunCMakeTest.cmake | 7 | ||||
-rw-r--r-- | Tests/RunCMake/try_run/lib.c | 4 | ||||
-rw-r--r-- | Tests/RunCMake/try_run/main.c | 8 |
9 files changed, 121 insertions, 1 deletions
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index a4d829b..b044757 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -275,6 +275,7 @@ function(add_RunCMake_test_try_compile) endif() endif() foreach(var + CMAKE_SYSTEM_NAME CMAKE_C_COMPILER_ID CMAKE_C_COMPILER_VERSION CMAKE_C_STANDARD_DEFAULT @@ -291,7 +292,8 @@ function(add_RunCMake_test_try_compile) endfunction() add_RunCMake_test_try_compile() -add_RunCMake_test(try_run) +add_RunCMake_test(try_run -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} + -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}) add_RunCMake_test(set) add_RunCMake_test(variable_watch) add_RunCMake_test(while) diff --git a/Tests/RunCMake/try_compile/LinkOptions.cmake b/Tests/RunCMake/try_compile/LinkOptions.cmake new file mode 100644 index 0000000..9b246c4 --- /dev/null +++ b/Tests/RunCMake/try_compile/LinkOptions.cmake @@ -0,0 +1,38 @@ + +enable_language(C) + +cmake_policy(SET CMP0054 NEW) + +set (lib_name "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}lib${CMAKE_STATIC_LIBRARY_SUFFIX}") +if (CMAKE_SYSTEM_NAME STREQUAL "Windows") + if (RunCMake_C_COMPILER_ID STREQUAL "MSVC") + if (CMAKE_SIZEOF_VOID_P EQUAL 4) + set (undef_flag /INCLUDE:_func) + else() + set (undef_flag /INCLUDE:func) + endif() + else() + if (CMAKE_SIZEOF_VOID_P EQUAL 4) + set (undef_flag -u _func) + else() + set (undef_flag -u func) + endif() + endif() +elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set (undef_flag -u _func) +else() + set (undef_flag -u func) +endif() + +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +try_compile(result ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib.c + COPY_FILE "${lib_name}") + +set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) +try_compile(result ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/main.c + OUTPUT_VARIABLE out + LINK_OPTIONS ${undef_flag} "${lib_name}") + +if(NOT result) + message(FATAL_ERROR "try_compile(... LINK_OPTIONS ...) failed:\n${out}") +endif() diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index 6a1bc64..77fb7a0 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -25,6 +25,13 @@ run_cmake(TargetTypeExe) run_cmake(TargetTypeInvalid) run_cmake(TargetTypeStatic) +if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$" AND + CMAKE_C_COMPILER_ID MATCHES "^(MSVC|GNU|Clang|AppleClang)$") + set (RunCMake_TEST_OPTIONS -DRunCMake_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}) + run_cmake(LinkOptions) + unset (RunCMake_TEST_OPTIONS) +endif() + if(CMAKE_C_STANDARD_DEFAULT) run_cmake(CStandard) elseif(DEFINED CMAKE_C_STANDARD_DEFAULT) diff --git a/Tests/RunCMake/try_compile/lib.c b/Tests/RunCMake/try_compile/lib.c new file mode 100644 index 0000000..b00c576 --- /dev/null +++ b/Tests/RunCMake/try_compile/lib.c @@ -0,0 +1,4 @@ + +void func() +{ +} diff --git a/Tests/RunCMake/try_compile/main.c b/Tests/RunCMake/try_compile/main.c new file mode 100644 index 0000000..2128ead --- /dev/null +++ b/Tests/RunCMake/try_compile/main.c @@ -0,0 +1,8 @@ +extern void func(); + +int main(void) +{ + func(); + + return 0; +} diff --git a/Tests/RunCMake/try_run/LinkOptions.cmake b/Tests/RunCMake/try_run/LinkOptions.cmake new file mode 100644 index 0000000..17af2f7 --- /dev/null +++ b/Tests/RunCMake/try_run/LinkOptions.cmake @@ -0,0 +1,42 @@ + +enable_language(C) + +cmake_policy(SET CMP0054 NEW) + +set (lib_name "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}lib${CMAKE_STATIC_LIBRARY_SUFFIX}") +if (CMAKE_SYSTEM_NAME STREQUAL "Windows") + if (RunCMake_C_COMPILER_ID STREQUAL "MSVC") + if (CMAKE_SIZEOF_VOID_P EQUAL 4) + set (undef_flag /INCLUDE:_func) + else() + set (undef_flag /INCLUDE:func) + endif() + else() + if (CMAKE_SIZEOF_VOID_P EQUAL 4) + set (undef_flag -u _func) + else() + set (undef_flag -u func) + endif() + endif() +elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set (undef_flag -u _func) +else() + set (undef_flag -u func) +endif() + +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +try_compile(result ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib.c + COPY_FILE "${lib_name}") + +set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE) +try_run(run_result compile_result ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/main.c + COMPILE_OUTPUT_VARIABLE compile_out + RUN_OUTPUT_VARIABLE run_out + LINK_OPTIONS ${undef_flag} "${lib_name}") + +if(NOT compile_result) + message(FATAL_ERROR "try_run(... LINK_OPTIONS ...) compilation failed:\n${compile_out}") +endif() +if(run_result STREQUAL "FAILED_TO_RUN") + message(FATAL_ERROR "try_run(... LINK_OPTIONS ...) execution failed:\n${run_out}") +endif() diff --git a/Tests/RunCMake/try_run/RunCMakeTest.cmake b/Tests/RunCMake/try_run/RunCMakeTest.cmake index 1ec9a55..3689562 100644 --- a/Tests/RunCMake/try_run/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_run/RunCMakeTest.cmake @@ -1,3 +1,10 @@ include(RunCMake) run_cmake(BadLinkLibraries) + +if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$" AND + CMAKE_C_COMPILER_ID MATCHES "^(MSVC|GNU|Clang|AppleClang)$") + set (RunCMake_TEST_OPTIONS -DRunCMake_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}) + run_cmake(LinkOptions) + unset (RunCMake_TEST_OPTIONS) +endif() diff --git a/Tests/RunCMake/try_run/lib.c b/Tests/RunCMake/try_run/lib.c new file mode 100644 index 0000000..b00c576 --- /dev/null +++ b/Tests/RunCMake/try_run/lib.c @@ -0,0 +1,4 @@ + +void func() +{ +} diff --git a/Tests/RunCMake/try_run/main.c b/Tests/RunCMake/try_run/main.c new file mode 100644 index 0000000..2128ead --- /dev/null +++ b/Tests/RunCMake/try_run/main.c @@ -0,0 +1,8 @@ +extern void func(); + +int main(void) +{ + func(); + + return 0; +} |