blob: 76a40da49608185d35f251b3eadd48aea64f9870 [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>
Andrew Wilkins1611ec42016-01-12 07:23:58 +000032#include <unordered_set>
Andrew Wilkinsdfd60882016-01-20 04:03:09 +000033#include <vector>
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
Andrew Wilkinsdfd60882016-01-20 04:03:09 +000049// LinkMode determines what libraries and flags are returned by llvm-config.
50enum 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
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000065/// \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.
Richard Diamond72303a22015-11-09 23:15:38 +000071/// \param RequiredLibs [out] - The ordered list of required
72/// libraries.
73/// \param GetComponentNames - Get the component names instead of the
74/// library name.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +000075static void VisitComponent(const std::string &Name,
76 const StringMap<AvailableComponent *> &ComponentMap,
77 std::set<AvailableComponent *> &VisitedComponents,
Richard Diamonda62513c2015-11-25 22:49:48 +000078 std::vector<std::string> &RequiredLibs,
Richard Diamond72303a22015-11-09 23:15:38 +000079 bool IncludeNonInstalled, bool GetComponentNames,
Andrew Wilkinsdfd60882016-01-20 04:03:09 +000080 const std::function<std::string(const StringRef &)>
81 *GetComponentLibraryPath,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +000082 std::vector<std::string> *Missing,
83 const std::string &DirSep) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000084 // Lookup the component.
85 AvailableComponent *AC = ComponentMap.lookup(Name);
Mehdi Amini907313a2016-02-12 18:43:10 +000086 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 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000094 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
Daniel Dunbarc364d682012-05-15 18:44:17 +0000102 // Only include non-installed components if requested.
103 if (!AC->IsInstalled && !IncludeNonInstalled)
104 return;
105
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000106 // Otherwise, visit all the dependencies.
107 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
108 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
Richard Diamond72303a22015-11-09 23:15:38 +0000109 RequiredLibs, IncludeNonInstalled, GetComponentNames,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000110 GetComponentLibraryPath, Missing, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000111 }
112
113 if (GetComponentNames) {
114 RequiredLibs.push_back(Name);
115 return;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000116 }
117
118 // Add to the required library list.
Richard Diamond72303a22015-11-09 23:15:38 +0000119 if (AC->Library) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000120 if (Missing && GetComponentLibraryPath) {
121 std::string path = (*GetComponentLibraryPath)(AC->Library);
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000122 if (DirSep == "\\") {
123 std::replace(path.begin(), path.end(), '/', '\\');
124 }
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000125 if (!sys::fs::exists(path))
126 Missing->push_back(path);
Richard Diamond72303a22015-11-09 23:15:38 +0000127 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000128 RequiredLibs.push_back(AC->Library);
Richard Diamond72303a22015-11-09 23:15:38 +0000129 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000130}
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.
Daniel Dunbarc364d682012-05-15 18:44:17 +0000137/// \param IncludeNonInstalled - Whether non-installed components should be
138/// reported.
Richard Diamond72303a22015-11-09 23:15:38 +0000139/// \param GetComponentNames - True if one would prefer the component names.
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000140static 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) {
Richard Diamonda62513c2015-11-25 22:49:48 +0000145 std::vector<std::string> RequiredLibs;
David Blaikieb7504172015-11-09 23:51:45 +0000146 std::set<AvailableComponent *> VisitedComponents;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000147
148 // Build a map of component names to information.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000149 StringMap<AvailableComponent *> ComponentMap;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000150 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,
Richard Diamond72303a22015-11-09 23:15:38 +0000168 RequiredLibs, IncludeNonInstalled, GetComponentNames,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000169 GetComponentLibraryPath, Missing, DirSep);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000170 }
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());
David Blaikieb7504172015-11-09 23:51:45 +0000175
176 return RequiredLibs;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000177}
178
179/* *** */
180
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000181static void usage() {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000182 errs() << "\
183usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
184\n\
185Get various configuration information needed to compile programs which use\n\
186LLVM. 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\
191Options:\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\
NAKAMURA Takumi800eb082013-12-25 02:24:32 +0000203 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000204 --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\
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000211 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Tom Stellard5268c172015-09-09 16:39:30 +0000212 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
Tom Stellard18bf6262015-11-04 20:57:43 +0000213 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
Quentin Colombet447f8522016-03-08 00:02:50 +0000214 --has-global-isel Print whether or not LLVM was built with global-isel support (YES or NO).\n\
Richard Diamond72303a22015-11-09 23:15:38 +0000215 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000216 --link-shared Link the components as shared libraries.\n\
217 --link-static Link the component libraries statically.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000218Typical components:\n\
219 all All LLVM libraries (default).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000220 engine Either a native JIT or a bitcode interpreter.\n";
221 exit(1);
222}
223
224/// \brief Compute the path to the main executable.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000225std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000226 // This just needs to be some symbol in the binary; C++ doesn't
227 // allow taking the address of ::main however.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000228 void *P = (void *)(intptr_t)GetExecutablePath;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000229 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000230}
231
Richard Diamond72303a22015-11-09 23:15:38 +0000232/// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
233/// the full list of components.
Richard Diamonda62513c2015-11-25 22:49:48 +0000234std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000235 const bool GetComponentNames,
236 const std::string &DirSep) {
Richard Diamond72303a22015-11-09 23:15:38 +0000237 std::vector<StringRef> DyLibComponents;
Richard Diamond72303a22015-11-09 23:15:38 +0000238
David Blaikieb7504172015-11-09 23:51:45 +0000239 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;
Richard Diamond72303a22015-11-09 23:15:38 +0000248 }
249
David Blaikieb7504172015-11-09 23:51:45 +0000250 assert(!DyLibComponents.empty());
Richard Diamond72303a22015-11-09 23:15:38 +0000251
David Blaikieb7504172015-11-09 23:51:45 +0000252 return ComputeLibsForComponents(DyLibComponents,
253 /*IncludeNonInstalled=*/IsInDevelopmentTree,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000254 GetComponentNames, nullptr, nullptr, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000255}
256
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000257int main(int argc, char **argv) {
258 std::vector<StringRef> Components;
259 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
Richard Diamond72303a22015-11-09 23:15:38 +0000260 bool PrintSystemLibs = false, PrintSharedMode = false;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000261 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.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000267 bool IsInDevelopmentTree;
268 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000269 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000270 std::string CurrentExecPrefix;
271 std::string ActiveObjRoot;
272
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000273 // 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
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000280 // Create an absolute path, and pop up one directory (we expect to be inside a
281 // bin dir).
282 sys::fs::make_absolute(CurrentPath);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000283 CurrentExecPrefix =
284 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000285
286 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000287 // locations (prefix style or CMake style).
288 if (sys::fs::equivalent(CurrentExecPrefix,
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000289 Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000290 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000291 DevelopmentTreeLayout = MakefileStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000292
293 // If we are in a development tree, then check if we are in a BuildTools
294 // directory. This indicates we are built for the build triple, but we
295 // always want to provide information for the host triple.
296 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
297 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
298 } else {
299 ActiveObjRoot = LLVM_OBJ_ROOT;
300 }
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000301 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000302 IsInDevelopmentTree = true;
303 DevelopmentTreeLayout = CMakeStyle;
304 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000305 } else if (sys::fs::equivalent(CurrentExecPrefix,
306 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000307 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000308 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000309 ActiveObjRoot = LLVM_OBJ_ROOT;
310 } else {
311 IsInDevelopmentTree = false;
Duncan Sandsf320be82012-02-23 08:25:25 +0000312 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000313 }
314
315 // Compute various directory locations based on the derived location
316 // information.
317 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
318 std::string ActiveIncludeOption;
319 if (IsInDevelopmentTree) {
320 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
321 ActivePrefix = CurrentExecPrefix;
322
323 // CMake organizes the products differently than a normal prefix style
324 // layout.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000325 switch (DevelopmentTreeLayout) {
326 case MakefileStyle:
NAKAMURA Takumi46c19032013-12-20 17:35:52 +0000327 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000328 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000329 ActiveLibDir =
330 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000331 break;
332 case CMakeStyle:
333 ActiveBinDir = ActiveObjRoot + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000334 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000335 break;
336 case CMakeBuildModeStyle:
NAKAMURA Takumi429a2222013-12-19 16:02:23 +0000337 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000338 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
Chandler Carruth7d587762014-12-29 11:16:25 +0000339 ActiveLibDir =
340 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000341 break;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000342 }
343
344 // We need to include files from both the source and object trees.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000345 ActiveIncludeOption =
346 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000347 } else {
348 ActivePrefix = CurrentExecPrefix;
349 ActiveIncludeDir = ActivePrefix + "/include";
350 ActiveBinDir = ActivePrefix + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000351 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000352 ActiveIncludeOption = "-I" + ActiveIncludeDir;
353 }
354
Richard Diamond72303a22015-11-09 23:15:38 +0000355 /// We only use `shared library` mode in cases where the static library form
356 /// of the components provided are not available; note however that this is
357 /// skipped if we're run from within the build dir. However, once installed,
358 /// we still need to provide correct output when the static archives are
359 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
360 /// in the first place. This can't be done at configure/build time.
361
362 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000363 StaticPrefix, StaticDir = "lib", DirSep = "/";
NAKAMURA Takumi0882a5d2016-02-10 01:12:55 +0000364 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
Richard Diamond72303a22015-11-09 23:15:38 +0000365 if (HostTriple.isOSWindows()) {
366 SharedExt = "dll";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000367 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000368 if (HostTriple.isOSCygMing()) {
369 StaticExt = "a";
NAKAMURA Takumi1621f812016-02-10 03:09:13 +0000370 StaticPrefix = "lib";
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000371 } else {
372 StaticExt = "lib";
373 DirSep = "\\";
374 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
375 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
376 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
377 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
378 std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
379 '\\');
380 }
Richard Diamond72303a22015-11-09 23:15:38 +0000381 SharedDir = ActiveBinDir;
382 StaticDir = ActiveLibDir;
Richard Diamond72303a22015-11-09 23:15:38 +0000383 } else if (HostTriple.isOSDarwin()) {
384 SharedExt = "dylib";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000385 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
Richard Diamond72303a22015-11-09 23:15:38 +0000386 StaticExt = "a";
387 StaticDir = SharedDir = ActiveLibDir;
388 StaticPrefix = SharedPrefix = "lib";
389 } else {
390 // default to the unix values:
391 SharedExt = "so";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000392 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
Richard Diamond72303a22015-11-09 23:15:38 +0000393 StaticExt = "a";
394 StaticDir = SharedDir = ActiveLibDir;
395 StaticPrefix = SharedPrefix = "lib";
396 }
397
398 const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
399
400 enum { CMake, AutoConf } ConfigTool;
401 if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) {
402 ConfigTool = CMake;
403 } else {
404 ConfigTool = AutoConf;
405 }
406
407 /// CMake style shared libs, ie each component is in a shared library.
408 const bool BuiltSharedLibs =
409 (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0);
410
411 bool DyLibExists = false;
412 const std::string DyLibName =
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000413 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
414
415 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
416 // for "--libs", etc, if they exist. This behaviour can be overridden with
417 // --link-static or --link-shared.
418 bool LinkDyLib = (std::strcmp(LLVM_LINK_DYLIB, "ON") == 0);
Richard Diamond72303a22015-11-09 23:15:38 +0000419
420 if (BuiltDyLib) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000421 std::string path((SharedDir + DirSep + DyLibName).str());
422 if (DirSep == "\\") {
423 std::replace(path.begin(), path.end(), '/', '\\');
424 }
425 DyLibExists = sys::fs::exists(path);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000426 if (!DyLibExists) {
427 // The shared library does not exist: don't error unless the user
428 // explicitly passes --link-shared.
429 LinkDyLib = false;
430 }
Richard Diamond72303a22015-11-09 23:15:38 +0000431 }
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000432 LinkMode LinkMode =
433 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
Richard Diamond72303a22015-11-09 23:15:38 +0000434
435 /// Get the component's library name without the lib prefix and the
436 /// extension. Returns true if Lib is in a recognized format.
437 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
438 StringRef &Out) {
439 if (Lib.startswith("lib")) {
440 unsigned FromEnd;
441 if (Lib.endswith(StaticExt)) {
442 FromEnd = StaticExt.size() + 1;
443 } else if (Lib.endswith(SharedExt)) {
444 FromEnd = SharedExt.size() + 1;
445 } else {
446 FromEnd = 0;
447 }
448
449 if (FromEnd != 0) {
450 Out = Lib.slice(3, Lib.size() - FromEnd);
451 return true;
452 }
453 }
454
455 return false;
456 };
457 /// Maps Unixizms to the host platform.
458 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000459 const bool Shared) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000460 std::string LibFileName;
461 if (Shared) {
462 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
463 } else {
464 // default to static
465 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000466 }
467
468 return LibFileName;
469 };
470 /// Get the full path for a possibly shared component library.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000471 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
472 auto LibFileName = GetComponentLibraryFileName(Name, Shared);
473 if (Shared) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000474 return (SharedDir + DirSep + LibFileName).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000475 } else {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000476 return (StaticDir + DirSep + LibFileName).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000477 }
478 };
479
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000480 raw_ostream &OS = outs();
481 for (int i = 1; i != argc; ++i) {
482 StringRef Arg = argv[i];
483
484 if (Arg.startswith("-")) {
485 HasAnyOption = true;
486 if (Arg == "--version") {
487 OS << PACKAGE_VERSION << '\n';
488 } else if (Arg == "--prefix") {
489 OS << ActivePrefix << '\n';
490 } else if (Arg == "--bindir") {
491 OS << ActiveBinDir << '\n';
492 } else if (Arg == "--includedir") {
493 OS << ActiveIncludeDir << '\n';
494 } else if (Arg == "--libdir") {
495 OS << ActiveLibDir << '\n';
496 } else if (Arg == "--cppflags") {
497 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
498 } else if (Arg == "--cflags") {
499 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
500 } else if (Arg == "--cxxflags") {
501 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
502 } else if (Arg == "--ldflags") {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000503 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
504 << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000505 } else if (Arg == "--system-libs") {
506 PrintSystemLibs = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000507 } else if (Arg == "--libs") {
508 PrintLibs = true;
509 } else if (Arg == "--libnames") {
510 PrintLibNames = true;
511 } else if (Arg == "--libfiles") {
512 PrintLibFiles = true;
513 } else if (Arg == "--components") {
Richard Diamond72303a22015-11-09 23:15:38 +0000514 /// If there are missing static archives and a dylib was
515 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
516 /// in the manifest.
Richard Diamonda62513c2015-11-25 22:49:48 +0000517 std::vector<std::string> Components;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000518 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarc364d682012-05-15 18:44:17 +0000519 // Only include non-installed components when in a development tree.
520 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
521 continue;
522
Richard Diamond72303a22015-11-09 23:15:38 +0000523 Components.push_back(AvailableComponents[j].Name);
524 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000525 std::string path(
526 GetComponentLibraryPath(AvailableComponents[j].Library, false));
527 if (DirSep == "\\") {
528 std::replace(path.begin(), path.end(), '/', '\\');
529 }
530 if (DyLibExists && !sys::fs::exists(path)) {
531 Components =
532 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000533 std::sort(Components.begin(), Components.end());
534 break;
535 }
536 }
537 }
538
539 for (unsigned I = 0; I < Components.size(); ++I) {
540 if (I) {
541 OS << ' ';
542 }
543
544 OS << Components[I];
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000545 }
546 OS << '\n';
547 } else if (Arg == "--targets-built") {
Daniel Dunbar30a89762011-12-16 00:04:43 +0000548 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000549 } else if (Arg == "--host-target") {
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +0000550 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000551 } else if (Arg == "--build-mode") {
NAKAMURA Takumi1b16e272013-12-03 14:35:17 +0000552 OS << build_mode << '\n';
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000553 } else if (Arg == "--assertion-mode") {
554#if defined(NDEBUG)
555 OS << "OFF\n";
556#else
557 OS << "ON\n";
558#endif
Tom Stellard5268c172015-09-09 16:39:30 +0000559 } else if (Arg == "--build-system") {
560 OS << LLVM_BUILD_SYSTEM << '\n';
Tom Stellard18bf6262015-11-04 20:57:43 +0000561 } else if (Arg == "--has-rtti") {
562 OS << LLVM_HAS_RTTI << '\n';
Quentin Colombet447f8522016-03-08 00:02:50 +0000563 } else if (Arg == "--has-global-isel") {
564 OS << LLVM_HAS_GLOBAL_ISEL << '\n';
Richard Diamond72303a22015-11-09 23:15:38 +0000565 } else if (Arg == "--shared-mode") {
566 PrintSharedMode = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000567 } else if (Arg == "--obj-root") {
NAKAMURA Takumi93a14622013-12-19 16:02:28 +0000568 OS << ActivePrefix << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000569 } else if (Arg == "--src-root") {
570 OS << LLVM_SRC_ROOT << '\n';
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000571 } else if (Arg == "--link-shared") {
572 LinkMode = LinkModeShared;
573 } else if (Arg == "--link-static") {
574 LinkMode = LinkModeStatic;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000575 } else {
576 usage();
577 }
578 } else {
579 Components.push_back(Arg);
580 }
581 }
582
583 if (!HasAnyOption)
584 usage();
585
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000586 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
587 errs() << "llvm-config: error: " << DyLibName << " is missing\n";
588 return 1;
589 }
590
Richard Diamond72303a22015-11-09 23:15:38 +0000591 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
592 PrintSharedMode) {
593
594 if (PrintSharedMode && BuiltSharedLibs) {
595 OS << "shared\n";
596 return 0;
597 }
598
Daniel Dunbarfbc6a892011-12-12 18:22:04 +0000599 // If no components were specified, default to "all".
600 if (Components.empty())
601 Components.push_back("all");
602
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000603 // Construct the list of all the required libraries.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000604 std::function<std::string(const StringRef &)>
605 GetComponentLibraryPathFunction = [&](const StringRef &Name) {
606 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
607 };
608 std::vector<std::string> MissingLibs;
609 std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
610 Components,
611 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000612 &GetComponentLibraryPathFunction, &MissingLibs, DirSep);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000613 if (!MissingLibs.empty()) {
614 switch (LinkMode) {
615 case LinkModeShared:
616 if (DyLibExists && !BuiltSharedLibs)
617 break;
618 // Using component shared libraries.
619 for (auto &Lib : MissingLibs)
620 errs() << "llvm-config: error: missing: " << Lib << "\n";
621 return 1;
622 case LinkModeAuto:
623 if (DyLibExists) {
624 LinkMode = LinkModeShared;
625 break;
626 }
627 errs()
628 << "llvm-config: error: component libraries and shared library\n\n";
629 // fall through
630 case LinkModeStatic:
631 for (auto &Lib : MissingLibs)
632 errs() << "llvm-config: error: missing: " << Lib << "\n";
633 return 1;
634 }
635 } else if (LinkMode == LinkModeAuto) {
636 LinkMode = LinkModeStatic;
637 }
Richard Diamond72303a22015-11-09 23:15:38 +0000638
639 if (PrintSharedMode) {
640 std::unordered_set<std::string> FullDyLibComponents;
Richard Diamonda62513c2015-11-25 22:49:48 +0000641 std::vector<std::string> DyLibComponents =
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000642 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000643
644 for (auto &Component : DyLibComponents) {
645 FullDyLibComponents.insert(Component);
646 }
647 DyLibComponents.clear();
648
649 for (auto &Lib : RequiredLibs) {
650 if (!FullDyLibComponents.count(Lib)) {
651 OS << "static\n";
652 return 0;
653 }
654 }
655 FullDyLibComponents.clear();
656
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000657 if (LinkMode == LinkModeShared) {
Richard Diamond72303a22015-11-09 23:15:38 +0000658 OS << "shared\n";
659 return 0;
660 } else {
661 OS << "static\n";
662 return 0;
663 }
664 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000665
Richard Osborne49ae1172014-03-03 15:06:14 +0000666 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000667
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000668 auto PrintForLib = [&](const StringRef &Lib) {
669 const bool Shared = LinkMode == LinkModeShared;
Richard Osborne49ae1172014-03-03 15:06:14 +0000670 if (PrintLibNames) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000671 OS << GetComponentLibraryFileName(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000672 } else if (PrintLibFiles) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000673 OS << GetComponentLibraryPath(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000674 } else if (PrintLibs) {
675 // If this is a typical library name, include it using -l.
Richard Diamond72303a22015-11-09 23:15:38 +0000676 StringRef LibName;
677 if (Lib.startswith("lib")) {
678 if (GetComponentLibraryNameSlice(Lib, LibName)) {
679 OS << "-l" << LibName;
680 } else {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000681 OS << "-l:" << GetComponentLibraryFileName(Lib, Shared);
Richard Diamond72303a22015-11-09 23:15:38 +0000682 }
683 } else {
684 // Otherwise, print the full path.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000685 OS << GetComponentLibraryPath(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000686 }
Richard Diamond72303a22015-11-09 23:15:38 +0000687 }
688 };
Richard Osborne49ae1172014-03-03 15:06:14 +0000689
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000690 if (LinkMode == LinkModeShared && !BuiltSharedLibs) {
691 PrintForLib(DyLibName);
Richard Diamond72303a22015-11-09 23:15:38 +0000692 } else {
693 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
Richard Diamonda62513c2015-11-25 22:49:48 +0000694 auto Lib = RequiredLibs[i];
Richard Diamond72303a22015-11-09 23:15:38 +0000695 if (i)
696 OS << ' ';
697
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000698 PrintForLib(Lib);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000699 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000700 }
Richard Osborne49ae1172014-03-03 15:06:14 +0000701 OS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000702 }
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000703
704 // Print SYSTEM_LIBS after --libs.
705 // FIXME: Each LLVM component may have its dependent system libs.
706 if (PrintSystemLibs)
707 OS << LLVM_SYSTEM_LIBS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000708 } else if (!Components.empty()) {
709 errs() << "llvm-config: error: components given, but unused\n\n";
710 usage();
711 }
712
713 return 0;
714}