llvm-config.cpp 28 KB

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