Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 4a6bb53

Browse files
committed
[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
1 parent 96cfeb3 commit 4a6bb53

File tree

20 files changed

+23
-23
lines changed

20 files changed

+23
-23
lines changed

cmake/modules/AddLLVM.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ macro(add_llvm_executable name)
768768
# libpthreads overrides some standard library symbols, so main
769769
# executable must be linked with it in order to provide consistent
770770
# API for all shared libaries loaded by this executable.
771-
target_link_libraries(${name} ${LLVM_PTHREAD_LIB})
771+
target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
772772
endif()
773773
endmacro(add_llvm_executable name)
774774

@@ -1093,7 +1093,7 @@ function(add_unittest test_suite test_name)
10931093
# libpthreads overrides some standard library symbols, so main
10941094
# executable must be linked with it in order to provide consistent
10951095
# API for all shared libaries loaded by this executable.
1096-
target_link_libraries(${test_name} gtest_main gtest ${LLVM_PTHREAD_LIB})
1096+
target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB})
10971097

10981098
add_dependencies(${test_suite} ${test_name})
10991099
get_target_property(test_suite_folder ${test_suite} FOLDER)

cmake/modules/LLVM-Config.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro(llvm_config executable)
8787
endif()
8888
endif()
8989

90-
target_link_libraries(${executable} LLVM)
90+
target_link_libraries(${executable} PRIVATE LLVM)
9191
endif()
9292

