1 //===-- llvm-config.cpp - LLVM project configuration utility --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This tool encapsulates information about an LLVM project configuration for 10 // use by other project's build environments (to determine installed path, 11 // available features, required libraries, etc.). 12 // 13 // Note that although this tool *may* be used by some parts of LLVM's build 14 // itself (i.e., the Makefiles use it to compute required libraries when linking 15 // tools), this tool is primarily designed to support external projects. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm/Config/llvm-config.h" 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/Support/FileSystem.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/WithColor.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include <cstdlib> 31 #include <set> 32 #include <unordered_set> 33 #include <vector> 34 35 using namespace llvm; 36 37 // Include the build time variables we can report to the user. This is generated 38 // at build time from the BuildVariables.inc.in file by the build system. 39 #include "BuildVariables.inc" 40 41 // Include the component table. This creates an array of struct 42 // AvailableComponent entries, which record the component name, library name, 43 // and required components for all of the available libraries. 44 // 45 // Not all components define a library, we also use "library groups" as a way to 46 // create entries for pseudo groups like x86 or all-targets. 47 #include "LibraryDependencies.inc" 48 49 // Built-in extensions also register their dependencies, but in a separate file, 50 // later in the process. 51 #include "ExtensionDependencies.inc" 52 53 // LinkMode determines what libraries and flags are returned by llvm-config. 54 enum LinkMode { 55 // LinkModeAuto will link with the default link mode for the installation, 56 // which is dependent on the value of LLVM_LINK_LLVM_DYLIB, and fall back 57 // to the alternative if the required libraries are not available. 58 LinkModeAuto = 0, 59 60 // LinkModeShared will link with the dynamic component libraries if they 61 // exist, and return an error otherwise. 62 LinkModeShared = 1, 63 64 // LinkModeStatic will link with the static component libraries if they 65 // exist, and return an error otherwise. 66 LinkModeStatic = 2, 67 }; 68 69 /// Traverse a single component adding to the topological ordering in 70 /// \arg RequiredLibs. 71 /// 72 /// \param Name - The component to traverse. 73 /// \param ComponentMap - A prebuilt map of component names to descriptors. 74 /// \param VisitedComponents [in] [out] - The set of already visited components. 75 /// \param RequiredLibs [out] - The ordered list of required 76 /// libraries. 77 /// \param GetComponentNames - Get the component names instead of the 78 /// library name. 79 static void VisitComponent(const std::string &Name, 80 const StringMap<AvailableComponent *> &ComponentMap, 81 std::set<AvailableComponent *> &VisitedComponents, 82 std::vector<std::string> &RequiredLibs, 83 bool IncludeNonInstalled, bool GetComponentNames, 84 const std::function<std::string(const StringRef &)> 85 *GetComponentLibraryPath, 86 std::vector<std::string> *Missing, 87 const std::string &DirSep) { 88 // Lookup the component. 89 AvailableComponent *AC = ComponentMap.lookup(Name); 90 if (!AC) { 91 errs() << "Can't find component: '" << Name << "' in the map. Available components are: "; 92 for (const auto &Component : ComponentMap) { 93 errs() << "'" << Component.first() << "' "; 94 } 95 errs() << "\n"; 96 report_fatal_error("abort"); 97 } 98 assert(AC && "Invalid component name!"); 99 100 // Add to the visited table. 101 if (!VisitedComponents.insert(AC).second) { 102 // We are done if the component has already been visited. 103 return; 104 } 105 106 // Only include non-installed components if requested. 107 if (!AC->IsInstalled && !IncludeNonInstalled) 108 return; 109 110 // Otherwise, visit all the dependencies. 111 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) { 112 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents, 113 RequiredLibs, IncludeNonInstalled, GetComponentNames, 114 GetComponentLibraryPath, Missing, DirSep); 115 } 116 117 // Special handling for the special 'extensions' component. Its content is 118 // not populated by llvm-build, but later in the process and loaded from 119 // ExtensionDependencies.inc. 120 if (Name == "extensions") { 121 for (auto const &AvailableExtension : AvailableExtensions) { 122 for (const char *const *Iter = &AvailableExtension.RequiredLibraries[0]; 123 *Iter; ++Iter) { 124 AvailableComponent *AC = ComponentMap.lookup(*Iter); 125 if (!AC) { 126 RequiredLibs.push_back(*Iter); 127 } else { 128 VisitComponent(*Iter, ComponentMap, VisitedComponents, RequiredLibs, 129 IncludeNonInstalled, GetComponentNames, 130 GetComponentLibraryPath, Missing, DirSep); 131 } 132 } 133 } 134 } 135 136 if (GetComponentNames) { 137 RequiredLibs.push_back(Name); 138 return; 139 } 140 141 // Add to the required library list. 142 if (AC->Library) { 143 if (Missing && GetComponentLibraryPath) { 144 std::string path = (*GetComponentLibraryPath)(AC->Library); 145 if (DirSep == "\\") { 146 std::replace(path.begin(), path.end(), '/', '\\'); 147 } 148 if (!sys::fs::exists(path)) 149 Missing->push_back(path); 150 } 151 RequiredLibs.push_back(AC->Library); 152 } 153 } 154 155 /// Compute the list of required libraries for a given list of 156 /// components, in an order suitable for passing to a linker (that is, libraries 157 /// appear prior to their dependencies). 158 /// 159 /// \param Components - The names of the components to find libraries for. 160 /// \param IncludeNonInstalled - Whether non-installed components should be 161 /// reported. 162 /// \param GetComponentNames - True if one would prefer the component names. 163 static std::vector<std::string> ComputeLibsForComponents( 164 const std::vector<StringRef> &Components, bool IncludeNonInstalled, 165 bool GetComponentNames, const std::function<std::string(const StringRef &)> 166 *GetComponentLibraryPath, 167 std::vector<std::string> *Missing, const std::string &DirSep) { 168 std::vector<std::string> RequiredLibs; 169 std::set<AvailableComponent *> VisitedComponents; 170 171 // Build a map of component names to information. 172 StringMap<AvailableComponent *> ComponentMap; 173 for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) { 174 AvailableComponent *AC = &AvailableComponents[i]; 175 ComponentMap[AC->Name] = AC; 176 } 177 178 // Visit the components. 179 for (unsigned i = 0, e = Components.size(); i != e; ++i) { 180 // Users are allowed to provide mixed case component names. 181 std::string ComponentLower = Components[i].lower(); 182 183 // Validate that the user supplied a valid component name. 184 if (!ComponentMap.count(ComponentLower)) { 185 llvm::errs() << "llvm-config: unknown component name: " << Components[i] 186 << "\n"; 187 exit(1); 188 } 189 190 VisitComponent(ComponentLower, ComponentMap, VisitedComponents, 191 RequiredLibs, IncludeNonInstalled, GetComponentNames, 192 GetComponentLibraryPath, Missing, DirSep); 193 } 194 195 // The list is now ordered with leafs first, we want the libraries to printed 196 // in the reverse order of dependency. 197 std::reverse(RequiredLibs.begin(), RequiredLibs.end()); 198 199 return RequiredLibs; 200 } 201 202 /* *** */ 203 204 static void usage() { 205 errs() << "\ 206 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\ 207 \n\ 208 Get various configuration information needed to compile programs which use\n\ 209 LLVM. Typically called from 'configure' scripts. Examples:\n\ 210 llvm-config --cxxflags\n\ 211 llvm-config --ldflags\n\ 212 llvm-config --libs engine bcreader scalaropts\n\ 213 \n\ 214 Options:\n\ 215 --version Print LLVM version.\n\ 216 --prefix Print the installation prefix.\n\ 217 --src-root Print the source root LLVM was built from.\n\ 218 --obj-root Print the object root used to build LLVM.\n\ 219 --bindir Directory containing LLVM executables.\n\ 220 --includedir Directory containing LLVM headers.\n\ 221 --libdir Directory containing LLVM libraries.\n\ 222 --cmakedir Directory containing LLVM cmake modules.\n\ 223 --cppflags C preprocessor flags for files that include LLVM headers.\n\ 224 --cflags C compiler flags for files that include LLVM headers.\n\ 225 --cxxflags C++ compiler flags for files that include LLVM headers.\n\ 226 --ldflags Print Linker flags.\n\ 227 --system-libs System Libraries needed to link against LLVM components.\n\ 228 --libs Libraries needed to link against LLVM components.\n\ 229 --libnames Bare library names for in-tree builds.\n\ 230 --libfiles Fully qualified library filenames for makefile depends.\n\ 231 --components List of all possible components.\n\ 232 --targets-built List of all targets currently built.\n\ 233 --host-target Target triple used to configure LLVM.\n\ 234 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\ 235 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\ 236 --build-system Print the build system used to build LLVM (always cmake).\n\ 237 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\ 238 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\ 239 --link-shared Link the components as shared libraries.\n\ 240 --link-static Link the component libraries statically.\n\ 241 --ignore-libllvm Ignore libLLVM and link component libraries instead.\n\ 242 Typical components:\n\ 243 all All LLVM libraries (default).\n\ 244 engine Either a native JIT or a bitcode interpreter.\n"; 245 exit(1); 246 } 247 248 /// Compute the path to the main executable. 249 std::string GetExecutablePath(const char *Argv0) { 250 // This just needs to be some symbol in the binary; C++ doesn't 251 // allow taking the address of ::main however. 252 void *P = (void *)(intptr_t)GetExecutablePath; 253 return llvm::sys::fs::getMainExecutable(Argv0, P); 254 } 255 256 /// Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into 257 /// the full list of components. 258 std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree, 259 const bool GetComponentNames, 260 const std::string &DirSep) { 261 std::vector<StringRef> DyLibComponents; 262 263 StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS); 264 size_t Offset = 0; 265 while (true) { 266 const size_t NextOffset = DyLibComponentsStr.find(';', Offset); 267 DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset-Offset)); 268 if (NextOffset == std::string::npos) { 269 break; 270 } 271 Offset = NextOffset + 1; 272 } 273 274 assert(!DyLibComponents.empty()); 275 276 return ComputeLibsForComponents(DyLibComponents, 277 /*IncludeNonInstalled=*/IsInDevelopmentTree, 278 GetComponentNames, nullptr, nullptr, DirSep); 279 } 280 281 int main(int argc, char **argv) { 282 std::vector<StringRef> Components; 283 bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false; 284 bool PrintSystemLibs = false, PrintSharedMode = false; 285 bool HasAnyOption = false; 286 287 // llvm-config is designed to support being run both from a development tree 288 // and from an installed path. We try and auto-detect which case we are in so 289 // that we can report the correct information when run from a development 290 // tree. 291 bool IsInDevelopmentTree; 292 enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout; 293 llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0])); 294 std::string CurrentExecPrefix; 295 std::string ActiveObjRoot; 296 297 // If CMAKE_CFG_INTDIR is given, honor it as build mode. 298 char const *build_mode = LLVM_BUILDMODE; 299 #if defined(CMAKE_CFG_INTDIR) 300 if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0')) 301 build_mode = CMAKE_CFG_INTDIR; 302 #endif 303 304 // Create an absolute path, and pop up one directory (we expect to be inside a 305 // bin dir). 306 sys::fs::make_absolute(CurrentPath); 307 CurrentExecPrefix = 308 sys::path::parent_path(sys::path::parent_path(CurrentPath)).str(); 309 310 // Check to see if we are inside a development tree by comparing to possible 311 // locations (prefix style or CMake style). 312 if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) { 313 IsInDevelopmentTree = true; 314 DevelopmentTreeLayout = CMakeStyle; 315 ActiveObjRoot = LLVM_OBJ_ROOT; 316 } else if (sys::fs::equivalent(sys::path::parent_path(CurrentExecPrefix), 317 LLVM_OBJ_ROOT)) { 318 IsInDevelopmentTree = true; 319 DevelopmentTreeLayout = CMakeBuildModeStyle; 320 ActiveObjRoot = LLVM_OBJ_ROOT; 321 } else { 322 IsInDevelopmentTree = false; 323 DevelopmentTreeLayout = CMakeStyle; // Initialized to avoid warnings. 324 } 325 326 // Compute various directory locations based on the derived location 327 // information. 328 std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir, 329 ActiveCMakeDir; 330 std::string ActiveIncludeOption; 331 if (IsInDevelopmentTree) { 332 ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include"; 333 ActivePrefix = CurrentExecPrefix; 334 335 // CMake organizes the products differently than a normal prefix style 336 // layout. 337 switch (DevelopmentTreeLayout) { 338 case CMakeStyle: 339 ActiveBinDir = ActiveObjRoot + "/bin"; 340 ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX; 341 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm"; 342 break; 343 case CMakeBuildModeStyle: 344 // FIXME: Should we consider the build-mode-specific path as the prefix? 345 ActivePrefix = ActiveObjRoot; 346 ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin"; 347 ActiveLibDir = 348 ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX; 349 // The CMake directory isn't separated by build mode. 350 ActiveCMakeDir = 351 ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX + "/cmake/llvm"; 352 break; 353 } 354 355 // We need to include files from both the source and object trees. 356 ActiveIncludeOption = 357 ("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include"); 358 } else { 359 ActivePrefix = CurrentExecPrefix; 360 { 361 SmallString<256> Path(LLVM_INSTALL_INCLUDEDIR); 362 sys::fs::make_absolute(ActivePrefix, Path); 363 ActiveIncludeDir = std::string(Path.str()); 364 } 365 { 366 SmallString<256> Path(LLVM_INSTALL_BINDIR); 367 sys::fs::make_absolute(ActivePrefix, Path); 368 ActiveBinDir = std::string(Path.str()); 369 } 370 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX; 371 ActiveCMakeDir = ActiveLibDir + "/cmake/llvm"; 372 ActiveIncludeOption = "-I" + ActiveIncludeDir; 373 } 374 375 /// We only use `shared library` mode in cases where the static library form 376 /// of the components provided are not available; note however that this is 377 /// skipped if we're run from within the build dir. However, once installed, 378 /// we still need to provide correct output when the static archives are 379 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present 380 /// in the first place. This can't be done at configure/build time. 381 382 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt, 383 StaticPrefix, StaticDir = "lib"; 384 std::string DirSep = "/"; 385 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE)); 386 if (HostTriple.isOSWindows()) { 387 SharedExt = "dll"; 388 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll"; 389 if (HostTriple.isOSCygMing()) { 390 SharedPrefix = "lib"; 391 StaticExt = "a"; 392 StaticPrefix = "lib"; 393 } else { 394 StaticExt = "lib"; 395 DirSep = "\\"; 396 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\'); 397 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\'); 398 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\'); 399 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\'); 400 std::replace(ActiveCMakeDir.begin(), ActiveCMakeDir.end(), '/', '\\'); 401 std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/', 402 '\\'); 403 } 404 SharedDir = ActiveBinDir; 405 StaticDir = ActiveLibDir; 406 } else if (HostTriple.isOSDarwin()) { 407 SharedExt = "dylib"; 408 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib"; 409 StaticExt = "a"; 410 StaticDir = SharedDir = ActiveLibDir; 411 StaticPrefix = SharedPrefix = "lib"; 412 } else { 413 // default to the unix values: 414 SharedExt = "so"; 415 SharedVersionedExt = LLVM_DYLIB_VERSION ".so"; 416 StaticExt = "a"; 417 StaticDir = SharedDir = ActiveLibDir; 418 StaticPrefix = SharedPrefix = "lib"; 419 } 420 421 const bool BuiltDyLib = !!LLVM_ENABLE_DYLIB; 422 423 /// CMake style shared libs, ie each component is in a shared library. 424 const bool BuiltSharedLibs = !!LLVM_ENABLE_SHARED; 425 426 bool DyLibExists = false; 427 const std::string DyLibName = 428 (SharedPrefix + "LLVM-" + SharedVersionedExt).str(); 429 430 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned 431 // for "--libs", etc, if they exist. This behaviour can be overridden with 432 // --link-static or --link-shared. 433 bool LinkDyLib = !!LLVM_LINK_DYLIB; 434 435 if (BuiltDyLib) { 436 std::string path((SharedDir + DirSep + DyLibName).str()); 437 if (DirSep == "\\") { 438 std::replace(path.begin(), path.end(), '/', '\\'); 439 } 440 DyLibExists = sys::fs::exists(path); 441 if (!DyLibExists) { 442 // The shared library does not exist: don't error unless the user 443 // explicitly passes --link-shared. 444 LinkDyLib = false; 445 } 446 } 447 LinkMode LinkMode = 448 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto; 449 450 /// Get the component's library name without the lib prefix and the 451 /// extension. Returns true if Lib is in a recognized format. 452 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib, 453 StringRef &Out) { 454 if (Lib.startswith("lib")) { 455 unsigned FromEnd; 456 if (Lib.endswith(StaticExt)) { 457 FromEnd = StaticExt.size() + 1; 458 } else if (Lib.endswith(SharedExt)) { 459 FromEnd = SharedExt.size() + 1; 460 } else { 461 FromEnd = 0; 462 } 463 464 if (FromEnd != 0) { 465 Out = Lib.slice(3, Lib.size() - FromEnd); 466 return true; 467 } 468 } 469 470 return false; 471 }; 472 /// Maps Unixizms to the host platform. 473 auto GetComponentLibraryFileName = [&](const StringRef &Lib, 474 const bool Shared) { 475 std::string LibFileName; 476 if (Shared) { 477 if (Lib == DyLibName) { 478 // Treat the DyLibName specially. It is not a component library and 479 // already has the necessary prefix and suffix (e.g. `.so`) added so 480 // just return it unmodified. 481 assert(Lib.endswith(SharedExt) && "DyLib is missing suffix"); 482 LibFileName = std::string(Lib); 483 } else { 484 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str(); 485 } 486 } else { 487 // default to static 488 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str(); 489 } 490 491 return LibFileName; 492 }; 493 /// Get the full path for a possibly shared component library. 494 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) { 495 auto LibFileName = GetComponentLibraryFileName(Name, Shared); 496 if (Shared) { 497 return (SharedDir + DirSep + LibFileName).str(); 498 } else { 499 return (StaticDir + DirSep + LibFileName).str(); 500 } 501 }; 502 503 raw_ostream &OS = outs(); 504 for (int i = 1; i != argc; ++i) { 505 StringRef Arg = argv[i]; 506 507 if (Arg.startswith("-")) { 508 HasAnyOption = true; 509 if (Arg == "--version") { 510 OS << PACKAGE_VERSION << '\n'; 511 } else if (Arg == "--prefix") { 512 OS << ActivePrefix << '\n'; 513 } else if (Arg == "--bindir") { 514 OS << ActiveBinDir << '\n'; 515 } else if (Arg == "--includedir") { 516 OS << ActiveIncludeDir << '\n'; 517 } else if (Arg == "--libdir") { 518 OS << ActiveLibDir << '\n'; 519 } else if (Arg == "--cmakedir") { 520 OS << ActiveCMakeDir << '\n'; 521 } else if (Arg == "--cppflags") { 522 OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n'; 523 } else if (Arg == "--cflags") { 524 OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n'; 525 } else if (Arg == "--cxxflags") { 526 OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n'; 527 } else if (Arg == "--ldflags") { 528 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L") 529 << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n'; 530 } else if (Arg == "--system-libs") { 531 PrintSystemLibs = true; 532 } else if (Arg == "--libs") { 533 PrintLibs = true; 534 } else if (Arg == "--libnames") { 535 PrintLibNames = true; 536 } else if (Arg == "--libfiles") { 537 PrintLibFiles = true; 538 } else if (Arg == "--components") { 539 /// If there are missing static archives and a dylib was 540 /// built, print LLVM_DYLIB_COMPONENTS instead of everything 541 /// in the manifest. 542 std::vector<std::string> Components; 543 for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) { 544 // Only include non-installed components when in a development tree. 545 if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree) 546 continue; 547 548 Components.push_back(AvailableComponents[j].Name); 549 if (AvailableComponents[j].Library && !IsInDevelopmentTree) { 550 std::string path( 551 GetComponentLibraryPath(AvailableComponents[j].Library, false)); 552 if (DirSep == "\\") { 553 std::replace(path.begin(), path.end(), '/', '\\'); 554 } 555 if (DyLibExists && !sys::fs::exists(path)) { 556 Components = 557 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep); 558 llvm::sort(Components); 559 break; 560 } 561 } 562 } 563 564 for (unsigned I = 0; I < Components.size(); ++I) { 565 if (I) { 566 OS << ' '; 567 } 568 569 OS << Components[I]; 570 } 571 OS << '\n'; 572 } else if (Arg == "--targets-built") { 573 OS << LLVM_TARGETS_BUILT << '\n'; 574 } else if (Arg == "--host-target") { 575 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n'; 576 } else if (Arg == "--build-mode") { 577 OS << build_mode << '\n'; 578 } else if (Arg == "--assertion-mode") { 579 #if defined(NDEBUG) 580 OS << "OFF\n"; 581 #else 582 OS << "ON\n"; 583 #endif 584 } else if (Arg == "--build-system") { 585 OS << LLVM_BUILD_SYSTEM << '\n'; 586 } else if (Arg == "--has-rtti") { 587 OS << (LLVM_HAS_RTTI ? "YES" : "NO") << '\n'; 588 } else if (Arg == "--shared-mode") { 589 PrintSharedMode = true; 590 } else if (Arg == "--obj-root") { 591 OS << ActivePrefix << '\n'; 592 } else if (Arg == "--src-root") { 593 OS << LLVM_SRC_ROOT << '\n'; 594 } else if (Arg == "--ignore-libllvm") { 595 LinkDyLib = false; 596 LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto; 597 } else if (Arg == "--link-shared") { 598 LinkMode = LinkModeShared; 599 } else if (Arg == "--link-static") { 600 LinkMode = LinkModeStatic; 601 } else { 602 usage(); 603 } 604 } else { 605 Components.push_back(Arg); 606 } 607 } 608 609 if (!HasAnyOption) 610 usage(); 611 612 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) { 613 WithColor::error(errs(), "llvm-config") << DyLibName << " is missing\n"; 614 return 1; 615 } 616 617 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs || 618 PrintSharedMode) { 619 620 if (PrintSharedMode && BuiltSharedLibs) { 621 OS << "shared\n"; 622 return 0; 623 } 624 625 // If no components were specified, default to "all". 626 if (Components.empty()) 627 Components.push_back("all"); 628 629 // Construct the list of all the required libraries. 630 std::function<std::string(const StringRef &)> 631 GetComponentLibraryPathFunction = [&](const StringRef &Name) { 632 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared); 633 }; 634 std::vector<std::string> MissingLibs; 635 std::vector<std::string> RequiredLibs = ComputeLibsForComponents( 636 Components, 637 /*IncludeNonInstalled=*/IsInDevelopmentTree, false, 638 &GetComponentLibraryPathFunction, &MissingLibs, DirSep); 639 if (!MissingLibs.empty()) { 640 switch (LinkMode) { 641 case LinkModeShared: 642 if (LinkDyLib && !BuiltSharedLibs) 643 break; 644 // Using component shared libraries. 645 for (auto &Lib : MissingLibs) 646 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n"; 647 return 1; 648 case LinkModeAuto: 649 if (DyLibExists) { 650 LinkMode = LinkModeShared; 651 break; 652 } 653 WithColor::error(errs(), "llvm-config") 654 << "component libraries and shared library\n\n"; 655 LLVM_FALLTHROUGH; 656 case LinkModeStatic: 657 for (auto &Lib : MissingLibs) 658 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n"; 659 return 1; 660 } 661 } else if (LinkMode == LinkModeAuto) { 662 LinkMode = LinkModeStatic; 663 } 664 665 if (PrintSharedMode) { 666 std::unordered_set<std::string> FullDyLibComponents; 667 std::vector<std::string> DyLibComponents = 668 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep); 669 670 for (auto &Component : DyLibComponents) { 671 FullDyLibComponents.insert(Component); 672 } 673 DyLibComponents.clear(); 674 675 for (auto &Lib : RequiredLibs) { 676 if (!FullDyLibComponents.count(Lib)) { 677 OS << "static\n"; 678 return 0; 679 } 680 } 681 FullDyLibComponents.clear(); 682 683 if (LinkMode == LinkModeShared) { 684 OS << "shared\n"; 685 return 0; 686 } else { 687 OS << "static\n"; 688 return 0; 689 } 690 } 691 692 if (PrintLibs || PrintLibNames || PrintLibFiles) { 693 694 auto PrintForLib = [&](const StringRef &Lib) { 695 const bool Shared = LinkMode == LinkModeShared; 696 if (PrintLibNames) { 697 OS << GetComponentLibraryFileName(Lib, Shared); 698 } else if (PrintLibFiles) { 699 OS << GetComponentLibraryPath(Lib, Shared); 700 } else if (PrintLibs) { 701 // On Windows, output full path to library without parameters. 702 // Elsewhere, if this is a typical library name, include it using -l. 703 if (HostTriple.isWindowsMSVCEnvironment()) { 704 OS << GetComponentLibraryPath(Lib, Shared); 705 } else { 706 StringRef LibName; 707 if (GetComponentLibraryNameSlice(Lib, LibName)) { 708 // Extract library name (remove prefix and suffix). 709 OS << "-l" << LibName; 710 } else { 711 // Lib is already a library name without prefix and suffix. 712 OS << "-l" << Lib; 713 } 714 } 715 } 716 }; 717 718 if (LinkMode == LinkModeShared && LinkDyLib) { 719 PrintForLib(DyLibName); 720 } else { 721 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) { 722 auto Lib = RequiredLibs[i]; 723 if (i) 724 OS << ' '; 725 726 PrintForLib(Lib); 727 } 728 } 729 OS << '\n'; 730 } 731 732 // Print SYSTEM_LIBS after --libs. 733 // FIXME: Each LLVM component may have its dependent system libs. 734 if (PrintSystemLibs) { 735 // Output system libraries only if linking against a static 736 // library (since the shared library links to all system libs 737 // already) 738 OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n'; 739 } 740 } else if (!Components.empty()) { 741 WithColor::error(errs(), "llvm-config") 742 << "components given, but unused\n\n"; 743 usage(); 744 } 745 746 return 0; 747 } 748