blob: fc3ab68f7d8cb1963ff891f3d4960179b23be00c [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
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"
Rafael Espindolaf3e397e2013-06-11 20:00:56 +000028#include "llvm/Support/PathV1.h"
Daniel Dunbarcb497b82011-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 Dunbarb5cd41e2012-05-15 18:44:17 +000058 std::vector<StringRef> &RequiredLibs,
59 bool IncludeNonInstalled) {
Daniel Dunbarcb497b82011-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 Dunbarb5cd41e2012-05-15 18:44:17 +000070 // Only include non-installed components if requested.
71 if (!AC->IsInstalled && !IncludeNonInstalled)
72 return;
73
Daniel Dunbarcb497b82011-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 Dunbarb5cd41e2012-05-15 18:44:17 +000077 RequiredLibs, IncludeNonInstalled);
Daniel Dunbarcb497b82011-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 Dunbarb5cd41e2012-05-15 18:44:17 +000092/// \param IncludeNonInstalled - Whether non-installed components should be
93/// reported.
Daniel Dunbarcb497b82011-12-01 20:18:09 +000094void ComputeLibsForComponents(const std::vector<StringRef> &Components,
Daniel Dunbarb5cd41e2012-05-15 18:44:17 +000095 std::vector<StringRef> &RequiredLibs,
96 bool IncludeNonInstalled) {
Daniel Dunbarcb497b82011-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 Dunbarb5cd41e2012-05-15 18:44:17 +0000119 RequiredLibs, IncludeNonInstalled);
Daniel Dunbarcb497b82011-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
129void usage() {
130 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\
151 --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\
158Typical components:\n\
159 all All LLVM libraries (default).\n\
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000160 engine Either a native JIT or a bitcode interpreter.\n";
161 exit(1);
162}
163
164/// \brief Compute the path to the main executable.
Rafael Espindola50188c12013-06-26 05:01:35 +0000165std::string GetExecutablePath(const char *Argv0) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000166 // This just needs to be some symbol in the binary; C++ doesn't
167 // allow taking the address of ::main however.
168 void *P = (void*) (intptr_t) GetExecutablePath;
Rafael Espindola50188c12013-06-26 05:01:35 +0000169 return llvm::sys::fs::getMainExecutable(Argv0, P);
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000170}
171
172int main(int argc, char **argv) {
173 std::vector<StringRef> Components;
174 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
175 bool HasAnyOption = false;
176
177 // llvm-config is designed to support being run both from a development tree
178 // and from an installed path. We try and auto-detect which case we are in so
179 // that we can report the correct information when run from a development
180 // tree.
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000181 bool IsInDevelopmentTree;
182 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
Rafael Espindola50188c12013-06-26 05:01:35 +0000183 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000184 std::string CurrentExecPrefix;
185 std::string ActiveObjRoot;
186
187 // Create an absolute path, and pop up one directory (we expect to be inside a
188 // bin dir).
189 sys::fs::make_absolute(CurrentPath);
190 CurrentExecPrefix = sys::path::parent_path(
191 sys::path::parent_path(CurrentPath)).str();
192
193 // Check to see if we are inside a development tree by comparing to possible
Daniel Dunbar40d65dc2012-05-15 22:07:18 +0000194 // locations (prefix style or CMake style).
195 if (sys::fs::equivalent(CurrentExecPrefix,
196 Twine(LLVM_OBJ_ROOT) + "/" + LLVM_BUILDMODE)) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000197 IsInDevelopmentTree = true;
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000198 DevelopmentTreeLayout = MakefileStyle;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000199
200 // If we are in a development tree, then check if we are in a BuildTools
201 // directory. This indicates we are built for the build triple, but we
202 // always want to provide information for the host triple.
203 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
204 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
205 } else {
206 ActiveObjRoot = LLVM_OBJ_ROOT;
207 }
Daniel Dunbar40d65dc2012-05-15 22:07:18 +0000208 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000209 IsInDevelopmentTree = true;
210 DevelopmentTreeLayout = CMakeStyle;
211 ActiveObjRoot = LLVM_OBJ_ROOT;
Daniel Dunbar40d65dc2012-05-15 22:07:18 +0000212 } else if (sys::fs::equivalent(CurrentExecPrefix,
213 Twine(LLVM_OBJ_ROOT) + "/bin")) {
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000214 IsInDevelopmentTree = true;
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000215 DevelopmentTreeLayout = CMakeBuildModeStyle;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000216 ActiveObjRoot = LLVM_OBJ_ROOT;
217 } else {
218 IsInDevelopmentTree = false;
Duncan Sands9778ad52012-02-23 08:25:25 +0000219 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000220 }
221
222 // Compute various directory locations based on the derived location
223 // information.
224 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
225 std::string ActiveIncludeOption;
226 if (IsInDevelopmentTree) {
227 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
228 ActivePrefix = CurrentExecPrefix;
229
230 // CMake organizes the products differently than a normal prefix style
231 // layout.
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000232 switch (DevelopmentTreeLayout) {
233 case MakefileStyle:
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000234 ActiveBinDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/bin";
235 ActiveLibDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/lib";
Peter Collingbourneb56900a2012-01-26 01:31:38 +0000236 break;
237 case CMakeStyle:
238 ActiveBinDir = ActiveObjRoot + "/bin";
239 ActiveLibDir = ActiveObjRoot + "/lib";
240 break;
241 case CMakeBuildModeStyle:
242 ActiveBinDir = ActiveObjRoot + "/bin/" + LLVM_BUILDMODE;
243 ActiveLibDir = ActiveObjRoot + "/lib/" + LLVM_BUILDMODE;
244 break;
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000245 }
246
247 // We need to include files from both the source and object trees.
248 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
249 "-I" + ActiveObjRoot + "/include");
250 } else {
251 ActivePrefix = CurrentExecPrefix;
252 ActiveIncludeDir = ActivePrefix + "/include";
253 ActiveBinDir = ActivePrefix + "/bin";
254 ActiveLibDir = ActivePrefix + "/lib";
255 ActiveIncludeOption = "-I" + ActiveIncludeDir;
256 }
257
258 raw_ostream &OS = outs();
259 for (int i = 1; i != argc; ++i) {
260 StringRef Arg = argv[i];
261
262 if (Arg.startswith("-")) {
263 HasAnyOption = true;
264 if (Arg == "--version") {
265 OS << PACKAGE_VERSION << '\n';
266 } else if (Arg == "--prefix") {
267 OS << ActivePrefix << '\n';
268 } else if (Arg == "--bindir") {
269 OS << ActiveBinDir << '\n';
270 } else if (Arg == "--includedir") {
271 OS << ActiveIncludeDir << '\n';
272 } else if (Arg == "--libdir") {
273 OS << ActiveLibDir << '\n';
274 } else if (Arg == "--cppflags") {
275 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
276 } else if (Arg == "--cflags") {
277 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
278 } else if (Arg == "--cxxflags") {
279 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
280 } else if (Arg == "--ldflags") {
281 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS
282 << ' ' << LLVM_SYSTEM_LIBS << '\n';
283 } else if (Arg == "--libs") {
284 PrintLibs = true;
285 } else if (Arg == "--libnames") {
286 PrintLibNames = true;
287 } else if (Arg == "--libfiles") {
288 PrintLibFiles = true;
289 } else if (Arg == "--components") {
290 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
Daniel Dunbarb5cd41e2012-05-15 18:44:17 +0000291 // Only include non-installed components when in a development tree.
292 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
293 continue;
294
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000295 OS << ' ';
296 OS << AvailableComponents[j].Name;
297 }
298 OS << '\n';
299 } else if (Arg == "--targets-built") {
Daniel Dunbar275dd942011-12-16 00:04:43 +0000300 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000301 } else if (Arg == "--host-target") {
302 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n';
303 } else if (Arg == "--build-mode") {
304 OS << LLVM_BUILDMODE << '\n';
305 } else if (Arg == "--obj-root") {
306 OS << LLVM_OBJ_ROOT << '\n';
307 } else if (Arg == "--src-root") {
308 OS << LLVM_SRC_ROOT << '\n';
309 } else {
310 usage();
311 }
312 } else {
313 Components.push_back(Arg);
314 }
315 }
316
317 if (!HasAnyOption)
318 usage();
319
320 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbar8033f612011-12-12 18:22:04 +0000321 // If no components were specified, default to "all".
322 if (Components.empty())
323 Components.push_back("all");
324
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000325 // Construct the list of all the required libraries.
326 std::vector<StringRef> RequiredLibs;
Daniel Dunbarb5cd41e2012-05-15 18:44:17 +0000327 ComputeLibsForComponents(Components, RequiredLibs,
328 /*IncludeNonInstalled=*/IsInDevelopmentTree);
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000329
330 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
331 StringRef Lib = RequiredLibs[i];
332 if (i)
333 OS << ' ';
334
335 if (PrintLibNames) {
336 OS << Lib;
337 } else if (PrintLibFiles) {
338 OS << ActiveLibDir << '/' << Lib;
339 } else if (PrintLibs) {
340 // If this is a typical library name, include it using -l.
341 if (Lib.startswith("lib") && Lib.endswith(".a")) {
342 OS << "-l" << Lib.slice(3, Lib.size()-2);
343 continue;
344 }
345
346 // Otherwise, print the full path.
347 OS << ActiveLibDir << '/' << Lib;
348 }
349 }
350 OS << '\n';
351 } else if (!Components.empty()) {
352 errs() << "llvm-config: error: components given, but unused\n\n";
353 usage();
354 }
355
356 return 0;
357}