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/Twine.h" 24 #include "llvm/Config/config.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/Path.h" 27 #include "llvm/Support/Program.h" 28 #include "llvm/Support/WithColor.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/TargetParser/Triple.h" 31 #include <cstdlib> 32 #include <set> 33 #include <unordered_set> 34 #include <vector> 35 36 using namespace llvm; 37 38 // Include the build time variables we can report to the user. This is generated 39 // at build time from the BuildVariables.inc.in file by the build system. 40 #include "BuildVariables.inc" 41 42 // Include the component table. This creates an array of struct 43 // AvailableComponent entries, which record the component name, library name, 44 // and required components for all of the available libraries. 45 // 46 // Not all components define a library, we also use "library groups" as a way to 47 // create entries for pseudo groups like x86 or all-targets. 48 #include "LibraryDependencies.inc" 49 50 // Built-in extensions also register their dependencies, but in a separate file, 51 // later in the process. 52 #include "ExtensionDependencies.inc" 53 54 // LinkMode determines what libraries and flags are returned by llvm-config. 55 enum LinkMode { 56 // LinkModeAuto will link with the default link mode for the installation, 57 // which is dependent on the value of LLVM_LINK_LLVM_DYLIB, and fall back 58 // to the alternative if the required libraries are not available. 59 LinkModeAuto = 0, 60 61 // LinkModeShared will link with the dynamic component libraries if they 62 // exist, and return an error otherwise. 63 LinkModeShared = 1, 64 65 // LinkModeStatic will link with the static component libraries if they 66 // exist, and return an error otherwise. 67 LinkModeStatic = 2, 68 }; 69 70 /// Traverse a single component adding to the topological ordering in 71 /// \arg RequiredLibs. 72 /// 73 /// \param Name - The component to traverse. 74 /// \param ComponentMap - A prebuilt map of component names to descriptors. 75 /// \param VisitedComponents [in] [out] - The set of already visited components. 76 /// \param RequiredLibs [out] - The ordered list of required 77 /// libraries. 78 /// \param GetComponentNames - Get the component names instead of the 79 /// library name. 80 static void VisitComponent(const std::string &Name, 81 const StringMap<AvailableComponent *> &ComponentMap, 82 std::set<AvailableComponent *> &VisitedComponents, 83 std::vector<std::string> &RequiredLibs, 84 bool IncludeNonInstalled, bool GetComponentNames, 85 const std::function<std::string(const StringRef &)> 86 *GetComponentLibraryPath, 87 std::vector<std::string> *Missing, 88 const std::string &DirSep) { 89 // Lookup the component. 90 AvailableComponent *AC = ComponentMap.lookup(Name); 91 if (!AC) { 92 errs() << "Can't find component: '" << Name << "' in the map. Available components are: "; 93 for (const auto &Component : ComponentMap) { 94 errs() << "'" << Component.first() << "' "; 95 } 96 errs() << "\n"; 97 report_fatal_error("abort"); 98 } 99 assert(AC && "Invalid component name!"); 100 101 // Add to the visited table. 102 if (!VisitedComponents.insert(AC).second) { 103 // We are done if the component has already been visited. 104 return; 105 } 106 107 // Only include non-installed components if requested. 108 if (!AC->IsInstalled && !IncludeNonInstalled) 109 return; 110 111 // Otherwise, visit all the dependencies. 112 for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) { 113 VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents, 114 RequiredLibs, IncludeNonInstalled, GetComponentNames, 115 GetComponentLibraryPath, Missing, DirSep); 116 } 117 118 // Special handling for the special 'extensions' component. Its content is 119 // not populated by llvm-build, but later in the process and loaded from 120 // ExtensionDependencies.inc. 121 if (Name == "extensions") { 122 for (auto const &AvailableExtension : AvailableExtensions) { 123 for (const char *const *Iter = &AvailableExtension.RequiredLibraries[0]; 124 *Iter; ++Iter) { 125 AvailableComponent *AC = ComponentMap.lookup(*Iter); 126 if (!AC) { 127 RequiredLibs.push_back(*Iter); 128 } else { 129 VisitComponent(*Iter, ComponentMap, VisitedComponents, RequiredLibs, 130 IncludeNonInstalled, GetComponentNames, 131 GetComponentLibraryPath, Missing, DirSep); 132 } 133 } 134 } 135 } 136 137 if (GetComponentNames) { 138 RequiredLibs.push_back(Name); 139 return; 140 } 141 142 // Add to the required library list. 143 if (AC->Library) { 144 if (Missing && GetComponentLibraryPath) { 145 std::string path = (*GetComponentLibraryPath)(AC->Library); 146 if (DirSep == "\\") { 147 std::replace(path.begin(), path.end(), '/', '\\'); 148 } 149 if (!sys::fs::exists(path)) 150 Missing->push_back(path); 151 } 152 RequiredLibs.push_back(AC->Library); 153 } 154 } 155 156 /// Compute the list of required libraries for a given list of 157 /// components, in an order suitable for passing to a linker (that is, libraries 158 /// appear prior to their dependencies). 159 /// 160 /// \param Components - The names of the components to find libraries for. 161 /// \param IncludeNonInstalled - Whether non-installed components should be 162 /// reported. 163 /// \param GetComponentNames - True if one would prefer the component names. 164 static std::vector<std::string> ComputeLibsForComponents( 165 const std::vector<StringRef> &Components, bool IncludeNonInstalled, 166 bool GetComponentNames, const std::function<std::string(const StringRef &)> 167 *GetComponentLibraryPath, 168 std::vector<std::string> *Missing, const std::string &DirSep) { 169 std::vector<std::string> RequiredLibs; 170 std::set<AvailableComponent *> VisitedComponents; 171 172 // Build a map of component names to information. 173 StringMap<AvailableComponent *> ComponentMap; 174 for (auto &AC : AvailableComponents) 175 ComponentMap[AC.Name] = &AC; 176 177 // Visit the components. 178 for (unsigned i = 0, e = Components.size(); i != e; ++i) { 179 // Users are allowed to provide mixed case component names. 180 std::string ComponentLower = Components[i].lower(); 181 182 // Validate that the user supplied a valid component name. 183 if (!ComponentMap.count(ComponentLower)) { 184 llvm::errs() << "llvm-config: unknown component name: " << Components[i] 185 << "\n"; 186 exit(1); 187 } 188 189 VisitComponent(ComponentLower, ComponentMap, VisitedComponents, 190 RequiredLibs, IncludeNonInstalled, GetComponentNames, 191 GetComponentLibraryPath, Missing, DirSep); 192 } 193 194 // The list is now ordered with leafs first, we want the libraries to printed 195 // in the reverse order of dependency. 196 std::reverse(RequiredLibs.begin(), RequiredLibs.end()); 197 198 return RequiredLibs; 199 } 200 201 /* *** */ 202 203 static void usage(bool ExitWithFailure = true) { 204 errs() << "\ 205 usage: llvm-config <OPTION>... [<COMPONENT>...]\n\ 206 \n\ 207 Get various configuration information needed to compile programs which use\n\ 208 LLVM. Typically called from 'configure' scripts. Examples:\n\ 209 llvm-config --cxxflags\n\ 210 llvm-config --ldflags\n\ 211 llvm-config --libs engine bcreader scalaropts\n\ 212 \n\ 213 Options:\n\ 214 --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\ 215 --bindir Directory containing LLVM executables.\n\ 216 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\ 217 --build-system Print the build system used to build LLVM (e.g. `cmake` or `gn`).\n\ 218 --cflags C compiler flags for files that include LLVM headers.\n\ 219 --cmakedir Directory containing LLVM CMake modules.\n\ 220 --components List of all possible components.\n\ 221 --cppflags C preprocessor flags for files that include LLVM headers.\n\ 222 --cxxflags C++ compiler flags for files that include LLVM headers.\n\ 223 --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\ 224 --help Print a summary of llvm-config arguments.\n\ 225 --host-target Target triple used to configure LLVM.\n\ 226 --ignore-libllvm Ignore libLLVM and link component libraries instead.\n\ 227 --includedir Directory containing LLVM headers.\n\ 228 --ldflags Print Linker flags.\n\ 229 --libdir Directory containing LLVM libraries.\n\ 230 --libfiles Fully qualified library filenames for makefile depends.\n\ 231 --libnames Bare library names for in-tree builds.\n\ 232 --libs Libraries needed to link against LLVM components.\n\ 233 --link-shared Link the components as shared libraries.\n\ 234 --link-static Link the component libraries statically.\n\ 235 --obj-root Print the object root used to build LLVM.\n\ 236 --prefix Print the installation prefix.\n\ 237 --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\ 238 --system-libs System Libraries needed to link against LLVM components.\n\ 239 --targets-built List of all targets currently built.\n\ 240 --version Print LLVM version.\n\ 241 Typical components:\n\ 242 all All LLVM libraries (default).\n\ 243 engine Either a native JIT or a bitcode interpreter.\n"; 244 if (ExitWithFailure) 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::vector<std::string> ActiveIncludeOptions; 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 ActiveIncludeOptions.push_back(ActiveIncludeDir); 357 ActiveIncludeOptions.push_back(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); 364 } 365 { 366 SmallString<256> Path(LLVM_TOOLS_INSTALL_DIR); 367 sys::fs::make_absolute(ActivePrefix, Path); 368 ActiveBinDir = std::string(Path); 369 } 370 ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX; 371 { 372 SmallString<256> Path(LLVM_INSTALL_PACKAGE_DIR); 373 sys::fs::make_absolute(ActivePrefix, Path); 374 ActiveCMakeDir = std::string(Path); 375 } 376 ActiveIncludeOptions.push_back(ActiveIncludeDir); 377 } 378 379 /// We only use `shared library` mode in cases where the static library form 380 /// of the components provided are not available; note however that this is 381 /// skipped if we're run from within the build dir. However, once installed, 382 /// we still need to provide correct output when the static archives are 383 /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present 384 /// in the first place. This can't be done at configure/build time. 385 386 StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt, 387 StaticPrefix, StaticDir = "lib"; 388 std::string DirSep = "/"; 389 const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE)); 390 if (HostTriple.isOSWindows()) { 391 SharedExt = "dll"; 392 SharedVersionedExt = LLVM_DYLIB_VERSION ".dll"; 393 if (HostTriple.isOSCygMing()) { 394 SharedPrefix = "lib"; 395 StaticExt = "a"; 396 StaticPrefix = "lib"; 397 } else { 398 StaticExt = "lib"; 399 DirSep = "\\"; 400 std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\'); 401 std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\'); 402 std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\'); 403 std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\'); 404 std::replace(ActiveCMakeDir.begin(), ActiveCMakeDir.end(), '/', '\\'); 405 for (auto &Include : ActiveIncludeOptions) 406 std::replace(Include.begin(), Include.end(), '/', '\\'); 407 } 408 SharedDir = ActiveBinDir; 409 StaticDir = ActiveLibDir; 410 } else if (HostTriple.isOSDarwin()) { 411 SharedExt = "dylib"; 412 SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib"; 413 StaticExt = "a"; 414 StaticDir = SharedDir = ActiveLibDir; 415 StaticPrefix = SharedPrefix = "lib"; 416 } else { 417 // default to the unix values: 418 SharedExt = "so"; 419 SharedVersionedExt = LLVM_DYLIB_VERSION ".so"; 420 StaticExt = "a"; 421 StaticDir = SharedDir = ActiveLibDir; 422 StaticPrefix = SharedPrefix = "lib"; 423 } 424 425 const bool BuiltDyLib = !!LLVM_ENABLE_DYLIB; 426 427 /// CMake style shared libs, ie each component is in a shared library. 428 const bool BuiltSharedLibs = !!LLVM_ENABLE_SHARED; 429 430 bool DyLibExists = false; 431 const std::string DyLibName = 432 (SharedPrefix + "LLVM-" + SharedVersionedExt).str(); 433 434 // If LLVM_LINK_DYLIB is ON, the single shared library will be returned 435 // for "--libs", etc, if they exist. This behaviour can be overridden with 436 // --link-static or --link-shared. 437 bool LinkDyLib = !!LLVM_LINK_DYLIB; 438 439 if (BuiltDyLib) { 440 std::string path((SharedDir + DirSep + DyLibName).str()); 441 if (DirSep == "\\") { 442 std::replace(path.begin(), path.end(), '/', '\\'); 443 } 444 DyLibExists = sys::fs::exists(path); 445 if (!DyLibExists) { 446 // The shared library does not exist: don't error unless the user 447 // explicitly passes --link-shared. 448 LinkDyLib = false; 449 } 450 } 451 LinkMode LinkMode = 452 (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto; 453 454 /// Get the component's library name without the lib prefix and the 455 /// extension. Returns true if Lib is in a recognized format. 456 auto GetComponentLibraryNameSlice = [&](const StringRef &Lib, 457 StringRef &Out) { 458 if (Lib.starts_with("lib")) { 459 unsigned FromEnd; 460 if (Lib.ends_with(StaticExt)) { 461 FromEnd = StaticExt.size() + 1; 462 } else if (Lib.ends_with(SharedExt)) { 463 FromEnd = SharedExt.size() + 1; 464 } else { 465 FromEnd = 0; 466 } 467 468 if (FromEnd != 0) { 469 Out = Lib.slice(3, Lib.size() - FromEnd); 470 return true; 471 } 472 } 473 474 return false; 475 }; 476 /// Maps Unixizms to the host platform. 477 auto GetComponentLibraryFileName = [&](const StringRef &Lib, 478 const bool Shared) { 479 std::string LibFileName; 480 if (Shared) { 481 if (Lib == DyLibName) { 482 // Treat the DyLibName specially. It is not a component library and 483 // already has the necessary prefix and suffix (e.g. `.so`) added so 484 // just return it unmodified. 485 assert(Lib.ends_with(SharedExt) && "DyLib is missing suffix"); 486 LibFileName = std::string(Lib); 487 } else { 488 LibFileName = (SharedPrefix + Lib + "." + SharedExt).str(); 489 } 490 } else { 491 // default to static 492 LibFileName = (StaticPrefix + Lib + "." + StaticExt).str(); 493 } 494 495 return LibFileName; 496 }; 497 /// Get the full path for a possibly shared component library. 498 auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) { 499 auto LibFileName = GetComponentLibraryFileName(Name, Shared); 500 if (Shared) { 501 return (SharedDir + DirSep + LibFileName).str(); 502 } else { 503 return (StaticDir + DirSep + LibFileName).str(); 504 } 505 }; 506 507 raw_ostream &OS = outs(); 508 509 // Render include paths and associated flags 510 auto RenderFlags = [&](StringRef Flags) { 511 bool First = true; 512 for (auto &Include : ActiveIncludeOptions) { 513 if (!First) 514 OS << ' '; 515 OS << "-I"; 516 sys::printArg(OS, Include, /*Quote=*/true); 517 First = false; 518 } 519 OS << ' ' << Flags << '\n'; 520 }; 521 522 for (int i = 1; i != argc; ++i) { 523 StringRef Arg = argv[i]; 524 525 if (Arg.starts_with("-")) { 526 HasAnyOption = true; 527 if (Arg == "--version") { 528 OS << PACKAGE_VERSION << '\n'; 529 } else if (Arg == "--prefix") { 530 sys::printArg(OS, ActivePrefix, /*Quote=*/true); 531 OS << '\n'; 532 } else if (Arg == "--bindir") { 533 sys::printArg(OS, ActiveBinDir, /*Quote=*/true); 534 OS << '\n'; 535 } else if (Arg == "--includedir") { 536 sys::printArg(OS, ActiveIncludeDir, /*Quote=*/true); 537 OS << '\n'; 538 } else if (Arg == "--libdir") { 539 sys::printArg(OS, ActiveLibDir, /*Quote=*/true); 540 OS << '\n'; 541 } else if (Arg == "--cmakedir") { 542 sys::printArg(OS, ActiveCMakeDir, /*Quote=*/true); 543 OS << '\n'; 544 } else if (Arg == "--cppflags") { 545 RenderFlags(LLVM_CPPFLAGS); 546 } else if (Arg == "--cflags") { 547 RenderFlags(LLVM_CFLAGS); 548 } else if (Arg == "--cxxflags") { 549 RenderFlags(LLVM_CXXFLAGS); 550 } else if (Arg == "--ldflags") { 551 OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L"); 552 sys::printArg(OS, ActiveLibDir, /*Quote=*/true); 553 OS << ' ' << LLVM_LDFLAGS << '\n'; 554 } else if (Arg == "--system-libs") { 555 PrintSystemLibs = true; 556 } else if (Arg == "--libs") { 557 PrintLibs = true; 558 } else if (Arg == "--libnames") { 559 PrintLibNames = true; 560 } else if (Arg == "--libfiles") { 561 PrintLibFiles = true; 562 } else if (Arg == "--components") { 563 /// If there are missing static archives and a dylib was 564 /// built, print LLVM_DYLIB_COMPONENTS instead of everything 565 /// in the manifest. 566 std::vector<std::string> Components; 567 for (const auto &AC : AvailableComponents) { 568 // Only include non-installed components when in a development tree. 569 if (!AC.IsInstalled && !IsInDevelopmentTree) 570 continue; 571 572 Components.push_back(AC.Name); 573 if (AC.Library && !IsInDevelopmentTree) { 574 std::string path(GetComponentLibraryPath(AC.Library, false)); 575 if (DirSep == "\\") { 576 std::replace(path.begin(), path.end(), '/', '\\'); 577 } 578 if (DyLibExists && !sys::fs::exists(path)) { 579 Components = 580 GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep); 581 llvm::sort(Components); 582 break; 583 } 584 } 585 } 586 587 for (unsigned I = 0; I < Components.size(); ++I) { 588 if (I) { 589 OS << ' '; 590 } 591 592 OS << Components[I]; 593 } 594 OS << '\n'; 595 } else if (Arg == "--targets-built") { 596 OS << LLVM_TARGETS_BUILT << '\n'; 597 } else if (Arg == "--host-target") { 598 OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n'; 599 } else if (Arg == "--build-mode") { 600 OS << build_mode << '\n'; 601 } else if (Arg == "--assertion-mode") { 602 #if defined(NDEBUG) 603 OS << "OFF\n"; 604 #else 605 OS << "ON\n"; 606 #endif 607 } else if (Arg == "--build-system") { 608 OS << LLVM_BUILD_SYSTEM << '\n'; 609 } else if (Arg == "--has-rtti") { 610 OS << (LLVM_HAS_RTTI ? "YES" : "NO") << '\n'; 611 } else if (Arg == "--shared-mode") { 612 PrintSharedMode = true; 613 } else if (Arg == "--obj-root") { 614 sys::printArg(OS, ActivePrefix, /*Quote=*/true); 615 OS << '\n'; 616 } else if (Arg == "--ignore-libllvm") { 617 LinkDyLib = false; 618 LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto; 619 } else if (Arg == "--link-shared") { 620 LinkMode = LinkModeShared; 621 } else if (Arg == "--link-static") { 622 LinkMode = LinkModeStatic; 623 } else if (Arg == "--help") { 624 usage(false); 625 } else { 626 usage(); 627 } 628 } else { 629 Components.push_back(Arg); 630 } 631 } 632 633 if (!HasAnyOption) 634 usage(); 635 636 if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) { 637 WithColor::error(errs(), "llvm-config") << DyLibName << " is missing\n"; 638 return 1; 639 } 640 641 if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs || 642 PrintSharedMode) { 643 644 if (PrintSharedMode && BuiltSharedLibs) { 645 OS << "shared\n"; 646 return 0; 647 } 648 649 // If no components were specified, default to "all". 650 if (Components.empty()) 651 Components.push_back("all"); 652 653 // Construct the list of all the required libraries. 654 std::function<std::string(const StringRef &)> 655 GetComponentLibraryPathFunction = [&](const StringRef &Name) { 656 return GetComponentLibraryPath(Name, LinkMode == LinkModeShared); 657 }; 658 std::vector<std::string> MissingLibs; 659 std::vector<std::string> RequiredLibs = ComputeLibsForComponents( 660 Components, 661 /*IncludeNonInstalled=*/IsInDevelopmentTree, false, 662 &GetComponentLibraryPathFunction, &MissingLibs, DirSep); 663 if (!MissingLibs.empty()) { 664 switch (LinkMode) { 665 case LinkModeShared: 666 if (LinkDyLib && !BuiltSharedLibs) 667 break; 668 // Using component shared libraries. 669 for (auto &Lib : MissingLibs) 670 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n"; 671 return 1; 672 case LinkModeAuto: 673 if (DyLibExists) { 674 LinkMode = LinkModeShared; 675 break; 676 } 677 WithColor::error(errs(), "llvm-config") 678 << "component libraries and shared library\n\n"; 679 [[fallthrough]]; 680 case LinkModeStatic: 681 for (auto &Lib : MissingLibs) 682 WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n"; 683 return 1; 684 } 685 } else if (LinkMode == LinkModeAuto) { 686 LinkMode = LinkModeStatic; 687 } 688 689 if (PrintSharedMode) { 690 std::unordered_set<std::string> FullDyLibComponents; 691 std::vector<std::string> DyLibComponents = 692 GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep); 693 694 for (auto &Component : DyLibComponents) { 695 FullDyLibComponents.insert(Component); 696 } 697 DyLibComponents.clear(); 698 699 for (auto &Lib : RequiredLibs) { 700 if (!FullDyLibComponents.count(Lib)) { 701 OS << "static\n"; 702 return 0; 703 } 704 } 705 FullDyLibComponents.clear(); 706 707 if (LinkMode == LinkModeShared) { 708 OS << "shared\n"; 709 return 0; 710 } else { 711 OS << "static\n"; 712 return 0; 713 } 714 } 715 716 if (PrintLibs || PrintLibNames || PrintLibFiles) { 717 718 auto PrintForLib = [&](const StringRef &Lib) { 719 const bool Shared = LinkMode == LinkModeShared; 720 std::string LibFileName; 721 if (PrintLibNames) { 722 LibFileName = GetComponentLibraryFileName(Lib, Shared); 723 } else if (PrintLibFiles) { 724 LibFileName = GetComponentLibraryPath(Lib, Shared); 725 } else if (PrintLibs) { 726 // On Windows, output full path to library without parameters. 727 // Elsewhere, if this is a typical library name, include it using -l. 728 if (HostTriple.isWindowsMSVCEnvironment()) { 729 LibFileName = GetComponentLibraryPath(Lib, Shared); 730 } else { 731 OS << "-l"; 732 StringRef LibName; 733 if (GetComponentLibraryNameSlice(Lib, LibName)) { 734 // Extract library name (remove prefix and suffix). 735 LibFileName = LibName; 736 } else { 737 // Lib is already a library name without prefix and suffix. 738 LibFileName = Lib; 739 } 740 } 741 } 742 if (!LibFileName.empty()) 743 sys::printArg(OS, LibFileName, /*Quote=*/true); 744 }; 745 746 if (LinkMode == LinkModeShared && LinkDyLib) { 747 PrintForLib(DyLibName); 748 } else { 749 for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) { 750 auto Lib = RequiredLibs[i]; 751 if (i) 752 OS << ' '; 753 754 PrintForLib(Lib); 755 } 756 } 757 OS << '\n'; 758 } 759 760 // Print SYSTEM_LIBS after --libs. 761 // FIXME: Each LLVM component may have its dependent system libs. 762 if (PrintSystemLibs) { 763 // Output system libraries only if linking against a static 764 // library (since the shared library links to all system libs 765 // already) 766 OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n'; 767 } 768 } else if (!Components.empty()) { 769 WithColor::error(errs(), "llvm-config") 770 << "components given, but unused\n\n"; 771 usage(); 772 } 773 774 return 0; 775 } 776