blob: 53368ff067ea5ae3c56b779eef4bf14d377006a0 [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"
23#include "llvm/ADT/Twine.h"
24#include "llvm/Config/config.h"
25#include "llvm/Config/llvm-config.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/Path.h"
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000028#include "llvm/Support/raw_ostream.h"
29#include <cstdlib>
30#include <set>
31#include <vector>
32
33using namespace llvm;
34
35// Include the build time variables we can report to the user. This is generated
36// at build time from the BuildVariables.inc.in file by the build system.
37#include "BuildVariables.inc"
38
39// Include the component table. This creates an array of struct
40// AvailableComponent entries, which record the component name, library name,
41// and required components for all of the available libraries.
42//
43// Not all components define a library, we also use "library groups" as a way to
44// create entries for pseudo groups like x86 or all-targets.
45#include "LibraryDependencies.inc"
46
47/// \brief Traverse a single component adding to the topological ordering in
48/// \arg RequiredLibs.
49///
50/// \param Name - The component to traverse.
51/// \param ComponentMap - A prebuilt map of component names to descriptors.
52/// \param VisitedComponents [in] [out] - The set of already visited components.
53/// \param RequiredLibs [out] - The ordered list of required libraries.
54static void VisitComponent(StringRef Name,
55 const StringMap<AvailableComponent*> &ComponentMap,
56 std::set<AvailableComponent*> &VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +000057 std::vector<StringRef> &RequiredLibs,
58 bool IncludeNonInstalled) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000059 // Lookup the component.
60 AvailableComponent *AC = ComponentMap.lookup(Name);
61 assert(AC && "Invalid component name!");
62
63 // Add to the visited table.
64 if (!VisitedComponents.insert(AC).second) {
65 // We are done if the component has already been visited.
66 return;
67 }
68
Daniel Dunbarc364d682012-05-15 18:44:17 +000069 // Only include non-installed components if requested.
70 if (!AC->IsInstalled && !IncludeNonInstalled)
71 return;
72
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000073 // Otherwise, visit all the dependencies.
74 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
75 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +000076 RequiredLibs, IncludeNonInstalled);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000077 }
78
79 // Add to the required library list.
80 if (AC->Library)
81 RequiredLibs.push_back(AC->Library);
82}
83
84/// \brief Compute the list of required libraries for a given list of
85/// components, in an order suitable for passing to a linker (that is, libraries
86/// appear prior to their dependencies).
87///
88/// \param Components - The names of the components to find libraries for.
89/// \param RequiredLibs [out] - On return, the ordered list of libraries that
90/// are required to link the given components.
Daniel Dunbarc364d682012-05-15 18:44:17 +000091/// \param IncludeNonInstalled - Whether non-installed components should be
92/// reported.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000093void ComputeLibsForComponents(const std::vector<StringRef> &Components,
Daniel Dunbarc364d682012-05-15 18:44:17 +000094 std::vector<StringRef> &RequiredLibs,
95 bool IncludeNonInstalled) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +000096 std::set<AvailableComponent*> VisitedComponents;
97
98 // Build a map of component names to information.
99 StringMap<AvailableComponent*> ComponentMap;
100 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
101 AvailableComponent *AC = &AvailableComponents[i];
102 ComponentMap[AC->Name] = AC;
103 }
104
105 // Visit the components.
106 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
107 // Users are allowed to provide mixed case component names.
108 std::string ComponentLower = Components[i].lower();
109
110 // Validate that the user supplied a valid component name.
111 if (!ComponentMap.count(ComponentLower)) {
112 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
113 << "\n";
114 exit(1);
115 }
116
117 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
Daniel Dunbarc364d682012-05-15 18:44:17 +0000118 RequiredLibs, IncludeNonInstalled);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000119 }
120
121 // The list is now ordered with leafs first, we want the libraries to printed
122 // in the reverse order of dependency.
123 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
124}
125
126/* *** */
127
128void usage() {
129 errs() << "\
130usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
131\n\
132Get various configuration information needed to compile programs which use\n\
133LLVM. Typically called from 'configure' scripts. Examples:\n\
134 llvm-config --cxxflags\n\
135 llvm-config --ldflags\n\
136 llvm-config --libs engine bcreader scalaropts\n\
137\n\
138Options:\n\
139 --version Print LLVM version.\n\
140 --prefix Print the installation prefix.\n\
141 --src-root Print the source root LLVM was built from.\n\
142 --obj-root Print the object root used to build LLVM.\n\
143 --bindir Directory containing LLVM executables.\n\
144 --includedir Directory containing LLVM headers.\n\
145 --libdir Directory containing LLVM libraries.\n\
146 --cppflags C preprocessor flags for files that include LLVM headers.\n\
147 --cflags C compiler flags for files that include LLVM headers.\n\
148 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
149 --ldflags Print Linker flags.\n\
NAKAMURA Takumi800eb082013-12-25 02:24:32 +0000150 --system-libs System Libraries needed to link against LLVM components.\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000151 --libs Libraries needed to link against LLVM components.\n\
152 --libnames Bare library names for in-tree builds.\n\
153 --libfiles Fully qualified library filenames for makefile depends.\n\
154 --components List of all possible components.\n\
155 --targets-built List of all targets currently built.\n\
156 --host-target Target triple used to configure LLVM.\n\
157 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000158 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000159Typical components:\n\
160 all All LLVM libraries (default).\n\
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000161 engine Either a native JIT or a bitcode interpreter.\n";
162 exit(1);
163}
164
165/// \brief Compute the path to the main executable.
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000166std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000167 // This just needs to be some symbol in the binary; C++ doesn't
168 // allow taking the address of ::main however.
169 void *P = (void*) (intptr_t) GetExecutablePath;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000170 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000171}
172
173int main(int argc, char **argv) {
174 std::vector<StringRef> Components;
175 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000176 bool PrintSystemLibs = false;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000177 bool HasAnyOption = false;
178
179 // llvm-config is designed to support being run both from a development tree
180 // and from an installed path. We try and auto-detect which case we are in so
181 // that we can report the correct information when run from a development
182 // tree.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000183 bool IsInDevelopmentTree;
184 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindolae03dfd92013-06-26 05:01:35 +0000185 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000186 std::string CurrentExecPrefix;
187 std::string ActiveObjRoot;
188
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000189 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
190 char const *build_mode = LLVM_BUILDMODE;
191#if defined(CMAKE_CFG_INTDIR)
192 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
193 build_mode = CMAKE_CFG_INTDIR;
194#endif
195
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000196 // Create an absolute path, and pop up one directory (we expect to be inside a
197 // bin dir).
198 sys::fs::make_absolute(CurrentPath);
199 CurrentExecPrefix = sys::path::parent_path(
200 sys::path::parent_path(CurrentPath)).str();
201
202 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000203 // locations (prefix style or CMake style).
204 if (sys::fs::equivalent(CurrentExecPrefix,
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000205 Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000206 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000207 DevelopmentTreeLayout = MakefileStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000208
209 // If we are in a development tree, then check if we are in a BuildTools
210 // directory. This indicates we are built for the build triple, but we
211 // always want to provide information for the host triple.
212 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
213 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
214 } else {
215 ActiveObjRoot = LLVM_OBJ_ROOT;
216 }
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000217 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000218 IsInDevelopmentTree = true;
219 DevelopmentTreeLayout = CMakeStyle;
220 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbarf1ab4022012-05-15 22:07:18 +0000221 } else if (sys::fs::equivalent(CurrentExecPrefix,
222 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000223 IsInDevelopmentTree = true;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000224 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000225 ActiveObjRoot = LLVM_OBJ_ROOT;
226 } else {
227 IsInDevelopmentTree = false;
Duncan Sandsf320be82012-02-23 08:25:25 +0000228 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000229 }
230
231 // Compute various directory locations based on the derived location
232 // information.
233 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
234 std::string ActiveIncludeOption;
235 if (IsInDevelopmentTree) {
236 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
237 ActivePrefix = CurrentExecPrefix;
238
239 // CMake organizes the products differently than a normal prefix style
240 // layout.
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000241 switch (DevelopmentTreeLayout) {
242 case MakefileStyle:
NAKAMURA Takumi46c19032013-12-20 17:35:52 +0000243 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi5a600a92013-12-20 17:35:46 +0000244 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
245 ActiveLibDir = ActiveObjRoot + "/" + build_mode + "/lib";
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000246 break;
247 case CMakeStyle:
248 ActiveBinDir = ActiveObjRoot + "/bin";
249 ActiveLibDir = ActiveObjRoot + "/lib";
250 break;
251 case CMakeBuildModeStyle:
NAKAMURA Takumi429a2222013-12-19 16:02:23 +0000252 ActivePrefix = ActiveObjRoot;
NAKAMURA Takumi7b789b32013-12-17 05:48:37 +0000253 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
254 ActiveLibDir = ActiveObjRoot + "/lib/" + build_mode;
Peter Collingbourne76e1c8c2012-01-26 01:31:38 +0000255 break;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000256 }
257
258 // We need to include files from both the source and object trees.
259 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
260 "-I" + ActiveObjRoot + "/include");
261 } else {
262 ActivePrefix = CurrentExecPrefix;
263 ActiveIncludeDir = ActivePrefix + "/include";
264 ActiveBinDir = ActivePrefix + "/bin";
265 ActiveLibDir = ActivePrefix + "/lib";
266 ActiveIncludeOption = "-I" + ActiveIncludeDir;
267 }
268
269 raw_ostream &OS = outs();
270 for (int i = 1; i != argc; ++i) {
271 StringRef Arg = argv[i];
272
273 if (Arg.startswith("-")) {
274 HasAnyOption = true;
275 if (Arg == "--version") {
276 OS << PACKAGE_VERSION << '\n';
277 } else if (Arg == "--prefix") {
278 OS << ActivePrefix << '\n';
279 } else if (Arg == "--bindir") {
280 OS << ActiveBinDir << '\n';
281 } else if (Arg == "--includedir") {
282 OS << ActiveIncludeDir << '\n';
283 } else if (Arg == "--libdir") {
284 OS << ActiveLibDir << '\n';
285 } else if (Arg == "--cppflags") {
286 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
287 } else if (Arg == "--cflags") {
288 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
289 } else if (Arg == "--cxxflags") {
290 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
291 } else if (Arg == "--ldflags") {
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000292 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
293 } else if (Arg == "--system-libs") {
294 PrintSystemLibs = true;
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000295 } else if (Arg == "--libs") {
296 PrintLibs = true;
297 } else if (Arg == "--libnames") {
298 PrintLibNames = true;
299 } else if (Arg == "--libfiles") {
300 PrintLibFiles = true;
301 } else if (Arg == "--components") {
302 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarc364d682012-05-15 18:44:17 +0000303 // Only include non-installed components when in a development tree.
304 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
305 continue;
306
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000307 OS << ' ';
308 OS << AvailableComponents[j].Name;
309 }
310 OS << '\n';
311 } else if (Arg == "--targets-built") {
Daniel Dunbar30a89762011-12-16 00:04:43 +0000312 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000313 } else if (Arg == "--host-target") {
314 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n';
315 } else if (Arg == "--build-mode") {
NAKAMURA Takumi1b16e272013-12-03 14:35:17 +0000316 OS << build_mode << '\n';
NAKAMURA Takumi303f0f52013-12-03 23:22:25 +0000317 } else if (Arg == "--assertion-mode") {
318#if defined(NDEBUG)
319 OS << "OFF\n";
320#else
321 OS << "ON\n";
322#endif
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000323 } else if (Arg == "--obj-root") {
NAKAMURA Takumi93a14622013-12-19 16:02:28 +0000324 OS << ActivePrefix << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000325 } else if (Arg == "--src-root") {
326 OS << LLVM_SRC_ROOT << '\n';
327 } else {
328 usage();
329 }
330 } else {
331 Components.push_back(Arg);
332 }
333 }
334
335 if (!HasAnyOption)
336 usage();
337
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000338 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
Daniel Dunbarfbc6a892011-12-12 18:22:04 +0000339 // If no components were specified, default to "all".
340 if (Components.empty())
341 Components.push_back("all");
342
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000343 // Construct the list of all the required libraries.
344 std::vector<StringRef> RequiredLibs;
Daniel Dunbarc364d682012-05-15 18:44:17 +0000345 ComputeLibsForComponents(Components, RequiredLibs,
346 /*IncludeNonInstalled=*/IsInDevelopmentTree);
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000347
348 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
349 StringRef Lib = RequiredLibs[i];
350 if (i)
351 OS << ' ';
352
353 if (PrintLibNames) {
354 OS << Lib;
355 } else if (PrintLibFiles) {
356 OS << ActiveLibDir << '/' << Lib;
357 } else if (PrintLibs) {
358 // If this is a typical library name, include it using -l.
359 if (Lib.startswith("lib") && Lib.endswith(".a")) {
360 OS << "-l" << Lib.slice(3, Lib.size()-2);
361 continue;
362 }
363
364 // Otherwise, print the full path.
365 OS << ActiveLibDir << '/' << Lib;
366 }
367 }
368 OS << '\n';
NAKAMURA Takumif8c58c82013-12-19 08:46:36 +0000369
370 // Print SYSTEM_LIBS after --libs.
371 // FIXME: Each LLVM component may have its dependent system libs.
372 if (PrintSystemLibs)
373 OS << LLVM_SYSTEM_LIBS << '\n';
Daniel Dunbarab0ad4e2011-12-01 20:18:09 +0000374 } else if (!Components.empty()) {
375 errs() << "llvm-config: error: components given, but unused\n\n";
376 usage();
377 }
378
379 return 0;
380}