llvm-config.cpp revision b5cd41e26f89aad2f2dc4f5dc37577f7abf8528a
1//===-- 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" 28#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 bool IncludeNonInstalled) { 59 // 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 69 // Only include non-installed components if requested. 70 if (!AC->IsInstalled && !IncludeNonInstalled) 71 return; 72 73 // Otherwise, visit all the dependencies. 74 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) { 75 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents, 76 RequiredLibs, IncludeNonInstalled); 77 } 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. 91/// \param IncludeNonInstalled - Whether non-installed components should be 92/// reported. 93void ComputeLibsForComponents(const std::vector<StringRef> &Components, 94 std::vector<StringRef> &RequiredLibs, 95 bool IncludeNonInstalled) { 96 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, 118 RequiredLibs, IncludeNonInstalled); 119 } 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\ 150 --libs Libraries needed to link against LLVM components.\n\ 151 --libnames Bare library names for in-tree builds.\n\ 152 --libfiles Fully qualified library filenames for makefile depends.\n\ 153 --components List of all possible components.\n\ 154 --targets-built List of all targets currently built.\n\ 155 --host-target Target triple used to configure LLVM.\n\ 156 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\ 157Typical components:\n\ 158 all All LLVM libraries (default).\n\ 159 engine Either a native JIT or a bitcode interpreter.\n"; 160 exit(1); 161} 162 163/// \brief Compute the path to the main executable. 164llvm::sys::Path GetExecutablePath(const char *Argv0) { 165 // This just needs to be some symbol in the binary; C++ doesn't 166 // allow taking the address of ::main however. 167 void *P = (void*) (intptr_t) GetExecutablePath; 168 return llvm::sys::Path::GetMainExecutable(Argv0, P); 169} 170 171int main(int argc, char **argv) { 172 std::vector<StringRef> Components; 173 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false; 174 bool HasAnyOption = false; 175 176 // llvm-config is designed to support being run both from a development tree 177 // and from an installed path. We try and auto-detect which case we are in so 178 // that we can report the correct information when run from a development 179 // tree. 180 bool IsInDevelopmentTree; 181 enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout; 182 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]).str()); 183 std::string CurrentExecPrefix; 184 std::string ActiveObjRoot; 185 186 // Create an absolute path, and pop up one directory (we expect to be inside a 187 // bin dir). 188 sys::fs::make_absolute(CurrentPath); 189 CurrentExecPrefix = sys::path::parent_path( 190 sys::path::parent_path(CurrentPath)).str(); 191 192 // Check to see if we are inside a development tree by comparing to possible 193 // locations (prefix style or CMake style). This could be wrong in the face of 194 // symbolic links, but is good enough. 195 if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/" + LLVM_BUILDMODE) { 196 IsInDevelopmentTree = true; 197 DevelopmentTreeLayout = MakefileStyle; 198 199 // If we are in a development tree, then check if we are in a BuildTools 200 // directory. This indicates we are built for the build triple, but we 201 // always want to provide information for the host triple. 202 if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") { 203 ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT); 204 } else { 205 ActiveObjRoot = LLVM_OBJ_ROOT; 206 } 207 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT)) { 208 IsInDevelopmentTree = true; 209 DevelopmentTreeLayout = CMakeStyle; 210 ActiveObjRoot = LLVM_OBJ_ROOT; 211 } else if (CurrentExecPrefix == std::string(LLVM_OBJ_ROOT) + "/bin") { 212 IsInDevelopmentTree = true; 213 DevelopmentTreeLayout = CMakeBuildModeStyle; 214 ActiveObjRoot = LLVM_OBJ_ROOT; 215 } else { 216 IsInDevelopmentTree = false; 217 DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings. 218 } 219 220 // Compute various directory locations based on the derived location 221 // information. 222 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir; 223 std::string ActiveIncludeOption; 224 if (IsInDevelopmentTree) { 225 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include"; 226 ActivePrefix = CurrentExecPrefix; 227 228 // CMake organizes the products differently than a normal prefix style 229 // layout. 230 switch (DevelopmentTreeLayout) { 231 case MakefileStyle: 232 ActiveBinDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/bin"; 233 ActiveLibDir = ActiveObjRoot + "/" + LLVM_BUILDMODE + "/lib"; 234 break; 235 case CMakeStyle: 236 ActiveBinDir = ActiveObjRoot + "/bin"; 237 ActiveLibDir = ActiveObjRoot + "/lib"; 238 break; 239 case CMakeBuildModeStyle: 240 ActiveBinDir = ActiveObjRoot + "/bin/" + LLVM_BUILDMODE; 241 ActiveLibDir = ActiveObjRoot + "/lib/" + LLVM_BUILDMODE; 242 break; 243 } 244 245 // We need to include files from both the source and object trees. 246 ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " + 247 "-I" + ActiveObjRoot + "/include"); 248 } else { 249 ActivePrefix = CurrentExecPrefix; 250 ActiveIncludeDir = ActivePrefix + "/include"; 251 ActiveBinDir = ActivePrefix + "/bin"; 252 ActiveLibDir = ActivePrefix + "/lib"; 253 ActiveIncludeOption = "-I" + ActiveIncludeDir; 254 } 255 256 raw_ostream &OS = outs(); 257 for (int i = 1; i != argc; ++i) { 258 StringRef Arg = argv[i]; 259 260 if (Arg.startswith("-")) { 261 HasAnyOption = true; 262 if (Arg == "--version") { 263 OS << PACKAGE_VERSION << '\n'; 264 } else if (Arg == "--prefix") { 265 OS << ActivePrefix << '\n'; 266 } else if (Arg == "--bindir") { 267 OS << ActiveBinDir << '\n'; 268 } else if (Arg == "--includedir") { 269 OS << ActiveIncludeDir << '\n'; 270 } else if (Arg == "--libdir") { 271 OS << ActiveLibDir << '\n'; 272 } else if (Arg == "--cppflags") { 273 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n'; 274 } else if (Arg == "--cflags") { 275 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n'; 276 } else if (Arg == "--cxxflags") { 277 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n'; 278 } else if (Arg == "--ldflags") { 279 OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS 280 << ' ' << LLVM_SYSTEM_LIBS << '\n'; 281 } else if (Arg == "--libs") { 282 PrintLibs = true; 283 } else if (Arg == "--libnames") { 284 PrintLibNames = true; 285 } else if (Arg == "--libfiles") { 286 PrintLibFiles = true; 287 } else if (Arg == "--components") { 288 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) { 289 // Only include non-installed components when in a development tree. 290 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree) 291 continue; 292 293 OS << ' '; 294 OS << AvailableComponents[j].Name; 295 } 296 OS << '\n'; 297 } else if (Arg == "--targets-built") { 298 OS << LLVM_TARGETS_BUILT << '\n'; 299 } else if (Arg == "--host-target") { 300 OS << LLVM_DEFAULT_TARGET_TRIPLE << '\n'; 301 } else if (Arg == "--build-mode") { 302 OS << LLVM_BUILDMODE << '\n'; 303 } else if (Arg == "--obj-root") { 304 OS << LLVM_OBJ_ROOT << '\n'; 305 } else if (Arg == "--src-root") { 306 OS << LLVM_SRC_ROOT << '\n'; 307 } else { 308 usage(); 309 } 310 } else { 311 Components.push_back(Arg); 312 } 313 } 314 315 if (!HasAnyOption) 316 usage(); 317 318 if (PrintLibs || PrintLibNames || PrintLibFiles) { 319 // If no components were specified, default to "all". 320 if (Components.empty()) 321 Components.push_back("all"); 322 323 // Construct the list of all the required libraries. 324 std::vector<StringRef> RequiredLibs; 325 ComputeLibsForComponents(Components, RequiredLibs, 326 /*IncludeNonInstalled=*/IsInDevelopmentTree); 327 328 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) { 329 StringRef Lib = RequiredLibs[i]; 330 if (i) 331 OS << ' '; 332 333 if (PrintLibNames) { 334 OS << Lib; 335 } else if (PrintLibFiles) { 336 OS << ActiveLibDir << '/' << Lib; 337 } else if (PrintLibs) { 338 // If this is a typical library name, include it using -l. 339 if (Lib.startswith("lib") && Lib.endswith(".a")) { 340 OS << "-l" << Lib.slice(3, Lib.size()-2); 341 continue; 342 } 343 344 // Otherwise, print the full path. 345 OS << ActiveLibDir << '/' << Lib; 346 } 347 } 348 OS << '\n'; 349 } else if (!Components.empty()) { 350 errs() << "llvm-config: error: components given, but unused\n\n"; 351 usage(); 352 } 353 354 return 0; 355} 356