Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[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/cfe/trunk@319840 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
smeenai committed Dec 5, 2017
1 parent 3b1d962 commit 456e35c
Show file tree
Hide file tree
Showing 32 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/clang-interpreter/CMakeLists.txt
Expand Up @@ -17,6 +17,7 @@ add_dependencies(clang-interpreter
)

target_link_libraries(clang-interpreter
PRIVATE
clangBasic
clangCodeGen
clangDriver
Expand Down
1 change: 1 addition & 0 deletions tools/arcmt-test/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ add_clang_executable(arcmt-test
)

target_link_libraries(arcmt-test
PRIVATE
clangARCMigrate
clangBasic
clangFrontend
Expand Down
2 changes: 2 additions & 0 deletions tools/c-arcmt-test/CMakeLists.txt
Expand Up @@ -4,10 +4,12 @@ add_clang_executable(c-arcmt-test

if (LLVM_BUILD_STATIC)
target_link_libraries(c-arcmt-test
PRIVATE
libclang_static
)
else()
target_link_libraries(c-arcmt-test
PRIVATE
libclang
)
endif()
Expand Down
3 changes: 2 additions & 1 deletion tools/c-index-test/CMakeLists.txt
Expand Up @@ -22,6 +22,7 @@ if (LLVM_BUILD_STATIC)
)
else()
target_link_libraries(c-index-test
PRIVATE
libclang
clangAST
clangBasic
Expand All @@ -39,7 +40,7 @@ set_target_properties(c-index-test
# If libxml2 is available, make it available for c-index-test.
if (CLANG_HAVE_LIBXML)
include_directories(SYSTEM ${LIBXML2_INCLUDE_DIR})
target_link_libraries(c-index-test ${LIBXML2_LIBRARIES})
target_link_libraries(c-index-test PRIVATE ${LIBXML2_LIBRARIES})
endif()

if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
Expand Down
1 change: 1 addition & 0 deletions tools/clang-check/CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ add_clang_executable(clang-check
)

target_link_libraries(clang-check
PRIVATE
clangAST
clangBasic
clangDriver
Expand Down
1 change: 1 addition & 0 deletions tools/clang-diff/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ add_clang_executable(clang-diff
)

target_link_libraries(clang-diff
PRIVATE
clangBasic
clangFrontend
clangTooling
Expand Down
1 change: 1 addition & 0 deletions tools/clang-format/CMakeLists.txt
Expand Up @@ -12,6 +12,7 @@ set(CLANG_FORMAT_LIB_DEPS
)

target_link_libraries(clang-format
PRIVATE
${CLANG_FORMAT_LIB_DEPS}
)

Expand Down
1 change: 1 addition & 0 deletions tools/clang-func-mapping/CMakeLists.txt
Expand Up @@ -10,6 +10,7 @@ add_clang_executable(clang-func-mapping
)

target_link_libraries(clang-func-mapping
PRIVATE
clangAST
clangBasic
clangCrossTU
Expand Down
1 change: 1 addition & 0 deletions tools/clang-fuzzer/CMakeLists.txt
Expand Up @@ -66,6 +66,7 @@ add_clang_executable(clang-fuzzer
)

target_link_libraries(clang-fuzzer
PRIVATE
${LLVM_LIB_FUZZING_ENGINE}
clangHandleCXX
)
1 change: 1 addition & 0 deletions tools/clang-import-test/CMakeLists.txt
Expand Up @@ -24,5 +24,6 @@ set(CLANG_IMPORT_TEST_LIB_DEPS
)

target_link_libraries(clang-import-test
PRIVATE
${CLANG_IMPORT_TEST_LIB_DEPS}
)
1 change: 1 addition & 0 deletions tools/clang-offload-bundler/CMakeLists.txt
Expand Up @@ -18,6 +18,7 @@ set(CLANG_OFFLOAD_BUNDLER_LIB_DEPS
add_dependencies(clang clang-offload-bundler)

target_link_libraries(clang-offload-bundler
PRIVATE
${CLANG_OFFLOAD_BUNDLER_LIB_DEPS}
)

Expand Down
1 change: 1 addition & 0 deletions tools/clang-refactor/CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ add_clang_tool(clang-refactor
)

target_link_libraries(clang-refactor
PRIVATE
clangAST
clangBasic
clangFormat
Expand Down
1 change: 1 addition & 0 deletions tools/clang-rename/CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_tool(clang-rename ClangRename.cpp)

target_link_libraries(clang-rename
PRIVATE
clangBasic
clangFrontend
clangRewrite
Expand Down
1 change: 1 addition & 0 deletions tools/diagtool/CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ add_clang_executable(diagtool
)

target_link_libraries(diagtool
PRIVATE
clangBasic
clangFrontend
)
Expand Down
4 changes: 3 additions & 1 deletion tools/driver/CMakeLists.txt
Expand Up @@ -38,6 +38,7 @@ add_clang_tool(clang
)

target_link_libraries(clang
PRIVATE
clangBasic
clangCodeGen
clangDriver
Expand Down Expand Up @@ -85,6 +86,7 @@ if (APPLE)

set(TOOL_INFO_PLIST_OUT "${CMAKE_CURRENT_BINARY_DIR}/${TOOL_INFO_PLIST}")
target_link_libraries(clang
PRIVATE
"-Wl,-sectcreate,__TEXT,__info_plist,${TOOL_INFO_PLIST_OUT}")
configure_file("${TOOL_INFO_PLIST}.in" "${TOOL_INFO_PLIST_OUT}" @ONLY)

Expand Down Expand Up @@ -127,5 +129,5 @@ if(CLANG_ORDER_FILE AND (LD64_EXECUTABLE OR GOLD_EXECUTABLE))
endif()

if(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
target_link_libraries(clang Polly)
target_link_libraries(clang PRIVATE Polly)
endif(WITH_POLLY AND LINK_POLLY_INTO_TOOLS)
1 change: 1 addition & 0 deletions unittests/AST/CMakeLists.txt
Expand Up @@ -21,6 +21,7 @@ add_clang_unittest(ASTTests
)

target_link_libraries(ASTTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
Expand Down
1 change: 1 addition & 0 deletions unittests/ASTMatchers/CMakeLists.txt
Expand Up @@ -18,6 +18,7 @@ add_clang_unittest(ASTMatchersTests
ASTMatchersTraversalTest.cpp)

target_link_libraries(ASTMatchersTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
Expand Down
1 change: 1 addition & 0 deletions unittests/ASTMatchers/Dynamic/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ add_clang_unittest(DynamicASTMatchersTests
RegistryTest.cpp)

target_link_libraries(DynamicASTMatchersTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
Expand Down
1 change: 1 addition & 0 deletions unittests/Analysis/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ add_clang_unittest(ClangAnalysisTests
)

target_link_libraries(ClangAnalysisTests
PRIVATE
clangAnalysis
clangAST
clangASTMatchers
Expand Down
1 change: 1 addition & 0 deletions unittests/Basic/CMakeLists.txt
Expand Up @@ -12,6 +12,7 @@ add_clang_unittest(BasicTests
)

target_link_libraries(BasicTests
PRIVATE
clangBasic
clangLex
)
1 change: 1 addition & 0 deletions unittests/CodeGen/CMakeLists.txt
Expand Up @@ -10,6 +10,7 @@ add_clang_unittest(ClangCodeGenTests
)

target_link_libraries(ClangCodeGenTests
PRIVATE
clangAST
clangBasic
clangCodeGen
Expand Down
1 change: 1 addition & 0 deletions unittests/CrossTU/CMakeLists.txt
Expand Up @@ -8,6 +8,7 @@ add_clang_unittest(CrossTUTests
)

target_link_libraries(CrossTUTests
PRIVATE
clangAST
clangBasic
clangCrossTU
Expand Down
1 change: 1 addition & 0 deletions unittests/Driver/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ add_clang_unittest(ClangDriverTests
)

target_link_libraries(ClangDriverTests
PRIVATE
clangDriver
clangBasic
)
1 change: 1 addition & 0 deletions unittests/Format/CMakeLists.txt
Expand Up @@ -20,6 +20,7 @@ add_clang_unittest(FormatTests
)

target_link_libraries(FormatTests
PRIVATE
clangBasic
clangFormat
clangFrontend
Expand Down
1 change: 1 addition & 0 deletions unittests/Frontend/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ add_clang_unittest(FrontendTests
PCHPreambleTest.cpp
)
target_link_libraries(FrontendTests
PRIVATE
clangAST
clangBasic
clangFrontend
Expand Down
1 change: 1 addition & 0 deletions unittests/Lex/CMakeLists.txt
Expand Up @@ -10,6 +10,7 @@ add_clang_unittest(LexTests
)

target_link_libraries(LexTests
PRIVATE
clangAST
clangBasic
clangLex
Expand Down
1 change: 1 addition & 0 deletions unittests/Rename/CMakeLists.txt
Expand Up @@ -14,6 +14,7 @@ add_clang_unittest(ClangRenameTests
)

target_link_libraries(ClangRenameTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
Expand Down
1 change: 1 addition & 0 deletions unittests/Rewrite/CMakeLists.txt
Expand Up @@ -6,5 +6,6 @@ add_clang_unittest(RewriteTests
RewriteBufferTest.cpp
)
target_link_libraries(RewriteTests
PRIVATE
clangRewrite
)
1 change: 1 addition & 0 deletions unittests/Sema/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ add_clang_unittest(SemaTests
)

target_link_libraries(SemaTests
PRIVATE
clangAST
clangBasic
clangFrontend
Expand Down
1 change: 1 addition & 0 deletions unittests/StaticAnalyzer/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ add_clang_unittest(StaticAnalysisTests
)

target_link_libraries(StaticAnalysisTests
PRIVATE
clangBasic
clangAnalysis
clangStaticAnalyzerCore
Expand Down
1 change: 1 addition & 0 deletions unittests/Tooling/CMakeLists.txt
Expand Up @@ -35,6 +35,7 @@ add_clang_unittest(ToolingTests
)

target_link_libraries(ToolingTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
Expand Down
1 change: 1 addition & 0 deletions unittests/libclang/CMakeLists.txt
Expand Up @@ -3,5 +3,6 @@ add_clang_unittest(libclangTests
)

target_link_libraries(libclangTests
PRIVATE
libclang
)

0 comments on commit 456e35c

Please sign in to comment.