llvm-config.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //===-- llvm-config.cpp - LLVM project configuration utility --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tool encapsulates information about an LLVM project configuration for
  11. // use by other project's build environments (to determine installed path,
  12. // available features, required libraries, etc.).
  13. //
  14. // Note that although this tool *may* be used by some parts of LLVM's build
  15. // itself (i.e., the Makefiles use it to compute required libraries when linking
  16. // tools), this tool is primarily designed to support external projects.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #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/Config/llvm-config.h"
  26. #include "llvm/Support/FileSystem.h"
  27. #include "llvm/Support/Path.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <cstdlib>
  30. #include <set>
  31. #include <vector>
  32. using namespace llvm;
  33. // Include the build time variables we can report to the user. This is generated
  34. // at build time from the BuildVariables.inc.in file by the build system.
  35. #include "BuildVariables.inc"
  36. // Include the component table. This creates an array of struct
  37. // AvailableComponent entries, which record the component name, library name,
  38. // and required components for all of the available libraries.
  39. //
  40. // Not all components define a library, we also use "library groups" as a way to
  41. // create entries for pseudo groups like x86 or all-targets.
  42. #include "LibraryDependencies.inc"
  43. /// \brief Traverse a single component adding to the topological ordering in
  44. /// \arg RequiredLibs.
  45. ///
  46. /// \param Name - The component to traverse.
  47. /// \param ComponentMap - A prebuilt map of component names to descriptors.
  48. /// \param VisitedComponents [in] [out] - The set of already visited components.
  49. /// \param RequiredLibs [out] - The ordered list of required libraries.
  50. static void VisitComponent(StringRef Name,
  51. const StringMap<AvailableComponent*> &ComponentMap,
  52. std::set<AvailableComponent*> &VisitedComponents,
  53. std::vector<StringRef> &RequiredLibs,
  54. bool IncludeNonInstalled) {
  55. // Lookup the component.
  56. AvailableComponent *AC = ComponentMap.lookup(Name);
  57. assert(AC && "Invalid component name!");
  58. // Add to the visited table.
  59. if (!VisitedComponents.insert(AC).second) {
  60. // We are done if the component has already been visited.
  61. return;
  62. }
  63. // Only include non-installed components if requested.
  64. if (!AC->IsInstalled && !IncludeNonInstalled)
  65. return;
  66. // Otherwise, visit all the dependencies.
  67. for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
  68. VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
  69. RequiredLibs, IncludeNonInstalled);
  70. }
  71. // Add to the required library list.
  72. if (AC->Library)
  73. RequiredLibs.push_back(AC->Library);
  74. }
  75. /// \brief Compute the list of required libraries for a given list of
  76. /// components, in an order suitable for passing to a linker (that is, libraries
  77. /// appear prior to their dependencies).
  78. ///
  79. /// \param Components - The names of the components to find libraries for.
  80. /// \param RequiredLibs [out] - On return, the ordered list of libraries that
  81. /// are required to link the given components.
  82. /// \param IncludeNonInstalled - Whether non-installed components should be
  83. /// reported.
  84. static void ComputeLibsForComponents(const std::vector<StringRef> &Components,
  85. std::vector<StringRef> &RequiredLibs,
  86. bool IncludeNonInstalled) {
  87. std::set<AvailableComponent*> VisitedComponents;
  88. // Build a map of component names to information.
  89. StringMap<AvailableComponent*> ComponentMap;
  90. for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
  91. AvailableComponent *AC = &AvailableComponents[i];
  92. ComponentMap[AC->Name] = AC;
  93. }
  94. // Visit the components.
  95. for (unsigned i = 0, e = Components.size(); i != e; ++i) {
  96. // Users are allowed to provide mixed case component names.
  97. std::string ComponentLower = Components[i].lower();
  98. // Validate that the user supplied a valid component name.
  99. if (!ComponentMap.count(ComponentLower)) {
  100. llvm::errs() << "llvm-config: unknown component name: " << Components[i]
  101. << "\n";
  102. exit(1);
  103. }
  104. VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
  105. RequiredLibs, IncludeNonInstalled);
  106. }
  107. // The list is now ordered with leafs first, we want the libraries to printed
  108. // in the reverse order of dependency.
  109. std::reverse(RequiredLibs.begin(), RequiredLibs.end());
  110. }
  111. /* *** */
  112. static void usage() {
  113. errs() << "\
  114. usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
  115. \n\
  116. Get various configuration information needed to compile programs which use\n\
  117. LLVM. Typically called from 'configure' scripts. Examples:\n\
  118. llvm-config --cxxflags\n\
  119. llvm-config --ldflags\n\
  120. llvm-config --libs engine bcreader scalaropts\n\
  121. \n\
  122. Options:\n\
  123. --version Print LLVM version.\n\
  124. --prefix Print the installation prefix.\n\
  125. --src-root Print the source root LLVM was built from.\n\
  126. --obj-root Print the object root used to build LLVM.\n\
  127. --bindir Directory containing LLVM executables.\n\
  128. --includedir Directory containing LLVM headers.\n\
  129. --libdir Directory containing LLVM libraries.\n\
  130. --cppflags C preprocessor flags for files that include LLVM headers.\n\
  131. --cflags C compiler flags for files that include LLVM headers.\n\
  132. --cxxflags C++ compiler flags for files that include LLVM headers.\n\
  133. --ldflags Print Linker flags.\n\
  134. --system-libs System Libraries needed to link against LLVM components.\n\
  135. --libs Libraries needed to link against LLVM components.\n\
  136. --libnames Bare library names for in-tree builds.\n\
  137. --libfiles Fully qualified library filenames for makefile depends.\n\
  138. --components List of all possible components.\n\
  139. --targets-built List of all targets currently built.\n\
  140. --host-target Target triple used to configure LLVM.\n\
  141. --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
  142. --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
  143. Typical components:\n\
  144. all All LLVM libraries (default).\n\
  145. engine Either a native JIT or a bitcode interpreter.\n";
  146. exit(1);
  147. }
  148. /// \brief Compute the path to the main executable.
  149. std::string GetExecutablePath(const char *Argv0) {
  150. // This just needs to be some symbol in the binary; C++ doesn't
  151. // allow taking the address of ::main however.
  152. void *P = (void*) (intptr_t) GetExecutablePath;
  153. return llvm::sys::fs::getMainExecutable(Argv0, P);
  154. }
  155. // HLSL Change: changed calling convention to __cdecl
  156. int __cdecl main(int argc, char **argv) {
  157. std::vector<StringRef> Components;
  158. bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
  159. bool PrintSystemLibs = false;
  160. bool HasAnyOption = false;
  161. // llvm-config is designed to support being run both from a development tree
  162. // and from an installed path. We try and auto-detect which case we are in so
  163. // that we can report the correct information when run from a development
  164. // tree.
  165. bool IsInDevelopmentTree;
  166. enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
  167. llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
  168. std::string CurrentExecPrefix;
  169. std::string ActiveObjRoot;
  170. // If CMAKE_CFG_INTDIR is given, honor it as build mode.
  171. char const *build_mode = LLVM_BUILDMODE;
  172. #if defined(CMAKE_CFG_INTDIR)
  173. if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
  174. build_mode = CMAKE_CFG_INTDIR;
  175. #endif
  176. // Create an absolute path, and pop up one directory (we expect to be inside a
  177. // bin dir).
  178. sys::fs::make_absolute(CurrentPath);
  179. CurrentExecPrefix = sys::path::parent_path(
  180. sys::path::parent_path(CurrentPath)).str();
  181. // Check to see if we are inside a development tree by comparing to possible
  182. // locations (prefix style or CMake style).
  183. if (sys::fs::equivalent(CurrentExecPrefix,
  184. Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
  185. IsInDevelopmentTree = true;
  186. DevelopmentTreeLayout = MakefileStyle;
  187. // If we are in a development tree, then check if we are in a BuildTools
  188. // directory. This indicates we are built for the build triple, but we
  189. // always want to provide information for the host triple.
  190. if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
  191. ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
  192. } else {
  193. ActiveObjRoot = LLVM_OBJ_ROOT;
  194. }
  195. } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
  196. IsInDevelopmentTree = true;
  197. DevelopmentTreeLayout = CMakeStyle;
  198. ActiveObjRoot = LLVM_OBJ_ROOT;
  199. } else if (sys::fs::equivalent(CurrentExecPrefix,
  200. Twine(LLVM_OBJ_ROOT) + "/bin")) {
  201. IsInDevelopmentTree = true;
  202. DevelopmentTreeLayout = CMakeBuildModeStyle;
  203. ActiveObjRoot = LLVM_OBJ_ROOT;
  204. } else {
  205. IsInDevelopmentTree = false;
  206. DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings.
  207. }
  208. // Compute various directory locations based on the derived location
  209. // information.
  210. std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
  211. std::string ActiveIncludeOption;
  212. if (IsInDevelopmentTree) {
  213. ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
  214. ActivePrefix = CurrentExecPrefix;
  215. // CMake organizes the products differently than a normal prefix style
  216. // layout.
  217. switch (DevelopmentTreeLayout) {
  218. case MakefileStyle:
  219. ActivePrefix = ActiveObjRoot;
  220. ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
  221. ActiveLibDir =
  222. ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
  223. break;
  224. case CMakeStyle:
  225. ActiveBinDir = ActiveObjRoot + "/bin";
  226. ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
  227. break;
  228. case CMakeBuildModeStyle:
  229. ActivePrefix = ActiveObjRoot;
  230. ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
  231. ActiveLibDir =
  232. ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode;
  233. break;
  234. }
  235. // We need to include files from both the source and object trees.
  236. ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
  237. "-I" + ActiveObjRoot + "/include");
  238. } else {
  239. ActivePrefix = CurrentExecPrefix;
  240. ActiveIncludeDir = ActivePrefix + "/include";
  241. ActiveBinDir = ActivePrefix + "/bin";
  242. ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
  243. ActiveIncludeOption = "-I" + ActiveIncludeDir;
  244. }
  245. raw_ostream &OS = outs();
  246. for (int i = 1; i != argc; ++i) {
  247. StringRef Arg = argv[i];
  248. if (Arg.startswith("-")) {
  249. HasAnyOption = true;
  250. if (Arg == "--version") {
  251. OS << PACKAGE_VERSION << '\n';
  252. } else if (Arg == "--prefix") {
  253. OS << ActivePrefix << '\n';
  254. } else if (Arg == "--bindir") {
  255. OS << ActiveBinDir << '\n';
  256. } else if (Arg == "--includedir") {
  257. OS << ActiveIncludeDir << '\n';
  258. } else if (Arg == "--libdir") {
  259. OS << ActiveLibDir << '\n';
  260. } else if (Arg == "--cppflags") {
  261. OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
  262. } else if (Arg == "--cflags") {
  263. OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
  264. } else if (Arg == "--cxxflags") {
  265. OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
  266. } else if (Arg == "--ldflags") {
  267. OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
  268. } else if (Arg == "--system-libs") {
  269. PrintSystemLibs = true;
  270. } else if (Arg == "--libs") {
  271. PrintLibs = true;
  272. } else if (Arg == "--libnames") {
  273. PrintLibNames = true;
  274. } else if (Arg == "--libfiles") {
  275. PrintLibFiles = true;
  276. } else if (Arg == "--components") {
  277. for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
  278. // Only include non-installed components when in a development tree.
  279. if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
  280. continue;
  281. OS << ' ';
  282. OS << AvailableComponents[j].Name;
  283. }
  284. OS << '\n';
  285. } else if (Arg == "--targets-built") {
  286. OS << LLVM_TARGETS_BUILT << '\n';
  287. } else if (Arg == "--host-target") {
  288. OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
  289. } else if (Arg == "--build-mode") {
  290. OS << build_mode << '\n';
  291. } else if (Arg == "--assertion-mode") {
  292. #if defined(NDEBUG)
  293. OS << "OFF\n";
  294. #else
  295. OS << "ON\n";
  296. #endif
  297. } else if (Arg == "--obj-root") {
  298. OS << ActivePrefix << '\n';
  299. } else if (Arg == "--src-root") {
  300. OS << LLVM_SRC_ROOT << '\n';
  301. } else {
  302. usage();
  303. }
  304. } else {
  305. Components.push_back(Arg);
  306. }
  307. }
  308. if (!HasAnyOption)
  309. usage();
  310. if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
  311. // If no components were specified, default to "all".
  312. if (Components.empty())
  313. Components.push_back("all");
  314. // Construct the list of all the required libraries.
  315. std::vector<StringRef> RequiredLibs;
  316. ComputeLibsForComponents(Components, RequiredLibs,
  317. /*IncludeNonInstalled=*/IsInDevelopmentTree);
  318. if (PrintLibs || PrintLibNames || PrintLibFiles) {
  319. for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
  320. StringRef Lib = RequiredLibs[i];
  321. if (i)
  322. OS << ' ';
  323. if (PrintLibNames) {
  324. OS << Lib;
  325. } else if (PrintLibFiles) {
  326. OS << ActiveLibDir << '/' << Lib;
  327. } else if (PrintLibs) {
  328. // If this is a typical library name, include it using -l.
  329. if (Lib.startswith("lib") && Lib.endswith(".a")) {
  330. OS << "-l" << Lib.slice(3, Lib.size()-2);
  331. continue;
  332. }
  333. // Otherwise, print the full path.
  334. OS << ActiveLibDir << '/' << Lib;
  335. }
  336. }
  337. OS << '\n';
  338. }
  339. // Print SYSTEM_LIBS after --libs.
  340. // FIXME: Each LLVM component may have its dependent system libs.
  341. if (PrintSystemLibs)
  342. OS << LLVM_SYSTEM_LIBS << '\n';
  343. } else if (!Components.empty()) {
  344. errs() << "llvm-config: error: components given, but unused\n\n";
  345. usage();
  346. }
  347. return 0;
  348. }