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