blob: 8bf2680d3918b32ef457396d19853041c6eda7fd [file] [log] [blame]
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +00001//===-- llvm-config.cpp - LLVM project configuration utility --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tool encapsulates information about an LLVM project configuration for
11// use by other project's build environments (to determine installed path,
12// available features, required libraries, etc.).
13//
14// Note that although this tool *may* be used by some parts of LLVM's build
15// itself (i.e., the Makefiles use it to compute required libraries when linking
16// tools), this tool is primarily designed to support external projects.
17//
18//===----------------------------------------------------------------------===//
19
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +000023#include "llvm/ADT/Triple.h"
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000024#include "llvm/ADT/Twine.h"
25#include "llvm/Config/config.h"
26#include "llvm/Config/llvm-config.h"
27#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/Path.h"
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000029#include "llvm/Support/raw_ostream.h"
30#include <cstdlib>
31#include <set>
32#include <vector>
33
34using namespace llvm;
35
36// Include the build time variables we can report to the user. This is generated
37// at build time from the BuildVariables.inc.in file by the build system.
38#include "BuildVariables.inc"
39
40// Include the component table. This creates an array of struct
41// AvailableComponent entries, which record the component name, library name,
42// and required components for all of the available libraries.
43//
44// Not all components define a library, we also use "library groups" as a way to
45// create entries for pseudo groups like x86 or all-targets.
46#include "LibraryDependencies.inc"
47
48/// \brief Traverse a single component adding to the topological ordering in
49/// \arg RequiredLibs.
50///
51/// \param Name - The component to traverse.
52/// \param ComponentMap - A prebuilt map of component names to descriptors.
53/// \param VisitedComponents [in] [out] - The set of already visited components.
54/// \param RequiredLibs [out] - The ordered list of required libraries.
55static void VisitComponent(StringRef Name,
56 const StringMap<AvailableComponent*> &ComponentMap,
57 std::set<AvailableComponent*> &VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +000058 std::vector<StringRef> &RequiredLibs,
59 bool IncludeNonInstalled) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000060 // Lookup the component.
61 AvailableComponent *AC = ComponentMap.lookup(Name);
62 assert(AC && "Invalid component name!");
63
64 // Add to the visited table.
65 if (!VisitedComponents.insert(AC).second) {
66 // We are done if the component has already been visited.
67 return;
68 }
69
Daniel Dunbarc364d682012-05-15 18:44:17 +000070 // Only include non-installed components if requested.
71 if (!AC->IsInstalled && !IncludeNonInstalled)
72 return;
73
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000074 // Otherwise, visit all the dependencies.
75 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
76 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +000077 RequiredLibs, IncludeNonInstalled);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000078 }
79
80 // Add to the required library list.
81 if (AC->Library)
82 RequiredLibs.push_back(AC->Library);
83}
84
85/// \brief Compute the list of required libraries for a given list of
86/// components, in an order suitable for passing to a linker (that is, libraries
87/// appear prior to their dependencies).
88///
89/// \param Components - The names of the components to find libraries for.
90/// \param RequiredLibs [out] - On return, the ordered list of libraries that
91/// are required to link the given components.
Daniel Dunbarc364d682012-05-15 18:44:17 +000092/// \param IncludeNonInstalled - Whether non-installed components should be
93/// reported.
Benjamin Kramerf044d3f2015-03-09 16:23:46 +000094static void ComputeLibsForComponents(const std::vector<StringRef> &Components,
95 std::vector<StringRef> &RequiredLibs,
96 bool IncludeNonInstalled) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000097 std::set<AvailableComponent*> VisitedComponents;
98
99 // Build a map of component names to information.
100 StringMap<AvailableComponent*> ComponentMap;
101 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
102 AvailableComponent *AC = &AvailableComponents[i];
103 ComponentMap[AC->Name] = AC;
104 }
105
106 // Visit the components.
107 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
108 // Users are allowed to provide mixed case component names.
109 std::string ComponentLower = Components[i].lower();
110
111 // Validate that the user supplied a valid component name.
112 if (!ComponentMap.count(ComponentLower)) {
113 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
114 << "\n";
115 exit(1);
116 }
117
118 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +0000119 RequiredLibs, IncludeNonInstalled);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000120 }
121
122 // The list is now ordered with leafs first, we want the libraries to printed
123 // in the reverse order of dependency.
124 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
125}
126
127/* *** */
128
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000129static void usage() {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000130 errs() << "\
131usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
132\n\
133Get various configuration information needed to compile programs which use\n\
134LLVM. Typically called from 'configure' scripts. Examples:\n\
135 llvm-config --cxxflags\n\
136 llvm-config --ldflags\n\
137 llvm-config --libs engine bcreader scalaropts\n\
138\n\
139Options:\n\
140 --version Print LLVM version.\n\
141 --prefix Print the installation prefix.\n\
142 --src-root Print the source root LLVM was built from.\n\
143 --obj-root Print the object root used to build LLVM.\n\
144 --bindir Directory containing LLVM executables.\n\
145 --includedir Directory containing LLVM headers.\n\
146 --libdir Directory containing LLVM libraries.\n\
147 --cppflags C preprocessor flags for files that include LLVM headers.\n\
148 --cflags C compiler flags for files that include LLVM headers.\n\
149 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
150 --ldflags Print Linker flags.\n\
NAKAMURA Takumi800eb082013-12-25 02:24:32 +0000151 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000152 --libs Libraries needed to link against LLVM components.\n\
153 --libnames Bare library names for in-tree builds.\n\
154 --libfiles Fully qualified library filenames for makefile depends.\n\
155 --components List of all possible components.\n\
156 --targets-built List of all targets currently built.\n\
157 --host-target Target triple used to configure LLVM.\n\
158 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000159 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Tom Stellard5268c172015-09-09 16:39:30 +0000160 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
Tom Stellard18bf6262015-11-04 20:57:43 +0000161 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000162Typical components:\n\
163 all All LLVM libraries (default).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000164 engine Either a native JIT or a bitcode interpreter.\n";
165 exit(1);
166}
167
168/// \brief Compute the path to the main executable.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000169std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000170 // This just needs to be some symbol in the binary; C++ doesn't
171 // allow taking the address of ::main however.
172 void *P = (void*) (intptr_t) GetExecutablePath;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000173 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000174}
175
176int main(int argc, char **argv) {
177 std::vector<StringRef> Components;
178 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000179 bool PrintSystemLibs = false;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000180 bool HasAnyOption = false;
181
182 // llvm-config is designed to support being run both from a development tree
183 // and from an installed path. We try and auto-detect which case we are in so
184 // that we can report the correct information when run from a development
185 // tree.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000186 bool IsInDevelopmentTree;
187 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000188 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000189 std::string CurrentExecPrefix;
190 std::string ActiveObjRoot;
191
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000192 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
193 char const *build_mode = LLVM_BUILDMODE;
194#if defined(CMAKE_CFG_INTDIR)
195 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
196 build_mode = CMAKE_CFG_INTDIR;
197#endif
198
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000199 // Create an absolute path, and pop up one directory (we expect to be inside a
200 // bin dir).
201 sys::fs::make_absolute(CurrentPath);
202 CurrentExecPrefix = sys::path::parent_path(
203 sys::path::parent_path(CurrentPath)).str();
204
205 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000206 // locations (prefix style or CMake style).
207 if (sys::fs::equivalent(CurrentExecPrefix,
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000208 Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000209 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000210 DevelopmentTreeLayout = MakefileStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000211
212 // If we are in a development tree, then check if we are in a BuildTools
213 // directory. This indicates we are built for the build triple, but we
214 // always want to provide information for the host triple.
215 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
216 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
217 } else {
218 ActiveObjRoot = LLVM_OBJ_ROOT;
219 }
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000220 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000221 IsInDevelopmentTree = true;
222 DevelopmentTreeLayout = CMakeStyle;
223 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000224 } else if (sys::fs::equivalent(CurrentExecPrefix,
225 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000226 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000227 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000228 ActiveObjRoot = LLVM_OBJ_ROOT;
229 } else {
230 IsInDevelopmentTree = false;
Duncan Sandsf320be82012-02-23 08:25:25 +0000231 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000232 }
233
234 // Compute various directory locations based on the derived location
235 // information.
236 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
237 std::string ActiveIncludeOption;
238 if (IsInDevelopmentTree) {
239 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
240 ActivePrefix = CurrentExecPrefix;
241
242 // CMake organizes the products differently than a normal prefix style
243 // layout.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000244 switch (DevelopmentTreeLayout) {
245 case MakefileStyle:
NAKAMURA Takumi46c19032013-12-20 17:35:52 +0000246 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000247 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000248 ActiveLibDir =
249 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000250 break;
251 case CMakeStyle:
252 ActiveBinDir = ActiveObjRoot + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000253 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000254 break;
255 case CMakeBuildModeStyle:
NAKAMURA Takumi429a2222013-12-19 16:02:23 +0000256 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000257 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
Chandler Carruth7d587762014-12-29 11:16:25 +0000258 ActiveLibDir =
259 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000260 break;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000261 }
262
263 // We need to include files from both the source and object trees.
264 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
265 "-I" + ActiveObjRoot + "/include");
266 } else {
267 ActivePrefix = CurrentExecPrefix;
268 ActiveIncludeDir = ActivePrefix + "/include";
269 ActiveBinDir = ActivePrefix + "/bin";
Chandler Carruth7d587762014-12-29 11:16:25 +0000270 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000271 ActiveIncludeOption = "-I" + ActiveIncludeDir;
272 }
273
274 raw_ostream &OS = outs();
275 for (int i = 1; i != argc; ++i) {
276 StringRef Arg = argv[i];
277
278 if (Arg.startswith("-")) {
279 HasAnyOption = true;
280 if (Arg == "--version") {
281 OS << PACKAGE_VERSION << '\n';
282 } else if (Arg == "--prefix") {
283 OS << ActivePrefix << '\n';
284 } else if (Arg == "--bindir") {
285 OS << ActiveBinDir << '\n';
286 } else if (Arg == "--includedir") {
287 OS << ActiveIncludeDir << '\n';
288 } else if (Arg == "--libdir") {
289 OS << ActiveLibDir << '\n';
290 } else if (Arg == "--cppflags") {
291 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
292 } else if (Arg == "--cflags") {
293 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
294 } else if (Arg == "--cxxflags") {
295 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
296 } else if (Arg == "--ldflags") {
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000297 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
298 } else if (Arg == "--system-libs") {
299 PrintSystemLibs = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000300 } else if (Arg == "--libs") {
301 PrintLibs = true;
302 } else if (Arg == "--libnames") {
303 PrintLibNames = true;
304 } else if (Arg == "--libfiles") {
305 PrintLibFiles = true;
306 } else if (Arg == "--components") {
307 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarc364d682012-05-15 18:44:17 +0000308 // Only include non-installed components when in a development tree.
309 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
310 continue;
311
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000312 OS << ' ';
313 OS << AvailableComponents[j].Name;
314 }
315 OS << '\n';
316 } else if (Arg == "--targets-built") {
Daniel Dunbar30a89762011-12-16 00:04:43 +0000317 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000318 } else if (Arg == "--host-target") {
Saleem Abdulrasool37511ec2014-03-29 01:08:53 +0000319 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000320 } else if (Arg == "--build-mode") {
NAKAMURA Takumi1b16e272013-12-03 14:35:17 +0000321 OS << build_mode << '\n';
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000322 } else if (Arg == "--assertion-mode") {
323#if defined(NDEBUG)
324 OS << "OFF\n";
325#else
326 OS << "ON\n";
327#endif
Tom Stellard5268c172015-09-09 16:39:30 +0000328 } else if (Arg == "--build-system") {
329 OS << LLVM_BUILD_SYSTEM << '\n';
Tom Stellard18bf6262015-11-04 20:57:43 +0000330 } else if (Arg == "--has-rtti") {
331 OS << LLVM_HAS_RTTI << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000332 } else if (Arg == "--obj-root") {
NAKAMURA Takumi93a14622013-12-19 16:02:28 +0000333 OS << ActivePrefix << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000334 } else if (Arg == "--src-root") {
335 OS << LLVM_SRC_ROOT << '\n';
336 } else {
337 usage();
338 }
339 } else {
340 Components.push_back(Arg);
341 }
342 }
343
344 if (!HasAnyOption)
345 usage();
346
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000347 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
Daniel Dunbarfbc6a892011-12-12 18:22:04 +0000348 // If no components were specified, default to "all".
349 if (Components.empty())
350 Components.push_back("all");
351
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000352 // Construct the list of all the required libraries.
353 std::vector<StringRef> RequiredLibs;
Daniel Dunbarc364d682012-05-15 18:44:17 +0000354 ComputeLibsForComponents(Components, RequiredLibs,
355 /*IncludeNonInstalled=*/IsInDevelopmentTree);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000356
Richard Osborne49ae1172014-03-03 15:06:14 +0000357 if (PrintLibs || PrintLibNames || PrintLibFiles) {
358 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
359 StringRef Lib = RequiredLibs[i];
360 if (i)
361 OS << ' ';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000362
Richard Osborne49ae1172014-03-03 15:06:14 +0000363 if (PrintLibNames) {
364 OS << Lib;
365 } else if (PrintLibFiles) {
366 OS << ActiveLibDir << '/' << Lib;
367 } else if (PrintLibs) {
368 // If this is a typical library name, include it using -l.
Richard Diamond23280472015-08-05 20:03:26 +0000369 if (Lib.startswith("lib") && Lib.endswith(".a")) {
370 OS << "-l" << Lib.slice(3, Lib.size()-2);
Richard Osborne49ae1172014-03-03 15:06:14 +0000371 continue;
372 }
373
374 // Otherwise, print the full path.
375 OS << ActiveLibDir << '/' << Lib;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000376 }
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000377 }
Richard Osborne49ae1172014-03-03 15:06:14 +0000378 OS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000379 }
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000380
381 // Print SYSTEM_LIBS after --libs.
382 // FIXME: Each LLVM component may have its dependent system libs.
383 if (PrintSystemLibs)
384 OS << LLVM_SYSTEM_LIBS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000385 } else if (!Components.empty()) {
386 errs() << "llvm-config: error: components given, but unused\n\n";
387 usage();
388 }
389
390 return 0;
391}