Home | History | Annotate | Download | only in llvm-config
      1 //===-- llvm-config.cpp - LLVM project configuration utility --------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This tool encapsulates information about an LLVM project configuration for
     11 // use by other project's build environments (to determine installed path,
     12 // available features, required libraries, etc.).
     13 //
     14 // Note that although this tool *may* be used by some parts of LLVM's build
     15 // itself (i.e., the Makefiles use it to compute required libraries when linking
     16 // tools), this tool is primarily designed to support external projects.
     17 //
     18 //===----------------------------------------------------------------------===//
     19 
     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/Config/llvm-config.h"
     27 #include "llvm/Support/FileSystem.h"
     28 #include "llvm/Support/Path.h"
     29 #include "llvm/Support/raw_ostream.h"
     30 #include <cstdlib>
     31 #include <set>
     32 #include <vector>
     33 #include <unordered_set>
     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 /// \brief Traverse a single component adding to the topological ordering in
     50 /// \arg RequiredLibs.
     51 ///
     52 /// \param Name - The component to traverse.
     53 /// \param ComponentMap - A prebuilt map of component names to descriptors.
     54 /// \param VisitedComponents [in] [out] - The set of already visited components.
     55 /// \param RequiredLibs [out] - The ordered list of required
     56 /// libraries.
     57 /// \param GetComponentNames - Get the component names instead of the
     58 /// library name.
     59 static void VisitComponent(const std::string& Name,
     60                            const StringMap<AvailableComponent*> &ComponentMap,
     61                            std::set<AvailableComponent*> &VisitedComponents,
     62                            std::vector<std::string> &RequiredLibs,
     63                            bool IncludeNonInstalled, bool GetComponentNames,
     64                            const std::string *ActiveLibDir, bool *HasMissing) {
     65   // Lookup the component.
     66   AvailableComponent *AC = ComponentMap.lookup(Name);
     67   assert(AC && "Invalid component name!");
     68 
     69   // Add to the visited table.
     70   if (!VisitedComponents.insert(AC).second) {
     71     // We are done if the component has already been visited.
     72     return;
     73   }
     74 
     75   // Only include non-installed components if requested.
     76   if (!AC->IsInstalled && !IncludeNonInstalled)
     77     return;
     78 
     79   // Otherwise, visit all the dependencies.
     80   for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
     81     VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
     82                    RequiredLibs, IncludeNonInstalled, GetComponentNames,
     83                    ActiveLibDir, HasMissing);
     84   }
     85 
     86   if (GetComponentNames) {
     87     RequiredLibs.push_back(Name);
     88     return;
     89   }
     90 
     91   // Add to the required library list.
     92   if (AC->Library) {
     93     if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) {
     94       *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library);
     95     }
     96     RequiredLibs.push_back(AC->Library);
     97   }
     98 }
     99 
    100 /// \brief Compute the list of required libraries for a given list of
    101 /// components, in an order suitable for passing to a linker (that is, libraries
    102 /// appear prior to their dependencies).
    103 ///
    104 /// \param Components - The names of the components to find libraries for.
    105 /// \param IncludeNonInstalled - Whether non-installed components should be
    106 /// reported.
    107 /// \param GetComponentNames - True if one would prefer the component names.
    108 static std::vector<std::string>
    109 ComputeLibsForComponents(const std::vector<StringRef> &Components,
    110                          bool IncludeNonInstalled, bool GetComponentNames,
    111                          const std::string *ActiveLibDir, bool *HasMissing) {
    112   std::vector<std::string> RequiredLibs;
    113   std::set<AvailableComponent *> VisitedComponents;
    114 
    115   // Build a map of component names to information.
    116   StringMap<AvailableComponent*> ComponentMap;
    117   for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
    118     AvailableComponent *AC = &AvailableComponents[i];
    119     ComponentMap[AC->Name] = AC;
    120   }
    121 
    122   // Visit the components.
    123   for (unsigned i = 0, e = Components.size(); i != e; ++i) {
    124     // Users are allowed to provide mixed case component names.
    125     std::string ComponentLower = Components[i].lower();
    126 
    127     // Validate that the user supplied a valid component name.
    128     if (!ComponentMap.count(ComponentLower)) {
    129       llvm::errs() << "llvm-config: unknown component name: " << Components[i]
    130                    << "\n";
    131       exit(1);
    132     }
    133 
    134     VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
    135                    RequiredLibs, IncludeNonInstalled, GetComponentNames,
    136                    ActiveLibDir, HasMissing);
    137   }
    138 
    139   // The list is now ordered with leafs first, we want the libraries to printed
    140   // in the reverse order of dependency.
    141   std::reverse(RequiredLibs.begin(), RequiredLibs.end());
    142 
    143   return RequiredLibs;
    144 }
    145 
    146 /* *** */
    147 
    148 static void usage() {
    149   errs() << "\
    150 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
    151 \n\
    152 Get various configuration information needed to compile programs which use\n\
    153 LLVM.  Typically called from 'configure' scripts.  Examples:\n\
    154   llvm-config --cxxflags\n\
    155   llvm-config --ldflags\n\
    156   llvm-config --libs engine bcreader scalaropts\n\
    157 \n\
    158 Options:\n\
    159   --version         Print LLVM version.\n\
    160   --prefix          Print the installation prefix.\n\
    161   --src-root        Print the source root LLVM was built from.\n\
    162   --obj-root        Print the object root used to build LLVM.\n\
    163   --bindir          Directory containing LLVM executables.\n\
    164   --includedir      Directory containing LLVM headers.\n\
    165   --libdir          Directory containing LLVM libraries.\n\
    166   --cppflags        C preprocessor flags for files that include LLVM headers.\n\
    167   --cflags          C compiler flags for files that include LLVM headers.\n\
    168   --cxxflags        C++ compiler flags for files that include LLVM headers.\n\
    169   --ldflags         Print Linker flags.\n\
    170   --system-libs     System Libraries needed to link against LLVM components.\n\
    171   --libs            Libraries needed to link against LLVM components.\n\
    172   --libnames        Bare library names for in-tree builds.\n\
    173   --libfiles        Fully qualified library filenames for makefile depends.\n\
    174   --components      List of all possible components.\n\
    175   --targets-built   List of all targets currently built.\n\
    176   --host-target     Target triple used to configure LLVM.\n\
    177   --build-mode      Print build mode of LLVM tree (e.g. Debug or Release).\n\
    178   --assertion-mode  Print assertion mode of LLVM tree (ON or OFF).\n\
    179   --build-system    Print the build system used to build LLVM (autoconf or cmake).\n\
    180   --has-rtti        Print whether or not LLVM was built with rtti (YES or NO).\n\
    181   --shared-mode     Print how the provided components can be collectively linked (`shared` or `static`).\n\
    182 Typical components:\n\
    183   all               All LLVM libraries (default).\n\
    184   engine            Either a native JIT or a bitcode interpreter.\n";
    185   exit(1);
    186 }
    187 
    188 /// \brief Compute the path to the main executable.
    189 std::string GetExecutablePath(const char *Argv0) {
    190   // This just needs to be some symbol in the binary; C++ doesn't
    191   // allow taking the address of ::main however.
    192   void *P = (void*) (intptr_t) GetExecutablePath;
    193   return llvm::sys::fs::getMainExecutable(Argv0, P);
    194 }
    195 
    196 /// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
    197 /// the full list of components.
    198 std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
    199                                                const bool GetComponentNames) {
    200   std::vector<StringRef> DyLibComponents;
    201 
    202   StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
    203   size_t Offset = 0;
    204   while (true) {
    205     const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
    206     DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset));
    207     if (NextOffset == std::string::npos) {
    208       break;
    209     }
    210     Offset = NextOffset + 1;
    211   }
    212 
    213   assert(!DyLibComponents.empty());
    214 
    215   return ComputeLibsForComponents(DyLibComponents,
    216                                   /*IncludeNonInstalled=*/IsInDevelopmentTree,
    217                                   GetComponentNames, nullptr, nullptr);
    218 }
    219 
    220 int main(int argc, char **argv) {
    221   std::vector<StringRef> Components;
    222   bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
    223   bool PrintSystemLibs = false, PrintSharedMode = false;
    224   bool HasAnyOption = false;
    225 
    226   // llvm-config is designed to support being run both from a development tree
    227   // and from an installed path. We try and auto-detect which case we are in so
    228   // that we can report the correct information when run from a development
    229   // tree.
    230   bool IsInDevelopmentTree;
    231   enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
    232   llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
    233   std::string CurrentExecPrefix;
    234   std::string ActiveObjRoot;
    235 
    236   // If CMAKE_CFG_INTDIR is given, honor it as build mode.
    237   char const *build_mode = LLVM_BUILDMODE;
    238 #if defined(CMAKE_CFG_INTDIR)
    239   if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
    240     build_mode = CMAKE_CFG_INTDIR;
    241 #endif
    242 
    243   // Create an absolute path, and pop up one directory (we expect to be inside a
    244   // bin dir).
    245   sys::fs::make_absolute(CurrentPath);
    246   CurrentExecPrefix = sys::path::parent_path(
    247     sys::path::parent_path(CurrentPath)).str();
    248 
    249   // Check to see if we are inside a development tree by comparing to possible
    250   // locations (prefix style or CMake style).
    251   if (sys::fs::equivalent(CurrentExecPrefix,
    252                           Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
    253     IsInDevelopmentTree = true;
    254     DevelopmentTreeLayout = MakefileStyle;
    255 
    256     // If we are in a development tree, then check if we are in a BuildTools
    257     // directory. This indicates we are built for the build triple, but we
    258     // always want to provide information for the host triple.
    259     if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
    260       ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
    261     } else {
    262       ActiveObjRoot = LLVM_OBJ_ROOT;
    263     }
    264   } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
    265     IsInDevelopmentTree = true;
    266     DevelopmentTreeLayout = CMakeStyle;
    267     ActiveObjRoot = LLVM_OBJ_ROOT;
    268   } else if (sys::fs::equivalent(CurrentExecPrefix,
    269                                  Twine(LLVM_OBJ_ROOT) + "/bin")) {
    270     IsInDevelopmentTree = true;
    271     DevelopmentTreeLayout = CMakeBuildModeStyle;
    272     ActiveObjRoot = LLVM_OBJ_ROOT;
    273   } else {
    274     IsInDevelopmentTree = false;
    275     DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
    276   }
    277 
    278   // Compute various directory locations based on the derived location
    279   // information.
    280   std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
    281   std::string ActiveIncludeOption;
    282   if (IsInDevelopmentTree) {
    283     ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
    284     ActivePrefix = CurrentExecPrefix;
    285 
    286     // CMake organizes the products differently than a normal prefix style
    287     // layout.
    288     switch (DevelopmentTreeLayout) {
    289     case MakefileStyle:
    290       ActivePrefix = ActiveObjRoot;
    291       ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
    292       ActiveLibDir =
    293           ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
    294       break;
    295     case CMakeStyle:
    296       ActiveBinDir = ActiveObjRoot + "/bin";
    297       ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
    298       break;
    299     case CMakeBuildModeStyle:
    300       ActivePrefix = ActiveObjRoot;
    301       ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
    302       ActiveLibDir =
    303           ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
    304       break;
    305     }
    306 
    307     // We need to include files from both the source and object trees.
    308     ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
    309                            "-I" + ActiveObjRoot + "/include");
    310   } else {
    311     ActivePrefix = CurrentExecPrefix;
    312     ActiveIncludeDir = ActivePrefix + "/include";
    313     ActiveBinDir = ActivePrefix + "/bin";
    314     ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
    315     ActiveIncludeOption = "-I" + ActiveIncludeDir;
    316   }
    317 
    318   /// We only use `shared library` mode in cases where the static library form
    319   /// of the components provided are not available; note however that this is
    320   /// skipped if we're run from within the build dir. However, once installed,
    321   /// we still need to provide correct output when the static archives are
    322   /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
    323   /// in the first place. This can't be done at configure/build time.
    324 
    325   StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
    326     StaticPrefix, StaticDir = "lib";
    327   const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE));
    328   if (HostTriple.isOSWindows()) {
    329     SharedExt = "dll";
    330     SharedVersionedExt = PACKAGE_VERSION ".dll";
    331     StaticExt = "a";
    332     SharedDir = ActiveBinDir;
    333     StaticDir = ActiveLibDir;
    334     StaticPrefix = SharedPrefix = "lib";
    335   } else if (HostTriple.isOSDarwin()) {
    336     SharedExt = "dylib";
    337     SharedVersionedExt = PACKAGE_VERSION ".dylib";
    338     StaticExt = "a";
    339     StaticDir = SharedDir = ActiveLibDir;
    340     StaticPrefix = SharedPrefix = "lib";
    341   } else {
    342     // default to the unix values:
    343     SharedExt = "so";
    344     SharedVersionedExt = PACKAGE_VERSION ".so";
    345     StaticExt = "a";
    346     StaticDir = SharedDir = ActiveLibDir;
    347     StaticPrefix = SharedPrefix = "lib";
    348   }
    349 
    350   const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
    351 
    352   enum { CMake, AutoConf } ConfigTool;
    353   if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) {
    354     ConfigTool = CMake;
    355   } else {
    356     ConfigTool = AutoConf;
    357   }
    358 
    359   /// CMake style shared libs, ie each component is in a shared library.
    360   const bool BuiltSharedLibs =
    361       (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0);
    362 
    363   bool DyLibExists = false;
    364   const std::string DyLibName =
    365     (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
    366 
    367   if (BuiltDyLib) {
    368     DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName);
    369   }
    370 
    371   /// Get the component's library name without the lib prefix and the
    372   /// extension. Returns true if Lib is in a recognized format.
    373   auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
    374                                           StringRef &Out) {
    375     if (Lib.startswith("lib")) {
    376       unsigned FromEnd;
    377       if (Lib.endswith(StaticExt)) {
    378         FromEnd = StaticExt.size() + 1;
    379       } else if (Lib.endswith(SharedExt)) {
    380         FromEnd = SharedExt.size() + 1;
    381       } else {
    382         FromEnd = 0;
    383       }
    384 
    385       if (FromEnd != 0) {
    386         Out = Lib.slice(3, Lib.size() - FromEnd);
    387         return true;
    388       }
    389     }
    390 
    391     return false;
    392   };
    393   /// Maps Unixizms to the host platform.
    394   auto GetComponentLibraryFileName = [&](const StringRef &Lib,
    395                                          const bool ForceShared) {
    396     std::string LibFileName = Lib;
    397     StringRef LibName;
    398     if (GetComponentLibraryNameSlice(Lib, LibName)) {
    399       if (BuiltSharedLibs || ForceShared) {
    400         LibFileName = (SharedPrefix + LibName + "." + SharedExt).str();
    401       } else {
    402         // default to static
    403         LibFileName = (StaticPrefix + LibName + "." + StaticExt).str();
    404       }
    405     }
    406 
    407     return LibFileName;
    408   };
    409   /// Get the full path for a possibly shared component library.
    410   auto GetComponentLibraryPath = [&](const StringRef &Name,
    411                                      const bool ForceShared) {
    412     auto LibFileName = GetComponentLibraryFileName(Name, ForceShared);
    413     if (BuiltSharedLibs || ForceShared) {
    414       return (SharedDir + "/" + LibFileName).str();
    415     } else {
    416       return (StaticDir + "/" + LibFileName).str();
    417     }
    418   };
    419 
    420   raw_ostream &OS = outs();
    421   for (int i = 1; i != argc; ++i) {
    422     StringRef Arg = argv[i];
    423 
    424     if (Arg.startswith("-")) {
    425       HasAnyOption = true;
    426       if (Arg == "--version") {
    427         OS << PACKAGE_VERSION << '\n';
    428       } else if (Arg == "--prefix") {
    429         OS << ActivePrefix << '\n';
    430       } else if (Arg == "--bindir") {
    431         OS << ActiveBinDir << '\n';
    432       } else if (Arg == "--includedir") {
    433         OS << ActiveIncludeDir << '\n';
    434       } else if (Arg == "--libdir") {
    435         OS << ActiveLibDir << '\n';
    436       } else if (Arg == "--cppflags") {
    437         OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
    438       } else if (Arg == "--cflags") {
    439         OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
    440       } else if (Arg == "--cxxflags") {
    441         OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
    442       } else if (Arg == "--ldflags") {
    443         OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
    444       } else if (Arg == "--system-libs") {
    445         PrintSystemLibs = true;
    446       } else if (Arg == "--libs") {
    447         PrintLibs = true;
    448       } else if (Arg == "--libnames") {
    449         PrintLibNames = true;
    450       } else if (Arg == "--libfiles") {
    451         PrintLibFiles = true;
    452       } else if (Arg == "--components") {
    453         /// If there are missing static archives and a dylib was
    454         /// built, print LLVM_DYLIB_COMPONENTS instead of everything
    455         /// in the manifest.
    456         std::vector<std::string> Components;
    457         for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
    458           // Only include non-installed components when in a development tree.
    459           if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
    460             continue;
    461 
    462           Components.push_back(AvailableComponents[j].Name);
    463           if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
    464             if (DyLibExists &&
    465                 !sys::fs::exists(GetComponentLibraryPath(
    466                     AvailableComponents[j].Library, false))) {
    467               Components = GetAllDyLibComponents(IsInDevelopmentTree, true);
    468               std::sort(Components.begin(), Components.end());
    469               break;
    470             }
    471           }
    472         }
    473 
    474         for (unsigned I = 0; I < Components.size(); ++I) {
    475           if (I) {
    476             OS << ' ';
    477           }
    478 
    479           OS << Components[I];
    480         }
    481         OS << '\n';
    482       } else if (Arg == "--targets-built") {
    483         OS << LLVM_TARGETS_BUILT << '\n';
    484       } else if (Arg == "--host-target") {
    485         OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
    486       } else if (Arg == "--build-mode") {
    487         OS << build_mode << '\n';
    488       } else if (Arg == "--assertion-mode") {
    489 #if defined(NDEBUG)
    490         OS << "OFF\n";
    491 #else
    492         OS << "ON\n";
    493 #endif
    494       } else if (Arg == "--build-system") {
    495         OS << LLVM_BUILD_SYSTEM << '\n';
    496       } else if (Arg == "--has-rtti") {
    497         OS << LLVM_HAS_RTTI << '\n';
    498       } else if (Arg == "--shared-mode") {
    499         PrintSharedMode = true;
    500       } else if (Arg == "--obj-root") {
    501         OS << ActivePrefix << '\n';
    502       } else if (Arg == "--src-root") {
    503         OS << LLVM_SRC_ROOT << '\n';
    504       } else {
    505         usage();
    506       }
    507     } else {
    508       Components.push_back(Arg);
    509     }
    510   }
    511 
    512   if (!HasAnyOption)
    513     usage();
    514 
    515   if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
    516       PrintSharedMode) {
    517 
    518     if (PrintSharedMode && BuiltSharedLibs) {
    519       OS << "shared\n";
    520       return 0;
    521     }
    522 
    523     // If no components were specified, default to "all".
    524     if (Components.empty())
    525       Components.push_back("all");
    526 
    527     // Construct the list of all the required libraries.
    528     bool HasMissing = false;
    529     std::vector<std::string> RequiredLibs =
    530         ComputeLibsForComponents(Components,
    531                                  /*IncludeNonInstalled=*/IsInDevelopmentTree,
    532                                  false, &ActiveLibDir, &HasMissing);
    533 
    534     if (PrintSharedMode) {
    535       std::unordered_set<std::string> FullDyLibComponents;
    536       std::vector<std::string> DyLibComponents =
    537           GetAllDyLibComponents(IsInDevelopmentTree, false);
    538 
    539       for (auto &Component : DyLibComponents) {
    540         FullDyLibComponents.insert(Component);
    541       }
    542       DyLibComponents.clear();
    543 
    544       for (auto &Lib : RequiredLibs) {
    545         if (!FullDyLibComponents.count(Lib)) {
    546           OS << "static\n";
    547           return 0;
    548         }
    549       }
    550       FullDyLibComponents.clear();
    551 
    552       if (HasMissing && DyLibExists) {
    553         OS << "shared\n";
    554         return 0;
    555       } else {
    556         OS << "static\n";
    557         return 0;
    558       }
    559     }
    560 
    561     if (PrintLibs || PrintLibNames || PrintLibFiles) {
    562 
    563       auto PrintForLib = [&](const StringRef &Lib, const bool ForceShared) {
    564         if (PrintLibNames) {
    565           OS << GetComponentLibraryFileName(Lib, ForceShared);
    566         } else if (PrintLibFiles) {
    567           OS << GetComponentLibraryPath(Lib, ForceShared);
    568         } else if (PrintLibs) {
    569           // If this is a typical library name, include it using -l.
    570           StringRef LibName;
    571           if (Lib.startswith("lib")) {
    572             if (GetComponentLibraryNameSlice(Lib, LibName)) {
    573               OS << "-l" << LibName;
    574             } else {
    575               OS << "-l:" << GetComponentLibraryFileName(Lib, ForceShared);
    576             }
    577           } else {
    578             // Otherwise, print the full path.
    579             OS << GetComponentLibraryPath(Lib, ForceShared);
    580           }
    581         }
    582       };
    583 
    584       if (HasMissing && DyLibExists) {
    585         PrintForLib(DyLibName, true);
    586       } else {
    587         for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
    588           auto Lib = RequiredLibs[i];
    589           if (i)
    590             OS << ' ';
    591 
    592           PrintForLib(Lib, false);
    593         }
    594       }
    595       OS << '\n';
    596     }
    597 
    598     // Print SYSTEM_LIBS after --libs.
    599     // FIXME: Each LLVM component may have its dependent system libs.
    600     if (PrintSystemLibs)
    601       OS << LLVM_SYSTEM_LIBS << '\n';
    602   } else if (!Components.empty()) {
    603     errs() << "llvm-config: error: components given, but unused\n\n";
    604     usage();
    605   }
    606 
    607   return 0;
    608 }
    609