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/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"
28 #include "llvm/Support/raw_ostream.h"
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"
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.
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"
47 /// \brief Traverse a single component adding to the topological ordering in
48 /// \arg RequiredLibs.
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.
54 static 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!");
62 // Add to the visited table.
63 if (!VisitedComponents.insert(AC).second) {
64 // We are done if the component has already been visited.
68 // Otherwise, visit all the dependencies.
69 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
70 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
74 // Add to the required library list.
76 RequiredLibs.push_back(AC->Library);
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).
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.
86 void ComputeLibsForComponents(const std::vector<StringRef> &Components,
87 std::vector<StringRef> &RequiredLibs) {
88 std::set<AvailableComponent*> VisitedComponents;
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;
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();
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]
109 VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
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());
122 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
124 Get various configuration information needed to compile programs which use\n\
125 LLVM. 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\
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\
149 Typical components:\n\
150 all All LLVM libraries (default).\n\
151 engine Either a native JIT or a bitcode interpreter.\n";
155 /// \brief Compute the path to the main executable.
156 llvm::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);
163 int main(int argc, char **argv) {
164 std::vector<StringRef> Components;
165 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
166 bool HasAnyOption = false;
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
172 bool IsInDevelopmentTree;
173 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
174 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]).str());
175 std::string CurrentExecPrefix;
176 std::string ActiveObjRoot;
178 // Create an absolute path, and pop up one directory (we expect to be inside a
180 sys::fs::make_absolute(CurrentPath);
181 CurrentExecPrefix = sys::path::parent_path(
182 sys::path::parent_path(CurrentPath)).str();
184 // Check to see if we are inside a development tree by comparing to possible
185 // locations (prefix style or CMake style). This could be wrong in the face of
186 // symbolic links, but is good enough.
187 if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/" + LLVM_BUILDMODE) {
188 IsInDevelopmentTree = true;
189 DevelopmentTreeLayout = MakefileStyle;
191 // If we are in a development tree, then check if we are in a BuildTools
192 // directory. This indicates we are built for the build triple, but we
193 // always want to provide information for the host triple.
194 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
195 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
197 ActiveObjRoot = LLVM_OBJ_ROOT;
199 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT)) {
200 IsInDevelopmentTree = true;
201 DevelopmentTreeLayout = CMakeStyle;
202 ActiveObjRoot = LLVM_OBJ_ROOT;
203 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/bin") {
204 IsInDevelopmentTree = true;
205 DevelopmentTreeLayout = CMakeBuildModeStyle;
206 ActiveObjRoot = LLVM_OBJ_ROOT;
208 IsInDevelopmentTree = false;
211 // Compute various directory locations based on the derived location
213 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
214 std::string ActiveIncludeOption;
215 if (IsInDevelopmentTree) {
216 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
217 ActivePrefix = CurrentExecPrefix;
219 // CMake organizes the products differently than a normal prefix style
221 switch (DevelopmentTreeLayout) {
223 ActiveBinDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/bin";
224 ActiveLibDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/lib";
227 ActiveBinDir = ActiveObjRoot + "/bin";
228 ActiveLibDir = ActiveObjRoot + "/lib";
230 case CMakeBuildModeStyle:
231 ActiveBinDir = ActiveObjRoot + "/bin/" + LLVM_BUILDMODE;
232 ActiveLibDir = ActiveObjRoot + "/lib/" + LLVM_BUILDMODE;
236 // We need to include files from both the source and object trees.
237 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
238 "-I" + ActiveObjRoot + "/include");
240 ActivePrefix = CurrentExecPrefix;
241 ActiveIncludeDir = ActivePrefix + "/include";
242 ActiveBinDir = ActivePrefix + "/bin";
243 ActiveLibDir = ActivePrefix + "/lib";
244 ActiveIncludeOption = "-I" + ActiveIncludeDir;
247 raw_ostream &OS = outs();
248 for (int i = 1; i != argc; ++i) {
249 StringRef Arg = argv[i];
251 if (Arg.startswith("-")) {
253 if (Arg == "--version") {
254 OS << PACKAGE_VERSION << '\n';
255 } else if (Arg == "--prefix") {
256 OS << ActivePrefix << '\n';
257 } else if (Arg == "--bindir") {
258 OS << ActiveBinDir << '\n';
259 } else if (Arg == "--includedir") {
260 OS << ActiveIncludeDir << '\n';
261 } else if (Arg == "--libdir") {
262 OS << ActiveLibDir << '\n';
263 } else if (Arg == "--cppflags") {
264 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
265 } else if (Arg == "--cflags") {
266 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
267 } else if (Arg == "--cxxflags") {
268 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
269 } else if (Arg == "--ldflags") {
270 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS
271 << ' ' << LLVM_SYSTEM_LIBS << '\n';
272 } else if (Arg == "--libs") {
274 } else if (Arg == "--libnames") {
275 PrintLibNames = true;
276 } else if (Arg == "--libfiles") {
277 PrintLibFiles = true;
278 } else if (Arg == "--components") {
279 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
281 OS << AvailableComponents[j].Name;
284 } else if (Arg == "--targets-built") {
285 OS << LLVM_TARGETS_BUILT << '\n';
286 } else if (Arg == "--host-target") {
287 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n';
288 } else if (Arg == "--build-mode") {
289 OS << LLVM_BUILDMODE << '\n';
290 } else if (Arg == "--obj-root") {
291 OS << LLVM_OBJ_ROOT << '\n';
292 } else if (Arg == "--src-root") {
293 OS << LLVM_SRC_ROOT << '\n';
298 Components.push_back(Arg);
305 if (PrintLibs || PrintLibNames || PrintLibFiles) {
306 // If no components were specified, default to "all".
307 if (Components.empty())
308 Components.push_back("all");
310 // Construct the list of all the required libraries.
311 std::vector<StringRef> RequiredLibs;
312 ComputeLibsForComponents(Components, RequiredLibs);
314 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
315 StringRef Lib = RequiredLibs[i];
321 } else if (PrintLibFiles) {
322 OS << ActiveLibDir << '/' << Lib;
323 } else if (PrintLibs) {
324 // If this is a typical library name, include it using -l.
325 if (Lib.startswith("lib") && Lib.endswith(".a")) {
326 OS << "-l" << Lib.slice(3, Lib.size()-2);
330 // Otherwise, print the full path.
331 OS << ActiveLibDir << '/' << Lib;
335 } else if (!Components.empty()) {
336 errs() << "llvm-config: error: components given, but unused\n\n";