1 //===-- llvm-config.cpp - LLVM project configuration utility --------------===//
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 tool encapsulates information about an LLVM project configuration for
10 // use by other project's build environments (to determine installed path,
11 // available features, required libraries, etc.).
12 //
13 // Note that although this tool *may* be used by some parts of LLVM's build
14 // itself (i.e., the Makefiles use it to compute required libraries when linking
15 // tools), this tool is primarily designed to support external projects.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Config/config.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/WithColor.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <cstdlib>
31 #include <set>
32 #include <unordered_set>
33 #include <vector>
34
35 using namespace llvm;
36
37 // Include the build time variables we can report to the user. This is generated
38 // at build time from the BuildVariables.inc.in file by the build system.
39 #include "BuildVariables.inc"
40
41 // Include the component table. This creates an array of struct
42 // AvailableComponent entries, which record the component name, library name,
43 // and required components for all of the available libraries.
44 //
45 // Not all components define a library, we also use "library groups" as a way to
46 // create entries for pseudo groups like x86 or all-targets.
47 #include "LibraryDependencies.inc"
48
49 // Built-in extensions also register their dependencies, but in a separate file,
50 // later in the process.
51 #include "ExtensionDependencies.inc"
52
53 // LinkMode determines what libraries and flags are returned by llvm-config.
54 enum LinkMode {
55 // LinkModeAuto will link with the default link mode for the installation,
56 // which is dependent on the value of LLVM_LINK_LLVM_DYLIB, and fall back
57 // to the alternative if the required libraries are not available.
58 LinkModeAuto = 0,
59
60 // LinkModeShared will link with the dynamic component libraries if they
61 // exist, and return an error otherwise.
62 LinkModeShared = 1,
63
64 // LinkModeStatic will link with the static component libraries if they
65 // exist, and return an error otherwise.
66 LinkModeStatic = 2,
67 };
68
69 /// Traverse a single component adding to the topological ordering in
70 /// \arg RequiredLibs.
71 ///
72 /// \param Name - The component to traverse.
73 /// \param ComponentMap - A prebuilt map of component names to descriptors.
74 /// \param VisitedComponents [in] [out] - The set of already visited components.
75 /// \param RequiredLibs [out] - The ordered list of required
76 /// libraries.
77 /// \param GetComponentNames - Get the component names instead of the
78 /// library name.
VisitComponent(const std::string & Name,const StringMap<AvailableComponent * > & ComponentMap,std::set<AvailableComponent * > & VisitedComponents,std::vector<std::string> & RequiredLibs,bool IncludeNonInstalled,bool GetComponentNames,const std::function<std::string (const StringRef &)> * GetComponentLibraryPath,std::vector<std::string> * Missing,const std::string & DirSep)79 static void VisitComponent(const std::string &Name,
80 const StringMap<AvailableComponent *> &ComponentMap,
81 std::set<AvailableComponent *> &VisitedComponents,
82 std::vector<std::string> &RequiredLibs,
83 bool IncludeNonInstalled, bool GetComponentNames,
84 const std::function<std::string(const StringRef &)>
85 *GetComponentLibraryPath,
86 std::vector<std::string> *Missing,
87 const std::string &DirSep) {
88 // Lookup the component.
89 AvailableComponent *AC = ComponentMap.lookup(Name);
90 if (!AC) {
91 errs() << "Can't find component: '" << Name << "' in the map. Available components are: ";
92 for (const auto &Component : ComponentMap) {
93 errs() << "'" << Component.first() << "' ";
94 }
95 errs() << "\n";
96 report_fatal_error("abort");
97 }
98 assert(AC && "Invalid component name!");
99
100 // Add to the visited table.
101 if (!VisitedComponents.insert(AC).second) {
102 // We are done if the component has already been visited.
103 return;
104 }
105
106 // Only include non-installed components if requested.
107 if (!AC->IsInstalled && !IncludeNonInstalled)
108 return;
109
110 // Otherwise, visit all the dependencies.
111 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
112 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
113 RequiredLibs, IncludeNonInstalled, GetComponentNames,
114 GetComponentLibraryPath, Missing, DirSep);
115 }
116
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
136 if (GetComponentNames) {
137 RequiredLibs.push_back(Name);
138 return;
139 }
140
141 // Add to the required library list.
142 if (AC->Library) {
143 if (Missing && GetComponentLibraryPath) {
144 std::string path = (*GetComponentLibraryPath)(AC->Library);
145 if (DirSep == "\\") {
146 std::replace(path.begin(), path.end(), '/', '\\');
147 }
148 if (!sys::fs::exists(path))
149 Missing->push_back(path);
150 }
151 RequiredLibs.push_back(AC->Library);
152 }
153 }
154
155 /// Compute the list of required libraries for a given list of
156 /// components, in an order suitable for passing to a linker (that is, libraries
157 /// appear prior to their dependencies).
158 ///
159 /// \param Components - The names of the components to find libraries for.
160 /// \param IncludeNonInstalled - Whether non-installed components should be
161 /// reported.
162 /// \param GetComponentNames - True if one would prefer the component names.
ComputeLibsForComponents(const std::vector<StringRef> & Components,bool IncludeNonInstalled,bool GetComponentNames,const std::function<std::string (const StringRef &)> * GetComponentLibraryPath,std::vector<std::string> * Missing,const std::string & DirSep)163 static std::vector<std::string> ComputeLibsForComponents(
164 const std::vector<StringRef> &Components, bool IncludeNonInstalled,
165 bool GetComponentNames, const std::function<std::string(const StringRef &)>
166 *GetComponentLibraryPath,
167 std::vector<std::string> *Missing, const std::string &DirSep) {
168 std::vector<std::string> RequiredLibs;
169 std::set<AvailableComponent *> VisitedComponents;
170
171 // Build a map of component names to information.
172 StringMap<AvailableComponent *> ComponentMap;
173 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
174 AvailableComponent *AC = &AvailableComponents[i];
175 ComponentMap[AC->Name] = AC;
176 }
177
178 // Visit the components.
179 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
180 // Users are allowed to provide mixed case component names.
181 std::string ComponentLower = Components[i].lower();
182
183 // Validate that the user supplied a valid component name.
184 if (!ComponentMap.count(ComponentLower)) {
185 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
186 << "\n";
187 exit(1);
188 }
189
190 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
191 RequiredLibs, IncludeNonInstalled, GetComponentNames,
192 GetComponentLibraryPath, Missing, DirSep);
193 }
194
195 // The list is now ordered with leafs first, we want the libraries to printed
196 // in the reverse order of dependency.
197 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
198
199 return RequiredLibs;
200 }
201
202 /* *** */
203
usage()204 static void usage() {
205 errs() << "\
206 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
207 \n\
208 Get various configuration information needed to compile programs which use\n\
209 LLVM. Typically called from 'configure' scripts. Examples:\n\
210 llvm-config --cxxflags\n\
211 llvm-config --ldflags\n\
212 llvm-config --libs engine bcreader scalaropts\n\
213 \n\
214 Options:\n\
215 --version Print LLVM version.\n\
216 --prefix Print the installation prefix.\n\
217 --src-root Print the source root LLVM was built from.\n\
218 --obj-root Print the object root used to build LLVM.\n\
219 --bindir Directory containing LLVM executables.\n\
220 --includedir Directory containing LLVM headers.\n\
221 --libdir Directory containing LLVM libraries.\n\
222 --cmakedir Directory containing LLVM cmake modules.\n\
223 --cppflags C preprocessor flags for files that include LLVM headers.\n\
224 --cflags C compiler flags for files that include LLVM headers.\n\
225 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
226 --ldflags Print Linker flags.\n\
227 --system-libs System Libraries needed to link against LLVM components.\n\
228 --libs Libraries needed to link against LLVM components.\n\
229 --libnames Bare library names for in-tree builds.\n\
230 --libfiles Fully qualified library filenames for makefile depends.\n\
231 --components List of all possible components.\n\
232 --targets-built List of all targets currently built.\n\
233 --host-target Target triple used to configure LLVM.\n\
234 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
235 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
236 --build-system Print the build system used to build LLVM (always cmake).\n\
237 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
238 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
239 --link-shared Link the components as shared libraries.\n\
240 --link-static Link the component libraries statically.\n\
241 --ignore-libllvm Ignore libLLVM and link component libraries instead.\n\
242 Typical components:\n\
243 all All LLVM libraries (default).\n\
244 engine Either a native JIT or a bitcode interpreter.\n";
245 exit(1);
246 }
247
248 /// Compute the path to the main executable.
GetExecutablePath(const char * Argv0)249 std::string GetExecutablePath(const char *Argv0) {
250 // This just needs to be some symbol in the binary; C++ doesn't
251 // allow taking the address of ::main however.
252 void *P = (void *)(intptr_t)GetExecutablePath;
253 return llvm::sys::fs::getMainExecutable(Argv0, P);
254 }
255
256 /// Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
257 /// the full list of components.
GetAllDyLibComponents(const bool IsInDevelopmentTree,const bool GetComponentNames,const std::string & DirSep)258 std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
259 const bool GetComponentNames,
260 const std::string &DirSep) {
261 std::vector<StringRef> DyLibComponents;
262
263 StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
264 size_t Offset = 0;
265 while (true) {
266 const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
267 DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset-Offset));
268 if (NextOffset == std::string::npos) {
269 break;
270 }
271 Offset = NextOffset + 1;
272 }
273
274 assert(!DyLibComponents.empty());
275
276 return ComputeLibsForComponents(DyLibComponents,
277 /*IncludeNonInstalled=*/IsInDevelopmentTree,
278 GetComponentNames, nullptr, nullptr, DirSep);
279 }
280
main(int argc,char ** argv)281 int main(int argc, char **argv) {
282 std::vector<StringRef> Components;
283 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
284 bool PrintSystemLibs = false, PrintSharedMode = false;
285 bool HasAnyOption = false;
286
287 // llvm-config is designed to support being run both from a development tree
288 // and from an installed path. We try and auto-detect which case we are in so
289 // that we can report the correct information when run from a development
290 // tree.
291 bool IsInDevelopmentTree;
292 enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
293 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
294 std::string CurrentExecPrefix;
295 std::string ActiveObjRoot;
296
297 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
298 char const *build_mode = LLVM_BUILDMODE;
299 #if defined(CMAKE_CFG_INTDIR)
300 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
301 build_mode = CMAKE_CFG_INTDIR;
302 #endif
303
304 // Create an absolute path, and pop up one directory (we expect to be inside a
305 // bin dir).
306 sys::fs::make_absolute(CurrentPath);
307 CurrentExecPrefix =
308 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
309
310 // Check to see if we are inside a development tree by comparing to possible
311 // locations (prefix style or CMake style).
312 if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
313 IsInDevelopmentTree = true;
314 DevelopmentTreeLayout = CMakeStyle;
315 ActiveObjRoot = LLVM_OBJ_ROOT;
316 } else if (sys::fs::equivalent(sys::path::parent_path(CurrentExecPrefix),
317 LLVM_OBJ_ROOT)) {
318 IsInDevelopmentTree = true;
319 DevelopmentTreeLayout = CMakeBuildModeStyle;
320 ActiveObjRoot = LLVM_OBJ_ROOT;
321 } else {
322 IsInDevelopmentTree = false;
323 DevelopmentTreeLayout = CMakeStyle; // Initialized to avoid warnings.
324 }
325
326 // Compute various directory locations based on the derived location
327 // information.
328 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir,
329 ActiveCMakeDir;
330 std::string ActiveIncludeOption;
331 if (IsInDevelopmentTree) {
332 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
333 ActivePrefix = CurrentExecPrefix;
334
335 // CMake organizes the products differently than a normal prefix style
336 // layout.
337 switch (DevelopmentTreeLayout) {
338 case CMakeStyle:
339 ActiveBinDir = ActiveObjRoot + "/bin";
340 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
341 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
342 break;
343 case CMakeBuildModeStyle:
344 // FIXME: Should we consider the build-mode-specific path as the prefix?
345 ActivePrefix = ActiveObjRoot;
346 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
347 ActiveLibDir =
348 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
349 // The CMake directory isn't separated by build mode.
350 ActiveCMakeDir =
351 ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX + "/cmake/llvm";
352 break;
353 }
354
355 // We need to include files from both the source and object trees.
356 ActiveIncludeOption =
357 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
358 } else {
359 ActivePrefix = CurrentExecPrefix;
360 ActiveIncludeDir = ActivePrefix + "/include";
361 SmallString<256> path(StringRef(LLVM_TOOLS_INSTALL_DIR));
362 sys::fs::make_absolute(ActivePrefix, path);
363 ActiveBinDir = std::string(path.str());
364 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
365 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
366 ActiveIncludeOption = "-I" + ActiveIncludeDir;
367 }
368
369 /// We only use `shared library` mode in cases where the static library form
370 /// of the components provided are not available; note however that this is
371 /// skipped if we're run from within the build dir. However, once installed,
372 /// we still need to provide correct output when the static archives are
373 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
374 /// in the first place. This can't be done at configure/build time.
375
376 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
377 StaticPrefix, StaticDir = "lib";
378 std::string DirSep = "/";
379 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
380 if (HostTriple.isOSWindows()) {
381 SharedExt = "dll";
382 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
383 if (HostTriple.isOSCygMing()) {
384 SharedPrefix = "lib";
385 StaticExt = "a";
386 StaticPrefix = "lib";
387 } else {
388 StaticExt = "lib";
389 DirSep = "\\";
390 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
391 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
392 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
393 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
394 std::replace(ActiveCMakeDir.begin(), ActiveCMakeDir.end(), '/', '\\');
395 std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
396 '\\');
397 }
398 SharedDir = ActiveBinDir;
399 StaticDir = ActiveLibDir;
400 } else if (HostTriple.isOSDarwin()) {
401 SharedExt = "dylib";
402 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
403 StaticExt = "a";
404 StaticDir = SharedDir = ActiveLibDir;
405 StaticPrefix = SharedPrefix = "lib";
406 } else {
407 // default to the unix values:
408 SharedExt = "so";
409 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
410 StaticExt = "a";
411 StaticDir = SharedDir = ActiveLibDir;
412 StaticPrefix = SharedPrefix = "lib";
413 }
414
415 const bool BuiltDyLib = !!LLVM_ENABLE_DYLIB;
416
417 /// CMake style shared libs, ie each component is in a shared library.
418 const bool BuiltSharedLibs = !!LLVM_ENABLE_SHARED;
419
420 bool DyLibExists = false;
421 const std::string DyLibName =
422 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
423
424 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
425 // for "--libs", etc, if they exist. This behaviour can be overridden with
426 // --link-static or --link-shared.
427 bool LinkDyLib = !!LLVM_LINK_DYLIB;
428
429 if (BuiltDyLib) {
430 std::string path((SharedDir + DirSep + DyLibName).str());
431 if (DirSep == "\\") {
432 std::replace(path.begin(), path.end(), '/', '\\');
433 }
434 DyLibExists = sys::fs::exists(path);
435 if (!DyLibExists) {
436 // The shared library does not exist: don't error unless the user
437 // explicitly passes --link-shared.
438 LinkDyLib = false;
439 }
440 }
441 LinkMode LinkMode =
442 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
443
444 /// Get the component's library name without the lib prefix and the
445 /// extension. Returns true if Lib is in a recognized format.
446 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
447 StringRef &Out) {
448 if (Lib.startswith("lib")) {
449 unsigned FromEnd;
450 if (Lib.endswith(StaticExt)) {
451 FromEnd = StaticExt.size() + 1;
452 } else if (Lib.endswith(SharedExt)) {
453 FromEnd = SharedExt.size() + 1;
454 } else {
455 FromEnd = 0;
456 }
457
458 if (FromEnd != 0) {
459 Out = Lib.slice(3, Lib.size() - FromEnd);
460 return true;
461 }
462 }
463
464 return false;
465 };
466 /// Maps Unixizms to the host platform.
467 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
468 const bool Shared) {
469 std::string LibFileName;
470 if (Shared) {
471 if (Lib == DyLibName) {
472 // Treat the DyLibName specially. It is not a component library and
473 // already has the necessary prefix and suffix (e.g. `.so`) added so
474 // just return it unmodified.
475 assert(Lib.endswith(SharedExt) && "DyLib is missing suffix");
476 LibFileName = std::string(Lib);
477 } else {
478 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
479 }
480 } else {
481 // default to static
482 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
483 }
484
485 return LibFileName;
486 };
487 /// Get the full path for a possibly shared component library.
488 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
489 auto LibFileName = GetComponentLibraryFileName(Name, Shared);
490 if (Shared) {
491 return (SharedDir + DirSep + LibFileName).str();
492 } else {
493 return (StaticDir + DirSep + LibFileName).str();
494 }
495 };
496
497 raw_ostream &OS = outs();
498 for (int i = 1; i != argc; ++i) {
499 StringRef Arg = argv[i];
500
501 if (Arg.startswith("-")) {
502 HasAnyOption = true;
503 if (Arg == "--version") {
504 OS << PACKAGE_VERSION << '\n';
505 } else if (Arg == "--prefix") {
506 OS << ActivePrefix << '\n';
507 } else if (Arg == "--bindir") {
508 OS << ActiveBinDir << '\n';
509 } else if (Arg == "--includedir") {
510 OS << ActiveIncludeDir << '\n';
511 } else if (Arg == "--libdir") {
512 OS << ActiveLibDir << '\n';
513 } else if (Arg == "--cmakedir") {
514 OS << ActiveCMakeDir << '\n';
515 } else if (Arg == "--cppflags") {
516 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
517 } else if (Arg == "--cflags") {
518 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
519 } else if (Arg == "--cxxflags") {
520 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
521 } else if (Arg == "--ldflags") {
522 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
523 << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
524 } else if (Arg == "--system-libs") {
525 PrintSystemLibs = true;
526 } else if (Arg == "--libs") {
527 PrintLibs = true;
528 } else if (Arg == "--libnames") {
529 PrintLibNames = true;
530 } else if (Arg == "--libfiles") {
531 PrintLibFiles = true;
532 } else if (Arg == "--components") {
533 /// If there are missing static archives and a dylib was
534 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
535 /// in the manifest.
536 std::vector<std::string> Components;
537 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
538 // Only include non-installed components when in a development tree.
539 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
540 continue;
541
542 Components.push_back(AvailableComponents[j].Name);
543 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
544 std::string path(
545 GetComponentLibraryPath(AvailableComponents[j].Library, false));
546 if (DirSep == "\\") {
547 std::replace(path.begin(), path.end(), '/', '\\');
548 }
549 if (DyLibExists && !sys::fs::exists(path)) {
550 Components =
551 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
552 llvm::sort(Components);
553 break;
554 }
555 }
556 }
557
558 for (unsigned I = 0; I < Components.size(); ++I) {
559 if (I) {
560 OS << ' ';
561 }
562
563 OS << Components[I];
564 }
565 OS << '\n';
566 } else if (Arg == "--targets-built") {
567 OS << LLVM_TARGETS_BUILT << '\n';
568 } else if (Arg == "--host-target") {
569 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
570 } else if (Arg == "--build-mode") {
571 OS << build_mode << '\n';
572 } else if (Arg == "--assertion-mode") {
573 #if defined(NDEBUG)
574 OS << "OFF\n";
575 #else
576 OS << "ON\n";
577 #endif
578 } else if (Arg == "--build-system") {
579 OS << LLVM_BUILD_SYSTEM << '\n';
580 } else if (Arg == "--has-rtti") {
581 OS << (LLVM_HAS_RTTI ? "YES" : "NO") << '\n';
582 } else if (Arg == "--shared-mode") {
583 PrintSharedMode = true;
584 } else if (Arg == "--obj-root") {
585 OS << ActivePrefix << '\n';
586 } else if (Arg == "--src-root") {
587 OS << LLVM_SRC_ROOT << '\n';
588 } else if (Arg == "--ignore-libllvm") {
589 LinkDyLib = false;
590 LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto;
591 } else if (Arg == "--link-shared") {
592 LinkMode = LinkModeShared;
593 } else if (Arg == "--link-static") {
594 LinkMode = LinkModeStatic;
595 } else {
596 usage();
597 }
598 } else {
599 Components.push_back(Arg);
600 }
601 }
602
603 if (!HasAnyOption)
604 usage();
605
606 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
607 WithColor::error(errs(), "llvm-config") << DyLibName << " is missing\n";
608 return 1;
609 }
610
611 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
612 PrintSharedMode) {
613
614 if (PrintSharedMode && BuiltSharedLibs) {
615 OS << "shared\n";
616 return 0;
617 }
618
619 // If no components were specified, default to "all".
620 if (Components.empty())
621 Components.push_back("all");
622
623 // Construct the list of all the required libraries.
624 std::function<std::string(const StringRef &)>
625 GetComponentLibraryPathFunction = [&](const StringRef &Name) {
626 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
627 };
628 std::vector<std::string> MissingLibs;
629 std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
630 Components,
631 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
632 &GetComponentLibraryPathFunction, &MissingLibs, DirSep);
633 if (!MissingLibs.empty()) {
634 switch (LinkMode) {
635 case LinkModeShared:
636 if (LinkDyLib && !BuiltSharedLibs)
637 break;
638 // Using component shared libraries.
639 for (auto &Lib : MissingLibs)
640 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n";
641 return 1;
642 case LinkModeAuto:
643 if (DyLibExists) {
644 LinkMode = LinkModeShared;
645 break;
646 }
647 WithColor::error(errs(), "llvm-config")
648 << "component libraries and shared library\n\n";
649 LLVM_FALLTHROUGH;
650 case LinkModeStatic:
651 for (auto &Lib : MissingLibs)
652 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n";
653 return 1;
654 }
655 } else if (LinkMode == LinkModeAuto) {
656 LinkMode = LinkModeStatic;
657 }
658
659 if (PrintSharedMode) {
660 std::unordered_set<std::string> FullDyLibComponents;
661 std::vector<std::string> DyLibComponents =
662 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
663
664 for (auto &Component : DyLibComponents) {
665 FullDyLibComponents.insert(Component);
666 }
667 DyLibComponents.clear();
668
669 for (auto &Lib : RequiredLibs) {
670 if (!FullDyLibComponents.count(Lib)) {
671 OS << "static\n";
672 return 0;
673 }
674 }
675 FullDyLibComponents.clear();
676
677 if (LinkMode == LinkModeShared) {
678 OS << "shared\n";
679 return 0;
680 } else {
681 OS << "static\n";
682 return 0;
683 }
684 }
685
686 if (PrintLibs || PrintLibNames || PrintLibFiles) {
687
688 auto PrintForLib = [&](const StringRef &Lib) {
689 const bool Shared = LinkMode == LinkModeShared;
690 if (PrintLibNames) {
691 OS << GetComponentLibraryFileName(Lib, Shared);
692 } else if (PrintLibFiles) {
693 OS << GetComponentLibraryPath(Lib, Shared);
694 } else if (PrintLibs) {
695 // On Windows, output full path to library without parameters.
696 // Elsewhere, if this is a typical library name, include it using -l.
697 if (HostTriple.isWindowsMSVCEnvironment()) {
698 OS << GetComponentLibraryPath(Lib, Shared);
699 } else {
700 StringRef LibName;
701 if (GetComponentLibraryNameSlice(Lib, LibName)) {
702 // Extract library name (remove prefix and suffix).
703 OS << "-l" << LibName;
704 } else {
705 // Lib is already a library name without prefix and suffix.
706 OS << "-l" << Lib;
707 }
708 }
709 }
710 };
711
712 if (LinkMode == LinkModeShared && LinkDyLib) {
713 PrintForLib(DyLibName);
714 } else {
715 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
716 auto Lib = RequiredLibs[i];
717 if (i)
718 OS << ' ';
719
720 PrintForLib(Lib);
721 }
722 }
723 OS << '\n';
724 }
725
726 // Print SYSTEM_LIBS after --libs.
727 // FIXME: Each LLVM component may have its dependent system libs.
728 if (PrintSystemLibs) {
729 // Output system libraries only if linking against a static
730 // library (since the shared library links to all system libs
731 // already)
732 OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n';
733 }
734 } else if (!Components.empty()) {
735 WithColor::error(errs(), "llvm-config")
736 << "components given, but unused\n\n";
737 usage();
738 }
739
740 return 0;
741 }
742