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