Skip to content

Commit 5f510e5

Browse files
serge-sans-pailletstellar
serge-sans-paille
authored andcommitted
Update compiler extension integration into the build system
The approach here is to create a new (empty) component, `Extensions', where all statically compiled extensions dynamically register their dependencies. That way we're more natively compatible with LLVMBuild and llvm-config. Fixes: https://bugs.llvm.org/show_bug.cgi?id=44870 Differential Revision: https://reviews.llvm.org/D78192 (cherry picked from commit 8f766e3)
1 parent 0c05269 commit 5f510e5

File tree

12 files changed

+113
-22
lines changed

12 files changed

+113
-22
lines changed

clang/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
Core
66
Coroutines
77
Coverage
8+
Extensions
89
FrontendOpenMP
910
IPO
1011
IRReader
@@ -96,8 +97,6 @@ add_clang_library(clangCodeGen
9697
TargetInfo.cpp
9798
VarBypassDetector.cpp
9899

99-
ENABLE_PLUGINS
100-
101100
DEPENDS
102101
${codegen_deps}
103102

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ endfunction(set_windows_version_resource_properties)
404404
# )
405405
function(llvm_add_library name)
406406
cmake_parse_arguments(ARG
407-
"MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB;ENABLE_PLUGINS"
407+
"MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB"
408408
"OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH"
409409
"ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS"
410410
${ARGN})
@@ -418,9 +418,6 @@ function(llvm_add_library name)
418418
else()
419419
llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS})
420420
endif()
421-
if(ARG_ENABLE_PLUGINS)
422-
set_property(GLOBAL APPEND PROPERTY LLVM_PLUGIN_TARGETS ${name})
423-
endif()
424421

425422
if(ARG_MODULE)
426423
if(ARG_SHARED OR ARG_STATIC)
@@ -753,7 +750,7 @@ endmacro(add_llvm_library name)
753750

754751
macro(add_llvm_executable name)
755752
cmake_parse_arguments(ARG
756-
"DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS;ENABLE_PLUGINS"
753+
"DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS"
757754
"ENTITLEMENTS;BUNDLE_PATH"
758755
"DEPENDS"
759756
${ARGN})
@@ -840,9 +837,6 @@ macro(add_llvm_executable name)
840837
# API for all shared libaries loaded by this executable.
841838
target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB})
842839
endif()
843-
if(ARG_ENABLE_PLUGINS)
844-
set_property(GLOBAL APPEND PROPERTY LLVM_PLUGIN_TARGETS ${name})
845-
endif()
846840

847841
llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH})
848842
endmacro(add_llvm_executable name)
@@ -915,18 +909,18 @@ function(process_llvm_pass_plugins)
915909
include(LLVMConfigExtensions)
916910
endif()
917911

918-
# Add static plugins to each plugin target.
912+
# Add static plugins to the Extension component
919913
foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
920-
get_property(llvm_plugin_targets GLOBAL PROPERTY LLVM_PLUGIN_TARGETS)
921-
foreach(llvm_plugin_target ${llvm_plugin_targets})
922-
set_property(TARGET ${llvm_plugin_target} APPEND PROPERTY LINK_LIBRARIES ${llvm_extension})
923-
set_property(TARGET ${llvm_plugin_target} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension})
924-
endforeach()
914+
set_property(TARGET LLVMExtensions APPEND PROPERTY LINK_LIBRARIES ${llvm_extension})
915+
set_property(TARGET LLVMExtensions APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension})
925916
endforeach()
926917

927-
# Eventually generate the extension header, and store config to a cmake file
918+
# Eventually generate the extension headers, and store config to a cmake file
928919
# for usage in third-party configuration.
929920
if(ARG_GEN_CONFIG)
921+
922+
## Part 1: Extension header to be included whenever we need extension
923+
# processing.
930924
set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm)
931925
set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}")
932926
file(WRITE
@@ -949,6 +943,57 @@ function(process_llvm_pass_plugins)
949943
"${ExtensionDef}.tmp"
950944
"${ExtensionDef}")
951945
file(REMOVE "${ExtensionDef}.tmp")
946+
947+
## Part 2: Extension header that captures each extension dependency, to be
948+
# used by llvm-config.
949+
set(ExtensionDeps "${LLVM_BINARY_DIR}/tools/llvm-config/ExtensionDependencies.inc")
950+
951+
# Max needed to correctly size the required library array.
952+
set(llvm_plugin_max_deps_length 0)
953+
foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
954+
get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
955+
list(LENGTH llvm_plugin_deps llvm_plugin_deps_length)
956+
if(llvm_plugin_deps_length GREATER llvm_plugin_max_deps_length)
957+
set(llvm_plugin_max_deps_length ${llvm_plugin_deps_length})
958+
endif()
959+
endforeach()
960+
961+
list(LENGTH LLVM_STATIC_EXTENSIONS llvm_static_extension_count)
962+
file(WRITE
963+
"${ExtensionDeps}.tmp"
964+
"#include <array>\n\
965+
struct ExtensionDescriptor {\n\
966+
const char* Name;\n\
967+
const char* const RequiredLibraries[1 + 1 + ${llvm_plugin_max_deps_length}];\n\
968+
};\n\
969+
std::array<ExtensionDescriptor, ${llvm_static_extension_count}> AvailableExtensions{\n")
970+
971+
foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS})
972+
get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES)
973+
974+
file(APPEND "${ExtensionDeps}.tmp" "{\"${llvm_extension}\", {")
975+
foreach(llvm_plugin_dep ${llvm_plugin_deps})
976+
# Turn library dependency back to component name, if possible.
977+
# That way llvm-config can avoid redundant dependencies.
978+
STRING(REGEX REPLACE "^-l" "" plugin_dep_name ${llvm_plugin_dep})
979+
STRING(REGEX MATCH "^LLVM" is_llvm_library ${plugin_dep_name})
980+
if(is_llvm_library)
981+
STRING(REGEX REPLACE "^LLVM" "" plugin_dep_name ${plugin_dep_name})
982+
STRING(TOLOWER ${plugin_dep_name} plugin_dep_name)
983+
endif()
984+
file(APPEND "${ExtensionDeps}.tmp" "\"${plugin_dep_name}\", ")
985+
endforeach()
986+
987+
# Self + mandatory trailing null, because the number of RequiredLibraries differs between extensions.
988+
file(APPEND "${ExtensionDeps}.tmp" \"${llvm_extension}\", "nullptr}},\n")
989+
endforeach()
990+
file(APPEND "${ExtensionDeps}.tmp" "};\n")
991+
992+
# only replace if there's an actual change
993+
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
994+
"${ExtensionDeps}.tmp"
995+
"${ExtensionDeps}")
996+
file(REMOVE "${ExtensionDeps}.tmp")
952997
endif()
953998
endfunction()
954999

llvm/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_subdirectory(BinaryFormat)
99
add_subdirectory(Bitcode)
1010
add_subdirectory(Bitstream)
1111
add_subdirectory(DWARFLinker)
12+
add_subdirectory(Extensions)
1213
add_subdirectory(Frontend)
1314
add_subdirectory(Transforms)
1415
add_subdirectory(Linker)

llvm/lib/Extensions/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_llvm_component_library(LLVMExtensions
2+
Extensions.cpp
3+
)

llvm/lib/Extensions/Extensions.cpp

Whitespace-only changes.

llvm/lib/Extensions/LLVMBuild.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
;===- ./lib/Extensions/LLVMBuild.txt -------------------------------*- Conf -*--===;
2+
;
3+
; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
; See https://llvm.org/LICENSE.txt for license information.
5+
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
;
7+
;===------------------------------------------------------------------------===;
8+
;
9+
; This is an LLVMBuild description file for the components in this subdirectory.
10+
;
11+
; For more information on the LLVMBuild system, please see:
12+
;
13+
; http://llvm.org/docs/LLVMBuild.html
14+
;
15+
;===------------------------------------------------------------------------===;
16+
17+
[component_0]
18+
type = Library
19+
name = Extensions
20+
parent = Libraries
21+
required_libraries =

llvm/lib/LLVMBuild.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ subdirectories =
2525
Demangle
2626
DWARFLinker
2727
ExecutionEngine
28+
Extensions
2829
Frontend
2930
FuzzMutate
3031
LineEditor

llvm/lib/LTO/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ add_llvm_component_library(LLVMLTO
1010

1111
ADDITIONAL_HEADER_DIRS
1212
${LLVM_MAIN_INCLUDE_DIR}/llvm/LTO
13-
1413
DEPENDS
1514
intrinsics_gen
1615
llvm_vcsrevision_h

llvm/lib/LTO/LLVMBuild.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ required_libraries =
2525
BitWriter
2626
CodeGen
2727
Core
28+
Extensions
2829
IPO
2930
InstCombine
3031
Linker

llvm/tools/bugpoint/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
66
Analysis
77
BitWriter
88
CodeGen
9+
Extensions
910
Core
1011
IPO
1112
IRReader
@@ -32,8 +33,6 @@ add_llvm_tool(bugpoint
3233
ToolRunner.cpp
3334
bugpoint.cpp
3435

35-
ENABLE_PLUGINS
36-
3736
DEPENDS
3837
intrinsics_gen
3938
SUPPORT_PLUGINS

llvm/tools/llvm-config/llvm-config.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ using namespace llvm;
4646
// create entries for pseudo groups like x86 or all-targets.
4747
#include "LibraryDependencies.inc"
4848

49+
// Built-in extensions also register their dependencies, but in a separate file,
50+
// later in the process.
51+
#include "ExtensionDependencies.inc"
52+
4953
// LinkMode determines what libraries and flags are returned by llvm-config.
5054
enum LinkMode {
5155
// LinkModeAuto will link with the default link mode for the installation,
@@ -110,6 +114,25 @@ static void VisitComponent(const std::string &Name,
110114
GetComponentLibraryPath, Missing, DirSep);
111115
}
112116

117+
// Special handling for the special 'extensions' component. Its content is
118+
// not populated by llvm-build, but later in the process and loaded from
119+
// ExtensionDependencies.inc.
120+
if (Name == "extensions") {
121+
for (auto const &AvailableExtension : AvailableExtensions) {
122+
for (const char *const *Iter = &AvailableExtension.RequiredLibraries[0];
123+
*Iter; ++Iter) {
124+
AvailableComponent *AC = ComponentMap.lookup(*Iter);
125+
if (!AC) {
126+
RequiredLibs.push_back(*Iter);
127+
} else {
128+
VisitComponent(*Iter, ComponentMap, VisitedComponents, RequiredLibs,
129+
IncludeNonInstalled, GetComponentNames,
130+
GetComponentLibraryPath, Missing, DirSep);
131+
}
132+
}
133+
}
134+
}
135+
113136
if (GetComponentNames) {
114137
RequiredLibs.push_back(Name);
115138
return;

llvm/tools/opt/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(LLVM_LINK_COMPONENTS
99
CodeGen
1010
Core
1111
Coroutines
12+
Extensions
1213
IPO
1314
IRReader
1415
InstCombine
@@ -33,8 +34,6 @@ add_llvm_tool(opt
3334
PrintSCC.cpp
3435
opt.cpp
3536

36-
ENABLE_PLUGINS
37-
3837
DEPENDS
3938
intrinsics_gen
4039
SUPPORT_PLUGINS

0 commit comments

Comments
 (0)