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 <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 // 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,
     55 
     56   // LinkModeShared will link with the dynamic component libraries if they
     57   // exist, and return an error otherwise.
     58   LinkModeShared = 1,
     59 
     60   // LinkModeStatic will link with the static component libraries if they
     61   // exist, and return an error otherwise.
     62   LinkModeStatic = 2,
     63 };
     64 
     65 /// \brief 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() << "' ";
     90     }
     91     errs() << "\n";
     92     report_fatal_error("abort");
     93   }
     94   assert(AC && "Invalid component name!");
     95 
     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;
    100   }
    101 
    102   // Only include non-installed components if requested.
    103   if (!AC->IsInstalled && !IncludeNonInstalled)
    104     return;
    105 
    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);
    111   }
    112 
    113   if (GetComponentNames) {
    114     RequiredLibs.push_back(Name);
    115     return;
    116   }
    117 
    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(), '/', '\\');
    124       }
    125       if (!sys::fs::exists(path))
    126         Missing->push_back(path);
    127     }
    128     RequiredLibs.push_back(AC->Library);
    129   }
    130 }
    131 
    132 /// \brief 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).
    135 ///
    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;
    147 
    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;
    153   }
    154 
    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();
    159 
    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);
    165     }
    166 
    167     VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
    168                    RequiredLibs, IncludeNonInstalled, GetComponentNames,
    169                    GetComponentLibraryPath, Missing, DirSep);
    170   }
    171 
    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());
    175 
    176   return RequiredLibs;
    177 }
    178 
    179 /* *** */
    180 
    181 static void usage() {
    182   errs() << "\
    183 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
    184 \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\
    190 \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   --cppflags        C preprocessor flags for files that include LLVM headers.\n\
    200   --cflags          C compiler flags for files that include LLVM headers.\n\
    201   --cxxflags        C++ compiler flags for files that include LLVM headers.\n\
    202   --ldflags         Print Linker flags.\n\
    203   --system-libs     System Libraries needed to link against LLVM components.\n\
    204   --libs            Libraries needed to link against LLVM components.\n\
    205   --libnames        Bare library names for in-tree builds.\n\
    206   --libfiles        Fully qualified library filenames for makefile depends.\n\
    207   --components      List of all possible components.\n\
    208   --targets-built   List of all targets currently built.\n\
    209   --host-target     Target triple used to configure LLVM.\n\
    210   --build-mode      Print build mode of LLVM tree (e.g. Debug or Release).\n\
    211   --assertion-mode  Print assertion mode of LLVM tree (ON or OFF).\n\
    212   --build-system    Print the build system used to build LLVM (always cmake).\n\
    213   --has-rtti        Print whether or not LLVM was built with rtti (YES or NO).\n\
    214   --has-global-isel Print whether or not LLVM was built with global-isel support (YES or NO).\n\
    215   --shared-mode     Print how the provided components can be collectively linked (`shared` or `static`).\n\
    216   --link-shared     Link the components as shared libraries.\n\
    217   --link-static     Link the component libraries statically.\n\
    218 Typical components:\n\
    219   all               All LLVM libraries (default).\n\
    220   engine            Either a native JIT or a bitcode interpreter.\n";
    221   exit(1);
    222 }
    223 
    224 /// \brief Compute the path to the main executable.
    225 std::string GetExecutablePath(const char *Argv0) {
    226   // This just needs to be some symbol in the binary; C++ doesn't
    227   // allow taking the address of ::main however.
    228   void *P = (void *)(intptr_t)GetExecutablePath;
    229   return llvm::sys::fs::getMainExecutable(Argv0, P);
    230 }
    231 
    232 /// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
    233 /// the full list of components.
    234 std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
    235                                                const bool GetComponentNames,
    236                                                const std::string &DirSep) {
    237   std::vector<StringRef> DyLibComponents;
    238 
    239   StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
    240   size_t Offset = 0;
    241   while (true) {
    242     const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
    243     DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset));
    244     if (NextOffset == std::string::npos) {
    245       break;
    246     }
    247     Offset = NextOffset + 1;
    248   }
    249 
    250   assert(!DyLibComponents.empty());
    251 
    252   return ComputeLibsForComponents(DyLibComponents,
    253                                   /*IncludeNonInstalled=*/IsInDevelopmentTree,
    254                                   GetComponentNames, nullptr, nullptr, DirSep);
    255 }
    256 
    257 int main(int argc, char **argv) {
    258   std::vector<StringRef> Components;
    259   bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
    260   bool PrintSystemLibs = false, PrintSharedMode = false;
    261   bool HasAnyOption = false;
    262 
    263   // llvm-config is designed to support being run both from a development tree
    264   // and from an installed path. We try and auto-detect which case we are in so
    265   // that we can report the correct information when run from a development
    266   // tree.
    267   bool IsInDevelopmentTree;
    268   enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
    269   llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
    270   std::string CurrentExecPrefix;
    271   std::string ActiveObjRoot;
    272 
    273   // If CMAKE_CFG_INTDIR is given, honor it as build mode.
    274   char const *build_mode = LLVM_BUILDMODE;
    275 #if defined(CMAKE_CFG_INTDIR)
    276   if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
    277     build_mode = CMAKE_CFG_INTDIR;
    278 #endif
    279 
    280   // Create an absolute path, and pop up one directory (we expect to be inside a
    281   // bin dir).
    282   sys::fs::make_absolute(CurrentPath);
    283   CurrentExecPrefix =
    284       sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
    285 
    286   // Check to see if we are inside a development tree by comparing to possible
    287   // locations (prefix style or CMake style).
    288   if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
    289     IsInDevelopmentTree = true;
    290     DevelopmentTreeLayout = CMakeStyle;
    291     ActiveObjRoot = LLVM_OBJ_ROOT;
    292   } else if (sys::fs::equivalent(CurrentExecPrefix,
    293                                  Twine(LLVM_OBJ_ROOT) + "/bin")) {
    294     IsInDevelopmentTree = true;
    295     DevelopmentTreeLayout = CMakeBuildModeStyle;
    296     ActiveObjRoot = LLVM_OBJ_ROOT;
    297   } else {
    298     IsInDevelopmentTree = false;
    299     DevelopmentTreeLayout = CMakeStyle; // Initialized to avoid warnings.
    300   }
    301 
    302   // Compute various directory locations based on the derived location
    303   // information.
    304   std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
    305   std::string ActiveIncludeOption;
    306   if (IsInDevelopmentTree) {
    307     ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
    308     ActivePrefix = CurrentExecPrefix;
    309 
    310     // CMake organizes the products differently than a normal prefix style
    311     // layout.
    312     switch (DevelopmentTreeLayout) {
    313     case CMakeStyle:
    314       ActiveBinDir = ActiveObjRoot + "/bin";
    315       ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
    316       break;
    317     case CMakeBuildModeStyle:
    318       ActivePrefix = ActiveObjRoot;
    319       ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
    320       ActiveLibDir =
    321           ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
    322       break;
    323     }
    324 
    325     // We need to include files from both the source and object trees.
    326     ActiveIncludeOption =
    327         ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
    328   } else {
    329     ActivePrefix = CurrentExecPrefix;
    330     ActiveIncludeDir = ActivePrefix + "/include";
    331     ActiveBinDir = ActivePrefix + "/bin";
    332     ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
    333     ActiveIncludeOption = "-I" + ActiveIncludeDir;
    334   }
    335 
    336   /// We only use `shared library` mode in cases where the static library form
    337   /// of the components provided are not available; note however that this is
    338   /// skipped if we're run from within the build dir. However, once installed,
    339   /// we still need to provide correct output when the static archives are
    340   /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
    341   /// in the first place. This can't be done at configure/build time.
    342 
    343   StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
    344       StaticPrefix, StaticDir = "lib", DirSep = "/";
    345   const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
    346   if (HostTriple.isOSWindows()) {
    347     SharedExt = "dll";
    348     SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
    349     if (HostTriple.isOSCygMing()) {
    350       StaticExt = "a";
    351       StaticPrefix = "lib";
    352     } else {
    353       StaticExt = "lib";
    354       DirSep = "\\";
    355       std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
    356       std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
    357       std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
    358       std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
    359       std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
    360                    '\\');
    361     }
    362     SharedDir = ActiveBinDir;
    363     StaticDir = ActiveLibDir;
    364   } else if (HostTriple.isOSDarwin()) {
    365     SharedExt = "dylib";
    366     SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
    367     StaticExt = "a";
    368     StaticDir = SharedDir = ActiveLibDir;
    369     StaticPrefix = SharedPrefix = "lib";
    370   } else {
    371     // default to the unix values:
    372     SharedExt = "so";
    373     SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
    374     StaticExt = "a";
    375     StaticDir = SharedDir = ActiveLibDir;
    376     StaticPrefix = SharedPrefix = "lib";
    377   }
    378 
    379   const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
    380 
    381   /// CMake style shared libs, ie each component is in a shared library.
    382   const bool BuiltSharedLibs = std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0;
    383 
    384   bool DyLibExists = false;
    385   const std::string DyLibName =
    386       (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
    387 
    388   // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
    389   // for "--libs", etc, if they exist. This behaviour can be overridden with
    390   // --link-static or --link-shared.
    391   bool LinkDyLib = (std::strcmp(LLVM_LINK_DYLIB, "ON") == 0);
    392 
    393   if (BuiltDyLib) {
    394     std::string path((SharedDir + DirSep + DyLibName).str());
    395     if (DirSep == "\\") {
    396       std::replace(path.begin(), path.end(), '/', '\\');
    397     }
    398     DyLibExists = sys::fs::exists(path);
    399     if (!DyLibExists) {
    400       // The shared library does not exist: don't error unless the user
    401       // explicitly passes --link-shared.
    402       LinkDyLib = false;
    403     }
    404   }
    405   LinkMode LinkMode =
    406       (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
    407 
    408   /// Get the component's library name without the lib prefix and the
    409   /// extension. Returns true if Lib is in a recognized format.
    410   auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
    411                                           StringRef &Out) {
    412     if (Lib.startswith("lib")) {
    413       unsigned FromEnd;
    414       if (Lib.endswith(StaticExt)) {
    415         FromEnd = StaticExt.size() + 1;
    416       } else if (Lib.endswith(SharedExt)) {
    417         FromEnd = SharedExt.size() + 1;
    418       } else {
    419         FromEnd = 0;
    420       }
    421 
    422       if (FromEnd != 0) {
    423         Out = Lib.slice(3, Lib.size() - FromEnd);
    424         return true;
    425       }
    426     }
    427 
    428     return false;
    429   };
    430   /// Maps Unixizms to the host platform.
    431   auto GetComponentLibraryFileName = [&](const StringRef &Lib,
    432                                          const bool Shared) {
    433     std::string LibFileName;
    434     if (Shared) {
    435       LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
    436     } else {
    437       // default to static
    438       LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
    439     }
    440 
    441     return LibFileName;
    442   };
    443   /// Get the full path for a possibly shared component library.
    444   auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
    445     auto LibFileName = GetComponentLibraryFileName(Name, Shared);
    446     if (Shared) {
    447       return (SharedDir + DirSep + LibFileName).str();
    448     } else {
    449       return (StaticDir + DirSep + LibFileName).str();
    450     }
    451   };
    452 
    453   raw_ostream &OS = outs();
    454   for (int i = 1; i != argc; ++i) {
    455     StringRef Arg = argv[i];
    456 
    457     if (Arg.startswith("-")) {
    458       HasAnyOption = true;
    459       if (Arg == "--version") {
    460         OS << PACKAGE_VERSION << '\n';
    461       } else if (Arg == "--prefix") {
    462         OS << ActivePrefix << '\n';
    463       } else if (Arg == "--bindir") {
    464         OS << ActiveBinDir << '\n';
    465       } else if (Arg == "--includedir") {
    466         OS << ActiveIncludeDir << '\n';
    467       } else if (Arg == "--libdir") {
    468         OS << ActiveLibDir << '\n';
    469       } else if (Arg == "--cppflags") {
    470         OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
    471       } else if (Arg == "--cflags") {
    472         OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
    473       } else if (Arg == "--cxxflags") {
    474         OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
    475       } else if (Arg == "--ldflags") {
    476         OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
    477            << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
    478       } else if (Arg == "--system-libs") {
    479         PrintSystemLibs = true;
    480       } else if (Arg == "--libs") {
    481         PrintLibs = true;
    482       } else if (Arg == "--libnames") {
    483         PrintLibNames = true;
    484       } else if (Arg == "--libfiles") {
    485         PrintLibFiles = true;
    486       } else if (Arg == "--components") {
    487         /// If there are missing static archives and a dylib was
    488         /// built, print LLVM_DYLIB_COMPONENTS instead of everything
    489         /// in the manifest.
    490         std::vector<std::string> Components;
    491         for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
    492           // Only include non-installed components when in a development tree.
    493           if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
    494             continue;
    495 
    496           Components.push_back(AvailableComponents[j].Name);
    497           if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
    498             std::string path(
    499                 GetComponentLibraryPath(AvailableComponents[j].Library, false));
    500             if (DirSep == "\\") {
    501               std::replace(path.begin(), path.end(), '/', '\\');
    502             }
    503             if (DyLibExists && !sys::fs::exists(path)) {
    504               Components =
    505                   GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
    506               std::sort(Components.begin(), Components.end());
    507               break;
    508             }
    509           }
    510         }
    511 
    512         for (unsigned I = 0; I < Components.size(); ++I) {
    513           if (I) {
    514             OS << ' ';
    515           }
    516 
    517           OS << Components[I];
    518         }
    519         OS << '\n';
    520       } else if (Arg == "--targets-built") {
    521         OS << LLVM_TARGETS_BUILT << '\n';
    522       } else if (Arg == "--host-target") {
    523         OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
    524       } else if (Arg == "--build-mode") {
    525         OS << build_mode << '\n';
    526       } else if (Arg == "--assertion-mode") {
    527 #if defined(NDEBUG)
    528         OS << "OFF\n";
    529 #else
    530         OS << "ON\n";
    531 #endif
    532       } else if (Arg == "--build-system") {
    533         OS << LLVM_BUILD_SYSTEM << '\n';
    534       } else if (Arg == "--has-rtti") {
    535         OS << LLVM_HAS_RTTI << '\n';
    536       } else if (Arg == "--has-global-isel") {
    537         OS << LLVM_HAS_GLOBAL_ISEL << '\n';
    538       } else if (Arg == "--shared-mode") {
    539         PrintSharedMode = true;
    540       } else if (Arg == "--obj-root") {
    541         OS << ActivePrefix << '\n';
    542       } else if (Arg == "--src-root") {
    543         OS << LLVM_SRC_ROOT << '\n';
    544       } else if (Arg == "--link-shared") {
    545         LinkMode = LinkModeShared;
    546       } else if (Arg == "--link-static") {
    547         LinkMode = LinkModeStatic;
    548       } else {
    549         usage();
    550       }
    551     } else {
    552       Components.push_back(Arg);
    553     }
    554   }
    555 
    556   if (!HasAnyOption)
    557     usage();
    558 
    559   if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
    560     errs() << "llvm-config: error: " << DyLibName << " is missing\n";
    561     return 1;
    562   }
    563 
    564   if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
    565       PrintSharedMode) {
    566 
    567     if (PrintSharedMode && BuiltSharedLibs) {
    568       OS << "shared\n";
    569       return 0;
    570     }
    571 
    572     // If no components were specified, default to "all".
    573     if (Components.empty())
    574       Components.push_back("all");
    575 
    576     // Construct the list of all the required libraries.
    577     std::function<std::string(const StringRef &)>
    578         GetComponentLibraryPathFunction = [&](const StringRef &Name) {
    579           return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
    580         };
    581     std::vector<std::string> MissingLibs;
    582     std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
    583         Components,
    584         /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
    585         &GetComponentLibraryPathFunction, &MissingLibs, DirSep);
    586     if (!MissingLibs.empty()) {
    587       switch (LinkMode) {
    588       case LinkModeShared:
    589         if (DyLibExists && !BuiltSharedLibs)
    590           break;
    591         // Using component shared libraries.
    592         for (auto &Lib : MissingLibs)
    593           errs() << "llvm-config: error: missing: " << Lib << "\n";
    594         return 1;
    595       case LinkModeAuto:
    596         if (DyLibExists) {
    597           LinkMode = LinkModeShared;
    598           break;
    599         }
    600         errs()
    601             << "llvm-config: error: component libraries and shared library\n\n";
    602       // fall through
    603       case LinkModeStatic:
    604         for (auto &Lib : MissingLibs)
    605           errs() << "llvm-config: error: missing: " << Lib << "\n";
    606         return 1;
    607       }
    608     } else if (LinkMode == LinkModeAuto) {
    609       LinkMode = LinkModeStatic;
    610     }
    611 
    612     if (PrintSharedMode) {
    613       std::unordered_set<std::string> FullDyLibComponents;
    614       std::vector<std::string> DyLibComponents =
    615           GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
    616 
    617       for (auto &Component : DyLibComponents) {
    618         FullDyLibComponents.insert(Component);
    619       }
    620       DyLibComponents.clear();
    621 
    622       for (auto &Lib : RequiredLibs) {
    623         if (!FullDyLibComponents.count(Lib)) {
    624           OS << "static\n";
    625           return 0;
    626         }
    627       }
    628       FullDyLibComponents.clear();
    629 
    630       if (LinkMode == LinkModeShared) {
    631         OS << "shared\n";
    632         return 0;
    633       } else {
    634         OS << "static\n";
    635         return 0;
    636       }
    637     }
    638 
    639     if (PrintLibs || PrintLibNames || PrintLibFiles) {
    640 
    641       auto PrintForLib = [&](const StringRef &Lib) {
    642         const bool Shared = LinkMode == LinkModeShared;
    643         if (PrintLibNames) {
    644           OS << GetComponentLibraryFileName(Lib, Shared);
    645         } else if (PrintLibFiles) {
    646           OS << GetComponentLibraryPath(Lib, Shared);
    647         } else if (PrintLibs) {
    648           // On Windows, output full path to library without parameters.
    649           // Elsewhere, if this is a typical library name, include it using -l.
    650           if (HostTriple.isWindowsMSVCEnvironment()) {
    651             OS << GetComponentLibraryPath(Lib, Shared);
    652           } else {
    653             StringRef LibName;
    654             if (GetComponentLibraryNameSlice(Lib, LibName)) {
    655               // Extract library name (remove prefix and suffix).
    656               OS << "-l" << LibName;
    657             } else {
    658               // Lib is already a library name without prefix and suffix.
    659               OS << "-l" << Lib;
    660             }
    661           }
    662         }
    663       };
    664 
    665       if (LinkMode == LinkModeShared && !BuiltSharedLibs) {
    666         PrintForLib(DyLibName);
    667       } else {
    668         for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
    669           auto Lib = RequiredLibs[i];
    670           if (i)
    671             OS << ' ';
    672 
    673           PrintForLib(Lib);
    674         }
    675       }
    676       OS << '\n';
    677     }
    678 
    679     // Print SYSTEM_LIBS after --libs.
    680     // FIXME: Each LLVM component may have its dependent system libs.
    681     if (PrintSystemLibs)
    682       OS << LLVM_SYSTEM_LIBS << '\n';
    683   } else if (!Components.empty()) {
    684     errs() << "llvm-config: error: components given, but unused\n\n";
    685     usage();
    686   }
    687 
    688   return 0;
    689 }
    690