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