llvm.org GIT mirror llvm / 6c6be14
Fix `llvm-config` to adapt to the install environment. Summary: This patch does a couple of things: - Adds a new argument `--shared-mode` which accepts a list of components and prints whether or not the provided components need to be linked statically or shared. - Fixes `--libnames` when CMake BUILD_SHARED_LIBS is used. - Fixes `--libnames`, `--libs`, and `--libfiles` for dylib when static components aren't installed. - Fixes `--libnames`, `--libs`, `--libfiles`, and `--components` to use LLVM_DYLIB_COMPONENTS as the component manifest for dylib linking. - Uses the host platform's usual convention for filename extensions and such, instead of always defaulting to Unix-izms. Because I don't own a Mac, I am not able to test the Mac platform dependent stuff locally. If someone would be willing to run a build for me on their machine (unless there's a better option), I'd appreciate it. Reviewers: jfb, brad.king, whitequark, beanz Subscribers: beanz, jauhien, llvm-commits Differential Revision: http://reviews.llvm.org/D13198 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252532 91177308-0d34-0410-b5e6-96231b3b80d8 Richard Diamond 5 years ago
5 changed file(s) with 263 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
276276
277277 build-deplibs: $(OutputLibs)
278278
279 $(OcamlDir)/%.so: $(LibDir)/%.so
280 $(Verb) ln -sf $< $@
279281 $(OcamlDir)/%.a: $(LibDir)/%.a
280282 $(Verb) ln -sf $< $@
281283
2727 #define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@"
2828 #define LLVM_BUILD_SYSTEM "@LLVM_BUILD_SYSTEM@"
2929 #define LLVM_HAS_RTTI "@LLVM_HAS_RTTI@"
30 #define LLVM_ENABLE_DYLIB "@LLVM_BUILD_LLVM_DYLIB@"
31 #define LLVM_ENABLE_SHARED "@LLVM_ENABLE_SHARED@"
32 #define LLVM_DYLIB_COMPONENTS "@LLVM_DYLIB_COMPONENTS@"
3030 set(LLVM_LDFLAGS "${CMAKE_CXX_LINK_FLAGS}")
3131 set(LLVM_BUILDMODE ${CMAKE_BUILD_TYPE})
3232 set(LLVM_SYSTEM_LIBS ${SYSTEM_LIBS})
33 if(BUILD_SHARED_LIBS)
34 set(LLVM_ENABLE_SHARED ON)
35 else()
36 set(LLVM_ENABLE_SHARED OFF)
37 endif()
3338 string(REPLACE ";" " " LLVM_TARGETS_BUILT "${LLVM_TARGETS_TO_BUILD}")
3439 configure_file(${BUILDVARIABLES_SRCPATH} ${BUILDVARIABLES_OBJPATH} @ONLY)
3540
7070 >> temp.sed
7171 $(Verb) $(ECHO) 's/@LLVM_TARGETS_BUILT@/$(subst /,\/,$(TARGETS_TO_BUILD))/' \
7272 >> temp.sed
73 $(if $(filter-out $(ENABLE_SHARED),0),\
74 $(Verb) $(ECHO) 's/@LLVM_BUILD_LLVM_DYLIB@/ON/',\
75 $(Verb) $(ECHO) 's/@LLVM_BUILD_LLVM_DYLIB@/OFF/') \
76 >> temp.sed
77 $(Verb) $(ECHO) 's/@LLVM_ENABLE_SHARED@/OFF/' \
78 >> temp.sed
79 $(Verb) $(ECHO) 's/@LLVM_DYLIB_COMPONENTS@/all/' \
80 >> temp.sed
7381 $(Verb) $(ECHO) 's/@LLVM_BUILD_SYSTEM@/autoconf/' \
7482 >> temp.sed
7583 $(Verb) $(ECHO) 's/@LLVM_HAS_RTTI@/$(LLVM_HAS_RTTI)/' \
2929 #include
3030 #include
3131 #include
32 #include
3233
3334 using namespace llvm;
3435
5051 /// \param Name - The component to traverse.
5152 /// \param ComponentMap - A prebuilt map of component names to descriptors.
5253 /// \param VisitedComponents [in] [out] - The set of already visited components.
53 /// \param RequiredLibs [out] - The ordered list of required libraries.
54 /// \param RequiredLibs [out] - The ordered list of required
55 /// libraries.
56 /// \param GetComponentNames - Get the component names instead of the
57 /// library name.
5458 static void VisitComponent(StringRef Name,
5559 const StringMap &ComponentMap,
5660 std::set &VisitedComponents,
5761 std::vector &RequiredLibs,
58 bool IncludeNonInstalled) {
62 bool IncludeNonInstalled, bool GetComponentNames,
63 const std::string *ActiveLibDir, bool *HasMissing) {
5964 // Lookup the component.
6065 AvailableComponent *AC = ComponentMap.lookup(Name);
6166 assert(AC && "Invalid component name!");
7378 // Otherwise, visit all the dependencies.
7479 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
7580 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
76 RequiredLibs, IncludeNonInstalled);
81 RequiredLibs, IncludeNonInstalled, GetComponentNames,
82 ActiveLibDir, HasMissing);
83 }
84
85 if (GetComponentNames) {
86 RequiredLibs.push_back(Name);
87 return;
7788 }
7889
7990 // Add to the required library list.
80 if (AC->Library)
91 if (AC->Library) {
92 if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) {
93 *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library);
94 }
8195 RequiredLibs.push_back(AC->Library);
96 }
8297 }
8398
8499 /// \brief Compute the list of required libraries for a given list of
90105 /// are required to link the given components.
91106 /// \param IncludeNonInstalled - Whether non-installed components should be
92107 /// reported.
108 /// \param GetComponentNames - True if one would prefer the component names.
93109 static void ComputeLibsForComponents(const std::vector &Components,
94110 std::vector &RequiredLibs,
95 bool IncludeNonInstalled) {
111 bool IncludeNonInstalled, bool GetComponentNames,
112 const std::string *ActiveLibDir,
113 bool *HasMissing) {
96114 std::set VisitedComponents;
97115
98116 // Build a map of component names to information.
115133 }
116134
117135 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
118 RequiredLibs, IncludeNonInstalled);
136 RequiredLibs, IncludeNonInstalled, GetComponentNames,
137 ActiveLibDir, HasMissing);
119138 }
120139
121140 // The list is now ordered with leafs first, we want the libraries to printed
158177 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
159178 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
160179 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
180 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
161181 Typical components:\n\
162182 all All LLVM libraries (default).\n\
163183 engine Either a native JIT or a bitcode interpreter.\n";
172192 return llvm::sys::fs::getMainExecutable(Argv0, P);
173193 }
174194
195 /// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
196 /// the full list of components.
197 std::vector GetAllDyLibComponents(const bool IsInDevelopmentTree,
198 const bool GetComponentNames) {
199 std::vector DyLibComponents;
200 {
201 StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
202 size_t Offset = 0;
203 while (true) {
204 const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
205 DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset));
206 if (NextOffset == std::string::npos) {
207 break;
208 }
209 Offset = NextOffset + 1;
210 }
211
212 assert(DyLibComponents.size() > 0);
213 }
214
215 std::vector Components;
216 ComputeLibsForComponents(DyLibComponents, Components,
217 /*IncludeNonInstalled=*/IsInDevelopmentTree,
218 GetComponentNames, nullptr, nullptr);
219
220 return std::move(Components);
221 }
222
175223 int main(int argc, char **argv) {
176224 std::vector Components;
177225 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
178 bool PrintSystemLibs = false;
226 bool PrintSystemLibs = false, PrintSharedMode = false;
179227 bool HasAnyOption = false;
180228
181229 // llvm-config is designed to support being run both from a development tree
269317 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
270318 ActiveIncludeOption = "-I" + ActiveIncludeDir;
271319 }
320
321 /// We only use `shared library` mode in cases where the static library form
322 /// of the components provided are not available; note however that this is
323 /// skipped if we're run from within the build dir. However, once installed,
324 /// we still need to provide correct output when the static archives are
325 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
326 /// in the first place. This can't be done at configure/build time.
327
328 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
329 StaticPrefix, StaticDir = "lib";
330 const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE));
331 if (HostTriple.isOSWindows()) {
332 SharedExt = "dll";
333 SharedVersionedExt = PACKAGE_VERSION ".dll";
334 StaticExt = "a";
335 SharedDir = ActiveBinDir;
336 StaticDir = ActiveLibDir;
337 StaticPrefix = SharedPrefix = "";
338 } else if (HostTriple.isOSDarwin()) {
339 SharedExt = "dylib";
340 SharedVersionedExt = PACKAGE_VERSION ".dylib";
341 StaticExt = "a";
342 StaticDir = SharedDir = ActiveLibDir;
343 StaticPrefix = SharedPrefix = "lib";
344 } else {
345 // default to the unix values:
346 SharedExt = "so";
347 SharedVersionedExt = PACKAGE_VERSION ".so";
348 StaticExt = "a";
349 StaticDir = SharedDir = ActiveLibDir;
350 StaticPrefix = SharedPrefix = "lib";
351 }
352
353 const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
354
355 enum { CMake, AutoConf } ConfigTool;
356 if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) {
357 ConfigTool = CMake;
358 } else {
359 ConfigTool = AutoConf;
360 }
361
362 /// CMake style shared libs, ie each component is in a shared library.
363 const bool BuiltSharedLibs =
364 (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0);
365
366 bool DyLibExists = false;
367 const std::string DyLibName =
368 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
369
370 if (BuiltDyLib) {
371 DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName);
372 }
373
374 /// Get the component's library name without the lib prefix and the
375 /// extension. Returns true if Lib is in a recognized format.
376 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
377 StringRef &Out) {
378 if (Lib.startswith("lib")) {
379 unsigned FromEnd;
380 if (Lib.endswith(StaticExt)) {
381 FromEnd = StaticExt.size() + 1;
382 } else if (Lib.endswith(SharedExt)) {
383 FromEnd = SharedExt.size() + 1;
384 } else {
385 FromEnd = 0;
386 }
387
388 if (FromEnd != 0) {
389 Out = Lib.slice(3, Lib.size() - FromEnd);
390 return true;
391 }
392 }
393
394 return false;
395 };
396 /// Maps Unixizms to the host platform.
397 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
398 const bool ForceShared) {
399 std::string LibFileName = Lib;
400 StringRef LibName;
401 if (GetComponentLibraryNameSlice(Lib, LibName)) {
402 if (BuiltSharedLibs || ForceShared) {
403 LibFileName = (SharedPrefix + LibName + "." + SharedExt).str();
404 } else {
405 // default to static
406 LibFileName = (StaticPrefix + LibName + "." + StaticExt).str();
407 }
408 }
409
410 return LibFileName;
411 };
412 /// Get the full path for a possibly shared component library.
413 auto GetComponentLibraryPath = [&](const StringRef &Name,
414 const bool ForceShared) {
415 auto LibFileName = GetComponentLibraryFileName(Name, ForceShared);
416 if (BuiltSharedLibs || ForceShared) {
417 return (SharedDir + "/" + LibFileName).str();
418 } else {
419 return (StaticDir + "/" + LibFileName).str();
420 }
421 };
272422
273423 raw_ostream &OS = outs();
274424 for (int i = 1; i != argc; ++i) {
303453 } else if (Arg == "--libfiles") {
304454 PrintLibFiles = true;
305455 } else if (Arg == "--components") {
456 /// If there are missing static archives and a dylib was
457 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
458 /// in the manifest.
459 std::vector Components;
306460 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
307461 // Only include non-installed components when in a development tree.
308462 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
309463 continue;
310464
311 OS << ' ';
312 OS << AvailableComponents[j].Name;
465 Components.push_back(AvailableComponents[j].Name);
466 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
467 if (DyLibExists &&
468 !sys::fs::exists(GetComponentLibraryPath(
469 AvailableComponents[j].Library, false))) {
470 Components = GetAllDyLibComponents(IsInDevelopmentTree, true);
471 std::sort(Components.begin(), Components.end());
472 break;
473 }
474 }
475 }
476
477 for (unsigned I = 0; I < Components.size(); ++I) {
478 if (I) {
479 OS << ' ';
480 }
481
482 OS << Components[I];
313483 }
314484 OS << '\n';
315485 } else if (Arg == "--targets-built") {
328498 OS << LLVM_BUILD_SYSTEM << '\n';
329499 } else if (Arg == "--has-rtti") {
330500 OS << LLVM_HAS_RTTI << '\n';
501 } else if (Arg == "--shared-mode") {
502 PrintSharedMode = true;
331503 } else if (Arg == "--obj-root") {
332504 OS << ActivePrefix << '\n';
333505 } else if (Arg == "--src-root") {
343515 if (!HasAnyOption)
344516 usage();
345517
346 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
518 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
519 PrintSharedMode) {
520
521 if (PrintSharedMode && BuiltSharedLibs) {
522 OS << "shared\n";
523 return 0;
524 }
525
347526 // If no components were specified, default to "all".
348527 if (Components.empty())
349528 Components.push_back("all");
350529
351530 // Construct the list of all the required libraries.
352531 std::vector RequiredLibs;
532 bool HasMissing = false;
353533 ComputeLibsForComponents(Components, RequiredLibs,
354 /*IncludeNonInstalled=*/IsInDevelopmentTree);
534 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
535 &ActiveLibDir, &HasMissing);
536
537 if (PrintSharedMode) {
538 std::unordered_set FullDyLibComponents;
539 std::vector DyLibComponents =
540 GetAllDyLibComponents(IsInDevelopmentTree, false);
541
542 for (auto &Component : DyLibComponents) {
543 FullDyLibComponents.insert(Component);
544 }
545 DyLibComponents.clear();
546
547 for (auto &Lib : RequiredLibs) {
548 if (!FullDyLibComponents.count(Lib)) {
549 OS << "static\n";
550 return 0;
551 }
552 }
553 FullDyLibComponents.clear();
554
555 if (HasMissing && DyLibExists) {
556 OS << "shared\n";
557 return 0;
558 } else {
559 OS << "static\n";
560 return 0;
561 }
562 }
355563
356564 if (PrintLibs || PrintLibNames || PrintLibFiles) {
357 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
358 StringRef Lib = RequiredLibs[i];
359 if (i)
360 OS << ' ';
361
565
566 auto PrintForLib = [&](const StringRef &Lib, const bool ForceShared) {
362567 if (PrintLibNames) {
363 OS << Lib;
568 OS << GetComponentLibraryFileName(Lib, ForceShared);
364569 } else if (PrintLibFiles) {
365 OS << ActiveLibDir << '/' << Lib;
570 OS << GetComponentLibraryPath(Lib, ForceShared);
366571 } else if (PrintLibs) {
367572 // If this is a typical library name, include it using -l.
368 if (Lib.startswith("lib") && Lib.endswith(".a")) {
369 OS << "-l" << Lib.slice(3, Lib.size()-2);
370 continue;
573 StringRef LibName;
574 if (Lib.startswith("lib")) {
575 if (GetComponentLibraryNameSlice(Lib, LibName)) {
576 OS << "-l" << LibName;
577 } else {
578 OS << "-l:" << GetComponentLibraryFileName(Lib, ForceShared);
579 }
580 } else {
581 // Otherwise, print the full path.
582 OS << GetComponentLibraryPath(Lib, ForceShared);
371583 }
372
373 // Otherwise, print the full path.
374 OS << ActiveLibDir << '/' << Lib;
584 }
585 };
586
587 if (HasMissing && DyLibExists) {
588 PrintForLib(DyLibName, true);
589 } else {
590 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
591 StringRef Lib = RequiredLibs[i];
592 if (i)
593 OS << ' ';
594
595 PrintForLib(Lib, false);
375596 }
376597 }
377598 OS << '\n';