Index: tools/llvm-config/llvm-config.cpp =================================================================== --- tools/llvm-config/llvm-config.cpp +++ tools/llvm-config/llvm-config.cpp @@ -53,12 +53,12 @@ // to the alternative if the required libraries are not available. LinkModeAuto = 0, - // LinkModeDyLib will link with the single dynamic library if it exists, - // and return an error if it does not exist. - LinkModeDyLib = 1, + // LinkModeShared will link with the dynamic component libraries if they + // exist, and return an error otherwise. + LinkModeShared = 1, - // LinkModeStatic will link with the individual component static libraries - // if they exist, and fail if any one does not exist. + // LinkModeStatic will link with the static component libraries if they + // exist, and return an error otherwise. LinkModeStatic = 2, }; @@ -77,7 +77,8 @@ std::set &VisitedComponents, std::vector &RequiredLibs, bool IncludeNonInstalled, bool GetComponentNames, - const std::string *ActiveLibDir, + const std::function + *GetComponentLibraryPath, std::vector *Missing) { // Lookup the component. AvailableComponent *AC = ComponentMap.lookup(Name); @@ -97,7 +98,7 @@ for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) { VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents, RequiredLibs, IncludeNonInstalled, GetComponentNames, - ActiveLibDir, Missing); + GetComponentLibraryPath, Missing); } if (GetComponentNames) { @@ -107,8 +108,8 @@ // Add to the required library list. if (AC->Library) { - if (Missing && ActiveLibDir) { - std::string path = *ActiveLibDir + "/" + AC->Library; + if (Missing && GetComponentLibraryPath) { + std::string path = (*GetComponentLibraryPath)(AC->Library); if (!sys::fs::exists(path)) Missing->push_back(path); } @@ -127,7 +128,8 @@ static std::vector ComputeLibsForComponents(const std::vector &Components, bool IncludeNonInstalled, bool GetComponentNames, - const std::string *ActiveLibDir, + const std::function + *GetComponentLibraryPath, std::vector *Missing) { std::vector RequiredLibs; std::set VisitedComponents; @@ -153,7 +155,7 @@ VisitComponent(ComponentLower, ComponentMap, VisitedComponents, RequiredLibs, IncludeNonInstalled, GetComponentNames, - ActiveLibDir, Missing); + GetComponentLibraryPath, Missing); } // The list is now ordered with leafs first, we want the libraries to printed @@ -199,8 +201,8 @@ --build-system Print the build system used to build LLVM (autoconf or cmake).\n\ --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\ --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\ - --link-shared Link the components as a shared library.\n\ - --link-static Link the components as a static libraries.\n\ + --link-shared Link the components as shared libraries.\n\ + --link-static Link the component libraries statically.\n\ Typical components:\n\ all All LLVM libraries (default).\n\ engine Either a native JIT or a bitcode interpreter.\n"; @@ -388,7 +390,7 @@ // If LLVM_LINK_DYLIB is ON, the single shared library will be returned // for "--libs", etc, if they exist. This behaviour can be overridden with - // --link-static or --link-shared. + // --link-components or --link-libllvm. bool LinkDyLib = (std::strcmp(LLVM_LINK_DYLIB, "ON") == 0); if (BuiltDyLib) { @@ -399,7 +401,8 @@ LinkDyLib = false; } } - LinkMode LinkMode = LinkDyLib ? LinkModeDyLib : LinkModeAuto; + LinkMode LinkMode = + (LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto; /// Get the component's library name without the lib prefix and the /// extension. Returns true if Lib is in a recognized format. @@ -425,11 +428,11 @@ }; /// Maps Unixizms to the host platform. auto GetComponentLibraryFileName = [&](const StringRef &Lib, - const bool ForceShared) { + const bool Shared) { std::string LibFileName = Lib; StringRef LibName; if (GetComponentLibraryNameSlice(Lib, LibName)) { - if (BuiltSharedLibs || ForceShared) { + if (Shared) { LibFileName = (SharedPrefix + LibName + "." + SharedExt).str(); } else { // default to static @@ -440,10 +443,9 @@ return LibFileName; }; /// Get the full path for a possibly shared component library. - auto GetComponentLibraryPath = [&](const StringRef &Name, - const bool ForceShared) { - auto LibFileName = GetComponentLibraryFileName(Name, ForceShared); - if (BuiltSharedLibs || ForceShared) { + auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) { + auto LibFileName = GetComponentLibraryFileName(Name, Shared); + if (Shared) { return (SharedDir + "/" + LibFileName).str(); } else { return (StaticDir + "/" + LibFileName).str(); @@ -535,7 +537,7 @@ } else if (Arg == "--src-root") { OS << LLVM_SRC_ROOT << '\n'; } else if (Arg == "--link-shared") { - LinkMode = LinkModeDyLib; + LinkMode = LinkModeShared; } else if (Arg == "--link-static") { LinkMode = LinkModeStatic; } else { @@ -549,9 +551,9 @@ if (!HasAnyOption) usage(); - if (LinkMode == LinkModeDyLib && !DyLibExists) { - errs() << "llvm-config: error: " << DyLibName << " is missing\n\n"; - usage(); + if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) { + errs() << "llvm-config: error: " << DyLibName << " is missing\n"; + return 1; } if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs || @@ -567,18 +569,27 @@ Components.push_back("all"); // Construct the list of all the required libraries. + std::function + GetComponentLibraryPathFunction = [&](const StringRef &Name) { + return GetComponentLibraryPath(Name, LinkMode == LinkModeShared); + }; std::vector MissingLibs; - std::vector RequiredLibs = - ComputeLibsForComponents(Components, - /*IncludeNonInstalled=*/IsInDevelopmentTree, - false, &ActiveLibDir, &MissingLibs); + std::vector RequiredLibs = ComputeLibsForComponents( + Components, + /*IncludeNonInstalled=*/IsInDevelopmentTree, false, + &GetComponentLibraryPathFunction, &MissingLibs); if (!MissingLibs.empty()) { switch (LinkMode) { - case LinkModeDyLib: - break; + case LinkModeShared: + if (DyLibExists && !BuiltSharedLibs) + break; + // Using component shared libraries. + for (auto &Lib : MissingLibs) + errs() << "llvm-config: error: missing: " << Lib << "\n"; + return 1; case LinkModeAuto: if (DyLibExists) { - LinkMode = LinkModeDyLib; + LinkMode = LinkModeShared; break; } errs() @@ -611,7 +622,7 @@ } FullDyLibComponents.clear(); - if (LinkMode == LinkModeDyLib) { + if (LinkMode == LinkModeShared) { OS << "shared\n"; return 0; } else { @@ -622,11 +633,12 @@ if (PrintLibs || PrintLibNames || PrintLibFiles) { - auto PrintForLib = [&](const StringRef &Lib, const bool ForceShared) { + auto PrintForLib = [&](const StringRef &Lib) { + const bool Shared = LinkMode == LinkModeShared; if (PrintLibNames) { - OS << GetComponentLibraryFileName(Lib, ForceShared); + OS << GetComponentLibraryFileName(Lib, Shared); } else if (PrintLibFiles) { - OS << GetComponentLibraryPath(Lib, ForceShared); + OS << GetComponentLibraryPath(Lib, Shared); } else if (PrintLibs) { // If this is a typical library name, include it using -l. StringRef LibName; @@ -634,24 +646,24 @@ if (GetComponentLibraryNameSlice(Lib, LibName)) { OS << "-l" << LibName; } else { - OS << "-l:" << GetComponentLibraryFileName(Lib, ForceShared); + OS << "-l:" << GetComponentLibraryFileName(Lib, Shared); } } else { // Otherwise, print the full path. - OS << GetComponentLibraryPath(Lib, ForceShared); + OS << GetComponentLibraryPath(Lib, Shared); } } }; - if (LinkMode == LinkModeDyLib) { - PrintForLib(DyLibName, true); + if (LinkMode == LinkModeShared && !BuiltSharedLibs) { + PrintForLib(DyLibName); } else { for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) { auto Lib = RequiredLibs[i]; if (i) OS << ' '; - PrintForLib(Lib, false); + PrintForLib(Lib); } } OS << '\n';