9393
explicit_llvm_config(${executable} ${link_components})
@@ -101,7 +101,7 @@ function(explicit_llvm_config executable)
101101
get_target_property(t ${executable} TYPE)
102102
if(t STREQUAL "STATIC_LIBRARY")
103103
target_link_libraries(${executable} INTERFACE ${LIBRARIES})
104-
elseif(t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
104+
elseif(t STREQUAL "EXECUTABLE" OR t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
105105
target_link_libraries(${executable} PRIVATE ${LIBRARIES})
106106
else()
107107
# Use plain form for legacy user.

examples/ParallelJIT/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ add_llvm_example(ParallelJIT
1111
ParallelJIT.cpp
1212
)
1313

14-
target_link_libraries(ParallelJIT ${LLVM_PTHREAD_LIB})
14+
target_link_libraries(ParallelJIT PRIVATE ${LLVM_PTHREAD_LIB})

tools/bugpoint/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ add_llvm_tool(bugpoint
3737
export_executable_symbols(bugpoint)
3838

3939
if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
40-
target_link_libraries(bugpoint Polly)
40+
target_link_libraries(bugpoint PRIVATE Polly)
4141
# Ensure LLVMTarget can resolve dependences in Polly.
42-
target_link_libraries(bugpoint LLVMTarget)
42+
target_link_libraries(bugpoint PRIVATE LLVMTarget)
4343
endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)

tools/dsymutil/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ add_llvm_tool(llvm-dsymutil
2222
)
2323

2424
IF(APPLE)
25-
target_link_libraries(llvm-dsymutil "-framework CoreFoundation")
25+
target_link_libraries(llvm-dsymutil PRIVATE "-framework CoreFoundation")
2626
ENDIF(APPLE)

tools/llvm-cfi-verify/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ add_llvm_tool(llvm-cfi-verify
1515
llvm-cfi-verify.cpp)
1616

1717
add_subdirectory(lib)
18-
target_link_libraries(llvm-cfi-verify LLVMCFIVerify)
18+
target_link_libraries(llvm-cfi-verify PRIVATE LLVMCFIVerify)

tools/llvm-objdump/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ add_llvm_tool(llvm-objdump
2323
)
2424

2525
if(HAVE_LIBXAR)
26-
target_link_libraries(llvm-objdump ${XAR_LIB})
26+
target_link_libraries(llvm-objdump PRIVATE ${XAR_LIB})
2727
endif()
2828

2929
if(LLVM_INSTALL_BINUTILS_SYMLINKS)

tools/opt/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ add_llvm_tool(opt
3737
export_executable_symbols(opt)
3838

3939
if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
40-
target_link_libraries(opt Polly)
40+
target_link_libraries(opt PRIVATE Polly)
4141
endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)

unittests/DebugInfo/CodeView/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ add_llvm_unittest(DebugInfoCodeViewTests
1111
${DebugInfoCodeViewSources}
1212
)
1313

14-
target_link_libraries(DebugInfoCodeViewTests LLVMTestingSupport)
14+
target_link_libraries(DebugInfoCodeViewTests PRIVATE LLVMTestingSupport)

unittests/DebugInfo/DWARF/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ add_llvm_unittest(DebugInfoDWARFTests
1818
${DebugInfoSources}
1919
)
2020

21-
target_link_libraries(DebugInfoDWARFTests LLVMTestingSupport)
21+
target_link_libraries(DebugInfoDWARFTests PRIVATE LLVMTestingSupport)

unittests/DebugInfo/MSF/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ add_llvm_unittest(DebugInfoMSFTests
1212
${DebugInfoMSFSources}
1313
)
1414

15-
target_link_libraries(DebugInfoMSFTests LLVMTestingSupport)
15+
target_link_libraries(DebugInfoMSFTests PRIVATE LLVMTestingSupport)

unittests/DebugInfo/PDB/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ add_llvm_unittest(DebugInfoPDBTests
1414
${DebugInfoPDBSources}
1515
)
1616

17-
target_link_libraries(DebugInfoPDBTests LLVMTestingSupport)
17+
target_link_libraries(DebugInfoPDBTests PRIVATE LLVMTestingSupport)

unittests/ExecutionEngine/Orc/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ add_llvm_unittest(OrcJITTests
2424
SymbolStringPoolTest.cpp
2525
)
2626

27-
target_link_libraries(OrcJITTests ${LLVM_PTHREAD_LIB})
27+
target_link_libraries(OrcJITTests PRIVATE ${LLVM_PTHREAD_LIB})

unittests/ProfileData/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ add_llvm_unittest(ProfileDataTests
1111
SampleProfTest.cpp
1212
)
1313

14-
target_link_libraries(ProfileDataTests LLVMTestingSupport)
14+
target_link_libraries(ProfileDataTests PRIVATE LLVMTestingSupport)

unittests/Support/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ add_llvm_unittest(SupportTests
7474
set_source_files_properties(AlignOfTest.cpp PROPERTIES COMPILE_FLAGS -w)
7575

7676
# ManagedStatic.cpp uses <pthread>.
77-
target_link_libraries(SupportTests LLVMTestingSupport ${LLVM_PTHREAD_LIB})
77+
target_link_libraries(SupportTests PRIVATE LLVMTestingSupport ${LLVM_PTHREAD_LIB})
7878

7979
add_subdirectory(DynamicLibrary)

unittests/Support/DynamicLibrary/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ add_library(DynamicLibraryLib STATIC ExportedFuncs.cxx)
44
set_target_properties(DynamicLibraryLib PROPERTIES FOLDER "Tests")
55

66
add_llvm_unittest(DynamicLibraryTests DynamicLibraryTest.cpp)
7-
target_link_libraries(DynamicLibraryTests DynamicLibraryLib)
7+
target_link_libraries(DynamicLibraryTests PRIVATE DynamicLibraryLib)
88
export_executable_symbols(DynamicLibraryTests)
99

1010
function(dynlib_add_module NAME)

unittests/tools/llvm-cfi-verify/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ set(LLVM_LINK_COMPONENTS
1414
add_llvm_unittest(CFIVerifyTests
1515
FileAnalysis.cpp
1616
GraphBuilder.cpp)
17-
target_link_libraries(CFIVerifyTests LLVMCFIVerify)
17+
target_link_libraries(CFIVerifyTests PRIVATE LLVMCFIVerify)

utils/FileCheck/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ add_llvm_utility(FileCheck
22
FileCheck.cpp
33
)
44

5-
target_link_libraries(FileCheck LLVMSupport)
5+
target_link_libraries(FileCheck PRIVATE LLVMSupport)

utils/not/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ add_llvm_utility(not
22
not.cpp
33
)
44

5-
target_link_libraries(not LLVMSupport)
5+
target_link_libraries(not PRIVATE LLVMSupport)

utils/yaml-bench/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ add_llvm_utility(yaml-bench
22
YAMLBench.cpp
33
)
44

5-
target_link_libraries(yaml-bench LLVMSupport)
5+
target_link_libraries(yaml-bench PRIVATE LLVMSupport)

0 commit comments

Comments
 (0)