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