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