blob: bec89fef98caa09bf72494dc2609203cf6c5df25 [file] [log] [blame]
Daniel Dunbarcb497b82011-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
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +000020#include "llvm/Config/llvm-config.h"
Daniel Dunbarcb497b82011-12-01 20:18:09 +000021#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/StringRef.h"
Saleem Abdulrasoolc06afdc2014-03-29 01:08:53 +000024#include "llvm/ADT/Triple.h"
Daniel Dunbarcb497b82011-12-01 20:18:09 +000025#include "llvm/ADT/Twine.h"
26#include "llvm/Config/config.h"
Daniel Dunbarcb497b82011-12-01 20:18:09 +000027#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/Path.h"
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +000029#include "llvm/Support/WithColor.h"
Daniel Dunbarcb497b82011-12-01 20:18:09 +000030#include "llvm/Support/raw_ostream.h"
31#include <cstdlib>
32#include <set>
Andrew Wilkinsa04a8182016-01-12 07:23:58 +000033#include <unordered_set>
Andrew Wilkins93083d42016-01-20 04:03:09 +000034#include <vector>
Daniel Dunbarcb497b82011-12-01 20:18:09 +000035
36using namespace llvm;
37
38// Include the build time variables we can report to the user. This is generated
39// at build time from the BuildVariables.inc.in file by the build system.
40#include "BuildVariables.inc"
41
42// Include the component table. This creates an array of struct
43// AvailableComponent entries, which record the component name, library name,
44// and required components for all of the available libraries.
45//
46// Not all components define a library, we also use "library groups" as a way to
47// create entries for pseudo groups like x86 or all-targets.
48#include "LibraryDependencies.inc"
49
Andrew Wilkins93083d42016-01-20 04:03:09 +000050// LinkMode determines what libraries and flags are returned by llvm-config.
51enum LinkMode {
52 // LinkModeAuto will link with the default link mode for the installation,
53 // which is dependent on the value of LLVM_LINK_LLVM_DYLIB, and fall back
54 // to the alternative if the required libraries are not available.
55 LinkModeAuto = 0,
56
57 // LinkModeShared will link with the dynamic component libraries if they
58 // exist, and return an error otherwise.
59 LinkModeShared = 1,
60
61 // LinkModeStatic will link with the static component libraries if they
62 // exist, and return an error otherwise.
63 LinkModeStatic = 2,
64};
65
Adrian Prantl26b584c2018-05-01 15:54:18 +000066/// Traverse a single component adding to the topological ordering in
Daniel Dunbarcb497b82011-12-01 20:18:09 +000067/// \arg RequiredLibs.
68///
69/// \param Name - The component to traverse.
70/// \param ComponentMap - A prebuilt map of component names to descriptors.
71/// \param VisitedComponents [in] [out] - The set of already visited components.
Richard Diamond6c6be142015-11-09 23:15:38 +000072/// \param RequiredLibs [out] - The ordered list of required
73/// libraries.
74/// \param GetComponentNames - Get the component names instead of the
75/// library name.
Andrew Wilkins93083d42016-01-20 04:03:09 +000076static void VisitComponent(const std::string &Name,
77 const StringMap<AvailableComponent *> &ComponentMap,
78 std::set<AvailableComponent *> &VisitedComponents,
Richard Diamond5efecd12015-11-25 22:49:48 +000079 std::vector<std::string> &RequiredLibs,
Richard Diamond6c6be142015-11-09 23:15:38 +000080 bool IncludeNonInstalled, bool GetComponentNames,
Andrew Wilkins93083d42016-01-20 04:03:09 +000081 const std::function<std::string(const StringRef &)>
82 *GetComponentLibraryPath,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +000083 std::vector<std::string> *Missing,
84 const std::string &DirSep) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +000085 // Lookup the component.
86 AvailableComponent *AC = ComponentMap.lookup(Name);
Mehdi Amini1b8de272016-02-12 18:43:10 +000087 if (!AC) {
88 errs() << "Can't find component: '" << Name << "' in the map. Available components are: ";
89 for (const auto &Component : ComponentMap) {
90 errs() << "'" << Component.first() << "' ";
91 }
92 errs() << "\n";
93 report_fatal_error("abort");
94 }
Daniel Dunbarcb497b82011-12-01 20:18:09 +000095 assert(AC && "Invalid component name!");
96
97 // Add to the visited table.
98 if (!VisitedComponents.insert(AC).second) {
99 // We are done if the component has already been visited.
100 return;
101 }
102
Daniel Dunbarb5cd41e2012-05-15 18:44:17 +0000103 // Only include non-installed components if requested.
104 if (!AC->IsInstalled && !IncludeNonInstalled)
105 return;
106
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000107 // Otherwise, visit all the dependencies.
108 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
109 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
Richard Diamond6c6be142015-11-09 23:15:38 +0000110 RequiredLibs, IncludeNonInstalled, GetComponentNames,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000111 GetComponentLibraryPath, Missing, DirSep);
Richard Diamond6c6be142015-11-09 23:15:38 +0000112 }
113
114 if (GetComponentNames) {
115 RequiredLibs.push_back(Name);
116 return;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000117 }
118
119 // Add to the required library list.
Richard Diamond6c6be142015-11-09 23:15:38 +0000120 if (AC->Library) {
Andrew Wilkins93083d42016-01-20 04:03:09 +0000121 if (Missing && GetComponentLibraryPath) {
122 std::string path = (*GetComponentLibraryPath)(AC->Library);
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000123 if (DirSep == "\\") {
124 std::replace(path.begin(), path.end(), '/', '\\');
125 }
Andrew Wilkins93083d42016-01-20 04:03:09 +0000126 if (!sys::fs::exists(path))
127 Missing->push_back(path);
Richard Diamond6c6be142015-11-09 23:15:38 +0000128 }
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000129 RequiredLibs.push_back(AC->Library);
Richard Diamond6c6be142015-11-09 23:15:38 +0000130 }
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000131}
132
Adrian Prantl26b584c2018-05-01 15:54:18 +0000133/// Compute the list of required libraries for a given list of
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000134/// components, in an order suitable for passing to a linker (that is, libraries
135/// appear prior to their dependencies).
136///
137/// \param Components - The names of the components to find libraries for.
Daniel Dunbarb5cd41e2012-05-15 18:44:17 +0000138/// \param IncludeNonInstalled - Whether non-installed components should be
139/// reported.
Richard Diamond6c6be142015-11-09 23:15:38 +0000140/// \param GetComponentNames - True if one would prefer the component names.
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000141static std::vector<std::string> ComputeLibsForComponents(
142 const std::vector<StringRef> &Components, bool IncludeNonInstalled,
143 bool GetComponentNames, const std::function<std::string(const StringRef &)>
144 *GetComponentLibraryPath,
145 std::vector<std::string> *Missing, const std::string &DirSep) {
Richard Diamond5efecd12015-11-25 22:49:48 +0000146 std::vector<std::string> RequiredLibs;
David Blaikie53b5ea72015-11-09 23:51:45 +0000147 std::set<AvailableComponent *> VisitedComponents;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000148
149 // Build a map of component names to information.
Andrew Wilkins93083d42016-01-20 04:03:09 +0000150 StringMap<AvailableComponent *> ComponentMap;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000151 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
152 AvailableComponent *AC = &AvailableComponents[i];
153 ComponentMap[AC->Name] = AC;
154 }
155
156 // Visit the components.
157 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
158 // Users are allowed to provide mixed case component names.
159 std::string ComponentLower = Components[i].lower();
160
161 // Validate that the user supplied a valid component name.
162 if (!ComponentMap.count(ComponentLower)) {
163 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
164 << "\n";
165 exit(1);
166 }
167
168 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
Richard Diamond6c6be142015-11-09 23:15:38 +0000169 RequiredLibs, IncludeNonInstalled, GetComponentNames,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000170 GetComponentLibraryPath, Missing, DirSep);
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000171 }
172
173 // The list is now ordered with leafs first, we want the libraries to printed
174 // in the reverse order of dependency.
175 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
David Blaikie53b5ea72015-11-09 23:51:45 +0000176
177 return RequiredLibs;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000178}
179
180/* *** */
181
Benjamin Kramer0df4e222015-03-09 16:23:46 +0000182static void usage() {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000183 errs() << "\
184usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
185\n\
186Get various configuration information needed to compile programs which use\n\
187LLVM. Typically called from 'configure' scripts. Examples:\n\
188 llvm-config --cxxflags\n\
189 llvm-config --ldflags\n\
190 llvm-config --libs engine bcreader scalaropts\n\
191\n\
192Options:\n\
193 --version Print LLVM version.\n\
194 --prefix Print the installation prefix.\n\
195 --src-root Print the source root LLVM was built from.\n\
196 --obj-root Print the object root used to build LLVM.\n\
197 --bindir Directory containing LLVM executables.\n\
198 --includedir Directory containing LLVM headers.\n\
199 --libdir Directory containing LLVM libraries.\n\
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000200 --cmakedir Directory containing LLVM cmake modules.\n\
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000201 --cppflags C preprocessor flags for files that include LLVM headers.\n\
202 --cflags C compiler flags for files that include LLVM headers.\n\
203 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
204 --ldflags Print Linker flags.\n\
NAKAMURA Takumi77a35982013-12-25 02:24:32 +0000205 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000206 --libs Libraries needed to link against LLVM components.\n\
207 --libnames Bare library names for in-tree builds.\n\
208 --libfiles Fully qualified library filenames for makefile depends.\n\
209 --components List of all possible components.\n\
210 --targets-built List of all targets currently built.\n\
211 --host-target Target triple used to configure LLVM.\n\
212 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
NAKAMURA Takumi04279572013-12-03 23:22:25 +0000213 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Filipe Cabecinhas8a96b702016-03-08 11:49:24 +0000214 --build-system Print the build system used to build LLVM (always cmake).\n\
Tom Stellard7ba0c202015-11-04 20:57:43 +0000215 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
Michal Gorny71b86982017-01-10 19:55:51 +0000216 --has-global-isel Print whether or not LLVM was built with global-isel support (ON or OFF).\n\
Richard Diamond6c6be142015-11-09 23:15:38 +0000217 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
Andrew Wilkins93083d42016-01-20 04:03:09 +0000218 --link-shared Link the components as shared libraries.\n\
219 --link-static Link the component libraries statically.\n\
Chris Bieneman741933d2016-12-13 22:17:59 +0000220 --ignore-libllvm Ignore libLLVM and link component libraries instead.\n\
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000221Typical components:\n\
222 all All LLVM libraries (default).\n\
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000223 engine Either a native JIT or a bitcode interpreter.\n";
224 exit(1);
225}
226
Adrian Prantl26b584c2018-05-01 15:54:18 +0000227/// Compute the path to the main executable.
Rafael Espindola50188c12013-06-26 05:01:35 +0000228std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000229 // This just needs to be some symbol in the binary; C++ doesn't
230 // allow taking the address of ::main however.
Andrew Wilkins93083d42016-01-20 04:03:09 +0000231 void *P = (void *)(intptr_t)GetExecutablePath;
Rafael Espindola50188c12013-06-26 05:01:35 +0000232 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000233}
234
Adrian Prantl26b584c2018-05-01 15:54:18 +0000235/// Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
Richard Diamond6c6be142015-11-09 23:15:38 +0000236/// the full list of components.
Richard Diamond5efecd12015-11-25 22:49:48 +0000237std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000238 const bool GetComponentNames,
239 const std::string &DirSep) {
Richard Diamond6c6be142015-11-09 23:15:38 +0000240 std::vector<StringRef> DyLibComponents;
Richard Diamond6c6be142015-11-09 23:15:38 +0000241
David Blaikie53b5ea72015-11-09 23:51:45 +0000242 StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
243 size_t Offset = 0;
244 while (true) {
245 const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
Marcello Maggionieea61482017-01-12 19:47:38 +0000246 DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset-Offset));
David Blaikie53b5ea72015-11-09 23:51:45 +0000247 if (NextOffset == std::string::npos) {
248 break;
249 }
250 Offset = NextOffset + 1;
Richard Diamond6c6be142015-11-09 23:15:38 +0000251 }
252
David Blaikie53b5ea72015-11-09 23:51:45 +0000253 assert(!DyLibComponents.empty());
Richard Diamond6c6be142015-11-09 23:15:38 +0000254
David Blaikie53b5ea72015-11-09 23:51:45 +0000255 return ComputeLibsForComponents(DyLibComponents,
256 /*IncludeNonInstalled=*/IsInDevelopmentTree,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000257 GetComponentNames, nullptr, nullptr, DirSep);
Richard Diamond6c6be142015-11-09 23:15:38 +0000258}
259
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000260int main(int argc, char **argv) {
261 std::vector<StringRef> Components;
262 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
Richard Diamond6c6be142015-11-09 23:15:38 +0000263 bool PrintSystemLibs = false, PrintSharedMode = false;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000264 bool HasAnyOption = false;
265
266 // llvm-config is designed to support being run both from a development tree
267 // and from an installed path. We try and auto-detect which case we are in so
268 // that we can report the correct information when run from a development
269 // tree.
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000270 bool IsInDevelopmentTree;
Filipe Cabecinhas8a96b702016-03-08 11:49:24 +0000271 enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindola50188c12013-06-26 05:01:35 +0000272 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000273 std::string CurrentExecPrefix;
274 std::string ActiveObjRoot;
275
NAKAMURA Takumi16d0e122013-12-17 05:48:37 +0000276 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
277 char const *build_mode = LLVM_BUILDMODE;
278#if defined(CMAKE_CFG_INTDIR)
279 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
280 build_mode = CMAKE_CFG_INTDIR;
281#endif
282
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000283 // Create an absolute path, and pop up one directory (we expect to be inside a
284 // bin dir).
285 sys::fs::make_absolute(CurrentPath);
Andrew Wilkins93083d42016-01-20 04:03:09 +0000286 CurrentExecPrefix =
287 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000288
289 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbar40d65dc2012-05-15 22:07:18 +0000290 // locations (prefix style or CMake style).
Filipe Cabecinhas8a96b702016-03-08 11:49:24 +0000291 if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000292 IsInDevelopmentTree = true;
293 DevelopmentTreeLayout = CMakeStyle;
294 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbar40d65dc2012-05-15 22:07:18 +0000295 } else if (sys::fs::equivalent(CurrentExecPrefix,
296 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000297 IsInDevelopmentTree = true;
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000298 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000299 ActiveObjRoot = LLVM_OBJ_ROOT;
300 } else {
301 IsInDevelopmentTree = false;
Filipe Cabecinhas8a96b702016-03-08 11:49:24 +0000302 DevelopmentTreeLayout = CMakeStyle; // Initialized to avoid warnings.
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000303 }
304
305 // Compute various directory locations based on the derived location
306 // information.
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000307 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir,
308 ActiveCMakeDir;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000309 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 Collingbourneb56900a2012-01-26 01:31:38 +0000316 switch (DevelopmentTreeLayout) {
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000317 case CMakeStyle:
318 ActiveBinDir = ActiveObjRoot + "/bin";
Chandler Carruth6e531642014-12-29 11:16:25 +0000319 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000320 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000321 break;
322 case CMakeBuildModeStyle:
NAKAMURA Takumiaf224d72013-12-19 16:02:23 +0000323 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi16d0e122013-12-17 05:48:37 +0000324 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
Chandler Carruth6e531642014-12-29 11:16:25 +0000325 ActiveLibDir =
326 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000327 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000328 break;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000329 }
330
331 // We need to include files from both the source and object trees.
Andrew Wilkins93083d42016-01-20 04:03:09 +0000332 ActiveIncludeOption =
333 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000334 } else {
335 ActivePrefix = CurrentExecPrefix;
336 ActiveIncludeDir = ActivePrefix + "/include";
Keno Fischerd840ad02017-06-01 20:51:55 +0000337 SmallString<256> path(StringRef(LLVM_TOOLS_INSTALL_DIR));
Keno Fischerbe2a63a2017-06-01 19:20:33 +0000338 sys::fs::make_absolute(ActivePrefix, path);
339 ActiveBinDir = path.str();
Chandler Carruth6e531642014-12-29 11:16:25 +0000340 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000341 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000342 ActiveIncludeOption = "-I" + ActiveIncludeDir;
343 }
344
Richard Diamond6c6be142015-11-09 23:15:38 +0000345 /// We only use `shared library` mode in cases where the static library form
346 /// of the components provided are not available; note however that this is
347 /// skipped if we're run from within the build dir. However, once installed,
348 /// we still need to provide correct output when the static archives are
349 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present
350 /// in the first place. This can't be done at configure/build time.
351
352 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000353 StaticPrefix, StaticDir = "lib", DirSep = "/";
NAKAMURA Takumia43ea0a2016-02-10 01:12:55 +0000354 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
Richard Diamond6c6be142015-11-09 23:15:38 +0000355 if (HostTriple.isOSWindows()) {
356 SharedExt = "dll";
Andrew Wilkins93083d42016-01-20 04:03:09 +0000357 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000358 if (HostTriple.isOSCygMing()) {
359 StaticExt = "a";
NAKAMURA Takumi4b5801c2016-02-10 03:09:13 +0000360 StaticPrefix = "lib";
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000361 } else {
362 StaticExt = "lib";
363 DirSep = "\\";
364 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
365 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
366 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
367 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000368 std::replace(ActiveCMakeDir.begin(), ActiveCMakeDir.end(), '/', '\\');
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000369 std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
370 '\\');
371 }
Richard Diamond6c6be142015-11-09 23:15:38 +0000372 SharedDir = ActiveBinDir;
373 StaticDir = ActiveLibDir;
Richard Diamond6c6be142015-11-09 23:15:38 +0000374 } else if (HostTriple.isOSDarwin()) {
375 SharedExt = "dylib";
Andrew Wilkins93083d42016-01-20 04:03:09 +0000376 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
Richard Diamond6c6be142015-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 Wilkins93083d42016-01-20 04:03:09 +0000383 SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
Richard Diamond6c6be142015-11-09 23:15:38 +0000384 StaticExt = "a";
385 StaticDir = SharedDir = ActiveLibDir;
386 StaticPrefix = SharedPrefix = "lib";
387 }
388
Michal Gorny71b86982017-01-10 19:55:51 +0000389 const bool BuiltDyLib = !!LLVM_ENABLE_DYLIB;
Richard Diamond6c6be142015-11-09 23:15:38 +0000390
Richard Diamond6c6be142015-11-09 23:15:38 +0000391 /// CMake style shared libs, ie each component is in a shared library.
Michal Gorny71b86982017-01-10 19:55:51 +0000392 const bool BuiltSharedLibs = !!LLVM_ENABLE_SHARED;
Richard Diamond6c6be142015-11-09 23:15:38 +0000393
394 bool DyLibExists = false;
395 const std::string DyLibName =
Andrew Wilkins93083d42016-01-20 04:03:09 +0000396 (SharedPrefix + "LLVM-" + SharedVersionedExt).str();
397
398 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned
399 // for "--libs", etc, if they exist. This behaviour can be overridden with
400 // --link-static or --link-shared.
Michal Gorny71b86982017-01-10 19:55:51 +0000401 bool LinkDyLib = !!LLVM_LINK_DYLIB;
Richard Diamond6c6be142015-11-09 23:15:38 +0000402
403 if (BuiltDyLib) {
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000404 std::string path((SharedDir + DirSep + DyLibName).str());
405 if (DirSep == "\\") {
406 std::replace(path.begin(), path.end(), '/', '\\');
407 }
408 DyLibExists = sys::fs::exists(path);
Andrew Wilkins93083d42016-01-20 04:03:09 +0000409 if (!DyLibExists) {
410 // The shared library does not exist: don't error unless the user
411 // explicitly passes --link-shared.
412 LinkDyLib = false;
413 }
Richard Diamond6c6be142015-11-09 23:15:38 +0000414 }
Andrew Wilkins93083d42016-01-20 04:03:09 +0000415 LinkMode LinkMode =
416 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
Richard Diamond6c6be142015-11-09 23:15:38 +0000417
418 /// Get the component's library name without the lib prefix and the
419 /// extension. Returns true if Lib is in a recognized format.
420 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
421 StringRef &Out) {
422 if (Lib.startswith("lib")) {
423 unsigned FromEnd;
424 if (Lib.endswith(StaticExt)) {
425 FromEnd = StaticExt.size() + 1;
426 } else if (Lib.endswith(SharedExt)) {
427 FromEnd = SharedExt.size() + 1;
428 } else {
429 FromEnd = 0;
430 }
431
432 if (FromEnd != 0) {
433 Out = Lib.slice(3, Lib.size() - FromEnd);
434 return true;
435 }
436 }
437
438 return false;
439 };
440 /// Maps Unixizms to the host platform.
441 auto GetComponentLibraryFileName = [&](const StringRef &Lib,
Andrew Wilkins93083d42016-01-20 04:03:09 +0000442 const bool Shared) {
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000443 std::string LibFileName;
444 if (Shared) {
Dan Liew5340b5b2016-12-12 23:07:22 +0000445 if (Lib == DyLibName) {
446 // Treat the DyLibName specially. It is not a component library and
447 // already has the necessary prefix and suffix (e.g. `.so`) added so
448 // just return it unmodified.
449 assert(Lib.endswith(SharedExt) && "DyLib is missing suffix");
450 LibFileName = Lib;
451 } else {
452 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
453 }
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000454 } else {
455 // default to static
456 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
Richard Diamond6c6be142015-11-09 23:15:38 +0000457 }
458
459 return LibFileName;
460 };
461 /// Get the full path for a possibly shared component library.
Andrew Wilkins93083d42016-01-20 04:03:09 +0000462 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
463 auto LibFileName = GetComponentLibraryFileName(Name, Shared);
464 if (Shared) {
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000465 return (SharedDir + DirSep + LibFileName).str();
Richard Diamond6c6be142015-11-09 23:15:38 +0000466 } else {
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000467 return (StaticDir + DirSep + LibFileName).str();
Richard Diamond6c6be142015-11-09 23:15:38 +0000468 }
469 };
470
Daniel Dunbarcb497b82011-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';
Michal Gorny7b7a32b2017-01-06 08:23:33 +0000487 } else if (Arg == "--cmakedir") {
488 OS << ActiveCMakeDir << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000489 } else if (Arg == "--cppflags") {
490 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
491 } else if (Arg == "--cflags") {
492 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
493 } else if (Arg == "--cxxflags") {
494 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
495 } else if (Arg == "--ldflags") {
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000496 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
497 << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
NAKAMURA Takumicbce2862013-12-19 08:46:36 +0000498 } else if (Arg == "--system-libs") {
499 PrintSystemLibs = true;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000500 } else if (Arg == "--libs") {
501 PrintLibs = true;
502 } else if (Arg == "--libnames") {
503 PrintLibNames = true;
504 } else if (Arg == "--libfiles") {
505 PrintLibFiles = true;
506 } else if (Arg == "--components") {
Richard Diamond6c6be142015-11-09 23:15:38 +0000507 /// If there are missing static archives and a dylib was
508 /// built, print LLVM_DYLIB_COMPONENTS instead of everything
509 /// in the manifest.
Richard Diamond5efecd12015-11-25 22:49:48 +0000510 std::vector<std::string> Components;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000511 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarb5cd41e2012-05-15 18:44:17 +0000512 // Only include non-installed components when in a development tree.
513 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
514 continue;
515
Richard Diamond6c6be142015-11-09 23:15:38 +0000516 Components.push_back(AvailableComponents[j].Name);
517 if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000518 std::string path(
519 GetComponentLibraryPath(AvailableComponents[j].Library, false));
520 if (DirSep == "\\") {
521 std::replace(path.begin(), path.end(), '/', '\\');
522 }
523 if (DyLibExists && !sys::fs::exists(path)) {
524 Components =
525 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
Fangrui Song3b35e172018-09-27 02:13:45 +0000526 llvm::sort(Components);
Richard Diamond6c6be142015-11-09 23:15:38 +0000527 break;
528 }
529 }
530 }
531
532 for (unsigned I = 0; I < Components.size(); ++I) {
533 if (I) {
534 OS << ' ';
535 }
536
537 OS << Components[I];
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000538 }
539 OS << '\n';
540 } else if (Arg == "--targets-built") {
Daniel Dunbar275dd942011-12-16 00:04:43 +0000541 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000542 } else if (Arg == "--host-target") {
Saleem Abdulrasoolc06afdc2014-03-29 01:08:53 +0000543 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000544 } else if (Arg == "--build-mode") {
NAKAMURA Takumi94f5a672013-12-03 14:35:17 +0000545 OS << build_mode << '\n';
NAKAMURA Takumi04279572013-12-03 23:22:25 +0000546 } else if (Arg == "--assertion-mode") {
547#if defined(NDEBUG)
548 OS << "OFF\n";
549#else
550 OS << "ON\n";
551#endif
Tom Stellard800a5b02015-09-09 16:39:30 +0000552 } else if (Arg == "--build-system") {
553 OS << LLVM_BUILD_SYSTEM << '\n';
Tom Stellard7ba0c202015-11-04 20:57:43 +0000554 } else if (Arg == "--has-rtti") {
Michal Gorny71b86982017-01-10 19:55:51 +0000555 OS << (LLVM_HAS_RTTI ? "YES" : "NO") << '\n';
Quentin Colombeta7b1d452016-03-08 00:02:50 +0000556 } else if (Arg == "--has-global-isel") {
Michal Gorny71b86982017-01-10 19:55:51 +0000557 OS << (LLVM_HAS_GLOBAL_ISEL ? "ON" : "OFF") << '\n';
Richard Diamond6c6be142015-11-09 23:15:38 +0000558 } else if (Arg == "--shared-mode") {
559 PrintSharedMode = true;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000560 } else if (Arg == "--obj-root") {
NAKAMURA Takumid4cf97b2013-12-19 16:02:28 +0000561 OS << ActivePrefix << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000562 } else if (Arg == "--src-root") {
563 OS << LLVM_SRC_ROOT << '\n';
Derek Schuff38087e52016-12-13 23:01:53 +0000564 } else if (Arg == "--ignore-libllvm") {
565 LinkDyLib = false;
566 LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto;
Andrew Wilkins93083d42016-01-20 04:03:09 +0000567 } else if (Arg == "--link-shared") {
568 LinkMode = LinkModeShared;
569 } else if (Arg == "--link-static") {
570 LinkMode = LinkModeStatic;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000571 } else {
572 usage();
573 }
574 } else {
575 Components.push_back(Arg);
576 }
577 }
578
579 if (!HasAnyOption)
580 usage();
581
Andrew Wilkins93083d42016-01-20 04:03:09 +0000582 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +0000583 WithColor::error(errs(), "llvm-config") << DyLibName << " is missing\n";
Andrew Wilkins93083d42016-01-20 04:03:09 +0000584 return 1;
585 }
586
Richard Diamond6c6be142015-11-09 23:15:38 +0000587 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
588 PrintSharedMode) {
589
590 if (PrintSharedMode && BuiltSharedLibs) {
591 OS << "shared\n";
592 return 0;
593 }
594
Daniel Dunbar8033f612011-12-12 18:22:04 +0000595 // If no components were specified, default to "all".
596 if (Components.empty())
597 Components.push_back("all");
598
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000599 // Construct the list of all the required libraries.
Andrew Wilkins93083d42016-01-20 04:03:09 +0000600 std::function<std::string(const StringRef &)>
601 GetComponentLibraryPathFunction = [&](const StringRef &Name) {
602 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
603 };
604 std::vector<std::string> MissingLibs;
605 std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
606 Components,
607 /*IncludeNonInstalled=*/IsInDevelopmentTree, false,
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000608 &GetComponentLibraryPathFunction, &MissingLibs, DirSep);
Andrew Wilkins93083d42016-01-20 04:03:09 +0000609 if (!MissingLibs.empty()) {
610 switch (LinkMode) {
611 case LinkModeShared:
Chris Bieneman23d01632016-12-13 23:08:52 +0000612 if (LinkDyLib && !BuiltSharedLibs)
Andrew Wilkins93083d42016-01-20 04:03:09 +0000613 break;
614 // Using component shared libraries.
615 for (auto &Lib : MissingLibs)
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +0000616 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n";
Andrew Wilkins93083d42016-01-20 04:03:09 +0000617 return 1;
618 case LinkModeAuto:
619 if (DyLibExists) {
620 LinkMode = LinkModeShared;
621 break;
622 }
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +0000623 WithColor::error(errs(), "llvm-config")
624 << "component libraries and shared library\n\n";
Justin Bogner7d7a23e2016-08-17 20:30:52 +0000625 LLVM_FALLTHROUGH;
Andrew Wilkins93083d42016-01-20 04:03:09 +0000626 case LinkModeStatic:
627 for (auto &Lib : MissingLibs)
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +0000628 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n";
Andrew Wilkins93083d42016-01-20 04:03:09 +0000629 return 1;
630 }
631 } else if (LinkMode == LinkModeAuto) {
632 LinkMode = LinkModeStatic;
633 }
Richard Diamond6c6be142015-11-09 23:15:38 +0000634
635 if (PrintSharedMode) {
636 std::unordered_set<std::string> FullDyLibComponents;
Richard Diamond5efecd12015-11-25 22:49:48 +0000637 std::vector<std::string> DyLibComponents =
Ehsan Akhgaria20946b2016-02-09 19:41:14 +0000638 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
Richard Diamond6c6be142015-11-09 23:15:38 +0000639
640 for (auto &Component : DyLibComponents) {
641 FullDyLibComponents.insert(Component);
642 }
643 DyLibComponents.clear();
644
645 for (auto &Lib : RequiredLibs) {
646 if (!FullDyLibComponents.count(Lib)) {
647 OS << "static\n";
648 return 0;
649 }
650 }
651 FullDyLibComponents.clear();
652
Andrew Wilkins93083d42016-01-20 04:03:09 +0000653 if (LinkMode == LinkModeShared) {
Richard Diamond6c6be142015-11-09 23:15:38 +0000654 OS << "shared\n";
655 return 0;
656 } else {
657 OS << "static\n";
658 return 0;
659 }
660 }
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000661
Richard Osborne7bc65d02014-03-03 15:06:14 +0000662 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000663
Andrew Wilkins93083d42016-01-20 04:03:09 +0000664 auto PrintForLib = [&](const StringRef &Lib) {
665 const bool Shared = LinkMode == LinkModeShared;
Richard Osborne7bc65d02014-03-03 15:06:14 +0000666 if (PrintLibNames) {
Andrew Wilkins93083d42016-01-20 04:03:09 +0000667 OS << GetComponentLibraryFileName(Lib, Shared);
Richard Osborne7bc65d02014-03-03 15:06:14 +0000668 } else if (PrintLibFiles) {
Andrew Wilkins93083d42016-01-20 04:03:09 +0000669 OS << GetComponentLibraryPath(Lib, Shared);
Richard Osborne7bc65d02014-03-03 15:06:14 +0000670 } else if (PrintLibs) {
Reid Kleckner07a4ec02016-03-14 21:39:58 +0000671 // On Windows, output full path to library without parameters.
672 // Elsewhere, if this is a typical library name, include it using -l.
673 if (HostTriple.isWindowsMSVCEnvironment()) {
674 OS << GetComponentLibraryPath(Lib, Shared);
675 } else {
676 StringRef LibName;
Richard Diamond6c6be142015-11-09 23:15:38 +0000677 if (GetComponentLibraryNameSlice(Lib, LibName)) {
Reid Kleckner07a4ec02016-03-14 21:39:58 +0000678 // Extract library name (remove prefix and suffix).
Richard Diamond6c6be142015-11-09 23:15:38 +0000679 OS << "-l" << LibName;
680 } else {
Reid Kleckner07a4ec02016-03-14 21:39:58 +0000681 // Lib is already a library name without prefix and suffix.
682 OS << "-l" << Lib;
Richard Diamond6c6be142015-11-09 23:15:38 +0000683 }
Richard Osborne7bc65d02014-03-03 15:06:14 +0000684 }
Richard Diamond6c6be142015-11-09 23:15:38 +0000685 }
686 };
Richard Osborne7bc65d02014-03-03 15:06:14 +0000687
Chris Bieneman23d01632016-12-13 23:08:52 +0000688 if (LinkMode == LinkModeShared && LinkDyLib) {
Andrew Wilkins93083d42016-01-20 04:03:09 +0000689 PrintForLib(DyLibName);
Richard Diamond6c6be142015-11-09 23:15:38 +0000690 } else {
691 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
Richard Diamond5efecd12015-11-25 22:49:48 +0000692 auto Lib = RequiredLibs[i];
Richard Diamond6c6be142015-11-09 23:15:38 +0000693 if (i)
694 OS << ' ';
695
Andrew Wilkins93083d42016-01-20 04:03:09 +0000696 PrintForLib(Lib);
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000697 }
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000698 }
Richard Osborne7bc65d02014-03-03 15:06:14 +0000699 OS << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000700 }
NAKAMURA Takumicbce2862013-12-19 08:46:36 +0000701
702 // Print SYSTEM_LIBS after --libs.
703 // FIXME: Each LLVM component may have its dependent system libs.
Michal Gorny4d352392017-01-06 21:33:54 +0000704 if (PrintSystemLibs) {
705 // Output system libraries only if linking against a static
706 // library (since the shared library links to all system libs
707 // already)
708 OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n';
709 }
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000710 } else if (!Components.empty()) {
Jonas Devliegherefc6fcec2018-06-23 16:50:09 +0000711 WithColor::error(errs(), "llvm-config")
712 << "components given, but unused\n\n";
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000713 usage();
714 }
715
716 return 0;
717}