From: Shoaib Meenai Date: Tue, 5 Dec 2017 21:49:56 +0000 (+0000) Subject: [CMake] Use PRIVATE in target_link_libraries for executables X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=456e35cbb384bd7fb97cf995163fd9d17e319b77;p=clang [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 --- diff --git a/examples/clang-interpreter/CMakeLists.txt b/examples/clang-interpreter/CMakeLists.txt index e7e59d9308..3084238844 100644 --- a/examples/clang-interpreter/CMakeLists.txt +++ b/examples/clang-interpreter/CMakeLists.txt @@ -17,6 +17,7 @@ add_dependencies(clang-interpreter ) target_link_libraries(clang-interpreter + PRIVATE clangBasic clangCodeGen clangDriver diff --git a/tools/arcmt-test/CMakeLists.txt b/tools/arcmt-test/CMakeLists.txt index 0cb2c0f98e..2b456be2fc 100644 --- a/tools/arcmt-test/CMakeLists.txt +++ b/tools/arcmt-test/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_executable(arcmt-test ) target_link_libraries(arcmt-test + PRIVATE clangARCMigrate clangBasic clangFrontend diff --git a/tools/c-arcmt-test/CMakeLists.txt b/tools/c-arcmt-test/CMakeLists.txt index 8914607358..08ac93c176 100644 --- a/tools/c-arcmt-test/CMakeLists.txt +++ b/tools/c-arcmt-test/CMakeLists.txt @@ -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() diff --git a/tools/c-index-test/CMakeLists.txt b/tools/c-index-test/CMakeLists.txt index c5cb0591c5..fdc713dc49 100644 --- a/tools/c-index-test/CMakeLists.txt +++ b/tools/c-index-test/CMakeLists.txt @@ -22,6 +22,7 @@ if (LLVM_BUILD_STATIC) ) else() target_link_libraries(c-index-test + PRIVATE libclang clangAST clangBasic @@ -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) diff --git a/tools/clang-check/CMakeLists.txt b/tools/clang-check/CMakeLists.txt index 04151a8e03..c5ace26c29 100644 --- a/tools/clang-check/CMakeLists.txt +++ b/tools/clang-check/CMakeLists.txt @@ -9,6 +9,7 @@ add_clang_executable(clang-check ) target_link_libraries(clang-check + PRIVATE clangAST clangBasic clangDriver diff --git a/tools/clang-diff/CMakeLists.txt b/tools/clang-diff/CMakeLists.txt index a1fc6275be..09bebf2cb6 100644 --- a/tools/clang-diff/CMakeLists.txt +++ b/tools/clang-diff/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_executable(clang-diff ) target_link_libraries(clang-diff + PRIVATE clangBasic clangFrontend clangTooling diff --git a/tools/clang-format/CMakeLists.txt b/tools/clang-format/CMakeLists.txt index c695ba3442..a295e8cd0b 100644 --- a/tools/clang-format/CMakeLists.txt +++ b/tools/clang-format/CMakeLists.txt @@ -12,6 +12,7 @@ set(CLANG_FORMAT_LIB_DEPS ) target_link_libraries(clang-format + PRIVATE ${CLANG_FORMAT_LIB_DEPS} ) diff --git a/tools/clang-func-mapping/CMakeLists.txt b/tools/clang-func-mapping/CMakeLists.txt index 8c10fcd757..ae28e28d53 100644 --- a/tools/clang-func-mapping/CMakeLists.txt +++ b/tools/clang-func-mapping/CMakeLists.txt @@ -10,6 +10,7 @@ add_clang_executable(clang-func-mapping ) target_link_libraries(clang-func-mapping + PRIVATE clangAST clangBasic clangCrossTU diff --git a/tools/clang-fuzzer/CMakeLists.txt b/tools/clang-fuzzer/CMakeLists.txt index abc501511f..eab921c7d6 100644 --- a/tools/clang-fuzzer/CMakeLists.txt +++ b/tools/clang-fuzzer/CMakeLists.txt @@ -66,6 +66,7 @@ add_clang_executable(clang-fuzzer ) target_link_libraries(clang-fuzzer + PRIVATE ${LLVM_LIB_FUZZING_ENGINE} clangHandleCXX ) diff --git a/tools/clang-import-test/CMakeLists.txt b/tools/clang-import-test/CMakeLists.txt index 85e833d37b..836efac8ac 100644 --- a/tools/clang-import-test/CMakeLists.txt +++ b/tools/clang-import-test/CMakeLists.txt @@ -24,5 +24,6 @@ set(CLANG_IMPORT_TEST_LIB_DEPS ) target_link_libraries(clang-import-test + PRIVATE ${CLANG_IMPORT_TEST_LIB_DEPS} ) diff --git a/tools/clang-offload-bundler/CMakeLists.txt b/tools/clang-offload-bundler/CMakeLists.txt index 6161d08ae5..8718015be7 100644 --- a/tools/clang-offload-bundler/CMakeLists.txt +++ b/tools/clang-offload-bundler/CMakeLists.txt @@ -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} ) diff --git a/tools/clang-refactor/CMakeLists.txt b/tools/clang-refactor/CMakeLists.txt index c20e83bacf..d2029066b9 100644 --- a/tools/clang-refactor/CMakeLists.txt +++ b/tools/clang-refactor/CMakeLists.txt @@ -9,6 +9,7 @@ add_clang_tool(clang-refactor ) target_link_libraries(clang-refactor + PRIVATE clangAST clangBasic clangFormat diff --git a/tools/clang-rename/CMakeLists.txt b/tools/clang-rename/CMakeLists.txt index e74f05d821..9689e1c680 100644 --- a/tools/clang-rename/CMakeLists.txt +++ b/tools/clang-rename/CMakeLists.txt @@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS add_clang_tool(clang-rename ClangRename.cpp) target_link_libraries(clang-rename + PRIVATE clangBasic clangFrontend clangRewrite diff --git a/tools/diagtool/CMakeLists.txt b/tools/diagtool/CMakeLists.txt index 3f7d80385a..beb6c35457 100644 --- a/tools/diagtool/CMakeLists.txt +++ b/tools/diagtool/CMakeLists.txt @@ -13,6 +13,7 @@ add_clang_executable(diagtool ) target_link_libraries(diagtool + PRIVATE clangBasic clangFrontend ) diff --git a/tools/driver/CMakeLists.txt b/tools/driver/CMakeLists.txt index 901b6d62e4..a0b190faab 100644 --- a/tools/driver/CMakeLists.txt +++ b/tools/driver/CMakeLists.txt @@ -38,6 +38,7 @@ add_clang_tool(clang ) target_link_libraries(clang + PRIVATE clangBasic clangCodeGen clangDriver @@ -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) @@ -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) diff --git a/unittests/AST/CMakeLists.txt b/unittests/AST/CMakeLists.txt index 45dfa7a972..9839cdb1f2 100644 --- a/unittests/AST/CMakeLists.txt +++ b/unittests/AST/CMakeLists.txt @@ -21,6 +21,7 @@ add_clang_unittest(ASTTests ) target_link_libraries(ASTTests + PRIVATE clangAST clangASTMatchers clangBasic diff --git a/unittests/ASTMatchers/CMakeLists.txt b/unittests/ASTMatchers/CMakeLists.txt index 563303157a..a876fc2d33 100644 --- a/unittests/ASTMatchers/CMakeLists.txt +++ b/unittests/ASTMatchers/CMakeLists.txt @@ -18,6 +18,7 @@ add_clang_unittest(ASTMatchersTests ASTMatchersTraversalTest.cpp) target_link_libraries(ASTMatchersTests + PRIVATE clangAST clangASTMatchers clangBasic diff --git a/unittests/ASTMatchers/Dynamic/CMakeLists.txt b/unittests/ASTMatchers/Dynamic/CMakeLists.txt index 506a65549e..848a820ade 100644 --- a/unittests/ASTMatchers/Dynamic/CMakeLists.txt +++ b/unittests/ASTMatchers/Dynamic/CMakeLists.txt @@ -8,6 +8,7 @@ add_clang_unittest(DynamicASTMatchersTests RegistryTest.cpp) target_link_libraries(DynamicASTMatchersTests + PRIVATE clangAST clangASTMatchers clangBasic diff --git a/unittests/Analysis/CMakeLists.txt b/unittests/Analysis/CMakeLists.txt index 62db8f652e..0056f82402 100644 --- a/unittests/Analysis/CMakeLists.txt +++ b/unittests/Analysis/CMakeLists.txt @@ -8,6 +8,7 @@ add_clang_unittest(ClangAnalysisTests ) target_link_libraries(ClangAnalysisTests + PRIVATE clangAnalysis clangAST clangASTMatchers diff --git a/unittests/Basic/CMakeLists.txt b/unittests/Basic/CMakeLists.txt index 3a9f34f3d2..b46c067dc2 100644 --- a/unittests/Basic/CMakeLists.txt +++ b/unittests/Basic/CMakeLists.txt @@ -12,6 +12,7 @@ add_clang_unittest(BasicTests ) target_link_libraries(BasicTests + PRIVATE clangBasic clangLex ) diff --git a/unittests/CodeGen/CMakeLists.txt b/unittests/CodeGen/CMakeLists.txt index c49776bc02..3fb79a0307 100644 --- a/unittests/CodeGen/CMakeLists.txt +++ b/unittests/CodeGen/CMakeLists.txt @@ -10,6 +10,7 @@ add_clang_unittest(ClangCodeGenTests ) target_link_libraries(ClangCodeGenTests + PRIVATE clangAST clangBasic clangCodeGen diff --git a/unittests/CrossTU/CMakeLists.txt b/unittests/CrossTU/CMakeLists.txt index 3c479c4473..652d91612f 100644 --- a/unittests/CrossTU/CMakeLists.txt +++ b/unittests/CrossTU/CMakeLists.txt @@ -8,6 +8,7 @@ add_clang_unittest(CrossTUTests ) target_link_libraries(CrossTUTests + PRIVATE clangAST clangBasic clangCrossTU diff --git a/unittests/Driver/CMakeLists.txt b/unittests/Driver/CMakeLists.txt index 2a3f41d63b..b8c800f59e 100644 --- a/unittests/Driver/CMakeLists.txt +++ b/unittests/Driver/CMakeLists.txt @@ -11,6 +11,7 @@ add_clang_unittest(ClangDriverTests ) target_link_libraries(ClangDriverTests + PRIVATE clangDriver clangBasic ) diff --git a/unittests/Format/CMakeLists.txt b/unittests/Format/CMakeLists.txt index 992db0e508..18e4432308 100644 --- a/unittests/Format/CMakeLists.txt +++ b/unittests/Format/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_unittest(FormatTests ) target_link_libraries(FormatTests + PRIVATE clangBasic clangFormat clangFrontend diff --git a/unittests/Frontend/CMakeLists.txt b/unittests/Frontend/CMakeLists.txt index c1f4f18635..f3c4336ea2 100644 --- a/unittests/Frontend/CMakeLists.txt +++ b/unittests/Frontend/CMakeLists.txt @@ -11,6 +11,7 @@ add_clang_unittest(FrontendTests PCHPreambleTest.cpp ) target_link_libraries(FrontendTests + PRIVATE clangAST clangBasic clangFrontend diff --git a/unittests/Lex/CMakeLists.txt b/unittests/Lex/CMakeLists.txt index ef0f06c0b3..ea6f9fd234 100644 --- a/unittests/Lex/CMakeLists.txt +++ b/unittests/Lex/CMakeLists.txt @@ -10,6 +10,7 @@ add_clang_unittest(LexTests ) target_link_libraries(LexTests + PRIVATE clangAST clangBasic clangLex diff --git a/unittests/Rename/CMakeLists.txt b/unittests/Rename/CMakeLists.txt index cecb2d39b9..b625a7a691 100644 --- a/unittests/Rename/CMakeLists.txt +++ b/unittests/Rename/CMakeLists.txt @@ -14,6 +14,7 @@ add_clang_unittest(ClangRenameTests ) target_link_libraries(ClangRenameTests + PRIVATE clangAST clangASTMatchers clangBasic diff --git a/unittests/Rewrite/CMakeLists.txt b/unittests/Rewrite/CMakeLists.txt index bee7ff6d55..8edd9ba8f8 100644 --- a/unittests/Rewrite/CMakeLists.txt +++ b/unittests/Rewrite/CMakeLists.txt @@ -6,5 +6,6 @@ add_clang_unittest(RewriteTests RewriteBufferTest.cpp ) target_link_libraries(RewriteTests + PRIVATE clangRewrite ) diff --git a/unittests/Sema/CMakeLists.txt b/unittests/Sema/CMakeLists.txt index c25db814b7..16fae820df 100644 --- a/unittests/Sema/CMakeLists.txt +++ b/unittests/Sema/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_unittest(SemaTests ) target_link_libraries(SemaTests + PRIVATE clangAST clangBasic clangFrontend diff --git a/unittests/StaticAnalyzer/CMakeLists.txt b/unittests/StaticAnalyzer/CMakeLists.txt index 4aa5efba77..4ca0be50e5 100644 --- a/unittests/StaticAnalyzer/CMakeLists.txt +++ b/unittests/StaticAnalyzer/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_unittest(StaticAnalysisTests ) target_link_libraries(StaticAnalysisTests + PRIVATE clangBasic clangAnalysis clangStaticAnalyzerCore diff --git a/unittests/Tooling/CMakeLists.txt b/unittests/Tooling/CMakeLists.txt index f9ddf7ffc1..557d1007ae 100644 --- a/unittests/Tooling/CMakeLists.txt +++ b/unittests/Tooling/CMakeLists.txt @@ -35,6 +35,7 @@ add_clang_unittest(ToolingTests ) target_link_libraries(ToolingTests + PRIVATE clangAST clangASTMatchers clangBasic diff --git a/unittests/libclang/CMakeLists.txt b/unittests/libclang/CMakeLists.txt index 1cdc45e2d2..36f6089787 100644 --- a/unittests/libclang/CMakeLists.txt +++ b/unittests/libclang/CMakeLists.txt @@ -3,5 +3,6 @@ add_clang_unittest(libclangTests ) target_link_libraries(libclangTests + PRIVATE libclang )