llvm.org GIT mirror llvm / 4a6bb53
[CMake] Use PRIVATE in target_link_libraries for executables We currently use target_link_libraries without an explicit scope specifier (INTERFACE, PRIVATE or PUBLIC) when linking executables. Dependencies added in this way apply to both the target and its dependencies, i.e. they become part of the executable's link interface and are transitive. Transitive dependencies generally don't make sense for executables, since you wouldn't normally be linking against an executable. This also causes issues for generating install export files when using LLVM_DISTRIBUTION_COMPONENTS. For example, clang has a lot of LLVM library dependencies, which are currently added as interface dependencies. If clang is in the distribution components but the LLVM libraries it depends on aren't (which is a perfectly legitimate use case if the LLVM libraries are being built static and there are therefore no run-time dependencies on them), CMake will complain about the LLVM libraries not being in export set when attempting to generate the install export file for clang. This is reasonable behavior on CMake's part, and the right thing is for LLVM's build system to explicitly use PRIVATE dependencies for executables. Unfortunately, CMake doesn't allow you to mix and match the keyword and non-keyword target_link_libraries signatures for a single target; i.e., if a single call to target_link_libraries for a particular target uses one of the INTERFACE, PRIVATE, or PUBLIC keywords, all other calls must also be updated to use those keywords. This means we must do this change in a single shot. I also fully expect to have missed some instances; I tested by enabling all the projects in the monorepo (except dragonegg), and configuring both with and without shared libraries, on both Darwin and Linux, but I'm planning to rely on the buildbots for other configurations (since it should be pretty easy to fix those). Even after this change, we still have a lot of target_link_libraries calls that don't specify a scope keyword, mostly for shared libraries. I'm thinking about addressing those in a follow-up, but that's a separate change IMO. Differential Revision: https://reviews.llvm.org/D40823 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319840 91177308-0d34-0410-b5e6-96231b3b80d8 Shoaib Meenai 1 year, 10 months ago
20 changed file(s) with 23 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
767767 # libpthreads overrides some standard library symbols, so main
768768 # executable must be linked with it in order to provide consistent
769769 # API for all shared libaries loaded by this executable.
770 target_link_libraries(${name} ${LLVM_PTHREAD_LIB})
770 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
771771 endif()
772772 endmacro(add_llvm_executable name)
773773
10921092 # libpthreads overrides some standard library symbols, so main
10931093 # executable must be linked with it in order to provide consistent
10941094 # API for all shared libaries loaded by this executable.
1095 target_link_libraries(${test_name} gtest_main gtest ${LLVM_PTHREAD_LIB})
1095 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
10961096
10971097 add_dependencies(${test_suite} ${test_name})
10981098 get_target_property(test_suite_folder ${test_suite} FOLDER)
8686 endif()
8787 endif()
8888
89 target_link_libraries(${executable} LLVM)
89 target_link_libraries(${executable} PRIVATE LLVM)
9090 endif()
9191
9292 explicit_llvm_config(${executable} ${link_components})
100100 get_target_property(t ${executable} TYPE)
101101 if(t STREQUAL "STATIC_LIBRARY")
102102 target_link_libraries(${executable} INTERFACE ${LIBRARIES})
103 elseif(t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
103 elseif(t STREQUAL "EXECUTABLE" OR t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
104104 target_link_libraries(${executable} PRIVATE ${LIBRARIES})
105105 else()
106106 # Use plain form for legacy user.
1010 ParallelJIT.cpp
1111 )
1212
13 target_link_libraries(ParallelJIT ${LLVM_PTHREAD_LIB})
13 target_link_libraries(ParallelJIT PRIVATE ${LLVM_PTHREAD_LIB})
3636 export_executable_symbols(bugpoint)
3737
3838 if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
39 target_link_libraries(bugpoint Polly)
39 target_link_libraries(bugpoint PRIVATE Polly)
4040 # Ensure LLVMTarget can resolve dependences in Polly.
41 target_link_libraries(bugpoint LLVMTarget)
41 target_link_libraries(bugpoint PRIVATE LLVMTarget)
4242 endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
2121 )
2222
2323 IF(APPLE)
24 target_link_libraries(llvm-dsymutil "-framework CoreFoundation")
24 target_link_libraries(llvm-dsymutil PRIVATE "-framework CoreFoundation")
2525 ENDIF(APPLE)
1414 llvm-cfi-verify.cpp)
1515
1616 add_subdirectory(lib)
17 target_link_libraries(llvm-cfi-verify LLVMCFIVerify)
17 target_link_libraries(llvm-cfi-verify PRIVATE LLVMCFIVerify)
2222 )
2323
2424 if(HAVE_LIBXAR)
25 target_link_libraries(llvm-objdump ${XAR_LIB})
25 target_link_libraries(llvm-objdump PRIVATE ${XAR_LIB})
2626 endif()
2727
2828 if(LLVM_INSTALL_BINUTILS_SYMLINKS)
3636 export_executable_symbols(opt)
3737
3838 if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
39 target_link_libraries(opt Polly)
39 target_link_libraries(opt PRIVATE Polly)
4040 endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
1010 ${DebugInfoCodeViewSources}
1111 )
1212
13 target_link_libraries(DebugInfoCodeViewTests LLVMTestingSupport)
13 target_link_libraries(DebugInfoCodeViewTests PRIVATE LLVMTestingSupport)
1717 ${DebugInfoSources}
1818 )
1919
20 target_link_libraries(DebugInfoDWARFTests LLVMTestingSupport)
20 target_link_libraries(DebugInfoDWARFTests PRIVATE LLVMTestingSupport)
1111 ${DebugInfoMSFSources}
1212 )
1313
14 target_link_libraries(DebugInfoMSFTests LLVMTestingSupport)
14 target_link_libraries(DebugInfoMSFTests PRIVATE LLVMTestingSupport)
1313 ${DebugInfoPDBSources}
1414 )
1515
16 target_link_libraries(DebugInfoPDBTests LLVMTestingSupport)
16 target_link_libraries(DebugInfoPDBTests PRIVATE LLVMTestingSupport)
2323 SymbolStringPoolTest.cpp
2424 )
2525
26 target_link_libraries(OrcJITTests ${LLVM_PTHREAD_LIB})
26 target_link_libraries(OrcJITTests PRIVATE ${LLVM_PTHREAD_LIB})
1010 SampleProfTest.cpp
1111 )
1212
13 target_link_libraries(ProfileDataTests LLVMTestingSupport)
13 target_link_libraries(ProfileDataTests PRIVATE LLVMTestingSupport)
7373 set_source_files_properties(AlignOfTest.cpp PROPERTIES COMPILE_FLAGS -w)
7474
7575 # ManagedStatic.cpp uses .
76 target_link_libraries(SupportTests LLVMTestingSupport ${LLVM_PTHREAD_LIB})
76 target_link_libraries(SupportTests PRIVATE LLVMTestingSupport ${LLVM_PTHREAD_LIB})
7777
7878 add_subdirectory(DynamicLibrary)
33 set_target_properties(DynamicLibraryLib PROPERTIES FOLDER "Tests")
44
55 add_llvm_unittest(DynamicLibraryTests DynamicLibraryTest.cpp)
6 target_link_libraries(DynamicLibraryTests DynamicLibraryLib)
6 target_link_libraries(DynamicLibraryTests PRIVATE DynamicLibraryLib)
77 export_executable_symbols(DynamicLibraryTests)
88
99 function(dynlib_add_module NAME)
1313 add_llvm_unittest(CFIVerifyTests
1414 FileAnalysis.cpp
1515 GraphBuilder.cpp)
16 target_link_libraries(CFIVerifyTests LLVMCFIVerify)
16 target_link_libraries(CFIVerifyTests PRIVATE LLVMCFIVerify)
11 FileCheck.cpp
22 )
33
4 target_link_libraries(FileCheck LLVMSupport)
4 target_link_libraries(FileCheck PRIVATE LLVMSupport)
11 not.cpp
22 )
33
4 target_link_libraries(not LLVMSupport)
4 target_link_libraries(not PRIVATE LLVMSupport)
11 YAMLBench.cpp
22 )
33
4 target_link_libraries(yaml-bench LLVMSupport)
4 target_link_libraries(yaml-bench PRIVATE LLVMSupport)