summaryrefslogtreecommitdiffstats
path: root/Tests/RunCMake/try_run/LinkOptions.cmake
diff options
context:
space:
mode:
authorMarc Chevrier <marc.chevrier@gmail.com>2018-11-19 16:23:45 (GMT)
committerMarc Chevrier <marc.chevrier@gmail.com>2018-12-01 16:56:23 (GMT)
commit29f9db5c63dbfa53acdb449fad78d716a4113a88 (patch)
tree6b6ba4b75a7e906a455024292fe9f5f25299c8a1 /Tests/RunCMake/try_run/LinkOptions.cmake
parent78c2edb129594b198157799b3eb327f4a3915908 (diff)
downloadCMake-29f9db5c63dbfa53acdb449fad78d716a4113a88.zip
CMake-29f9db5c63dbfa53acdb449fad78d716a4113a88.tar.gz
CMake-29f9db5c63dbfa53acdb449fad78d716a4113a88.tar.bz2
try_compile/try_run: Add support for LINK_OPTIONS option.
Diffstat (limited to 'Tests/RunCMake/try_run/LinkOptions.cmake')
-rw-r--r--Tests/RunCMake/try_run/LinkOptions.cmake42
1 files changed, 42 insertions, 0 deletions
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()