llvm-config.cpp 28 KB

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