blob: 934d710d497835b2cd9c4ff537e2fc02b720d2c7 [file] [log] [blame]
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +00001//===-- 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"
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +000023#include "llvm/ADT/Triple.h"
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000024#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"
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000029#include "llvm/Support/raw_ostream.h"
30#include <cstdlib>
31#include <set>
32#include <vector>
Richard Diamond72303a22015-11-09 23:15:38 +000033#include <unordered_set>
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000034
35using 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.
Richard Diamond72303a22015-11-09 23:15:38 +000055/// \param RequiredLibs [out] - The ordered list of required
56/// libraries.
57/// \param GetComponentNames - Get the component names instead of the
58/// library name.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000059static void VisitComponent(StringRef Name,
60 const StringMap<AvailableComponent*> &ComponentMap,
61 std::set<AvailableComponent*> &VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +000062 std::vector<StringRef> &RequiredLibs,
Richard Diamond72303a22015-11-09 23:15:38 +000063 bool IncludeNonInstalled, bool GetComponentNames,
64 const std::string *ActiveLibDir, bool *HasMissing) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000065 // 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
Daniel Dunbarc364d682012-05-15 18:44:17 +000075 // Only include non-installed components if requested.
76 if (!AC->IsInstalled && !IncludeNonInstalled)
77 return;
78
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000079 // Otherwise, visit all the dependencies.
80 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
81 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
Richard Diamond72303a22015-11-09 23:15:38 +000082 RequiredLibs, IncludeNonInstalled, GetComponentNames,
83 ActiveLibDir, HasMissing);
84 }
85
86 if (GetComponentNames) {
87 RequiredLibs.push_back(Name);
88 return;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000089 }
90
91 // Add to the required library list.
Richard Diamond72303a22015-11-09 23:15:38 +000092 if (AC->Library) {
93 if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) {
94 *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library);
95 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000096 RequiredLibs.push_back(AC->Library);
Richard Diamond72303a22015-11-09 23:15:38 +000097 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000098}
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 RequiredLibs [out] - On return, the ordered list of libraries that
106/// are required to link the given components.
Daniel Dunbarc364d682012-05-15 18:44:17 +0000107/// \param IncludeNonInstalled - Whether non-installed components should be
108/// reported.
Richard Diamond72303a22015-11-09 23:15:38 +0000109/// \param GetComponentNames - True if one would prefer the component names.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000110static void ComputeLibsForComponents(const std::vector<StringRef> &Components,
111 std::vector<StringRef> &RequiredLibs,
Richard Diamond72303a22015-11-09 23:15:38 +0000112 bool IncludeNonInstalled, bool GetComponentNames,
113 const std::string *ActiveLibDir,
114 bool *HasMissing) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000115 std::set<AvailableComponent*> VisitedComponents;
116
117 // Build a map of component names to information.
118 StringMap<AvailableComponent*> ComponentMap;
119 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
120 AvailableComponent *AC = &AvailableComponents[i];
121 ComponentMap[AC->Name] = AC;
122 }
123
124 // Visit the components.
125 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
126 // Users are allowed to provide mixed case component names.
127 std::string ComponentLower = Components[i].lower();
128
129 // Validate that the user supplied a valid component name.
130 if (!ComponentMap.count(ComponentLower)) {
131 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
132 << "\n";
133 exit(1);
134 }
135
136 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
Richard Diamond72303a22015-11-09 23:15:38 +0000137 RequiredLibs, IncludeNonInstalled, GetComponentNames,
138 ActiveLibDir, HasMissing);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000139 }
140
141 // The list is now ordered with leafs first, we want the libraries to printed
142 // in the reverse order of dependency.
143 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
144}
145
146/* *** */
147
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000148static void usage() {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000149 errs() << "\
150usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
151\n\
152Get various configuration information needed to compile programs which use\n\
153LLVM. 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\
158Options:\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\
NAKAMURA Takumi800eb082013-12-25 02:24:32 +0000170 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000171 --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\
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000178 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Tom Stellard5268c172015-09-09 16:39:30 +0000179 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
Tom Stellard18bf6262015-11-04 20:57:43 +0000180 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
Richard Diamond72303a22015-11-09 23:15:38 +0000181 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000182Typical components:\n\
183 all All LLVM libraries (default).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000184 engine Either a native JIT or a bitcode interpreter.\n";
185 exit(1);
186}
187
188/// \brief Compute the path to the main executable.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000189std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000190 // 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;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000193 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000194}
195
Richard Diamond72303a22015-11-09 23:15:38 +0000196/// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
197/// the full list of components.
198std::vector<StringRef> 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.size() > 0);
214 }
215
216 std::vector<StringRef> Components;
217 ComputeLibsForComponents(DyLibComponents, Components,
218 /*IncludeNonInstalled=*/IsInDevelopmentTree,
219 GetComponentNames, nullptr, nullptr);
220
Reid Klecknerdd02e8b2015-11-09 23:37:26 +0000221 return Components;
Richard Diamond72303a22015-11-09 23:15:38 +0000222}
223
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000224int main(int argc, char **argv) {
225 std::vector<StringRef> Components;
226 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
Richard Diamond72303a22015-11-09 23:15:38 +0000227 bool PrintSystemLibs = false, PrintSharedMode = false;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000228 bool HasAnyOption = false;
229
230 // llvm-config is designed to support being run both from a development tree
231 // and from an installed path. We try and auto-detect which case we are in so
232 // that we can report the correct information when run from a development
233 // tree.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000234 bool IsInDevelopmentTree;
235 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000236 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000237 std::string CurrentExecPrefix;
238 std::string ActiveObjRoot;
239
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000240 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
241 char const *build_mode = LLVM_BUILDMODE;
242#if defined(CMAKE_CFG_INTDIR)
243 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
244 build_mode = CMAKE_CFG_INTDIR;
245#endif
246
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000247 // Create an absolute path, and pop up one directory (we expect to be inside a
248 // bin dir).
249 sys::fs::make_absolute(CurrentPath);
250 CurrentExecPrefix = sys::path::parent_path(
251 sys::path::parent_path(CurrentPath)).str();
252
253 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000254 // locations (prefix style or CMake style).
255 if (sys::fs::equivalent(CurrentExecPrefix,
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000256 Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000257 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000258 DevelopmentTreeLayout = MakefileStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000259
260 // If we are in a development tree, then check if we are in a BuildTools
261 // directory. This indicates we are built for the build triple, but we
262 // always want to provide information for the host triple.
263 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
264 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
265 } else {
266 ActiveObjRoot = LLVM_OBJ_ROOT;
267 }
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000268 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000269 IsInDevelopmentTree = true;
270 DevelopmentTreeLayout = CMakeStyle;
271 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000272 } else if (sys::fs::equivalent(CurrentExecPrefix,
273 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000274 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000275 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000276 ActiveObjRoot = LLVM_OBJ_ROOT;
277 } else {
278 IsInDevelopmentTree = false;
Duncan Sandsf320be82012-02-23 08:25:25 +0000279 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000280 }
281
282 // Compute various directory locations based on the derived location
283 // information.
284 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
285 std::string ActiveIncludeOption;
286 if (IsInDevelopmentTree) {
287 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
288 ActivePrefix = CurrentExecPrefix;
289
290 // CMake organizes the products differently than a normal prefix style
291 // layout.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000292 switch (DevelopmentTreeLayout) {
293 case MakefileStyle:
NAKAMURA Takumi46c19032013-12-20 17:35:52 +0000294 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000295 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000296 ActiveLibDir =
297 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000298 break;
299 case CMakeStyle:
300 ActiveBinDir = ActiveObjRoot + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000301 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000302 break;
303 case CMakeBuildModeStyle:
NAKAMURA Takumi429a2222013-12-19 16:02:23 +0000304 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000305 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
Chandler Carruth7d587762014-12-29 11:16:25 +0000306 ActiveLibDir =
307 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000308 break;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000309 }
310
311 // We need to include files from both the source and object trees.
312 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
313 "-I" + ActiveObjRoot + "/include");
314 } else {
315 ActivePrefix = CurrentExecPrefix;
316 ActiveIncludeDir = ActivePrefix + "/include";
317 ActiveBinDir = ActivePrefix + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000318 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000319 ActiveIncludeOption = "-I" + ActiveIncludeDir;
320 }
321
Richard Diamond72303a22015-11-09 23:15:38 +0000322 /// We only use `shared library` mode in cases where the static library form
323 /// of the components provided are not available; note however that this is
324 /// skipped if we're run from within the build dir. However, once installed,
325 /// we still need to provide correct output when the static archives are
326 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
327 /// in the first place. This can't be done at configure/build time.
328
329 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
330 StaticPrefix, StaticDir = "lib";
331 const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE));
332 if (HostTriple.isOSWindows()) {
333 SharedExt = "dll";
334 SharedVersionedExt = PACKAGE_VERSION ".dll";
335 StaticExt = "a";
336 SharedDir = ActiveBinDir;
337 StaticDir = ActiveLibDir;
338 StaticPrefix = SharedPrefix = "";
339 } else if (HostTriple.isOSDarwin()) {
340 SharedExt = "dylib";
341 SharedVersionedExt = PACKAGE_VERSION ".dylib";
342 StaticExt = "a";
343 StaticDir = SharedDir = ActiveLibDir;
344 StaticPrefix = SharedPrefix = "lib";
345 } else {
346 // default to the unix values:
347 SharedExt = "so";
348 SharedVersionedExt = PACKAGE_VERSION ".so";
349 StaticExt = "a";
350 StaticDir = SharedDir = ActiveLibDir;
351 StaticPrefix = SharedPrefix = "lib";
352 }
353
354 const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
355
356 enum { CMake, AutoConf } ConfigTool;
357 if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) {
358 ConfigTool = CMake;
359 } else {
360 ConfigTool = AutoConf;
361 }
362
363 /// CMake style shared libs, ie each component is in a shared library.
364 const bool BuiltSharedLibs =
365 (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0);
366
367 bool DyLibExists = false;
368 const std::string DyLibName =
369 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
370
371 if (BuiltDyLib) {
372 DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName);
373 }
374
375 /// Get the component's library name without the lib prefix and the
376 /// extension. Returns true if Lib is in a recognized format.
377 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
378 StringRef &Out) {
379 if (Lib.startswith("lib")) {
380 unsigned FromEnd;
381 if (Lib.endswith(StaticExt)) {
382 FromEnd = StaticExt.size() + 1;
383 } else if (Lib.endswith(SharedExt)) {
384 FromEnd = SharedExt.size() + 1;
385 } else {
386 FromEnd = 0;
387 }
388
389 if (FromEnd != 0) {
390 Out = Lib.slice(3, Lib.size() - FromEnd);
391 return true;
392 }
393 }
394
395 return false;
396 };
397 /// Maps Unixizms to the host platform.
398 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
399 const bool ForceShared) {
400 std::string LibFileName = Lib;
401 StringRef LibName;
402 if (GetComponentLibraryNameSlice(Lib, LibName)) {
403 if (BuiltSharedLibs || ForceShared) {
404 LibFileName = (SharedPrefix + LibName + "." + SharedExt).str();
405 } else {
406 // default to static
407 LibFileName = (StaticPrefix + LibName + "." + StaticExt).str();
408 }
409 }
410
411 return LibFileName;
412 };
413 /// Get the full path for a possibly shared component library.
414 auto GetComponentLibraryPath = [&](const StringRef &Name,
415 const bool ForceShared) {
416 auto LibFileName = GetComponentLibraryFileName(Name, ForceShared);
417 if (BuiltSharedLibs || ForceShared) {
418 return (SharedDir + "/" + LibFileName).str();
419 } else {
420 return (StaticDir + "/" + LibFileName).str();
421 }
422 };
423
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000424 raw_ostream &OS = outs();
425 for (int i = 1; i != argc; ++i) {
426 StringRef Arg = argv[i];
427
428 if (Arg.startswith("-")) {
429 HasAnyOption = true;
430 if (Arg == "--version") {
431 OS << PACKAGE_VERSION << '\n';
432 } else if (Arg == "--prefix") {
433 OS << ActivePrefix << '\n';
434 } else if (Arg == "--bindir") {
435 OS << ActiveBinDir << '\n';
436 } else if (Arg == "--includedir") {
437 OS << ActiveIncludeDir << '\n';
438 } else if (Arg == "--libdir") {
439 OS << ActiveLibDir << '\n';
440 } else if (Arg == "--cppflags") {
441 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
442 } else if (Arg == "--cflags") {
443 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
444 } else if (Arg == "--cxxflags") {
445 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
446 } else if (Arg == "--ldflags") {
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000447 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
448 } else if (Arg == "--system-libs") {
449 PrintSystemLibs = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000450 } else if (Arg == "--libs") {
451 PrintLibs = true;
452 } else if (Arg == "--libnames") {
453 PrintLibNames = true;
454 } else if (Arg == "--libfiles") {
455 PrintLibFiles = true;
456 } else if (Arg == "--components") {
Richard Diamond72303a22015-11-09 23:15:38 +0000457 /// If there are missing static archives and a dylib was
458 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
459 /// in the manifest.
460 std::vector<StringRef> Components;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000461 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarc364d682012-05-15 18:44:17 +0000462 // Only include non-installed components when in a development tree.
463 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
464 continue;
465
Richard Diamond72303a22015-11-09 23:15:38 +0000466 Components.push_back(AvailableComponents[j].Name);
467 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
468 if (DyLibExists &&
469 !sys::fs::exists(GetComponentLibraryPath(
470 AvailableComponents[j].Library, false))) {
471 Components = GetAllDyLibComponents(IsInDevelopmentTree, true);
472 std::sort(Components.begin(), Components.end());
473 break;
474 }
475 }
476 }
477
478 for (unsigned I = 0; I < Components.size(); ++I) {
479 if (I) {
480 OS << ' ';
481 }
482
483 OS << Components[I];
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000484 }
485 OS << '\n';
486 } else if (Arg == "--targets-built") {
Daniel Dunbar30a89762011-12-16 00:04:43 +0000487 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000488 } else if (Arg == "--host-target") {
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +0000489 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000490 } else if (Arg == "--build-mode") {
NAKAMURA Takumi1b16e272013-12-03 14:35:17 +0000491 OS << build_mode << '\n';
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000492 } else if (Arg == "--assertion-mode") {
493#if defined(NDEBUG)
494 OS << "OFF\n";
495#else
496 OS << "ON\n";
497#endif
Tom Stellard5268c172015-09-09 16:39:30 +0000498 } else if (Arg == "--build-system") {
499 OS << LLVM_BUILD_SYSTEM << '\n';
Tom Stellard18bf6262015-11-04 20:57:43 +0000500 } else if (Arg == "--has-rtti") {
501 OS << LLVM_HAS_RTTI << '\n';
Richard Diamond72303a22015-11-09 23:15:38 +0000502 } else if (Arg == "--shared-mode") {
503 PrintSharedMode = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000504 } else if (Arg == "--obj-root") {
NAKAMURA Takumi93a14622013-12-19 16:02:28 +0000505 OS << ActivePrefix << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000506 } else if (Arg == "--src-root") {
507 OS << LLVM_SRC_ROOT << '\n';
508 } else {
509 usage();
510 }
511 } else {
512 Components.push_back(Arg);
513 }
514 }
515
516 if (!HasAnyOption)
517 usage();
518
Richard Diamond72303a22015-11-09 23:15:38 +0000519 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
520 PrintSharedMode) {
521
522 if (PrintSharedMode && BuiltSharedLibs) {
523 OS << "shared\n";
524 return 0;
525 }
526
Daniel Dunbarfbc6a892011-12-12 18:22:04 +0000527 // If no components were specified, default to "all".
528 if (Components.empty())
529 Components.push_back("all");
530
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000531 // Construct the list of all the required libraries.
532 std::vector<StringRef> RequiredLibs;
Richard Diamond72303a22015-11-09 23:15:38 +0000533 bool HasMissing = false;
Daniel Dunbarc364d682012-05-15 18:44:17 +0000534 ComputeLibsForComponents(Components, RequiredLibs,
Richard Diamond72303a22015-11-09 23:15:38 +0000535 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
536 &ActiveLibDir, &HasMissing);
537
538 if (PrintSharedMode) {
539 std::unordered_set<std::string> FullDyLibComponents;
540 std::vector<StringRef> DyLibComponents =
541 GetAllDyLibComponents(IsInDevelopmentTree, false);
542
543 for (auto &Component : DyLibComponents) {
544 FullDyLibComponents.insert(Component);
545 }
546 DyLibComponents.clear();
547
548 for (auto &Lib : RequiredLibs) {
549 if (!FullDyLibComponents.count(Lib)) {
550 OS << "static\n";
551 return 0;
552 }
553 }
554 FullDyLibComponents.clear();
555
556 if (HasMissing && DyLibExists) {
557 OS << "shared\n";
558 return 0;
559 } else {
560 OS << "static\n";
561 return 0;
562 }
563 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000564
Richard Osborne49ae1172014-03-03 15:06:14 +0000565 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000566
Richard Diamond72303a22015-11-09 23:15:38 +0000567 auto PrintForLib = [&](const StringRef &Lib, const bool ForceShared) {
Richard Osborne49ae1172014-03-03 15:06:14 +0000568 if (PrintLibNames) {
Richard Diamond72303a22015-11-09 23:15:38 +0000569 OS << GetComponentLibraryFileName(Lib, ForceShared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000570 } else if (PrintLibFiles) {
Richard Diamond72303a22015-11-09 23:15:38 +0000571 OS << GetComponentLibraryPath(Lib, ForceShared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000572 } else if (PrintLibs) {
573 // If this is a typical library name, include it using -l.
Richard Diamond72303a22015-11-09 23:15:38 +0000574 StringRef LibName;
575 if (Lib.startswith("lib")) {
576 if (GetComponentLibraryNameSlice(Lib, LibName)) {
577 OS << "-l" << LibName;
578 } else {
579 OS << "-l:" << GetComponentLibraryFileName(Lib, ForceShared);
580 }
581 } else {
582 // Otherwise, print the full path.
583 OS << GetComponentLibraryPath(Lib, ForceShared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000584 }
Richard Diamond72303a22015-11-09 23:15:38 +0000585 }
586 };
Richard Osborne49ae1172014-03-03 15:06:14 +0000587
Richard Diamond72303a22015-11-09 23:15:38 +0000588 if (HasMissing && DyLibExists) {
589 PrintForLib(DyLibName, true);
590 } else {
591 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
592 StringRef Lib = RequiredLibs[i];
593 if (i)
594 OS << ' ';
595
596 PrintForLib(Lib, false);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000597 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000598 }
Richard Osborne49ae1172014-03-03 15:06:14 +0000599 OS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000600 }
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000601
602 // Print SYSTEM_LIBS after --libs.
603 // FIXME: Each LLVM component may have its dependent system libs.
604 if (PrintSystemLibs)
605 OS << LLVM_SYSTEM_LIBS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000606 } else if (!Components.empty()) {
607 errs() << "llvm-config: error: components given, but unused\n\n";
608 usage();
609 }
610
611 return 0;
612}