diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2018-10-02 15:34:57 (GMT) |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2018-11-11 16:34:09 (GMT) |
commit | c4b4d8b3a67718e29edb5676273e528dab566672 (patch) | |
tree | ddb8839ae8a5a15b5ffe8dd8190cf873841475da /Tests | |
parent | 724a0346f7bd424ce0e5db246cee46db9f377a6f (diff) | |
download | CMake-c4b4d8b3a67718e29edb5676273e528dab566672.zip CMake-c4b4d8b3a67718e29edb5676273e528dab566672.tar.gz CMake-c4b4d8b3a67718e29edb5676273e528dab566672.tar.bz2 |
POSITION_INDEPENDENT_CODE: Manage link flags for executables
Fixes: #14983, #16561
Diffstat (limited to 'Tests')
11 files changed, 229 insertions, 1 deletions
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index e23926f..b6b6519 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -190,7 +190,8 @@ add_RunCMake_test(PolicyScope) add_RunCMake_test(WriteCompilerDetectionHeader) add_RunCMake_test(SourceProperties) if(NOT WIN32) - add_RunCMake_test(PositionIndependentCode) + add_RunCMake_test(PositionIndependentCode -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} + -DCMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}) endif() if(NOT CMAKE_GENERATOR MATCHES "Visual Studio") add_RunCMake_test(VisibilityPreset) diff --git a/Tests/RunCMake/PositionIndependentCode/CMP0083-cmp0083_new-check.cmake b/Tests/RunCMake/PositionIndependentCode/CMP0083-cmp0083_new-check.cmake new file mode 100644 index 0000000..255e63d --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/CMP0083-cmp0083_new-check.cmake @@ -0,0 +1,22 @@ + +include ("${RunCMake_TEST_BINARY_DIR}/${RunCMake_TEST_CONFIG}/CMP0083_config.cmake") + + +# retrieve default type of executable +check_executable ("${cmp0083_ref}" ref) + +if (ref STREQUAL "PIE") + # check no_pie executable is really no position independent + check_executable ("${cmp0083_new_no_pie}" new_no_pie) + if (NOT new_no_pie STREQUAL "NO_PIE") + set (RunCMake_TEST_FAILED "CMP0083(NEW) do not produce expected executable.") + endif() +elseif (ref STREQUAL "NO_PIE") + # check pie executable is really position independent + check_executable ("${cmp0083_new_pie}" new_pie) + if (NOT new_pie MATCHES "PIE") + set (RunCMake_TEST_FAILED "CMP0083(NEW) do not produce expected executable.") + endif() +else() + set (RunCMake_TEST_FAILED "CMP0083(NEW) unexpected result.") +endif() diff --git a/Tests/RunCMake/PositionIndependentCode/CMP0083-cmp0083_old-check.cmake b/Tests/RunCMake/PositionIndependentCode/CMP0083-cmp0083_old-check.cmake new file mode 100644 index 0000000..b66b672 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/CMP0083-cmp0083_old-check.cmake @@ -0,0 +1,20 @@ + +include ("${RunCMake_TEST_BINARY_DIR}/${RunCMake_TEST_CONFIG}/CMP0083_config.cmake") + + +# retrieve default type of executable +check_executable ("${cmp0083_ref}" ref) + +# POSITION_INDEPENDENT_CODE must not have influence on executable +# pie and no_pie executable must have same type as reference +check_executable ("${cmp0083_old_pie}" old_pie) +if (NOT old_pie STREQUAL ref) + set (RunCMake_TEST_FAILED "CMP0083(OLD) do not produce expected executable.") + return() +endif() + +check_executable ("${cmp0083_old_no_pie}" old_no_pie) +if (NOT old_no_pie STREQUAL ref) + set (RunCMake_TEST_FAILED "CMP0083(OLD) do not produce expected executable.") + return() +endif() diff --git a/Tests/RunCMake/PositionIndependentCode/CMP0083.cmake b/Tests/RunCMake/PositionIndependentCode/CMP0083.cmake new file mode 100644 index 0000000..9713ea4 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/CMP0083.cmake @@ -0,0 +1,45 @@ + +# create reference to detect default : PIE or not +add_executable (cmp0083_ref main.cpp) + + +set (CMAKE_POSITION_INDEPENDENT_CODE ON) + +cmake_policy(SET CMP0083 NEW) +add_executable (cmp0083_new_pie main.cpp) + + +cmake_policy(SET CMP0083 OLD) +add_executable (cmp0083_old_pie main.cpp) + + +set (CMAKE_POSITION_INDEPENDENT_CODE OFF) + +cmake_policy(SET CMP0083 NEW) +add_executable (cmp0083_new_no_pie main.cpp) + + +cmake_policy(SET CMP0083 OLD) +add_executable (cmp0083_old_no_pie main.cpp) + +# high-level targets +add_custom_target(cmp0083_new) +add_dependencies(cmp0083_new cmp0083_ref cmp0083_new_pie cmp0083_new_no_pie) + +# high-level targets +add_custom_target(cmp0083_old) +add_dependencies(cmp0083_old cmp0083_ref cmp0083_old_pie cmp0083_old_no_pie) + + +# generate file holding paths to executables +file (GENERATE OUTPUT "${CMAKE_BINARY_DIR}/$<CONFIG>/CMP0083_config.cmake" + CONTENT +[==[ +include ("${RunCMake_TEST_SOURCE_DIR}/PIE_validator.cmake") + +set (cmp0083_ref "$<TARGET_FILE:cmp0083_ref>") +set (cmp0083_new_pie "$<TARGET_FILE:cmp0083_new_pie>") +set (cmp0083_old_pie "$<TARGET_FILE:cmp0083_old_pie>") +set (cmp0083_new_no_pie "$<TARGET_FILE:cmp0083_new_no_pie>") +set (cmp0083_old_no_pie "$<TARGET_FILE:cmp0083_old_no_pie>") +]==]) diff --git a/Tests/RunCMake/PositionIndependentCode/CheckPIESupported.cmake b/Tests/RunCMake/PositionIndependentCode/CheckPIESupported.cmake new file mode 100644 index 0000000..1e0a2c9 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/CheckPIESupported.cmake @@ -0,0 +1,12 @@ + +if (CMAKE_CXX_LINK_OPTIONS_PIE) + file(WRITE "${PIE_SUPPORTED}" "\nset(PIE_SUPPORTED TRUE)\n") +else() + file(WRITE "${PIE_SUPPORTED}" "\nset(PIE_SUPPORTED FALSE)\n") +endif() + +if (CMAKE_CXX_LINK_OPTIONS_NO_PIE) + file(APPEND "${PIE_SUPPORTED}" "\nset(NO_PIE_SUPPORTED TRUE)\n") +else() + file(APPEND "${PIE_SUPPORTED}" "\nset(NO_PIE_SUPPORTED FALSE)\n") +endif() diff --git a/Tests/RunCMake/PositionIndependentCode/PIE-pie_off-check.cmake b/Tests/RunCMake/PositionIndependentCode/PIE-pie_off-check.cmake new file mode 100644 index 0000000..096395c --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/PIE-pie_off-check.cmake @@ -0,0 +1,7 @@ + +include ("${RunCMake_TEST_BINARY_DIR}/${RunCMake_TEST_CONFIG}/PIE_config.cmake") + +check_executable ("${pie_off}" status) +if (NOT status STREQUAL "NO_PIE") + set (RunCMake_TEST_FAILED "Executable is NOT 'no PIE' (${status}).") +endif() diff --git a/Tests/RunCMake/PositionIndependentCode/PIE-pie_on-check.cmake b/Tests/RunCMake/PositionIndependentCode/PIE-pie_on-check.cmake new file mode 100644 index 0000000..bf3d018 --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/PIE-pie_on-check.cmake @@ -0,0 +1,7 @@ + +include ("${RunCMake_TEST_BINARY_DIR}/${RunCMake_TEST_CONFIG}/PIE_config.cmake") + +check_executable ("${pie_on}" status) +if (NOT status STREQUAL "PIE") + set (RunCMake_TEST_FAILED "Executable is NOT 'PIE' (${status}).") +endif() diff --git a/Tests/RunCMake/PositionIndependentCode/PIE.cmake b/Tests/RunCMake/PositionIndependentCode/PIE.cmake new file mode 100644 index 0000000..a9d579d --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/PIE.cmake @@ -0,0 +1,19 @@ + +cmake_policy(SET CMP0083 NEW) + +add_executable (pie_on main.cpp) +set_property(TARGET pie_on PROPERTY POSITION_INDEPENDENT_CODE ON) + +add_executable (pie_off main.cpp) +set_property(TARGET pie_off PROPERTY POSITION_INDEPENDENT_CODE OFF) + + +# generate file holding paths to executables +file (GENERATE OUTPUT "${CMAKE_BINARY_DIR}/$<CONFIG>/PIE_config.cmake" + CONTENT +[==[ +include ("${RunCMake_TEST_SOURCE_DIR}/PIE_validator.cmake") + +set (pie_on "$<TARGET_FILE:pie_on>") +set (pie_off "$<TARGET_FILE:pie_off>") +]==]) diff --git a/Tests/RunCMake/PositionIndependentCode/PIE_validator.cmake b/Tests/RunCMake/PositionIndependentCode/PIE_validator.cmake new file mode 100644 index 0000000..7be35db --- /dev/null +++ b/Tests/RunCMake/PositionIndependentCode/PIE_validator.cmake @@ -0,0 +1,32 @@ + +include_guard(GLOBAL) + +function (CHECK_EXECUTABLE executable result) + set (${result} "UNKNOWN" PARENT_SCOPE) + + if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set (tool otool -hv) + else() + set (tool "${CMAKE_COMMAND}" -E env LANG=C LC_ALL=C readelf -lW) + endif() + + execute_process(COMMAND ${tool} "${executable}" + OUTPUT_VARIABLE output + ERROR_VARIABLE output) + + if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + if (output MATCHES "( |\t)PIE( |\n|$)") + set (${result} "PIE" PARENT_SCOPE) + else() + set (${result} "NO_PIE" PARENT_SCOPE) + endif() + else() + if (output MATCHES "Elf file type is DYN") + set (${result} "PIE" PARENT_SCOPE) + elseif (output MATCHES "Elf file type is EXEC") + set (${result} "NO_PIE" PARENT_SCOPE) + else() + message(SEND_ERROR "Did not find a known file type") + endif() + endif() +endfunction() diff --git a/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake b/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake index 20187d3..6efa0d4 100644 --- a/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake +++ b/Tests/RunCMake/PositionIndependentCode/RunCMakeTest.cmake @@ -9,3 +9,65 @@ run_cmake(Conflict6) run_cmake(Debug) run_cmake(Genex1) run_cmake(Genex2) + +set(RunCMake_TEST_OPTIONS "-DPIE_SUPPORTED=${RunCMake_BINARY_DIR}/PIESupported.cmake") +run_cmake(CheckPIESupported) +include ("${RunCMake_BINARY_DIR}/PIESupported.cmake" OPTIONAL) + +if (PIE_SUPPORTED OR NO_PIE_SUPPORTED) + if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|(Free|Net|Open)BSD)$") + # try to locate readelf needed for validation + find_program (READELF NAMES readelf) + endif() + if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + # try to locate otool needed for validation + find_program (OTOOL NAMES otool) + endif() + + if ((READELF OR OTOOL) AND + (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" + OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" + OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")) + macro(run_cmake_target test subtest) + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${test}-build) + set(RunCMake_TEST_NO_CLEAN 1) + set(RunCMake_TEST_CONFIG Release) + run_cmake_command(${test}-${subtest} ${CMAKE_COMMAND} --build . --config Release --target ${subtest} ${ARGN}) + + unset(RunCMake_TEST_BINARY_DIR) + unset(RunCMake_TEST_NO_CLEAN) + endmacro() + + set(RunCMake_TEST_SOURCE_DIR "${RunCMake_SOURCE_DIR}") + set(RunCMake_TEST_OUTPUT_MERGE TRUE) + if (NOT RunCMake_GENERATOR_IS_MULTI_CONFIG) + set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Release) + endif() + + run_cmake(PIE) + if (PIE_SUPPORTED) + run_cmake_target(PIE pie_on) + endif() + if (NO_PIE_SUPPORTED) + run_cmake_target(PIE pie_off) + endif() + + run_cmake(CMP0083) + run_cmake_target(CMP0083 cmp0083_ref) + + # retrieve default mode + include("${RunCMake_SOURCE_DIR}/PIE_validator.cmake") + include("${RunCMake_BINARY_DIR}/CMP0083-build/Release/CMP0083_config.cmake") + check_executable("${cmp0083_ref}" cmp0083_ref_mode) + + if ((cmp0083_ref_mode STREQUAL "PIE" AND NO_PIE_SUPPORTED) + OR (cmp0083_ref_mode STREQUAL "NO_PIE" AND PIE_SUPPORTED)) + run_cmake_target(CMP0083 cmp0083_new) + endif() + run_cmake_target(CMP0083 cmp0083_old) + + unset(RunCMake_TEST_SOURCE_DIR) + unset(RunCMake_TEST_OPTIONS) + unset(RunCMake_TEST_OUTPUT_MERGE) + endif() +endif() diff --git a/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt b/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt index 2441a9c..0bcf886 100644 --- a/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt +++ b/Tests/RunCMake/TargetPolicies/PolicyList-stderr.txt @@ -26,6 +26,7 @@ \* CMP0073 \* CMP0076 \* CMP0081 + \* CMP0083 Call Stack \(most recent call first\): CMakeLists.txt:3 \(include\) |