blob: 08b096afb052f4746f7cc4cc3087c1f8bd8e67f7 [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\
Michal Gorny1099e012017-01-06 08:23:33 +0000199 --cmakedir Directory containing LLVM cmake modules.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000200 --cppflags C preprocessor flags for files that include LLVM headers.\n\
201 --cflags C compiler flags for files that include LLVM headers.\n\
202 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
203 --ldflags Print Linker flags.\n\
NAKAMURA Takumi800eb082013-12-25 02:24:32 +0000204 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000205 --libs Libraries needed to link against LLVM components.\n\
206 --libnames Bare library names for in-tree builds.\n\
207 --libfiles Fully qualified library filenames for makefile depends.\n\
208 --components List of all possible components.\n\
209 --targets-built List of all targets currently built.\n\
210 --host-target Target triple used to configure LLVM.\n\
211 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000212 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Filipe Cabecinhasa7e63b12016-03-08 11:49:24 +0000213 --build-system Print the build system used to build LLVM (always cmake).\n\
Tom Stellard18bf6262015-11-04 20:57:43 +0000214 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
Michal Gorny69113242017-01-10 19:55:51 +0000215 --has-global-isel Print whether or not LLVM was built with global-isel support (ON or OFF).\n\
Richard Diamond72303a22015-11-09 23:15:38 +0000216 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000217 --link-shared Link the components as shared libraries.\n\
218 --link-static Link the component libraries statically.\n\
Chris Bieneman7f6611c2016-12-13 22:17:59 +0000219 --ignore-libllvm Ignore libLLVM and link component libraries instead.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000220Typical components:\n\
221 all All LLVM libraries (default).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000222 engine Either a native JIT or a bitcode interpreter.\n";
223 exit(1);
224}
225
226/// \brief Compute the path to the main executable.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000227std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000228 // This just needs to be some symbol in the binary; C++ doesn't
229 // allow taking the address of ::main however.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000230 void *P = (void *)(intptr_t)GetExecutablePath;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000231 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000232}
233
Richard Diamond72303a22015-11-09 23:15:38 +0000234/// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
235/// the full list of components.
Richard Diamonda62513c2015-11-25 22:49:48 +0000236std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000237 const bool GetComponentNames,
238 const std::string &DirSep) {
Richard Diamond72303a22015-11-09 23:15:38 +0000239 std::vector<StringRef> DyLibComponents;
Richard Diamond72303a22015-11-09 23:15:38 +0000240
David Blaikieb7504172015-11-09 23:51:45 +0000241 StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
242 size_t Offset = 0;
243 while (true) {
244 const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
Marcello Maggionia5f1ff12017-01-12 19:47:38 +0000245 DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset-Offset));
David Blaikieb7504172015-11-09 23:51:45 +0000246 if (NextOffset == std::string::npos) {
247 break;
248 }
249 Offset = NextOffset + 1;
Richard Diamond72303a22015-11-09 23:15:38 +0000250 }
251
David Blaikieb7504172015-11-09 23:51:45 +0000252 assert(!DyLibComponents.empty());
Richard Diamond72303a22015-11-09 23:15:38 +0000253
David Blaikieb7504172015-11-09 23:51:45 +0000254 return ComputeLibsForComponents(DyLibComponents,
255 /*IncludeNonInstalled=*/IsInDevelopmentTree,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000256 GetComponentNames, nullptr, nullptr, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000257}
258
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000259int main(int argc, char **argv) {
260 std::vector<StringRef> Components;
261 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
Richard Diamond72303a22015-11-09 23:15:38 +0000262 bool PrintSystemLibs = false, PrintSharedMode = false;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000263 bool HasAnyOption = false;
264
265 // llvm-config is designed to support being run both from a development tree
266 // and from an installed path. We try and auto-detect which case we are in so
267 // that we can report the correct information when run from a development
268 // tree.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000269 bool IsInDevelopmentTree;
Filipe Cabecinhasa7e63b12016-03-08 11:49:24 +0000270 enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000271 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000272 std::string CurrentExecPrefix;
273 std::string ActiveObjRoot;
274
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000275 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
276 char const *build_mode = LLVM_BUILDMODE;
277#if defined(CMAKE_CFG_INTDIR)
278 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
279 build_mode = CMAKE_CFG_INTDIR;
280#endif
281
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000282 // Create an absolute path, and pop up one directory (we expect to be inside a
283 // bin dir).
284 sys::fs::make_absolute(CurrentPath);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000285 CurrentExecPrefix =
286 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000287
288 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000289 // locations (prefix style or CMake style).
Filipe Cabecinhasa7e63b12016-03-08 11:49:24 +0000290 if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000291 IsInDevelopmentTree = true;
292 DevelopmentTreeLayout = CMakeStyle;
293 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000294 } else if (sys::fs::equivalent(CurrentExecPrefix,
295 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000296 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000297 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000298 ActiveObjRoot = LLVM_OBJ_ROOT;
299 } else {
300 IsInDevelopmentTree = false;
Filipe Cabecinhasa7e63b12016-03-08 11:49:24 +0000301 DevelopmentTreeLayout = CMakeStyle; // Initialized to avoid warnings.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000302 }
303
304 // Compute various directory locations based on the derived location
305 // information.
Michal Gorny1099e012017-01-06 08:23:33 +0000306 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir,
307 ActiveCMakeDir;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000308 std::string ActiveIncludeOption;
309 if (IsInDevelopmentTree) {
310 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
311 ActivePrefix = CurrentExecPrefix;
312
313 // CMake organizes the products differently than a normal prefix style
314 // layout.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000315 switch (DevelopmentTreeLayout) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000316 case CMakeStyle:
317 ActiveBinDir = ActiveObjRoot + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000318 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
Michal Gorny1099e012017-01-06 08:23:33 +0000319 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000320 break;
321 case CMakeBuildModeStyle:
NAKAMURA Takumi429a2222013-12-19 16:02:23 +0000322 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000323 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
Chandler Carruth7d587762014-12-29 11:16:25 +0000324 ActiveLibDir =
325 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
Michal Gorny1099e012017-01-06 08:23:33 +0000326 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000327 break;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000328 }
329
330 // We need to include files from both the source and object trees.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000331 ActiveIncludeOption =
332 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000333 } else {
334 ActivePrefix = CurrentExecPrefix;
335 ActiveIncludeDir = ActivePrefix + "/include";
Keno Fischer189a8112017-06-01 20:51:55 +0000336 SmallString<256> path(StringRef(LLVM_TOOLS_INSTALL_DIR));
Keno Fischer532a9e82017-06-01 19:20:33 +0000337 sys::fs::make_absolute(ActivePrefix, path);
338 ActiveBinDir = path.str();
Chandler Carruth7d587762014-12-29 11:16:25 +0000339 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
Michal Gorny1099e012017-01-06 08:23:33 +0000340 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000341 ActiveIncludeOption = "-I" + ActiveIncludeDir;
342 }
343
Richard Diamond72303a22015-11-09 23:15:38 +0000344 /// We only use `shared library` mode in cases where the static library form
345 /// of the components provided are not available; note however that this is
346 /// skipped if we're run from within the build dir. However, once installed,
347 /// we still need to provide correct output when the static archives are
348 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
349 /// in the first place. This can't be done at configure/build time.
350
351 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000352 StaticPrefix, StaticDir = "lib", DirSep = "/";
NAKAMURA Takumi0882a5d2016-02-10 01:12:55 +0000353 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
Richard Diamond72303a22015-11-09 23:15:38 +0000354 if (HostTriple.isOSWindows()) {
355 SharedExt = "dll";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000356 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000357 if (HostTriple.isOSCygMing()) {
358 StaticExt = "a";
NAKAMURA Takumi1621f812016-02-10 03:09:13 +0000359 StaticPrefix = "lib";
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000360 } else {
361 StaticExt = "lib";
362 DirSep = "\\";
363 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
364 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
365 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
366 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
Michal Gorny1099e012017-01-06 08:23:33 +0000367 std::replace(ActiveCMakeDir.begin(), ActiveCMakeDir.end(), '/', '\\');
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000368 std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
369 '\\');
370 }
Richard Diamond72303a22015-11-09 23:15:38 +0000371 SharedDir = ActiveBinDir;
372 StaticDir = ActiveLibDir;
Richard Diamond72303a22015-11-09 23:15:38 +0000373 } else if (HostTriple.isOSDarwin()) {
374 SharedExt = "dylib";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000375 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
Richard Diamond72303a22015-11-09 23:15:38 +0000376 StaticExt = "a";
377 StaticDir = SharedDir = ActiveLibDir;
378 StaticPrefix = SharedPrefix = "lib";
379 } else {
380 // default to the unix values:
381 SharedExt = "so";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000382 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
Richard Diamond72303a22015-11-09 23:15:38 +0000383 StaticExt = "a";
384 StaticDir = SharedDir = ActiveLibDir;
385 StaticPrefix = SharedPrefix = "lib";
386 }
387
Michal Gorny69113242017-01-10 19:55:51 +0000388 const bool BuiltDyLib = !!LLVM_ENABLE_DYLIB;
Richard Diamond72303a22015-11-09 23:15:38 +0000389
Richard Diamond72303a22015-11-09 23:15:38 +0000390 /// CMake style shared libs, ie each component is in a shared library.
Michal Gorny69113242017-01-10 19:55:51 +0000391 const bool BuiltSharedLibs = !!LLVM_ENABLE_SHARED;
Richard Diamond72303a22015-11-09 23:15:38 +0000392
393 bool DyLibExists = false;
394 const std::string DyLibName =
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000395 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
396
397 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
398 // for "--libs", etc, if they exist. This behaviour can be overridden with
399 // --link-static or --link-shared.
Michal Gorny69113242017-01-10 19:55:51 +0000400 bool LinkDyLib = !!LLVM_LINK_DYLIB;
Richard Diamond72303a22015-11-09 23:15:38 +0000401
402 if (BuiltDyLib) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000403 std::string path((SharedDir + DirSep + DyLibName).str());
404 if (DirSep == "\\") {
405 std::replace(path.begin(), path.end(), '/', '\\');
406 }
407 DyLibExists = sys::fs::exists(path);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000408 if (!DyLibExists) {
409 // The shared library does not exist: don't error unless the user
410 // explicitly passes --link-shared.
411 LinkDyLib = false;
412 }
Richard Diamond72303a22015-11-09 23:15:38 +0000413 }
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000414 LinkMode LinkMode =
415 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
Richard Diamond72303a22015-11-09 23:15:38 +0000416
417 /// Get the component's library name without the lib prefix and the
418 /// extension. Returns true if Lib is in a recognized format.
419 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
420 StringRef &Out) {
421 if (Lib.startswith("lib")) {
422 unsigned FromEnd;
423 if (Lib.endswith(StaticExt)) {
424 FromEnd = StaticExt.size() + 1;
425 } else if (Lib.endswith(SharedExt)) {
426 FromEnd = SharedExt.size() + 1;
427 } else {
428 FromEnd = 0;
429 }
430
431 if (FromEnd != 0) {
432 Out = Lib.slice(3, Lib.size() - FromEnd);
433 return true;
434 }
435 }
436
437 return false;
438 };
439 /// Maps Unixizms to the host platform.
440 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000441 const bool Shared) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000442 std::string LibFileName;
443 if (Shared) {
Dan Liew197d2f02016-12-12 23:07:22 +0000444 if (Lib == DyLibName) {
445 // Treat the DyLibName specially. It is not a component library and
446 // already has the necessary prefix and suffix (e.g. `.so`) added so
447 // just return it unmodified.
448 assert(Lib.endswith(SharedExt) && "DyLib is missing suffix");
449 LibFileName = Lib;
450 } else {
451 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
452 }
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000453 } else {
454 // default to static
455 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000456 }
457
458 return LibFileName;
459 };
460 /// Get the full path for a possibly shared component library.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000461 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
462 auto LibFileName = GetComponentLibraryFileName(Name, Shared);
463 if (Shared) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000464 return (SharedDir + DirSep + LibFileName).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000465 } else {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000466 return (StaticDir + DirSep + LibFileName).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000467 }
468 };
469
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000470 raw_ostream &OS = outs();
471 for (int i = 1; i != argc; ++i) {
472 StringRef Arg = argv[i];
473
474 if (Arg.startswith("-")) {
475 HasAnyOption = true;
476 if (Arg == "--version") {
477 OS << PACKAGE_VERSION << '\n';
478 } else if (Arg == "--prefix") {
479 OS << ActivePrefix << '\n';
480 } else if (Arg == "--bindir") {
481 OS << ActiveBinDir << '\n';
482 } else if (Arg == "--includedir") {
483 OS << ActiveIncludeDir << '\n';
484 } else if (Arg == "--libdir") {
485 OS << ActiveLibDir << '\n';
Michal Gorny1099e012017-01-06 08:23:33 +0000486 } else if (Arg == "--cmakedir") {
487 OS << ActiveCMakeDir << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000488 } else if (Arg == "--cppflags") {
489 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
490 } else if (Arg == "--cflags") {
491 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
492 } else if (Arg == "--cxxflags") {
493 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
494 } else if (Arg == "--ldflags") {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000495 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
496 << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000497 } else if (Arg == "--system-libs") {
498 PrintSystemLibs = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000499 } else if (Arg == "--libs") {
500 PrintLibs = true;
501 } else if (Arg == "--libnames") {
502 PrintLibNames = true;
503 } else if (Arg == "--libfiles") {
504 PrintLibFiles = true;
505 } else if (Arg == "--components") {
Richard Diamond72303a22015-11-09 23:15:38 +0000506 /// If there are missing static archives and a dylib was
507 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
508 /// in the manifest.
Richard Diamonda62513c2015-11-25 22:49:48 +0000509 std::vector<std::string> Components;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000510 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarc364d682012-05-15 18:44:17 +0000511 // Only include non-installed components when in a development tree.
512 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
513 continue;
514
Richard Diamond72303a22015-11-09 23:15:38 +0000515 Components.push_back(AvailableComponents[j].Name);
516 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000517 std::string path(
518 GetComponentLibraryPath(AvailableComponents[j].Library, false));
519 if (DirSep == "\\") {
520 std::replace(path.begin(), path.end(), '/', '\\');
521 }
522 if (DyLibExists && !sys::fs::exists(path)) {
523 Components =
524 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000525 std::sort(Components.begin(), Components.end());
526 break;
527 }
528 }
529 }
530
531 for (unsigned I = 0; I < Components.size(); ++I) {
532 if (I) {
533 OS << ' ';
534 }
535
536 OS << Components[I];
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000537 }
538 OS << '\n';
539 } else if (Arg == "--targets-built") {
Daniel Dunbar30a89762011-12-16 00:04:43 +0000540 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000541 } else if (Arg == "--host-target") {
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +0000542 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000543 } else if (Arg == "--build-mode") {
NAKAMURA Takumi1b16e272013-12-03 14:35:17 +0000544 OS << build_mode << '\n';
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000545 } else if (Arg == "--assertion-mode") {
546#if defined(NDEBUG)
547 OS << "OFF\n";
548#else
549 OS << "ON\n";
550#endif
Tom Stellard5268c172015-09-09 16:39:30 +0000551 } else if (Arg == "--build-system") {
552 OS << LLVM_BUILD_SYSTEM << '\n';
Tom Stellard18bf6262015-11-04 20:57:43 +0000553 } else if (Arg == "--has-rtti") {
Michal Gorny69113242017-01-10 19:55:51 +0000554 OS << (LLVM_HAS_RTTI ? "YES" : "NO") << '\n';
Quentin Colombet447f8522016-03-08 00:02:50 +0000555 } else if (Arg == "--has-global-isel") {
Michal Gorny69113242017-01-10 19:55:51 +0000556 OS << (LLVM_HAS_GLOBAL_ISEL ? "ON" : "OFF") << '\n';
Richard Diamond72303a22015-11-09 23:15:38 +0000557 } else if (Arg == "--shared-mode") {
558 PrintSharedMode = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000559 } else if (Arg == "--obj-root") {
NAKAMURA Takumi93a14622013-12-19 16:02:28 +0000560 OS << ActivePrefix << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000561 } else if (Arg == "--src-root") {
562 OS << LLVM_SRC_ROOT << '\n';
Derek Schuff7ff587a2016-12-13 23:01:53 +0000563 } else if (Arg == "--ignore-libllvm") {
564 LinkDyLib = false;
565 LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto;
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000566 } else if (Arg == "--link-shared") {
567 LinkMode = LinkModeShared;
568 } else if (Arg == "--link-static") {
569 LinkMode = LinkModeStatic;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000570 } else {
571 usage();
572 }
573 } else {
574 Components.push_back(Arg);
575 }
576 }
577
578 if (!HasAnyOption)
579 usage();
580
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000581 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
582 errs() << "llvm-config: error: " << DyLibName << " is missing\n";
583 return 1;
584 }
585
Richard Diamond72303a22015-11-09 23:15:38 +0000586 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
587 PrintSharedMode) {
588
589 if (PrintSharedMode && BuiltSharedLibs) {
590 OS << "shared\n";
591 return 0;
592 }
593
Daniel Dunbarfbc6a892011-12-12 18:22:04 +0000594 // If no components were specified, default to "all".
595 if (Components.empty())
596 Components.push_back("all");
597
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000598 // Construct the list of all the required libraries.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000599 std::function<std::string(const StringRef &)>
600 GetComponentLibraryPathFunction = [&](const StringRef &Name) {
601 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
602 };
603 std::vector<std::string> MissingLibs;
604 std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
605 Components,
606 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000607 &GetComponentLibraryPathFunction, &MissingLibs, DirSep);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000608 if (!MissingLibs.empty()) {
609 switch (LinkMode) {
610 case LinkModeShared:
Chris Bienemanda1c84c2016-12-13 23:08:52 +0000611 if (LinkDyLib && !BuiltSharedLibs)
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000612 break;
613 // Using component shared libraries.
614 for (auto &Lib : MissingLibs)
615 errs() << "llvm-config: error: missing: " << Lib << "\n";
616 return 1;
617 case LinkModeAuto:
618 if (DyLibExists) {
619 LinkMode = LinkModeShared;
620 break;
621 }
622 errs()
623 << "llvm-config: error: component libraries and shared library\n\n";
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000624 LLVM_FALLTHROUGH;
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000625 case LinkModeStatic:
626 for (auto &Lib : MissingLibs)
627 errs() << "llvm-config: error: missing: " << Lib << "\n";
628 return 1;
629 }
630 } else if (LinkMode == LinkModeAuto) {
631 LinkMode = LinkModeStatic;
632 }
Richard Diamond72303a22015-11-09 23:15:38 +0000633
634 if (PrintSharedMode) {
635 std::unordered_set<std::string> FullDyLibComponents;
Richard Diamonda62513c2015-11-25 22:49:48 +0000636 std::vector<std::string> DyLibComponents =
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000637 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000638
639 for (auto &Component : DyLibComponents) {
640 FullDyLibComponents.insert(Component);
641 }
642 DyLibComponents.clear();
643
644 for (auto &Lib : RequiredLibs) {
645 if (!FullDyLibComponents.count(Lib)) {
646 OS << "static\n";
647 return 0;
648 }
649 }
650 FullDyLibComponents.clear();
651
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000652 if (LinkMode == LinkModeShared) {
Richard Diamond72303a22015-11-09 23:15:38 +0000653 OS << "shared\n";
654 return 0;
655 } else {
656 OS << "static\n";
657 return 0;
658 }
659 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000660
Richard Osborne49ae1172014-03-03 15:06:14 +0000661 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000662
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000663 auto PrintForLib = [&](const StringRef &Lib) {
664 const bool Shared = LinkMode == LinkModeShared;
Richard Osborne49ae1172014-03-03 15:06:14 +0000665 if (PrintLibNames) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000666 OS << GetComponentLibraryFileName(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000667 } else if (PrintLibFiles) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000668 OS << GetComponentLibraryPath(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000669 } else if (PrintLibs) {
Reid Klecknerecb40902016-03-14 21:39:58 +0000670 // On Windows, output full path to library without parameters.
671 // Elsewhere, if this is a typical library name, include it using -l.
672 if (HostTriple.isWindowsMSVCEnvironment()) {
673 OS << GetComponentLibraryPath(Lib, Shared);
674 } else {
675 StringRef LibName;
Richard Diamond72303a22015-11-09 23:15:38 +0000676 if (GetComponentLibraryNameSlice(Lib, LibName)) {
Reid Klecknerecb40902016-03-14 21:39:58 +0000677 // Extract library name (remove prefix and suffix).
Richard Diamond72303a22015-11-09 23:15:38 +0000678 OS << "-l" << LibName;
679 } else {
Reid Klecknerecb40902016-03-14 21:39:58 +0000680 // Lib is already a library name without prefix and suffix.
681 OS << "-l" << Lib;
Richard Diamond72303a22015-11-09 23:15:38 +0000682 }
Richard Osborne49ae1172014-03-03 15:06:14 +0000683 }
Richard Diamond72303a22015-11-09 23:15:38 +0000684 }
685 };
Richard Osborne49ae1172014-03-03 15:06:14 +0000686
Chris Bienemanda1c84c2016-12-13 23:08:52 +0000687 if (LinkMode == LinkModeShared && LinkDyLib) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000688 PrintForLib(DyLibName);
Richard Diamond72303a22015-11-09 23:15:38 +0000689 } else {
690 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
Richard Diamonda62513c2015-11-25 22:49:48 +0000691 auto Lib = RequiredLibs[i];
Richard Diamond72303a22015-11-09 23:15:38 +0000692 if (i)
693 OS << ' ';
694
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000695 PrintForLib(Lib);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000696 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000697 }
Richard Osborne49ae1172014-03-03 15:06:14 +0000698 OS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000699 }
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000700
701 // Print SYSTEM_LIBS after --libs.
702 // FIXME: Each LLVM component may have its dependent system libs.
Michal Gornyd1b95482017-01-06 21:33:54 +0000703 if (PrintSystemLibs) {
704 // Output system libraries only if linking against a static
705 // library (since the shared library links to all system libs
706 // already)
707 OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n';
708 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000709 } else if (!Components.empty()) {
710 errs() << "llvm-config: error: components given, but unused\n\n";
711 usage();
712 }
713
714 return 0;
715}