blob: 2bb0aebc80eaa28df20964127c62ca497f377a76 [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"
Daniel Dunbarcb497b82011-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,
57 std::vector<StringRef> &RequiredLibs) {
58 // Lookup the component.
59 AvailableComponent *AC = ComponentMap.lookup(Name);
60 assert(AC && "Invalid component name!");
61
62 // Add to the visited table.
63 if (!VisitedComponents.insert(AC).second) {
64 // We are done if the component has already been visited.
65 return;
66 }
67
68 // Otherwise, visit all the dependencies.
69 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
70 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
71 RequiredLibs);
72 }
73
74 // Add to the required library list.
75 if (AC->Library)
76 RequiredLibs.push_back(AC->Library);
77}
78
79/// \brief Compute the list of required libraries for a given list of
80/// components, in an order suitable for passing to a linker (that is, libraries
81/// appear prior to their dependencies).
82///
83/// \param Components - The names of the components to find libraries for.
84/// \param RequiredLibs [out] - On return, the ordered list of libraries that
85/// are required to link the given components.
86void ComputeLibsForComponents(const std::vector<StringRef> &Components,
87 std::vector<StringRef> &RequiredLibs) {
88 std::set<AvailableComponent*> VisitedComponents;
89
90 // Build a map of component names to information.
91 StringMap<AvailableComponent*> ComponentMap;
92 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
93 AvailableComponent *AC = &AvailableComponents[i];
94 ComponentMap[AC->Name] = AC;
95 }
96
97 // Visit the components.
98 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
99 // Users are allowed to provide mixed case component names.
100 std::string ComponentLower = Components[i].lower();
101
102 // Validate that the user supplied a valid component name.
103 if (!ComponentMap.count(ComponentLower)) {
104 llvm::errs() << "llvm-config: unknown component name: " << Components[i]
105 << "\n";
106 exit(1);
107 }
108
109 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
110 RequiredLibs);
111 }
112
113 // The list is now ordered with leafs first, we want the libraries to printed
114 // in the reverse order of dependency.
115 std::reverse(RequiredLibs.begin(), RequiredLibs.end());
116}
117
118/* *** */
119
120void usage() {
121 errs() << "\
122usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
123\n\
124Get various configuration information needed to compile programs which use\n\
125LLVM. Typically called from 'configure' scripts. Examples:\n\
126 llvm-config --cxxflags\n\
127 llvm-config --ldflags\n\
128 llvm-config --libs engine bcreader scalaropts\n\
129\n\
130Options:\n\
131 --version Print LLVM version.\n\
132 --prefix Print the installation prefix.\n\
133 --src-root Print the source root LLVM was built from.\n\
134 --obj-root Print the object root used to build LLVM.\n\
135 --bindir Directory containing LLVM executables.\n\
136 --includedir Directory containing LLVM headers.\n\
137 --libdir Directory containing LLVM libraries.\n\
138 --cppflags C preprocessor flags for files that include LLVM headers.\n\
139 --cflags C compiler flags for files that include LLVM headers.\n\
140 --cxxflags C++ compiler flags for files that include LLVM headers.\n\
141 --ldflags Print Linker flags.\n\
142 --libs Libraries needed to link against LLVM components.\n\
143 --libnames Bare library names for in-tree builds.\n\
144 --libfiles Fully qualified library filenames for makefile depends.\n\
145 --components List of all possible components.\n\
146 --targets-built List of all targets currently built.\n\
147 --host-target Target triple used to configure LLVM.\n\
148 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
149Typical components:\n\
150 all All LLVM libraries (default).\n\
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000151 engine Either a native JIT or a bitcode interpreter.\n";
152 exit(1);
153}
154
155/// \brief Compute the path to the main executable.
156llvm::sys::Path GetExecutablePath(const char *Argv0) {
157 // This just needs to be some symbol in the binary; C++ doesn't
158 // allow taking the address of ::main however.
159 void *P = (void*) (intptr_t) GetExecutablePath;
160 return llvm::sys::Path::GetMainExecutable(Argv0, P);
161}
162
163int main(int argc, char **argv) {
164 std::vector<StringRef> Components;
165 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
166 bool HasAnyOption = false;
167
168 // llvm-config is designed to support being run both from a development tree
169 // and from an installed path. We try and auto-detect which case we are in so
170 // that we can report the correct information when run from a development
171 // tree.
172 bool IsInDevelopmentTree, DevelopmentTreeLayoutIsCMakeStyle;
173 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]).str());
174 std::string CurrentExecPrefix;
175 std::string ActiveObjRoot;
176
177 // Create an absolute path, and pop up one directory (we expect to be inside a
178 // bin dir).
179 sys::fs::make_absolute(CurrentPath);
180 CurrentExecPrefix = sys::path::parent_path(
181 sys::path::parent_path(CurrentPath)).str();
182
183 // Check to see if we are inside a development tree by comparing to possible
184 // locations (prefix style or CMake style). This could be wrong in the face of
185 // symbolic links, but is good enough.
186 if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/" + LLVM_BUILDMODE) {
187 IsInDevelopmentTree = true;
188 DevelopmentTreeLayoutIsCMakeStyle = false;
189
190 // If we are in a development tree, then check if we are in a BuildTools
191 // directory. This indicates we are built for the build triple, but we
192 // always want to provide information for the host triple.
193 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
194 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
195 } else {
196 ActiveObjRoot = LLVM_OBJ_ROOT;
197 }
198 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/bin") {
199 IsInDevelopmentTree = true;
200 DevelopmentTreeLayoutIsCMakeStyle = true;
201 ActiveObjRoot = LLVM_OBJ_ROOT;
202 } else {
203 IsInDevelopmentTree = false;
204 }
205
206 // Compute various directory locations based on the derived location
207 // information.
208 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
209 std::string ActiveIncludeOption;
210 if (IsInDevelopmentTree) {
211 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
212 ActivePrefix = CurrentExecPrefix;
213
214 // CMake organizes the products differently than a normal prefix style
215 // layout.
216 if (DevelopmentTreeLayoutIsCMakeStyle) {
217 ActiveBinDir = ActiveObjRoot + "/bin/" + LLVM_BUILDMODE;
218 ActiveLibDir = ActiveObjRoot + "/lib/" + LLVM_BUILDMODE;
219 } else {
220 ActiveBinDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/bin";
221 ActiveLibDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/lib";
222 }
223
224 // We need to include files from both the source and object trees.
225 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
226 "-I" + ActiveObjRoot + "/include");
227 } else {
228 ActivePrefix = CurrentExecPrefix;
229 ActiveIncludeDir = ActivePrefix + "/include";
230 ActiveBinDir = ActivePrefix + "/bin";
231 ActiveLibDir = ActivePrefix + "/lib";
232 ActiveIncludeOption = "-I" + ActiveIncludeDir;
233 }
234
235 raw_ostream &OS = outs();
236 for (int i = 1; i != argc; ++i) {
237 StringRef Arg = argv[i];
238
239 if (Arg.startswith("-")) {
240 HasAnyOption = true;
241 if (Arg == "--version") {
242 OS << PACKAGE_VERSION << '\n';
243 } else if (Arg == "--prefix") {
244 OS << ActivePrefix << '\n';
245 } else if (Arg == "--bindir") {
246 OS << ActiveBinDir << '\n';
247 } else if (Arg == "--includedir") {
248 OS << ActiveIncludeDir << '\n';
249 } else if (Arg == "--libdir") {
250 OS << ActiveLibDir << '\n';
251 } else if (Arg == "--cppflags") {
252 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
253 } else if (Arg == "--cflags") {
254 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
255 } else if (Arg == "--cxxflags") {
256 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
257 } else if (Arg == "--ldflags") {
258 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS
259 << ' ' << LLVM_SYSTEM_LIBS << '\n';
260 } else if (Arg == "--libs") {
261 PrintLibs = true;
262 } else if (Arg == "--libnames") {
263 PrintLibNames = true;
264 } else if (Arg == "--libfiles") {
265 PrintLibFiles = true;
266 } else if (Arg == "--components") {
267 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
268 OS << ' ';
269 OS << AvailableComponents[j].Name;
270 }
271 OS << '\n';
272 } else if (Arg == "--targets-built") {
Daniel Dunbar275dd942011-12-16 00:04:43 +0000273 OS << LLVM_TARGETS_BUILT << '\n';
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000274 } else if (Arg == "--host-target") {
275 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n';
276 } else if (Arg == "--build-mode") {
277 OS << LLVM_BUILDMODE << '\n';
278 } else if (Arg == "--obj-root") {
279 OS << LLVM_OBJ_ROOT << '\n';
280 } else if (Arg == "--src-root") {
281 OS << LLVM_SRC_ROOT << '\n';
282 } else {
283 usage();
284 }
285 } else {
286 Components.push_back(Arg);
287 }
288 }
289
290 if (!HasAnyOption)
291 usage();
292
293 if (PrintLibs || PrintLibNames || PrintLibFiles) {
Daniel Dunbar8033f612011-12-12 18:22:04 +0000294 // If no components were specified, default to "all".
295 if (Components.empty())
296 Components.push_back("all");
297
Daniel Dunbarcb497b82011-12-01 20:18:09 +0000298 // Construct the list of all the required libraries.
299 std::vector<StringRef> RequiredLibs;
300 ComputeLibsForComponents(Components, RequiredLibs);
301
302 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
303 StringRef Lib = RequiredLibs[i];
304 if (i)
305 OS << ' ';
306
307 if (PrintLibNames) {
308 OS << Lib;
309 } else if (PrintLibFiles) {
310 OS << ActiveLibDir << '/' << Lib;
311 } else if (PrintLibs) {
312 // If this is a typical library name, include it using -l.
313 if (Lib.startswith("lib") && Lib.endswith(".a")) {
314 OS << "-l" << Lib.slice(3, Lib.size()-2);
315 continue;
316 }
317
318 // Otherwise, print the full path.
319 OS << ActiveLibDir << '/' << Lib;
320 }
321 }
322 OS << '\n';
323 } else if (!Components.empty()) {
324 errs() << "llvm-config: error: components given, but unused\n\n";
325 usage();
326 }
327
328 return 0;
329}