llvm.org GIT mirror llvm / ecd5a90
tools/llvm-config: improve shared library support Summary: r252532 added support for reporting the monolithic library when LLVM_BUILD_LLVM_DYLIB is used. This would only be done if the individual components were not found, and the dynamic library is found. This diff extends this as follows: - If LLVM_LINK_LLVM_DYLIB is set, then prefer the shared library, even if all component libraries exist. - Two flags, --link-shared and --link-static are introduced to provide explicit guidance. If --link-shared is passed and the shared library does not exist, an error results. Additionally, changed the expected shared library names from (e.g.) LLVM-3.8.0 to LLVM-3.8. The former exists only in an installation (and then only in CMake builds I think?), and not in the build tree; this breaks usage of llvm-config during builds, e.g. by llvm-go. Reviewers: DiamondLovesYou, beanz Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D15033 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257003 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Wilkins 3 years ago
6 changed file(s) with 104 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
910910 set(cppflags "${cppflags} -I${d}")
911911 endforeach(d)
912912 set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
913 if (LLVM_LINK_LLVM_DYLIB)
914 set(linkmode "dylib")
915 else()
916 set(linkmode "component-libs")
917 endif()
918913 add_custom_command(OUTPUT ${binpath}
919 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}" "linkmode=${linkmode}"
914 COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "go=${GO_EXECUTABLE}" "cc=${cc}" "cxx=${cxx}" "cppflags=${cppflags}" "ldflags=${ldflags}"
920915 ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
921916 DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
922917 ${llvmlibs} ${ARG_DEPENDS}
2828 #define LLVM_BUILD_SYSTEM "@LLVM_BUILD_SYSTEM@"
2929 #define LLVM_HAS_RTTI "@LLVM_HAS_RTTI@"
3030 #define LLVM_ENABLE_DYLIB "@LLVM_BUILD_LLVM_DYLIB@"
31 #define LLVM_LINK_DYLIB "@LLVM_LINK_LLVM_DYLIB@"
3132 #define LLVM_ENABLE_SHARED "@LLVM_ENABLE_SHARED@"
3233 #define LLVM_DYLIB_COMPONENTS "@LLVM_DYLIB_COMPONENTS@"
34 #define LLVM_DYLIB_VERSION "@LLVM_DYLIB_VERSION@"
2525 set(LLVM_CXXFLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${COMPILE_FLAGS} ${LLVM_DEFINITIONS}")
2626 set(LLVM_BUILD_SYSTEM cmake)
2727 set(LLVM_HAS_RTTI ${LLVM_CONFIG_HAS_RTTI})
28 set(LLVM_DYLIB_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}${LLVM_VERSION_SUFFIX}")
2829
2930 # Use the C++ link flags, since they should be a superset of C link flags.
3031 set(LLVM_LDFLAGS "${CMAKE_CXX_LINK_FLAGS}")
4343 LLVM_HAS_RTTI := YES
4444 endif
4545
46 LLVM_DYLIB_VERSION := $(LLVM_VERSION_MAJOR).$(LLVM_VERSION_MINOR)$(LLVM_VERSION_SUFFIX)
47
4648 # This is blank for now. We need to be careful about adding stuff here:
4749 # LDFLAGS tend not to be portable, and we don't currently require the
4850 # user to use libtool when linking against LLVM.
8284 >> temp.sed
8385 $(Verb) $(ECHO) 's/@LLVM_HAS_RTTI@/$(LLVM_HAS_RTTI)/' \
8486 >> temp.sed
87 $(Verb) $(ECHO) 's/@LLVM_DYLIB_VERSION@/$(LLVM_DYLIB_VERSION)/' \
88 >> temp.sed
8589 $(Verb) $(SED) -f temp.sed < $< > $@
8690 $(Verb) $(RM) temp.sed
8791
2828 #include "llvm/Support/raw_ostream.h"
2929 #include
3030 #include
31 #include
3132 #include
32 #include
3333
3434 using namespace llvm;
3535
4444 // Not all components define a library, we also use "library groups" as a way to
4545 // create entries for pseudo groups like x86 or all-targets.
4646 #include "LibraryDependencies.inc"
47
48 // LinkMode determines what libraries and flags are returned by llvm-config.
49 enum LinkMode {
50 // LinkModeAuto will link with the default link mode for the installation,
51 // which is dependent on the value of LLVM_LINK_LLVM_DYLIB, and fall back
52 // to the alternative if the required libraries are not available.
53 LinkModeAuto = 0,
54
55 // LinkModeDyLib will link with the single dynamic library if it exists,
56 // and return an error if it does not exist.
57 LinkModeDyLib = 1,
58
59 // LinkModeStatic will link with the individual component static libraries
60 // if they exist, and fail if any one does not exist.
61 LinkModeStatic = 2,
62 };
4763
4864 /// \brief Traverse a single component adding to the topological ordering in
4965 /// \arg RequiredLibs.
5571 /// libraries.
5672 /// \param GetComponentNames - Get the component names instead of the
5773 /// library name.
58 static void VisitComponent(const std::string& Name,
59 const StringMap &ComponentMap,
60 std::set &VisitedComponents,
74 static void VisitComponent(const std::string &Name,
75 const StringMap &ComponentMap,
76 std::set &VisitedComponents,
6177 std::vector &RequiredLibs,
6278 bool IncludeNonInstalled, bool GetComponentNames,
63 const std::string *ActiveLibDir, bool *HasMissing) {
79 const std::string *ActiveLibDir,
80 std::vector *Missing) {
6481 // Lookup the component.
6582 AvailableComponent *AC = ComponentMap.lookup(Name);
6683 assert(AC && "Invalid component name!");
7996 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
8097 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
8198 RequiredLibs, IncludeNonInstalled, GetComponentNames,
82 ActiveLibDir, HasMissing);
99 ActiveLibDir, Missing);
83100 }
84101
85102 if (GetComponentNames) {
89106
90107 // Add to the required library list.
91108 if (AC->Library) {
92 if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) {
93 *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library);
109 if (Missing && ActiveLibDir) {
110 std::string path = *ActiveLibDir + "/" + AC->Library;
111 if (!sys::fs::exists(path))
112 Missing->push_back(path);
94113 }
95114 RequiredLibs.push_back(AC->Library);
96115 }
107126 static std::vector
108127 ComputeLibsForComponents(const std::vector &Components,
109128 bool IncludeNonInstalled, bool GetComponentNames,
110 const std::string *ActiveLibDir, bool *HasMissing) {
129 const std::string *ActiveLibDir,
130 std::vector *Missing) {
111131 std::vector RequiredLibs;
112132 std::set VisitedComponents;
113133
114134 // Build a map of component names to information.
115 StringMap*> ComponentMap;
135 StringMap *> ComponentMap;
116136 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
117137 AvailableComponent *AC = &AvailableComponents[i];
118138 ComponentMap[AC->Name] = AC;
132152
133153 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
134154 RequiredLibs, IncludeNonInstalled, GetComponentNames,
135 ActiveLibDir, HasMissing);
155 ActiveLibDir, Missing);
136156 }
137157
138158 // The list is now ordered with leafs first, we want the libraries to printed
178198 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
179199 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
180200 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
201 --link-shared Link the components as a shared library.\n\
202 --link-static Link the components as a static libraries.\n\
181203 Typical components:\n\
182204 all All LLVM libraries (default).\n\
183205 engine Either a native JIT or a bitcode interpreter.\n";
188210 std::string GetExecutablePath(const char *Argv0) {
189211 // This just needs to be some symbol in the binary; C++ doesn't
190212 // allow taking the address of ::main however.
191 void *P = (void*) (intptr_t) GetExecutablePath;
213 void *P = (void *)(intptr_t)GetExecutablePath;
192214 return llvm::sys::fs::getMainExecutable(Argv0, P);
193215 }
194216
242264 // Create an absolute path, and pop up one directory (we expect to be inside a
243265 // bin dir).
244266 sys::fs::make_absolute(CurrentPath);
245 CurrentExecPrefix = sys::path::parent_path(
246 sys::path::parent_path(CurrentPath)).str();
267 CurrentExecPrefix =
268 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
247269
248270 // Check to see if we are inside a development tree by comparing to possible
249271 // locations (prefix style or CMake style).
304326 }
305327
306328 // We need to include files from both the source and object trees.
307 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
308 "-I" + ActiveObjRoot + "/include");
329 ActiveIncludeOption =
330 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
309331 } else {
310332 ActivePrefix = CurrentExecPrefix;
311333 ActiveIncludeDir = ActivePrefix + "/include";
322344 /// in the first place. This can't be done at configure/build time.
323345
324346 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
325 StaticPrefix, StaticDir = "lib";
347 StaticPrefix, StaticDir = "lib";
326348 const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE));
327349 if (HostTriple.isOSWindows()) {
328350 SharedExt = "dll";
329 SharedVersionedExt = PACKAGE_VERSION ".dll";
351 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
330352 StaticExt = "a";
331353 SharedDir = ActiveBinDir;
332354 StaticDir = ActiveLibDir;
333355 StaticPrefix = SharedPrefix = "lib";
334356 } else if (HostTriple.isOSDarwin()) {
335357 SharedExt = "dylib";
336 SharedVersionedExt = PACKAGE_VERSION ".dylib";
358 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
337359 StaticExt = "a";
338360 StaticDir = SharedDir = ActiveLibDir;
339361 StaticPrefix = SharedPrefix = "lib";
340362 } else {
341363 // default to the unix values:
342364 SharedExt = "so";
343 SharedVersionedExt = PACKAGE_VERSION ".so";
365 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
344366 StaticExt = "a";
345367 StaticDir = SharedDir = ActiveLibDir;
346368 StaticPrefix = SharedPrefix = "lib";
361383
362384 bool DyLibExists = false;
363385 const std::string DyLibName =
364 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
386 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
387
388 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
389 // for "--libs", etc, if they exist. This behaviour can be overridden with
390 // --link-static or --link-shared.
391 bool LinkDyLib = (std::strcmp(LLVM_LINK_DYLIB, "ON") == 0);
365392
366393 if (BuiltDyLib) {
367394 DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName);
368 }
395 if (!DyLibExists) {
396 // The shared library does not exist: don't error unless the user
397 // explicitly passes --link-shared.
398 LinkDyLib = false;
399 }
400 }
401 LinkMode LinkMode = LinkDyLib ? LinkModeDyLib : LinkModeAuto;
369402
370403 /// Get the component's library name without the lib prefix and the
371404 /// extension. Returns true if Lib is in a recognized format.
500533 OS << ActivePrefix << '\n';
501534 } else if (Arg == "--src-root") {
502535 OS << LLVM_SRC_ROOT << '\n';
536 } else if (Arg == "--link-shared") {
537 LinkMode = LinkModeDyLib;
538 } else if (Arg == "--link-static") {
539 LinkMode = LinkModeStatic;
503540 } else {
504541 usage();
505542 }
510547
511548 if (!HasAnyOption)
512549 usage();
550
551 if (LinkMode == LinkModeDyLib && !DyLibExists) {
552 errs() << "llvm-config: error: " << DyLibName << " is missing\n\n";
553 usage();
554 }
513555
514556 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
515557 PrintSharedMode) {
524566 Components.push_back("all");
525567
526568 // Construct the list of all the required libraries.
527 bool HasMissing = false;
569 std::vector MissingLibs;
528570 std::vector RequiredLibs =
529571 ComputeLibsForComponents(Components,
530572 /*IncludeNonInstalled=*/IsInDevelopmentTree,
531 false, &ActiveLibDir, &HasMissing);
573 false, &ActiveLibDir, &MissingLibs);
574 if (!MissingLibs.empty()) {
575 switch (LinkMode) {
576 case LinkModeDyLib:
577 break;
578 case LinkModeAuto:
579 if (DyLibExists) {
580 LinkMode = LinkModeDyLib;
581 break;
582 }
583 errs()
584 << "llvm-config: error: component libraries and shared library\n\n";
585 // fall through
586 case LinkModeStatic:
587 for (auto &Lib : MissingLibs)
588 errs() << "llvm-config: error: missing: " << Lib << "\n";
589 return 1;
590 }
591 } else if (LinkMode == LinkModeAuto) {
592 LinkMode = LinkModeStatic;
593 }
532594
533595 if (PrintSharedMode) {
534596 std::unordered_set FullDyLibComponents;
548610 }
549611 FullDyLibComponents.clear();
550612
551 if (HasMissing && DyLibExists) {
613 if (LinkMode == LinkModeDyLib) {
552614 OS << "shared\n";
553615 return 0;
554616 } else {
580642 }
581643 };
582644
583 if (HasMissing && DyLibExists) {
645 if (LinkMode == LinkModeDyLib) {
584646 PrintForLib(DyLibName, true);
585647 } else {
586648 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
8787 return outstr
8888 }
8989
90 func llvmFlags(linkmode string) compilerFlags {
91 ldflags := llvmConfig("--ldflags")
92 switch linkmode {
93 case linkmodeComponentLibs:
94 ldflags += " " + llvmConfig(append([]string{"--libs"}, components...)...)
95 case linkmodeDylib:
96 ldflags += " -lLLVM"
97 default:
98 panic("invalid linkmode: " + linkmode)
99 }
100 ldflags += " " + llvmConfig("--system-libs")
90 func llvmFlags() compilerFlags {
91 ldflags := llvmConfig("--ldflags", "--libs", "--system-libs")
10192 if runtime.GOOS != "darwin" {
10293 // OS X doesn't like -rpath with cgo. See:
10394 // https://code.google.com/p/go/issues/detail?id=7293
132123 fmt.Println(strings.Join(components, " "))
133124 }
134125
135 func printConfig(linkmode string) {
136 flags := llvmFlags(linkmode)
126 func printConfig() {
127 flags := llvmFlags()
137128
138129 fmt.Printf(`// +build !byollvm
139130
152143 `, flags.cpp, flags.cxx, flags.ld)
153144 }
154145
155 func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode string) {
146 func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags string) {
156147 args = addTag(args, "byollvm")
157148
158149 srcdir := llvmConfig("--src-root")
181172 newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...)
182173 newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator))
183174
184 flags := llvmFlags(linkmode)
175 flags := llvmFlags()
185176
186177 newenv := []string{
187178 "CC=" + cc,
249240 ldflags := os.Getenv("CGO_LDFLAGS")
250241 gocmd := "go"
251242 llgo := ""
252 linkmode := linkmodeComponentLibs
253243
254244 flags := []struct {
255245 name string
261251 {"llgo", &llgo},
262252 {"cppflags", &cppflags},
263253 {"ldflags", &ldflags},
264 {"linkmode", &linkmode},
265254 }
266255
267256 args := os.Args[1:]
282271
283272 switch args[0] {
284273 case "build", "get", "install", "run", "test":
285 runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode)
274 runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags)
286275 case "print-components":
287276 printComponents()
288277 case "print-config":
289 printConfig(linkmode)
278 printConfig()
290279 default:
291280 usage()
292281 }