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