1 //===-- llvm-config.cpp - LLVM project configuration utility --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.).
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.
18 //===----------------------------------------------------------------------===//
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Triple.h"
24 #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"
29 #include "llvm/Support/raw_ostream.h"
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"
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.
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"
48 /// \brief Traverse a single component adding to the topological ordering in
49 /// \arg RequiredLibs.
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.
55 static void VisitComponent(StringRef Name,
56 const StringMap<AvailableComponent*> &ComponentMap,
57 std::set<AvailableComponent*> &VisitedComponents,
58 std::vector<StringRef> &RequiredLibs,
59 bool IncludeNonInstalled) {
60 // Lookup the component.
61 AvailableComponent *AC = ComponentMap.lookup(Name);
62 assert(AC && "Invalid component name!");
64 // Add to the visited table.
65 if (!VisitedComponents.insert(AC).second) {
66 // We are done if the component has already been visited.
70 // Only include non-installed components if requested.
71 if (!AC->IsInstalled && !IncludeNonInstalled)
74 // Otherwise, visit all the dependencies.
75 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
76 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
77 RequiredLibs, IncludeNonInstalled);
80 // Add to the required library list.
82 RequiredLibs.push_back(AC->Library);
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).
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.
92 /// \param IncludeNonInstalled - Whether non-installed components should be
94 static void ComputeLibsForComponents(const std::vector<StringRef> &Components,
95 std::vector<StringRef> &RequiredLibs,
96 bool IncludeNonInstalled) {
97 std::set<AvailableComponent*> VisitedComponents;
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;
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();
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]
118 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
119 RequiredLibs, IncludeNonInstalled);
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());
129 static void usage() {
131 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
133 Get various configuration information needed to compile programs which use\n\
134 LLVM. 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\
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 --system-libs System Libraries needed to link against LLVM components.\n\
152 --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\
159 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
160 --build-system Print the build system used to build LLVM (autoconf or cmake).\n\
161 Typical components:\n\
162 all All LLVM libraries (default).\n\
163 engine Either a native JIT or a bitcode interpreter.\n";
167 /// \brief Compute the path to the main executable.
168 std::string GetExecutablePath(const char *Argv0) {
169 // This just needs to be some symbol in the binary; C++ doesn't
170 // allow taking the address of ::main however.
171 void *P = (void*) (intptr_t) GetExecutablePath;
172 return llvm::sys::fs::getMainExecutable(Argv0, P);
175 int main(int argc, char **argv) {
176 std::vector<StringRef> Components;
177 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
178 bool PrintSystemLibs = false;
179 bool HasAnyOption = false;
181 // llvm-config is designed to support being run both from a development tree
182 // and from an installed path. We try and auto-detect which case we are in so
183 // that we can report the correct information when run from a development
185 bool IsInDevelopmentTree;
186 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
187 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
188 std::string CurrentExecPrefix;
189 std::string ActiveObjRoot;
191 // If CMAKE_CFG_INTDIR is given, honor it as build mode.
192 char const *build_mode = LLVM_BUILDMODE;
193 #if defined(CMAKE_CFG_INTDIR)
194 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
195 build_mode = CMAKE_CFG_INTDIR;
198 // Create an absolute path, and pop up one directory (we expect to be inside a
200 sys::fs::make_absolute(CurrentPath);
201 CurrentExecPrefix = sys::path::parent_path(
202 sys::path::parent_path(CurrentPath)).str();
204 // Check to see if we are inside a development tree by comparing to possible
205 // locations (prefix style or CMake style).
206 if (sys::fs::equivalent(CurrentExecPrefix,
207 Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
208 IsInDevelopmentTree = true;
209 DevelopmentTreeLayout = MakefileStyle;
211 // If we are in a development tree, then check if we are in a BuildTools
212 // directory. This indicates we are built for the build triple, but we
213 // always want to provide information for the host triple.
214 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
215 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
217 ActiveObjRoot = LLVM_OBJ_ROOT;
219 } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
220 IsInDevelopmentTree = true;
221 DevelopmentTreeLayout = CMakeStyle;
222 ActiveObjRoot = LLVM_OBJ_ROOT;
223 } else if (sys::fs::equivalent(CurrentExecPrefix,
224 Twine(LLVM_OBJ_ROOT) + "/bin")) {
225 IsInDevelopmentTree = true;
226 DevelopmentTreeLayout = CMakeBuildModeStyle;
227 ActiveObjRoot = LLVM_OBJ_ROOT;
229 IsInDevelopmentTree = false;
230 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
233 // Compute various directory locations based on the derived location
235 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
236 std::string ActiveIncludeOption;
237 if (IsInDevelopmentTree) {
238 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
239 ActivePrefix = CurrentExecPrefix;
241 // CMake organizes the products differently than a normal prefix style
243 switch (DevelopmentTreeLayout) {
245 ActivePrefix = ActiveObjRoot;
246 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
248 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
251 ActiveBinDir = ActiveObjRoot + "/bin";
252 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
254 case CMakeBuildModeStyle:
255 ActivePrefix = ActiveObjRoot;
256 ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
258 ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
262 // We need to include files from both the source and object trees.
263 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
264 "-I" + ActiveObjRoot + "/include");
266 ActivePrefix = CurrentExecPrefix;
267 ActiveIncludeDir = ActivePrefix + "/include";
268 ActiveBinDir = ActivePrefix + "/bin";
269 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
270 ActiveIncludeOption = "-I" + ActiveIncludeDir;
273 raw_ostream &OS = outs();
274 for (int i = 1; i != argc; ++i) {
275 StringRef Arg = argv[i];
277 if (Arg.startswith("-")) {
279 if (Arg == "--version") {
280 OS << PACKAGE_VERSION << '\n';
281 } else if (Arg == "--prefix") {
282 OS << ActivePrefix << '\n';
283 } else if (Arg == "--bindir") {
284 OS << ActiveBinDir << '\n';
285 } else if (Arg == "--includedir") {
286 OS << ActiveIncludeDir << '\n';
287 } else if (Arg == "--libdir") {
288 OS << ActiveLibDir << '\n';
289 } else if (Arg == "--cppflags") {
290 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
291 } else if (Arg == "--cflags") {
292 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
293 } else if (Arg == "--cxxflags") {
294 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
295 } else if (Arg == "--ldflags") {
296 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
297 } else if (Arg == "--system-libs") {
298 PrintSystemLibs = true;
299 } else if (Arg == "--libs") {
301 } else if (Arg == "--libnames") {
302 PrintLibNames = true;
303 } else if (Arg == "--libfiles") {
304 PrintLibFiles = true;
305 } else if (Arg == "--components") {
306 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
307 // Only include non-installed components when in a development tree.
308 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
312 OS << AvailableComponents[j].Name;
315 } else if (Arg == "--targets-built") {
316 OS << LLVM_TARGETS_BUILT << '\n';
317 } else if (Arg == "--host-target") {
318 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
319 } else if (Arg == "--build-mode") {
320 OS << build_mode << '\n';
321 } else if (Arg == "--assertion-mode") {
327 } else if (Arg == "--build-system") {
328 OS << LLVM_BUILD_SYSTEM << '\n';
329 } else if (Arg == "--obj-root") {
330 OS << ActivePrefix << '\n';
331 } else if (Arg == "--src-root") {
332 OS << LLVM_SRC_ROOT << '\n';
337 Components.push_back(Arg);
344 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
345 // If no components were specified, default to "all".
346 if (Components.empty())
347 Components.push_back("all");
349 // Construct the list of all the required libraries.
350 std::vector<StringRef> RequiredLibs;
351 ComputeLibsForComponents(Components, RequiredLibs,
352 /*IncludeNonInstalled=*/IsInDevelopmentTree);
354 if (PrintLibs || PrintLibNames || PrintLibFiles) {
355 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
356 StringRef Lib = RequiredLibs[i];
362 } else if (PrintLibFiles) {
363 OS << ActiveLibDir << '/' << Lib;
364 } else if (PrintLibs) {
365 // If this is a typical library name, include it using -l.
366 if (Lib.startswith("lib") && Lib.endswith(".a")) {
367 OS << "-l" << Lib.slice(3, Lib.size()-2);
371 // Otherwise, print the full path.
372 OS << ActiveLibDir << '/' << Lib;
378 // Print SYSTEM_LIBS after --libs.
379 // FIXME: Each LLVM component may have its dependent system libs.
381 OS << LLVM_SYSTEM_LIBS << '\n';
382 } else if (!Components.empty()) {
383 errs() << "llvm-config: error: components given, but unused\n\n";