| 1 |
enable_language(C) |
| 2 |
|
| 3 |
set(cfg_up) |
| 4 |
set(cfg_slash /) |
| 5 |
if(cfg_dir) |
| 6 |
set(cfg_up /..) |
| 7 |
set(cfg_slash) |
| 8 |
endif() |
| 9 |
|
| 10 |
if(NOT CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN) |
| 11 |
if(CMAKE_C_PLATFORM_ID STREQUAL "Linux") |
| 12 |
# Sanity check for platform that is definitely known to support $ORIGIN. |
| 13 |
message(FATAL_ERROR "Platform fails to report relative RPATH support") |
| 14 |
else() |
| 15 |
message(STATUS "Platform does not support relative RPATHs, skipping") |
| 16 |
endif() |
| 17 |
return() |
| 18 |
endif() |
| 19 |
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON) |
| 20 |
|
| 21 |
function(CheckRpath target rpath) |
| 22 |
add_custom_command( |
| 23 |
TARGET ${target} |
| 24 |
POST_BUILD |
| 25 |
COMMAND ${CMAKE_COMMAND} -Dfile=$<TARGET_FILE:${target}> -Drpath=${rpath} |
| 26 |
-P "${CMAKE_CURRENT_SOURCE_DIR}/RelativeCheck.cmake" |
| 27 |
VERBATIM |
| 28 |
) |
| 29 |
endfunction() |
| 30 |
|
| 31 |
if(CMAKE_C_COMPILER_ID STREQUAL "XL" AND CMAKE_BINARY_DIR MATCHES " ") |
| 32 |
# XL 16.1.0.0 fails building the library if the output path contains a space. |
| 33 |
set(externDir) |
| 34 |
message(STATUS "Skipping external library test because of a toolchain bug") |
| 35 |
else() |
| 36 |
get_filename_component(externDir "${CMAKE_BINARY_DIR}" DIRECTORY) |
| 37 |
set(externDir "${externDir}/Relative-extern") |
| 38 |
endif() |
| 39 |
|
| 40 |
add_library(utils SHARED A.c) |
| 41 |
add_library(utils-sub SHARED A.c) |
| 42 |
set_property(TARGET utils-sub PROPERTY LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libs) |
| 43 |
if(externDir) |
| 44 |
add_library(utils-extern SHARED A.c) |
| 45 |
set_property(TARGET utils-extern PROPERTY LIBRARY_OUTPUT_DIRECTORY ${externDir}) |
| 46 |
endif() |
| 47 |
|
| 48 |
add_executable(main main.c) |
| 49 |
target_link_libraries(main utils) |
| 50 |
CheckRpath(main "\$ORIGIN") |
| 51 |
|
| 52 |
add_executable(main-norel main.c) |
| 53 |
target_link_libraries(main-norel utils) |
| 54 |
set_property(TARGET main-norel PROPERTY BUILD_RPATH_USE_ORIGIN OFF) |
| 55 |
CheckRpath(main-norel "${CMAKE_CURRENT_BINARY_DIR}${cfg_dir}") |
| 56 |
|
| 57 |
add_executable(mainsub main.c) |
| 58 |
target_link_libraries(mainsub utils) |
| 59 |
set_property(TARGET mainsub PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) |
| 60 |
CheckRpath(mainsub "\$ORIGIN${cfg_up}/..${cfg_dir}${cfg_slash}") |
| 61 |
|
| 62 |
add_executable(main-sub main.c) |
| 63 |
target_link_libraries(main-sub utils-sub) |
| 64 |
CheckRpath(main-sub "\$ORIGIN${cfg_up}/libs${cfg_dir}") |
| 65 |
|
| 66 |
add_executable(mainsub-sub main.c) |
| 67 |
target_link_libraries(mainsub-sub utils-sub) |
| 68 |
set_property(TARGET mainsub-sub PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) |
| 69 |
CheckRpath(mainsub-sub "\$ORIGIN${cfg_up}/../libs${cfg_dir}") |
| 70 |
|
| 71 |
if(externDir) |
| 72 |
# Binaries linking to libraries outside the build tree should have an absolute RPATH. |
| 73 |
add_executable(main-extern main.c) |
| 74 |
target_link_libraries(main-extern utils-extern) |
| 75 |
CheckRpath(main-extern "${externDir}${cfg_dir}") |
| 76 |
endif() |