Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 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 | |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/StringMap.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
Saleem Abdulrasool | 37511ec | 2014-03-29 01:08:53 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Triple.h" |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Twine.h" |
| 25 | #include "llvm/Config/config.h" |
| 26 | #include "llvm/Config/llvm-config.h" |
| 27 | #include "llvm/Support/FileSystem.h" |
| 28 | #include "llvm/Support/Path.h" |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include <cstdlib> |
| 31 | #include <set> |
| 32 | #include <vector> |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 33 | #include <unordered_set> |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 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 | /// \brief Traverse a single component adding to the topological ordering in |
| 50 | /// \arg RequiredLibs. |
| 51 | /// |
| 52 | /// \param Name - The component to traverse. |
| 53 | /// \param ComponentMap - A prebuilt map of component names to descriptors. |
| 54 | /// \param VisitedComponents [in] [out] - The set of already visited components. |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 55 | /// \param RequiredLibs [out] - The ordered list of required |
| 56 | /// libraries. |
| 57 | /// \param GetComponentNames - Get the component names instead of the |
| 58 | /// library name. |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 59 | static void VisitComponent(StringRef Name, |
| 60 | const StringMap<AvailableComponent*> &ComponentMap, |
| 61 | std::set<AvailableComponent*> &VisitedComponents, |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 62 | std::vector<StringRef> &RequiredLibs, |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 63 | bool IncludeNonInstalled, bool GetComponentNames, |
| 64 | const std::string *ActiveLibDir, bool *HasMissing) { |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 65 | // Lookup the component. |
| 66 | AvailableComponent *AC = ComponentMap.lookup(Name); |
| 67 | assert(AC && "Invalid component name!"); |
| 68 | |
| 69 | // Add to the visited table. |
| 70 | if (!VisitedComponents.insert(AC).second) { |
| 71 | // We are done if the component has already been visited. |
| 72 | return; |
| 73 | } |
| 74 | |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 75 | // Only include non-installed components if requested. |
| 76 | if (!AC->IsInstalled && !IncludeNonInstalled) |
| 77 | return; |
| 78 | |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 79 | // Otherwise, visit all the dependencies. |
| 80 | for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) { |
| 81 | VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents, |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 82 | RequiredLibs, IncludeNonInstalled, GetComponentNames, |
| 83 | ActiveLibDir, HasMissing); |
| 84 | } |
| 85 | |
| 86 | if (GetComponentNames) { |
| 87 | RequiredLibs.push_back(Name); |
| 88 | return; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Add to the required library list. |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 92 | if (AC->Library) { |
| 93 | if (!IncludeNonInstalled && HasMissing && !*HasMissing && ActiveLibDir) { |
| 94 | *HasMissing = !sys::fs::exists(*ActiveLibDir + "/" + AC->Library); |
| 95 | } |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 96 | RequiredLibs.push_back(AC->Library); |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 97 | } |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// \brief Compute the list of required libraries for a given list of |
| 101 | /// components, in an order suitable for passing to a linker (that is, libraries |
| 102 | /// appear prior to their dependencies). |
| 103 | /// |
| 104 | /// \param Components - The names of the components to find libraries for. |
| 105 | /// \param RequiredLibs [out] - On return, the ordered list of libraries that |
| 106 | /// are required to link the given components. |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 107 | /// \param IncludeNonInstalled - Whether non-installed components should be |
| 108 | /// reported. |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 109 | /// \param GetComponentNames - True if one would prefer the component names. |
David Blaikie | b750417 | 2015-11-09 23:51:45 +0000 | [diff] [blame^] | 110 | static std::vector<StringRef> |
| 111 | ComputeLibsForComponents(const std::vector<StringRef> &Components, |
| 112 | bool IncludeNonInstalled, bool GetComponentNames, |
| 113 | const std::string *ActiveLibDir, bool *HasMissing) { |
| 114 | std::vector<StringRef> RequiredLibs; |
| 115 | std::set<AvailableComponent *> VisitedComponents; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 116 | |
| 117 | // Build a map of component names to information. |
| 118 | StringMap<AvailableComponent*> ComponentMap; |
| 119 | for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) { |
| 120 | AvailableComponent *AC = &AvailableComponents[i]; |
| 121 | ComponentMap[AC->Name] = AC; |
| 122 | } |
| 123 | |
| 124 | // Visit the components. |
| 125 | for (unsigned i = 0, e = Components.size(); i != e; ++i) { |
| 126 | // Users are allowed to provide mixed case component names. |
| 127 | std::string ComponentLower = Components[i].lower(); |
| 128 | |
| 129 | // Validate that the user supplied a valid component name. |
| 130 | if (!ComponentMap.count(ComponentLower)) { |
| 131 | llvm::errs() << "llvm-config: unknown component name: " << Components[i] |
| 132 | << "\n"; |
| 133 | exit(1); |
| 134 | } |
| 135 | |
| 136 | VisitComponent(ComponentLower, ComponentMap, VisitedComponents, |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 137 | RequiredLibs, IncludeNonInstalled, GetComponentNames, |
| 138 | ActiveLibDir, HasMissing); |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // The list is now ordered with leafs first, we want the libraries to printed |
| 142 | // in the reverse order of dependency. |
| 143 | std::reverse(RequiredLibs.begin(), RequiredLibs.end()); |
David Blaikie | b750417 | 2015-11-09 23:51:45 +0000 | [diff] [blame^] | 144 | |
| 145 | return RequiredLibs; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* *** */ |
| 149 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 150 | static void usage() { |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 151 | errs() << "\ |
| 152 | usage: llvm-config <OPTION>... [<COMPONENT>...]\n\ |
| 153 | \n\ |
| 154 | Get various configuration information needed to compile programs which use\n\ |
| 155 | LLVM. Typically called from 'configure' scripts. Examples:\n\ |
| 156 | llvm-config --cxxflags\n\ |
| 157 | llvm-config --ldflags\n\ |
| 158 | llvm-config --libs engine bcreader scalaropts\n\ |
| 159 | \n\ |
| 160 | Options:\n\ |
| 161 | --version Print LLVM version.\n\ |
| 162 | --prefix Print the installation prefix.\n\ |
| 163 | --src-root Print the source root LLVM was built from.\n\ |
| 164 | --obj-root Print the object root used to build LLVM.\n\ |
| 165 | --bindir Directory containing LLVM executables.\n\ |
| 166 | --includedir Directory containing LLVM headers.\n\ |
| 167 | --libdir Directory containing LLVM libraries.\n\ |
| 168 | --cppflags C preprocessor flags for files that include LLVM headers.\n\ |
| 169 | --cflags C compiler flags for files that include LLVM headers.\n\ |
| 170 | --cxxflags C++ compiler flags for files that include LLVM headers.\n\ |
| 171 | --ldflags Print Linker flags.\n\ |
NAKAMURA Takumi | 800eb08 | 2013-12-25 02:24:32 +0000 | [diff] [blame] | 172 | --system-libs System Libraries needed to link against LLVM components.\n\ |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 173 | --libs Libraries needed to link against LLVM components.\n\ |
| 174 | --libnames Bare library names for in-tree builds.\n\ |
| 175 | --libfiles Fully qualified library filenames for makefile depends.\n\ |
| 176 | --components List of all possible components.\n\ |
| 177 | --targets-built List of all targets currently built.\n\ |
| 178 | --host-target Target triple used to configure LLVM.\n\ |
| 179 | --build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\ |
NAKAMURA Takumi | 303f0f5 | 2013-12-03 23:22:25 +0000 | [diff] [blame] | 180 | --assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\ |
Tom Stellard | 5268c17 | 2015-09-09 16:39:30 +0000 | [diff] [blame] | 181 | --build-system Print the build system used to build LLVM (autoconf or cmake).\n\ |
Tom Stellard | 18bf626 | 2015-11-04 20:57:43 +0000 | [diff] [blame] | 182 | --has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\ |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 183 | --shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\ |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 184 | Typical components:\n\ |
| 185 | all All LLVM libraries (default).\n\ |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 186 | engine Either a native JIT or a bitcode interpreter.\n"; |
| 187 | exit(1); |
| 188 | } |
| 189 | |
| 190 | /// \brief Compute the path to the main executable. |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 191 | std::string GetExecutablePath(const char *Argv0) { |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 192 | // This just needs to be some symbol in the binary; C++ doesn't |
| 193 | // allow taking the address of ::main however. |
| 194 | void *P = (void*) (intptr_t) GetExecutablePath; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 195 | return llvm::sys::fs::getMainExecutable(Argv0, P); |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 198 | /// \brief Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into |
| 199 | /// the full list of components. |
| 200 | std::vector<StringRef> GetAllDyLibComponents(const bool IsInDevelopmentTree, |
| 201 | const bool GetComponentNames) { |
| 202 | std::vector<StringRef> DyLibComponents; |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 203 | |
David Blaikie | b750417 | 2015-11-09 23:51:45 +0000 | [diff] [blame^] | 204 | StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS); |
| 205 | size_t Offset = 0; |
| 206 | while (true) { |
| 207 | const size_t NextOffset = DyLibComponentsStr.find(';', Offset); |
| 208 | DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset)); |
| 209 | if (NextOffset == std::string::npos) { |
| 210 | break; |
| 211 | } |
| 212 | Offset = NextOffset + 1; |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 213 | } |
| 214 | |
David Blaikie | b750417 | 2015-11-09 23:51:45 +0000 | [diff] [blame^] | 215 | assert(!DyLibComponents.empty()); |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 216 | |
David Blaikie | b750417 | 2015-11-09 23:51:45 +0000 | [diff] [blame^] | 217 | return ComputeLibsForComponents(DyLibComponents, |
| 218 | /*IncludeNonInstalled=*/IsInDevelopmentTree, |
| 219 | GetComponentNames, nullptr, nullptr); |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 222 | int main(int argc, char **argv) { |
| 223 | std::vector<StringRef> Components; |
| 224 | bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false; |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 225 | bool PrintSystemLibs = false, PrintSharedMode = false; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 226 | bool HasAnyOption = false; |
| 227 | |
| 228 | // llvm-config is designed to support being run both from a development tree |
| 229 | // and from an installed path. We try and auto-detect which case we are in so |
| 230 | // that we can report the correct information when run from a development |
| 231 | // tree. |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 232 | bool IsInDevelopmentTree; |
| 233 | enum { MakefileStyle, CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout; |
Rafael Espindola | e03dfd9 | 2013-06-26 05:01:35 +0000 | [diff] [blame] | 234 | llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0])); |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 235 | std::string CurrentExecPrefix; |
| 236 | std::string ActiveObjRoot; |
| 237 | |
NAKAMURA Takumi | 7b789b3 | 2013-12-17 05:48:37 +0000 | [diff] [blame] | 238 | // If CMAKE_CFG_INTDIR is given, honor it as build mode. |
| 239 | char const *build_mode = LLVM_BUILDMODE; |
| 240 | #if defined(CMAKE_CFG_INTDIR) |
| 241 | if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0')) |
| 242 | build_mode = CMAKE_CFG_INTDIR; |
| 243 | #endif |
| 244 | |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 245 | // Create an absolute path, and pop up one directory (we expect to be inside a |
| 246 | // bin dir). |
| 247 | sys::fs::make_absolute(CurrentPath); |
| 248 | CurrentExecPrefix = sys::path::parent_path( |
| 249 | sys::path::parent_path(CurrentPath)).str(); |
| 250 | |
| 251 | // Check to see if we are inside a development tree by comparing to possible |
Daniel Dunbar | f1ab402 | 2012-05-15 22:07:18 +0000 | [diff] [blame] | 252 | // locations (prefix style or CMake style). |
| 253 | if (sys::fs::equivalent(CurrentExecPrefix, |
NAKAMURA Takumi | 5a600a9 | 2013-12-20 17:35:46 +0000 | [diff] [blame] | 254 | Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) { |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 255 | IsInDevelopmentTree = true; |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 256 | DevelopmentTreeLayout = MakefileStyle; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 257 | |
| 258 | // If we are in a development tree, then check if we are in a BuildTools |
| 259 | // directory. This indicates we are built for the build triple, but we |
| 260 | // always want to provide information for the host triple. |
| 261 | if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") { |
| 262 | ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT); |
| 263 | } else { |
| 264 | ActiveObjRoot = LLVM_OBJ_ROOT; |
| 265 | } |
Daniel Dunbar | f1ab402 | 2012-05-15 22:07:18 +0000 | [diff] [blame] | 266 | } else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) { |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 267 | IsInDevelopmentTree = true; |
| 268 | DevelopmentTreeLayout = CMakeStyle; |
| 269 | ActiveObjRoot = LLVM_OBJ_ROOT; |
Daniel Dunbar | f1ab402 | 2012-05-15 22:07:18 +0000 | [diff] [blame] | 270 | } else if (sys::fs::equivalent(CurrentExecPrefix, |
| 271 | Twine(LLVM_OBJ_ROOT) + "/bin")) { |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 272 | IsInDevelopmentTree = true; |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 273 | DevelopmentTreeLayout = CMakeBuildModeStyle; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 274 | ActiveObjRoot = LLVM_OBJ_ROOT; |
| 275 | } else { |
| 276 | IsInDevelopmentTree = false; |
Duncan Sands | f320be8 | 2012-02-23 08:25:25 +0000 | [diff] [blame] | 277 | DevelopmentTreeLayout = MakefileStyle; // Initialized to avoid warnings. |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Compute various directory locations based on the derived location |
| 281 | // information. |
| 282 | std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir; |
| 283 | std::string ActiveIncludeOption; |
| 284 | if (IsInDevelopmentTree) { |
| 285 | ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include"; |
| 286 | ActivePrefix = CurrentExecPrefix; |
| 287 | |
| 288 | // CMake organizes the products differently than a normal prefix style |
| 289 | // layout. |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 290 | switch (DevelopmentTreeLayout) { |
| 291 | case MakefileStyle: |
NAKAMURA Takumi | 46c1903 | 2013-12-20 17:35:52 +0000 | [diff] [blame] | 292 | ActivePrefix = ActiveObjRoot; |
NAKAMURA Takumi | 5a600a9 | 2013-12-20 17:35:46 +0000 | [diff] [blame] | 293 | ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin"; |
Chandler Carruth | 7d58776 | 2014-12-29 11:16:25 +0000 | [diff] [blame] | 294 | ActiveLibDir = |
| 295 | ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX; |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 296 | break; |
| 297 | case CMakeStyle: |
| 298 | ActiveBinDir = ActiveObjRoot + "/bin"; |
Chandler Carruth | 7d58776 | 2014-12-29 11:16:25 +0000 | [diff] [blame] | 299 | ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX; |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 300 | break; |
| 301 | case CMakeBuildModeStyle: |
NAKAMURA Takumi | 429a222 | 2013-12-19 16:02:23 +0000 | [diff] [blame] | 302 | ActivePrefix = ActiveObjRoot; |
NAKAMURA Takumi | 7b789b3 | 2013-12-17 05:48:37 +0000 | [diff] [blame] | 303 | ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode; |
Chandler Carruth | 7d58776 | 2014-12-29 11:16:25 +0000 | [diff] [blame] | 304 | ActiveLibDir = |
| 305 | ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX + "/" + build_mode; |
Peter Collingbourne | 76e1c8c | 2012-01-26 01:31:38 +0000 | [diff] [blame] | 306 | break; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // We need to include files from both the source and object trees. |
| 310 | ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " + |
| 311 | "-I" + ActiveObjRoot + "/include"); |
| 312 | } else { |
| 313 | ActivePrefix = CurrentExecPrefix; |
| 314 | ActiveIncludeDir = ActivePrefix + "/include"; |
| 315 | ActiveBinDir = ActivePrefix + "/bin"; |
Chandler Carruth | 7d58776 | 2014-12-29 11:16:25 +0000 | [diff] [blame] | 316 | ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 317 | ActiveIncludeOption = "-I" + ActiveIncludeDir; |
| 318 | } |
| 319 | |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 320 | /// We only use `shared library` mode in cases where the static library form |
| 321 | /// of the components provided are not available; note however that this is |
| 322 | /// skipped if we're run from within the build dir. However, once installed, |
| 323 | /// we still need to provide correct output when the static archives are |
| 324 | /// removed or, as in the case of CMake's `BUILD_SHARED_LIBS`, never present |
| 325 | /// in the first place. This can't be done at configure/build time. |
| 326 | |
| 327 | StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt, |
| 328 | StaticPrefix, StaticDir = "lib"; |
| 329 | const Triple HostTriple(Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE)); |
| 330 | if (HostTriple.isOSWindows()) { |
| 331 | SharedExt = "dll"; |
| 332 | SharedVersionedExt = PACKAGE_VERSION ".dll"; |
| 333 | StaticExt = "a"; |
| 334 | SharedDir = ActiveBinDir; |
| 335 | StaticDir = ActiveLibDir; |
| 336 | StaticPrefix = SharedPrefix = ""; |
| 337 | } else if (HostTriple.isOSDarwin()) { |
| 338 | SharedExt = "dylib"; |
| 339 | SharedVersionedExt = PACKAGE_VERSION ".dylib"; |
| 340 | StaticExt = "a"; |
| 341 | StaticDir = SharedDir = ActiveLibDir; |
| 342 | StaticPrefix = SharedPrefix = "lib"; |
| 343 | } else { |
| 344 | // default to the unix values: |
| 345 | SharedExt = "so"; |
| 346 | SharedVersionedExt = PACKAGE_VERSION ".so"; |
| 347 | StaticExt = "a"; |
| 348 | StaticDir = SharedDir = ActiveLibDir; |
| 349 | StaticPrefix = SharedPrefix = "lib"; |
| 350 | } |
| 351 | |
| 352 | const bool BuiltDyLib = (std::strcmp(LLVM_ENABLE_DYLIB, "ON") == 0); |
| 353 | |
| 354 | enum { CMake, AutoConf } ConfigTool; |
| 355 | if (std::strcmp(LLVM_BUILD_SYSTEM, "cmake") == 0) { |
| 356 | ConfigTool = CMake; |
| 357 | } else { |
| 358 | ConfigTool = AutoConf; |
| 359 | } |
| 360 | |
| 361 | /// CMake style shared libs, ie each component is in a shared library. |
| 362 | const bool BuiltSharedLibs = |
| 363 | (ConfigTool == CMake && std::strcmp(LLVM_ENABLE_SHARED, "ON") == 0); |
| 364 | |
| 365 | bool DyLibExists = false; |
| 366 | const std::string DyLibName = |
| 367 | (SharedPrefix + "LLVM-" + SharedVersionedExt).str(); |
| 368 | |
| 369 | if (BuiltDyLib) { |
| 370 | DyLibExists = sys::fs::exists(SharedDir + "/" + DyLibName); |
| 371 | } |
| 372 | |
| 373 | /// Get the component's library name without the lib prefix and the |
| 374 | /// extension. Returns true if Lib is in a recognized format. |
| 375 | auto GetComponentLibraryNameSlice = [&](const StringRef &Lib, |
| 376 | StringRef &Out) { |
| 377 | if (Lib.startswith("lib")) { |
| 378 | unsigned FromEnd; |
| 379 | if (Lib.endswith(StaticExt)) { |
| 380 | FromEnd = StaticExt.size() + 1; |
| 381 | } else if (Lib.endswith(SharedExt)) { |
| 382 | FromEnd = SharedExt.size() + 1; |
| 383 | } else { |
| 384 | FromEnd = 0; |
| 385 | } |
| 386 | |
| 387 | if (FromEnd != 0) { |
| 388 | Out = Lib.slice(3, Lib.size() - FromEnd); |
| 389 | return true; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return false; |
| 394 | }; |
| 395 | /// Maps Unixizms to the host platform. |
| 396 | auto GetComponentLibraryFileName = [&](const StringRef &Lib, |
| 397 | const bool ForceShared) { |
| 398 | std::string LibFileName = Lib; |
| 399 | StringRef LibName; |
| 400 | if (GetComponentLibraryNameSlice(Lib, LibName)) { |
| 401 | if (BuiltSharedLibs || ForceShared) { |
| 402 | LibFileName = (SharedPrefix + LibName + "." + SharedExt).str(); |
| 403 | } else { |
| 404 | // default to static |
| 405 | LibFileName = (StaticPrefix + LibName + "." + StaticExt).str(); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return LibFileName; |
| 410 | }; |
| 411 | /// Get the full path for a possibly shared component library. |
| 412 | auto GetComponentLibraryPath = [&](const StringRef &Name, |
| 413 | const bool ForceShared) { |
| 414 | auto LibFileName = GetComponentLibraryFileName(Name, ForceShared); |
| 415 | if (BuiltSharedLibs || ForceShared) { |
| 416 | return (SharedDir + "/" + LibFileName).str(); |
| 417 | } else { |
| 418 | return (StaticDir + "/" + LibFileName).str(); |
| 419 | } |
| 420 | }; |
| 421 | |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 422 | raw_ostream &OS = outs(); |
| 423 | for (int i = 1; i != argc; ++i) { |
| 424 | StringRef Arg = argv[i]; |
| 425 | |
| 426 | if (Arg.startswith("-")) { |
| 427 | HasAnyOption = true; |
| 428 | if (Arg == "--version") { |
| 429 | OS << PACKAGE_VERSION << '\n'; |
| 430 | } else if (Arg == "--prefix") { |
| 431 | OS << ActivePrefix << '\n'; |
| 432 | } else if (Arg == "--bindir") { |
| 433 | OS << ActiveBinDir << '\n'; |
| 434 | } else if (Arg == "--includedir") { |
| 435 | OS << ActiveIncludeDir << '\n'; |
| 436 | } else if (Arg == "--libdir") { |
| 437 | OS << ActiveLibDir << '\n'; |
| 438 | } else if (Arg == "--cppflags") { |
| 439 | OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n'; |
| 440 | } else if (Arg == "--cflags") { |
| 441 | OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n'; |
| 442 | } else if (Arg == "--cxxflags") { |
| 443 | OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n'; |
| 444 | } else if (Arg == "--ldflags") { |
NAKAMURA Takumi | f8c58c8 | 2013-12-19 08:46:36 +0000 | [diff] [blame] | 445 | OS << "-L" << ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n'; |
| 446 | } else if (Arg == "--system-libs") { |
| 447 | PrintSystemLibs = true; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 448 | } else if (Arg == "--libs") { |
| 449 | PrintLibs = true; |
| 450 | } else if (Arg == "--libnames") { |
| 451 | PrintLibNames = true; |
| 452 | } else if (Arg == "--libfiles") { |
| 453 | PrintLibFiles = true; |
| 454 | } else if (Arg == "--components") { |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 455 | /// If there are missing static archives and a dylib was |
| 456 | /// built, print LLVM_DYLIB_COMPONENTS instead of everything |
| 457 | /// in the manifest. |
| 458 | std::vector<StringRef> Components; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 459 | for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) { |
Daniel Dunbar | c364d68 | 2012-05-15 18:44:17 +0000 | [diff] [blame] | 460 | // Only include non-installed components when in a development tree. |
| 461 | if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree) |
| 462 | continue; |
| 463 | |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 464 | Components.push_back(AvailableComponents[j].Name); |
| 465 | if (AvailableComponents[j].Library && !IsInDevelopmentTree) { |
| 466 | if (DyLibExists && |
| 467 | !sys::fs::exists(GetComponentLibraryPath( |
| 468 | AvailableComponents[j].Library, false))) { |
| 469 | Components = GetAllDyLibComponents(IsInDevelopmentTree, true); |
| 470 | std::sort(Components.begin(), Components.end()); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | for (unsigned I = 0; I < Components.size(); ++I) { |
| 477 | if (I) { |
| 478 | OS << ' '; |
| 479 | } |
| 480 | |
| 481 | OS << Components[I]; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 482 | } |
| 483 | OS << '\n'; |
| 484 | } else if (Arg == "--targets-built") { |
Daniel Dunbar | 30a8976 | 2011-12-16 00:04:43 +0000 | [diff] [blame] | 485 | OS << LLVM_TARGETS_BUILT << '\n'; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 486 | } else if (Arg == "--host-target") { |
Saleem Abdulrasool | 37511ec | 2014-03-29 01:08:53 +0000 | [diff] [blame] | 487 | OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n'; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 488 | } else if (Arg == "--build-mode") { |
NAKAMURA Takumi | 1b16e27 | 2013-12-03 14:35:17 +0000 | [diff] [blame] | 489 | OS << build_mode << '\n'; |
NAKAMURA Takumi | 303f0f5 | 2013-12-03 23:22:25 +0000 | [diff] [blame] | 490 | } else if (Arg == "--assertion-mode") { |
| 491 | #if defined(NDEBUG) |
| 492 | OS << "OFF\n"; |
| 493 | #else |
| 494 | OS << "ON\n"; |
| 495 | #endif |
Tom Stellard | 5268c17 | 2015-09-09 16:39:30 +0000 | [diff] [blame] | 496 | } else if (Arg == "--build-system") { |
| 497 | OS << LLVM_BUILD_SYSTEM << '\n'; |
Tom Stellard | 18bf626 | 2015-11-04 20:57:43 +0000 | [diff] [blame] | 498 | } else if (Arg == "--has-rtti") { |
| 499 | OS << LLVM_HAS_RTTI << '\n'; |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 500 | } else if (Arg == "--shared-mode") { |
| 501 | PrintSharedMode = true; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 502 | } else if (Arg == "--obj-root") { |
NAKAMURA Takumi | 93a1462 | 2013-12-19 16:02:28 +0000 | [diff] [blame] | 503 | OS << ActivePrefix << '\n'; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 504 | } else if (Arg == "--src-root") { |
| 505 | OS << LLVM_SRC_ROOT << '\n'; |
| 506 | } else { |
| 507 | usage(); |
| 508 | } |
| 509 | } else { |
| 510 | Components.push_back(Arg); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (!HasAnyOption) |
| 515 | usage(); |
| 516 | |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 517 | if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs || |
| 518 | PrintSharedMode) { |
| 519 | |
| 520 | if (PrintSharedMode && BuiltSharedLibs) { |
| 521 | OS << "shared\n"; |
| 522 | return 0; |
| 523 | } |
| 524 | |
Daniel Dunbar | fbc6a89 | 2011-12-12 18:22:04 +0000 | [diff] [blame] | 525 | // If no components were specified, default to "all". |
| 526 | if (Components.empty()) |
| 527 | Components.push_back("all"); |
| 528 | |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 529 | // Construct the list of all the required libraries. |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 530 | bool HasMissing = false; |
David Blaikie | b750417 | 2015-11-09 23:51:45 +0000 | [diff] [blame^] | 531 | std::vector<StringRef> RequiredLibs = |
| 532 | ComputeLibsForComponents(Components, |
| 533 | /*IncludeNonInstalled=*/IsInDevelopmentTree, |
| 534 | false, &ActiveLibDir, &HasMissing); |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 535 | |
| 536 | if (PrintSharedMode) { |
| 537 | std::unordered_set<std::string> FullDyLibComponents; |
| 538 | std::vector<StringRef> DyLibComponents = |
| 539 | GetAllDyLibComponents(IsInDevelopmentTree, false); |
| 540 | |
| 541 | for (auto &Component : DyLibComponents) { |
| 542 | FullDyLibComponents.insert(Component); |
| 543 | } |
| 544 | DyLibComponents.clear(); |
| 545 | |
| 546 | for (auto &Lib : RequiredLibs) { |
| 547 | if (!FullDyLibComponents.count(Lib)) { |
| 548 | OS << "static\n"; |
| 549 | return 0; |
| 550 | } |
| 551 | } |
| 552 | FullDyLibComponents.clear(); |
| 553 | |
| 554 | if (HasMissing && DyLibExists) { |
| 555 | OS << "shared\n"; |
| 556 | return 0; |
| 557 | } else { |
| 558 | OS << "static\n"; |
| 559 | return 0; |
| 560 | } |
| 561 | } |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 562 | |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 563 | if (PrintLibs || PrintLibNames || PrintLibFiles) { |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 564 | |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 565 | auto PrintForLib = [&](const StringRef &Lib, const bool ForceShared) { |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 566 | if (PrintLibNames) { |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 567 | OS << GetComponentLibraryFileName(Lib, ForceShared); |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 568 | } else if (PrintLibFiles) { |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 569 | OS << GetComponentLibraryPath(Lib, ForceShared); |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 570 | } else if (PrintLibs) { |
| 571 | // If this is a typical library name, include it using -l. |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 572 | StringRef LibName; |
| 573 | if (Lib.startswith("lib")) { |
| 574 | if (GetComponentLibraryNameSlice(Lib, LibName)) { |
| 575 | OS << "-l" << LibName; |
| 576 | } else { |
| 577 | OS << "-l:" << GetComponentLibraryFileName(Lib, ForceShared); |
| 578 | } |
| 579 | } else { |
| 580 | // Otherwise, print the full path. |
| 581 | OS << GetComponentLibraryPath(Lib, ForceShared); |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 582 | } |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 583 | } |
| 584 | }; |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 585 | |
Richard Diamond | 72303a2 | 2015-11-09 23:15:38 +0000 | [diff] [blame] | 586 | if (HasMissing && DyLibExists) { |
| 587 | PrintForLib(DyLibName, true); |
| 588 | } else { |
| 589 | for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) { |
| 590 | StringRef Lib = RequiredLibs[i]; |
| 591 | if (i) |
| 592 | OS << ' '; |
| 593 | |
| 594 | PrintForLib(Lib, false); |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 595 | } |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 596 | } |
Richard Osborne | 49ae117 | 2014-03-03 15:06:14 +0000 | [diff] [blame] | 597 | OS << '\n'; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 598 | } |
NAKAMURA Takumi | f8c58c8 | 2013-12-19 08:46:36 +0000 | [diff] [blame] | 599 | |
| 600 | // Print SYSTEM_LIBS after --libs. |
| 601 | // FIXME: Each LLVM component may have its dependent system libs. |
| 602 | if (PrintSystemLibs) |
| 603 | OS << LLVM_SYSTEM_LIBS << '\n'; |
Daniel Dunbar | ab0ad4e | 2011-12-01 20:18:09 +0000 | [diff] [blame] | 604 | } else if (!Components.empty()) { |
| 605 | errs() << "llvm-config: error: components given, but unused\n\n"; |
| 606 | usage(); |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |