blob: a867e1b60d825901e188060085380b2be28169cc [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);
86 assert(AC && "Invalid component name!");
87
88 // Add to the visited table.
89 if (!VisitedComponents.insert(AC).second) {
90 // We are done if the component has already been visited.
91 return;
92 }
93
Daniel Dunbarc364d682012-05-15 18:44:17 +000094 // Only include non-installed components if requested.
95 if (!AC->IsInstalled && !IncludeNonInstalled)
96 return;
97
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000098 // Otherwise, visit all the dependencies.
99 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
100 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
Richard Diamond72303a22015-11-09 23:15:38 +0000101 RequiredLibs, IncludeNonInstalled, GetComponentNames,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000102 GetComponentLibraryPath, Missing, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000103 }
104
105 if (GetComponentNames) {
106 RequiredLibs.push_back(Name);
107 return;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000108 }
109
110 // Add to the required library list.
Richard Diamond72303a22015-11-09 23:15:38 +0000111 if (AC->Library) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000112 if (Missing && GetComponentLibraryPath) {
113 std::string path = (*GetComponentLibraryPath)(AC->Library);
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000114 if (DirSep == "\\") {
115 std::replace(path.begin(), path.end(), '/', '\\');
116 }
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000117 if (!sys::fs::exists(path))
118 Missing->push_back(path);
Richard Diamond72303a22015-11-09 23:15:38 +0000119 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000120 RequiredLibs.push_back(AC->Library);
Richard Diamond72303a22015-11-09 23:15:38 +0000121 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000122}
123
124/// \brief Compute the list of required libraries for a given list of
125/// components, in an order suitable for passing to a linker (that is, libraries
126/// appear prior to their dependencies).
127///
128/// \param Components - The names of the components to find libraries for.
Daniel Dunbarc364d682012-05-15 18:44:17 +0000129/// \param IncludeNonInstalled - Whether non-installed components should be
130/// reported.
Richard Diamond72303a22015-11-09 23:15:38 +0000131/// \param GetComponentNames - True if one would prefer the component names.
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000132static std::vector<std::string> ComputeLibsForComponents(
133 const std::vector<StringRef> &Components, bool IncludeNonInstalled,
134 bool GetComponentNames, const std::function<std::string(const StringRef &)>
135 *GetComponentLibraryPath,
136 std::vector<std::string> *Missing, const std::string &DirSep) {
Richard Diamonda62513c2015-11-25 22:49:48 +0000137 std::vector<std::string> RequiredLibs;
David Blaikieb7504172015-11-09 23:51:45 +0000138 std::set<AvailableComponent *> VisitedComponents;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000139
140 // Build a map of component names to information.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000141 StringMap<AvailableComponent *> ComponentMap;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000142 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
143 AvailableComponent *AC = &AvailableComponents[i];
144 ComponentMap[AC->Name] = AC;
145 }
146
147 // Visit the components.
148 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
149 // Users are allowed to provide mixed case component names.
150 std::string ComponentLower = Components[i].lower();
151
152 // Validate that the user supplied a valid component name.
153 if (!ComponentMap.count(ComponentLower)) {
154 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
155 << "\n";
156 exit(1);
157 }
158
159 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
Richard Diamond72303a22015-11-09 23:15:38 +0000160 RequiredLibs, IncludeNonInstalled, GetComponentNames,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000161 GetComponentLibraryPath, Missing, DirSep);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000162 }
163
164 // The list is now ordered with leafs first, we want the libraries to printed
165 // in the reverse order of dependency.
166 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
David Blaikieb7504172015-11-09 23:51:45 +0000167
168 return RequiredLibs;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000169}
170
171/* *** */
172
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000173static void usage() {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000174 errs() << "\
175usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
176\n\
177Get various configuration information needed to compile programs which use\n\
178LLVM. Typically called from 'configure' scripts. Examples:\n\
179 llvm-config --cxxflags\n\
180 llvm-config --ldflags\n\
181 llvm-config --libs engine bcreader scalaropts\n\
182\n\
183Options:\n\
184 --version Print LLVM version.\n\
185 --prefix Print the installation prefix.\n\
186 --src-root Print the source root LLVM was built from.\n\
187 --obj-root Print the object root used to build LLVM.\n\
188 --bindir Directory containing LLVM executables.\n\
189 --includedir Directory containing LLVM headers.\n\
190 --libdir Directory containing LLVM libraries.\n\
191 --cppflags C preprocessor flags for files that include LLVM headers.\n\
192 --cflags C compiler flags for files that include LLVM headers.\n\
193 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
194 --ldflags Print Linker flags.\n\
NAKAMURA Takumi800eb082013-12-25 02:24:32 +0000195 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000196 --libs Libraries needed to link against LLVM components.\n\
197 --libnames Bare library names for in-tree builds.\n\
198 --libfiles Fully qualified library filenames for makefile depends.\n\
199 --components List of all possible components.\n\
200 --targets-built List of all targets currently built.\n\
201 --host-target Target triple used to configure LLVM.\n\
202 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000203 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Tom Stellard5268c172015-09-09 16:39:30 +0000204 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
Tom Stellard18bf6262015-11-04 20:57:43 +0000205 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
Richard Diamond72303a22015-11-09 23:15:38 +0000206 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000207 --link-shared Link the components as shared libraries.\n\
208 --link-static Link the component libraries statically.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000209Typical components:\n\
210 all All LLVM libraries (default).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000211 engine Either a native JIT or a bitcode interpreter.\n";
212 exit(1);
213}
214
215/// \brief Compute the path to the main executable.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000216std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000217 // This just needs to be some symbol in the binary; C++ doesn't
218 // allow taking the address of ::main however.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000219 void *P = (void *)(intptr_t)GetExecutablePath;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000220 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000221}
222
Richard Diamond72303a22015-11-09 23:15:38 +0000223/// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
224/// the full list of components.
Richard Diamonda62513c2015-11-25 22:49:48 +0000225std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000226 const bool GetComponentNames,
227 const std::string &DirSep) {
Richard Diamond72303a22015-11-09 23:15:38 +0000228 std::vector<StringRef> DyLibComponents;
Richard Diamond72303a22015-11-09 23:15:38 +0000229
David Blaikieb7504172015-11-09 23:51:45 +0000230 StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
231 size_t Offset = 0;
232 while (true) {
233 const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
234 DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset));
235 if (NextOffset == std::string::npos) {
236 break;
237 }
238 Offset = NextOffset + 1;
Richard Diamond72303a22015-11-09 23:15:38 +0000239 }
240
David Blaikieb7504172015-11-09 23:51:45 +0000241 assert(!DyLibComponents.empty());
Richard Diamond72303a22015-11-09 23:15:38 +0000242
David Blaikieb7504172015-11-09 23:51:45 +0000243 return ComputeLibsForComponents(DyLibComponents,
244 /*IncludeNonInstalled=*/IsInDevelopmentTree,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000245 GetComponentNames, nullptr, nullptr, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000246}
247
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000248int main(int argc, char **argv) {
249 std::vector<StringRef> Components;
250 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
Richard Diamond72303a22015-11-09 23:15:38 +0000251 bool PrintSystemLibs = false, PrintSharedMode = false;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000252 bool HasAnyOption = false;
253
254 // llvm-config is designed to support being run both from a development tree
255 // and from an installed path. We try and auto-detect which case we are in so
256 // that we can report the correct information when run from a development
257 // tree.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000258 bool IsInDevelopmentTree;
259 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000260 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000261 std::string CurrentExecPrefix;
262 std::string ActiveObjRoot;
263
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000264 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
265 char const *build_mode = LLVM_BUILDMODE;
266#if defined(CMAKE_CFG_INTDIR)
267 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
268 build_mode = CMAKE_CFG_INTDIR;
269#endif
270
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000271 // Create an absolute path, and pop up one directory (we expect to be inside a
272 // bin dir).
273 sys::fs::make_absolute(CurrentPath);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000274 CurrentExecPrefix =
275 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000276
277 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000278 // locations (prefix style or CMake style).
279 if (sys::fs::equivalent(CurrentExecPrefix,
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000280 Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000281 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000282 DevelopmentTreeLayout = MakefileStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000283
284 // If we are in a development tree, then check if we are in a BuildTools
285 // directory. This indicates we are built for the build triple, but we
286 // always want to provide information for the host triple.
287 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
288 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
289 } else {
290 ActiveObjRoot = LLVM_OBJ_ROOT;
291 }
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000292 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000293 IsInDevelopmentTree = true;
294 DevelopmentTreeLayout = CMakeStyle;
295 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000296 } else if (sys::fs::equivalent(CurrentExecPrefix,
297 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000298 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000299 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000300 ActiveObjRoot = LLVM_OBJ_ROOT;
301 } else {
302 IsInDevelopmentTree = false;
Duncan Sandsf320be82012-02-23 08:25:25 +0000303 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000304 }
305
306 // Compute various directory locations based on the derived location
307 // information.
308 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
309 std::string ActiveIncludeOption;
310 if (IsInDevelopmentTree) {
311 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
312 ActivePrefix = CurrentExecPrefix;
313
314 // CMake organizes the products differently than a normal prefix style
315 // layout.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000316 switch (DevelopmentTreeLayout) {
317 case MakefileStyle:
NAKAMURA Takumi46c19032013-12-20 17:35:52 +0000318 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000319 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000320 ActiveLibDir =
321 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000322 break;
323 case CMakeStyle:
324 ActiveBinDir = ActiveObjRoot + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000325 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000326 break;
327 case CMakeBuildModeStyle:
NAKAMURA Takumi429a2222013-12-19 16:02:23 +0000328 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000329 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
Chandler Carruth7d587762014-12-29 11:16:25 +0000330 ActiveLibDir =
331 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000332 break;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000333 }
334
335 // We need to include files from both the source and object trees.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000336 ActiveIncludeOption =
337 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000338 } else {
339 ActivePrefix = CurrentExecPrefix;
340 ActiveIncludeDir = ActivePrefix + "/include";
341 ActiveBinDir = ActivePrefix + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000342 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000343 ActiveIncludeOption = "-I" + ActiveIncludeDir;
344 }
345
Richard Diamond72303a22015-11-09 23:15:38 +0000346 /// We only use `shared library` mode in cases where the static library form
347 /// of the components provided are not available; note however that this is
348 /// skipped if we're run from within the build dir. However, once installed,
349 /// we still need to provide correct output when the static archives are
350 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
351 /// in the first place. This can't be done at configure/build time.
352
353 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000354 StaticPrefix, StaticDir = "lib", DirSep = "/";
NAKAMURA Takumi0882a5d2016-02-10 01:12:55 +0000355 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
Richard Diamond72303a22015-11-09 23:15:38 +0000356 if (HostTriple.isOSWindows()) {
357 SharedExt = "dll";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000358 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000359 if (HostTriple.isOSCygMing()) {
360 StaticExt = "a";
NAKAMURA Takumi1621f812016-02-10 03:09:13 +0000361 StaticPrefix = "lib";
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000362 } else {
363 StaticExt = "lib";
364 DirSep = "\\";
365 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
366 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
367 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
368 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
369 std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
370 '\\');
371 }
Richard Diamond72303a22015-11-09 23:15:38 +0000372 SharedDir = ActiveBinDir;
373 StaticDir = ActiveLibDir;
Richard Diamond72303a22015-11-09 23:15:38 +0000374 } else if (HostTriple.isOSDarwin()) {
375 SharedExt = "dylib";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000376 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
Richard Diamond72303a22015-11-09 23:15:38 +0000377 StaticExt = "a";
378 StaticDir = SharedDir = ActiveLibDir;
379 StaticPrefix = SharedPrefix = "lib";
380 } else {
381 // default to the unix values:
382 SharedExt = "so";
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000383 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
Richard Diamond72303a22015-11-09 23:15:38 +0000384 StaticExt = "a";
385 StaticDir = SharedDir = ActiveLibDir;
386 StaticPrefix = SharedPrefix = "lib";
387 }
388
389 const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0);
390
391 enum { CMake, AutoConf } ConfigTool;
392 if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) {
393 ConfigTool = CMake;
394 } else {
395 ConfigTool = AutoConf;
396 }
397
398 /// CMake style shared libs, ie each component is in a shared library.
399 const bool BuiltSharedLibs =
400 (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0);
401
402 bool DyLibExists = false;
403 const std::string DyLibName =
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000404 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
405
406 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
407 // for "--libs", etc, if they exist. This behaviour can be overridden with
408 // --link-static or --link-shared.
409 bool LinkDyLib = (std::strcmp(LLVM_LINK_DYLIB, "ON") == 0);
Richard Diamond72303a22015-11-09 23:15:38 +0000410
411 if (BuiltDyLib) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000412 std::string path((SharedDir + DirSep + DyLibName).str());
413 if (DirSep == "\\") {
414 std::replace(path.begin(), path.end(), '/', '\\');
415 }
416 DyLibExists = sys::fs::exists(path);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000417 if (!DyLibExists) {
418 // The shared library does not exist: don't error unless the user
419 // explicitly passes --link-shared.
420 LinkDyLib = false;
421 }
Richard Diamond72303a22015-11-09 23:15:38 +0000422 }
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000423 LinkMode LinkMode =
424 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
Richard Diamond72303a22015-11-09 23:15:38 +0000425
426 /// Get the component's library name without the lib prefix and the
427 /// extension. Returns true if Lib is in a recognized format.
428 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
429 StringRef &Out) {
430 if (Lib.startswith("lib")) {
431 unsigned FromEnd;
432 if (Lib.endswith(StaticExt)) {
433 FromEnd = StaticExt.size() + 1;
434 } else if (Lib.endswith(SharedExt)) {
435 FromEnd = SharedExt.size() + 1;
436 } else {
437 FromEnd = 0;
438 }
439
440 if (FromEnd != 0) {
441 Out = Lib.slice(3, Lib.size() - FromEnd);
442 return true;
443 }
444 }
445
446 return false;
447 };
448 /// Maps Unixizms to the host platform.
449 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000450 const bool Shared) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000451 std::string LibFileName;
452 if (Shared) {
453 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
454 } else {
455 // default to static
456 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000457 }
458
459 return LibFileName;
460 };
461 /// Get the full path for a possibly shared component library.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000462 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
463 auto LibFileName = GetComponentLibraryFileName(Name, Shared);
464 if (Shared) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000465 return (SharedDir + DirSep + LibFileName).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000466 } else {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000467 return (StaticDir + DirSep + LibFileName).str();
Richard Diamond72303a22015-11-09 23:15:38 +0000468 }
469 };
470
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000471 raw_ostream &OS = outs();
472 for (int i = 1; i != argc; ++i) {
473 StringRef Arg = argv[i];
474
475 if (Arg.startswith("-")) {
476 HasAnyOption = true;
477 if (Arg == "--version") {
478 OS << PACKAGE_VERSION << '\n';
479 } else if (Arg == "--prefix") {
480 OS << ActivePrefix << '\n';
481 } else if (Arg == "--bindir") {
482 OS << ActiveBinDir << '\n';
483 } else if (Arg == "--includedir") {
484 OS << ActiveIncludeDir << '\n';
485 } else if (Arg == "--libdir") {
486 OS << ActiveLibDir << '\n';
487 } else if (Arg == "--cppflags") {
488 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
489 } else if (Arg == "--cflags") {
490 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
491 } else if (Arg == "--cxxflags") {
492 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
493 } else if (Arg == "--ldflags") {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000494 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
495 << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000496 } else if (Arg == "--system-libs") {
497 PrintSystemLibs = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000498 } else if (Arg == "--libs") {
499 PrintLibs = true;
500 } else if (Arg == "--libnames") {
501 PrintLibNames = true;
502 } else if (Arg == "--libfiles") {
503 PrintLibFiles = true;
504 } else if (Arg == "--components") {
Richard Diamond72303a22015-11-09 23:15:38 +0000505 /// If there are missing static archives and a dylib was
506 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
507 /// in the manifest.
Richard Diamonda62513c2015-11-25 22:49:48 +0000508 std::vector<std::string> Components;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000509 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarc364d682012-05-15 18:44:17 +0000510 // Only include non-installed components when in a development tree.
511 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
512 continue;
513
Richard Diamond72303a22015-11-09 23:15:38 +0000514 Components.push_back(AvailableComponents[j].Name);
515 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000516 std::string path(
517 GetComponentLibraryPath(AvailableComponents[j].Library, false));
518 if (DirSep == "\\") {
519 std::replace(path.begin(), path.end(), '/', '\\');
520 }
521 if (DyLibExists && !sys::fs::exists(path)) {
522 Components =
523 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000524 std::sort(Components.begin(), Components.end());
525 break;
526 }
527 }
528 }
529
530 for (unsigned I = 0; I < Components.size(); ++I) {
531 if (I) {
532 OS << ' ';
533 }
534
535 OS << Components[I];
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000536 }
537 OS << '\n';
538 } else if (Arg == "--targets-built") {
Daniel Dunbar30a89762011-12-16 00:04:43 +0000539 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000540 } else if (Arg == "--host-target") {
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +0000541 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000542 } else if (Arg == "--build-mode") {
NAKAMURA Takumi1b16e272013-12-03 14:35:17 +0000543 OS << build_mode << '\n';
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000544 } else if (Arg == "--assertion-mode") {
545#if defined(NDEBUG)
546 OS << "OFF\n";
547#else
548 OS << "ON\n";
549#endif
Tom Stellard5268c172015-09-09 16:39:30 +0000550 } else if (Arg == "--build-system") {
551 OS << LLVM_BUILD_SYSTEM << '\n';
Tom Stellard18bf6262015-11-04 20:57:43 +0000552 } else if (Arg == "--has-rtti") {
553 OS << LLVM_HAS_RTTI << '\n';
Richard Diamond72303a22015-11-09 23:15:38 +0000554 } else if (Arg == "--shared-mode") {
555 PrintSharedMode = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000556 } else if (Arg == "--obj-root") {
NAKAMURA Takumi93a14622013-12-19 16:02:28 +0000557 OS << ActivePrefix << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000558 } else if (Arg == "--src-root") {
559 OS << LLVM_SRC_ROOT << '\n';
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000560 } else if (Arg == "--link-shared") {
561 LinkMode = LinkModeShared;
562 } else if (Arg == "--link-static") {
563 LinkMode = LinkModeStatic;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000564 } else {
565 usage();
566 }
567 } else {
568 Components.push_back(Arg);
569 }
570 }
571
572 if (!HasAnyOption)
573 usage();
574
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000575 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
576 errs() << "llvm-config: error: " << DyLibName << " is missing\n";
577 return 1;
578 }
579
Richard Diamond72303a22015-11-09 23:15:38 +0000580 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
581 PrintSharedMode) {
582
583 if (PrintSharedMode && BuiltSharedLibs) {
584 OS << "shared\n";
585 return 0;
586 }
587
Daniel Dunbarfbc6a892011-12-12 18:22:04 +0000588 // If no components were specified, default to "all".
589 if (Components.empty())
590 Components.push_back("all");
591
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000592 // Construct the list of all the required libraries.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000593 std::function<std::string(const StringRef &)>
594 GetComponentLibraryPathFunction = [&](const StringRef &Name) {
595 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
596 };
597 std::vector<std::string> MissingLibs;
598 std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
599 Components,
600 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000601 &GetComponentLibraryPathFunction, &MissingLibs, DirSep);
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000602 if (!MissingLibs.empty()) {
603 switch (LinkMode) {
604 case LinkModeShared:
605 if (DyLibExists && !BuiltSharedLibs)
606 break;
607 // Using component shared libraries.
608 for (auto &Lib : MissingLibs)
609 errs() << "llvm-config: error: missing: " << Lib << "\n";
610 return 1;
611 case LinkModeAuto:
612 if (DyLibExists) {
613 LinkMode = LinkModeShared;
614 break;
615 }
616 errs()
617 << "llvm-config: error: component libraries and shared library\n\n";
618 // fall through
619 case LinkModeStatic:
620 for (auto &Lib : MissingLibs)
621 errs() << "llvm-config: error: missing: " << Lib << "\n";
622 return 1;
623 }
624 } else if (LinkMode == LinkModeAuto) {
625 LinkMode = LinkModeStatic;
626 }
Richard Diamond72303a22015-11-09 23:15:38 +0000627
628 if (PrintSharedMode) {
629 std::unordered_set<std::string> FullDyLibComponents;
Richard Diamonda62513c2015-11-25 22:49:48 +0000630 std::vector<std::string> DyLibComponents =
Ehsan Akhgari155ca8f2016-02-09 19:41:14 +0000631 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
Richard Diamond72303a22015-11-09 23:15:38 +0000632
633 for (auto &Component : DyLibComponents) {
634 FullDyLibComponents.insert(Component);
635 }
636 DyLibComponents.clear();
637
638 for (auto &Lib : RequiredLibs) {
639 if (!FullDyLibComponents.count(Lib)) {
640 OS << "static\n";
641 return 0;
642 }
643 }
644 FullDyLibComponents.clear();
645
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000646 if (LinkMode == LinkModeShared) {
Richard Diamond72303a22015-11-09 23:15:38 +0000647 OS << "shared\n";
648 return 0;
649 } else {
650 OS << "static\n";
651 return 0;
652 }
653 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000654
Richard Osborne49ae1172014-03-03 15:06:14 +0000655 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000656
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000657 auto PrintForLib = [&](const StringRef &Lib) {
658 const bool Shared = LinkMode == LinkModeShared;
Richard Osborne49ae1172014-03-03 15:06:14 +0000659 if (PrintLibNames) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000660 OS << GetComponentLibraryFileName(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000661 } else if (PrintLibFiles) {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000662 OS << GetComponentLibraryPath(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000663 } else if (PrintLibs) {
664 // If this is a typical library name, include it using -l.
Richard Diamond72303a22015-11-09 23:15:38 +0000665 StringRef LibName;
666 if (Lib.startswith("lib")) {
667 if (GetComponentLibraryNameSlice(Lib, LibName)) {
668 OS << "-l" << LibName;
669 } else {
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000670 OS << "-l:" << GetComponentLibraryFileName(Lib, Shared);
Richard Diamond72303a22015-11-09 23:15:38 +0000671 }
672 } else {
673 // Otherwise, print the full path.
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000674 OS << GetComponentLibraryPath(Lib, Shared);
Richard Osborne49ae1172014-03-03 15:06:14 +0000675 }
Richard Diamond72303a22015-11-09 23:15:38 +0000676 }
677 };
Richard Osborne49ae1172014-03-03 15:06:14 +0000678
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000679 if (LinkMode == LinkModeShared && !BuiltSharedLibs) {
680 PrintForLib(DyLibName);
Richard Diamond72303a22015-11-09 23:15:38 +0000681 } else {
682 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
Richard Diamonda62513c2015-11-25 22:49:48 +0000683 auto Lib = RequiredLibs[i];
Richard Diamond72303a22015-11-09 23:15:38 +0000684 if (i)
685 OS << ' ';
686
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000687 PrintForLib(Lib);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000688 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000689 }
Richard Osborne49ae1172014-03-03 15:06:14 +0000690 OS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000691 }
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000692
693 // Print SYSTEM_LIBS after --libs.
694 // FIXME: Each LLVM component may have its dependent system libs.
695 if (PrintSystemLibs)
696 OS << LLVM_SYSTEM_LIBS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000697 } else if (!Components.empty()) {
698 errs() << "llvm-config: error: components given, but unused\n\n";
699 usage();
700 }
701
702 return 0;
703}