Path: blob/master/Tests/CMakeCommands/target_link_libraries/CMakeLists.txt
3153 views
cmake_minimum_required(VERSION 3.10)12if(POLICY CMP0129)3cmake_policy(SET CMP0129 NEW)4endif()56project(target_link_libraries)78file(WRITE9"${CMAKE_CURRENT_BINARY_DIR}/main.cxx"10"int main() { return 0; }11"12)1314add_executable(15target_link_libraries16"${CMAKE_CURRENT_BINARY_DIR}/main.cxx"17)1819macro(ASSERT_PROPERTY _target _property _value)20get_target_property(_out ${_target} ${_property})21if (NOT _out)22set(_out "")23endif()24if (NOT "${_out}" STREQUAL "${_value}")25message(SEND_ERROR "Target ${_target} does not have property ${_property} with value ${_value}. Actual value: ${_out}")26endif()27endmacro()2829include(GenerateExportHeader)30set(CMAKE_INCLUDE_CURRENT_DIR ON)3132add_library(depA SHARED depA.cpp)33generate_export_header(depA)3435add_library(depB SHARED depB.cpp)36generate_export_header(depB)3738target_link_libraries(depB LINK_PRIVATE depA LINK_PRIVATE depA)3940add_library(libgenex SHARED libgenex.cpp)41generate_export_header(libgenex)4243set_property(TARGET depB APPEND PROPERTY44LINK_LIBRARIES $<1:libgenex>45)4647add_library(depC SHARED depC.cpp)48generate_export_header(depC)4950target_link_libraries(depC LINK_PUBLIC depA LINK_PUBLIC depA)5152assert_property(depA INTERFACE_LINK_LIBRARIES "")53assert_property(depB INTERFACE_LINK_LIBRARIES "")54assert_property(depC INTERFACE_LINK_LIBRARIES "depA;depA")5556add_executable(targetA targetA.cpp)5758target_link_libraries(targetA LINK_INTERFACE_LIBRARIES depA depB)5960assert_property(targetA INTERFACE_LINK_LIBRARIES "depA;depB")6162set_target_properties(targetA PROPERTIES INTERFACE_LINK_LIBRARIES "")6364assert_property(targetA INTERFACE_LINK_LIBRARIES "")6566add_subdirectory(subdir)67target_link_libraries(targetA subdirlib)6869target_link_libraries(targetA depB depC)7071assert_property(targetA INTERFACE_LINK_LIBRARIES ";subdirlib;depB;depC")7273# Exclude depIfaceOnly from ALL so that it will only be built if something74# depends on it. As it is in the link interface of depB, targetA75# will depend on it. That dependency is what is being tested here.76add_library(depIfaceOnly SHARED EXCLUDE_FROM_ALL depIfaceOnly.cpp)77generate_export_header(depIfaceOnly)78set_property(TARGET depB APPEND PROPERTY INTERFACE_LINK_LIBRARIES depIfaceOnly)7980add_library(depD SHARED depD.cpp)81generate_export_header(depD)82set_property(TARGET depD APPEND PROPERTY83INTERFACE_LINK_LIBRARIES84$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:depA>85)8687add_executable(targetB targetB.cpp)88target_link_libraries(targetB depD)8990macro(create_header _name)91file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${_name}")92file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_name}/${_name}.h" "//${_name}.h\n")93endmacro()9495create_header(foo)96create_header(bar)9798add_library(depG SHARED depG.cpp)99generate_export_header(depG)100target_include_directories(depG INTERFACE101"${CMAKE_CURRENT_BINARY_DIR}/foo"102"${CMAKE_CURRENT_BINARY_DIR}/bar"103)104target_compile_definitions(depG INTERFACE105TEST_DEF106)107108109add_executable(targetC targetC.cpp)110if(NOT BORLAND AND NOT WATCOM)111# Linking to a target containing a + should be non-fatal, though it does112# not work at all on Borland or watcom113add_library(wrapc++ empty.cpp)114target_link_libraries(targetC wrapc++)115endif()116# The TARGET_PROPERTY expression is duplicated below to test that there is no117# shortcutting of the evaluation by returning an empty string.118set(_exe_test $<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>)119target_link_libraries(targetC $<$<AND:${_exe_test},${_exe_test}>:depG>)120121add_library(libConsumer empty.cpp)122# This line causes $<$<CONFIG:Debug>:depA> to be used when123# determining the include directories for libConsumer based on the124# interface properties of its LINK_LIBRARIES. Because the above expression125# evaluates to the empty string in non-Debug cases, ensure that that causes126# no problems.127target_link_libraries(libConsumer debug depA)128129add_subdirectory(cmp0022)130131add_executable(newsignature1 newsignature1.cpp)132target_link_libraries(newsignature1 PRIVATE depC INTERFACE depD PUBLIC depB PRIVATE subdirlib INTERFACE INTERFACE PUBLIC)133134assert_property(newsignature1 INTERFACE_LINK_LIBRARIES "depD;depB")135assert_property(newsignature1 LINK_LIBRARIES "depC;depB;subdirlib")136137#----------------------------------------------------------------------------138# Test cross-directory linking.139cmake_policy(PUSH)140cmake_policy(SET CMP0022 NEW)141cmake_policy(SET CMP0079 NEW)142add_executable(TopDir TopDir.c)143add_library(TopDirInterface INTERFACE)144target_link_libraries(TopDir PRIVATE TopDirInterface)145add_subdirectory(SubDirA)146add_subdirectory(SubDirB)147target_link_libraries(SubDirB TopDirImported)148add_subdirectory(SubDirC)149target_link_libraries(SubDirC PRIVATE SubDirC2)150target_link_libraries(TopDir PRIVATE SubDirC)151add_library(TopDirImported IMPORTED INTERFACE)152target_compile_definitions(TopDirImported INTERFACE DEF_TopDirImported)153cmake_policy(POP)154155#----------------------------------------------------------------------------156# Test $<COMPILE_ONLY:> genex.157cmake_policy(SET CMP0099 NEW)158add_library(dont_link_too SHARED compile_only.cpp)159target_compile_definitions(dont_link_too PUBLIC USE_EXAMPLE PRIVATE HAVE_FUNCTION)160target_link_options(dont_link_too INTERFACE invalid_link_option)161target_link_libraries(dont_link_too INTERFACE invalid_link_library)162163add_library(uses_compile_only_genex SHARED compile_only.cpp)164target_link_libraries(uses_compile_only_genex PUBLIC $<COMPILE_ONLY:dont_link_too>)165166add_library(uses_compile_only_genex_static STATIC compile_only.cpp)167target_link_libraries(uses_compile_only_genex_static PRIVATE $<COMPILE_ONLY:dont_link_too>)168add_executable(uses_via_static_linking main.cxx)169target_link_libraries(uses_via_static_linking PRIVATE uses_compile_only_genex_static)170171add_library(uses_compile_only_genex_obj SHARED compile_only.cpp)172target_compile_definitions(uses_compile_only_genex_obj PRIVATE HAVE_FUNCTION)173target_link_libraries(uses_compile_only_genex_obj PRIVATE $<COMPILE_ONLY:dont_link_too>)174175cmake_policy(SET CMP0131 NEW)176add_library(only_link_too OBJECT link_only.cpp)177target_compile_options(only_link_too INTERFACE INVALID_COMPILER_FLAG_ARRRRGH)178179add_executable(uses_link_only_genex_obj compile_only.cpp)180target_compile_definitions(uses_link_only_genex_obj PUBLIC USE_EXAMPLE)181target_link_libraries(uses_link_only_genex_obj PRIVATE $<LINK_ONLY:only_link_too>)182183184