#include "llvm/Config/llvm-config.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/config.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdlib>
#include <set>
#include <unordered_set>
#include <vector>
using namespace llvm;
#include "BuildVariables.inc"
#include "LibraryDependencies.inc"
#include "ExtensionDependencies.inc"
enum LinkMode {
LinkModeAuto = 0,
LinkModeShared = 1,
LinkModeStatic = 2,
};
static void VisitComponent(const std::string &Name,
const StringMap<AvailableComponent *> &ComponentMap,
std::set<AvailableComponent *> &VisitedComponents,
std::vector<std::string> &RequiredLibs,
bool IncludeNonInstalled, bool GetComponentNames,
const std::function<std::string(const StringRef &)>
*GetComponentLibraryPath,
std::vector<std::string> *Missing,
const std::string &DirSep) {
AvailableComponent *AC = ComponentMap.lookup(Name);
if (!AC) {
errs() << "Can't find component: '" << Name << "' in the map. Available components are: ";
for (const auto &Component : ComponentMap) {
errs() << "'" << Component.first() << "' ";
}
errs() << "\n";
report_fatal_error("abort");
}
assert(AC && "Invalid component name!");
if (!VisitedComponents.insert(AC).second) {
return;
}
if (!AC->IsInstalled && !IncludeNonInstalled)
return;
for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
RequiredLibs, IncludeNonInstalled, GetComponentNames,
GetComponentLibraryPath, Missing, DirSep);
}
if (Name == "extensions") {
for (auto const &AvailableExtension : AvailableExtensions) {
for (const char *const *Iter = &AvailableExtension.RequiredLibraries[0];
*Iter; ++Iter) {
AvailableComponent *AC = ComponentMap.lookup(*Iter);
if (!AC) {
RequiredLibs.push_back(*Iter);
} else {
VisitComponent(*Iter, ComponentMap, VisitedComponents, RequiredLibs,
IncludeNonInstalled, GetComponentNames,
GetComponentLibraryPath, Missing, DirSep);
}
}
}
}
if (GetComponentNames) {
RequiredLibs.push_back(Name);
return;
}
if (AC->Library) {
if (Missing && GetComponentLibraryPath) {
std::string path = (*GetComponentLibraryPath)(AC->Library);
if (DirSep == "\\") {
std::replace(path.begin(), path.end(), '/', '\\');
}
if (!sys::fs::exists(path))
Missing->push_back(path);
}
RequiredLibs.push_back(AC->Library);
}
}
static std::vector<std::string> ComputeLibsForComponents(
const std::vector<StringRef> &Components, bool IncludeNonInstalled,
bool GetComponentNames, const std::function<std::string(const StringRef &)>
*GetComponentLibraryPath,
std::vector<std::string> *Missing, const std::string &DirSep) {
std::vector<std::string> RequiredLibs;
std::set<AvailableComponent *> VisitedComponents;
StringMap<AvailableComponent *> ComponentMap;
for (unsigned i = 0; i != array_lengthof(AvailableComponents); ++i) {
AvailableComponent *AC = &AvailableComponents[i];
ComponentMap[AC->Name] = AC;
}
for (unsigned i = 0, e = Components.size(); i != e; ++i) {
std::string ComponentLower = Components[i].lower();
if (!ComponentMap.count(ComponentLower)) {
llvm::errs() << "llvm-config: unknown component name: " << Components[i]
<< "\n";
exit(1);
}
VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
RequiredLibs, IncludeNonInstalled, GetComponentNames,
GetComponentLibraryPath, Missing, DirSep);
}
std::reverse(RequiredLibs.begin(), RequiredLibs.end());
return RequiredLibs;
}
static void usage(bool ExitWithFailure = true) {
errs() << "\
usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
\n\
Get various configuration information needed to compile programs which use\n\
LLVM. Typically called from 'configure' scripts. Examples:\n\
llvm-config --cxxflags\n\
llvm-config --ldflags\n\
llvm-config --libs engine bcreader scalaropts\n\
\n\
Options:\n\
--assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
--bindir Directory containing LLVM executables.\n\
--build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
--build-system Print the build system used to build LLVM (e.g. `cmake` or `gn`).\n\
--cflags C compiler flags for files that include LLVM headers.\n\
--cmakedir Directory containing LLVM CMake modules.\n\
--components List of all possible components.\n\
--cppflags C preprocessor flags for files that include LLVM headers.\n\
--cxxflags C++ compiler flags for files that include LLVM headers.\n\
--has-rtti Print whether or not LLVM was built with rtti (YES or NO).\n\
--help Print a summary of llvm-config arguments.\n\
--host-target Target triple used to configure LLVM.\n\
--ignore-libllvm Ignore libLLVM and link component libraries instead.\n\
--includedir Directory containing LLVM headers.\n\
--ldflags Print Linker flags.\n\
--libdir Directory containing LLVM libraries.\n\
--libfiles Fully qualified library filenames for makefile depends.\n\
--libnames Bare library names for in-tree builds.\n\
--libs Libraries needed to link against LLVM components.\n\
--link-shared Link the components as shared libraries.\n\
--link-static Link the component libraries statically.\n\
--obj-root Print the object root used to build LLVM.\n\
--prefix Print the installation prefix.\n\
--shared-mode Print how the provided components can be collectively linked (`shared` or `static`).\n\
--src-root Print the source root LLVM was built from.\n\
--system-libs System Libraries needed to link against LLVM components.\n\
--targets-built List of all targets currently built.\n\
--version Print LLVM version.\n\
Typical components:\n\
all All LLVM libraries (default).\n\
engine Either a native JIT or a bitcode interpreter.\n";
if (ExitWithFailure)
exit(1);
}
std::string GetExecutablePath(const char *Argv0) {
void *P = (void *)(intptr_t)GetExecutablePath;
return llvm::sys::fs::getMainExecutable(Argv0, P);
}
std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
const bool GetComponentNames,
const std::string &DirSep) {
std::vector<StringRef> DyLibComponents;
StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
size_t Offset = 0;
while (true) {
const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset-Offset));
if (NextOffset == std::string::npos) {
break;
}
Offset = NextOffset + 1;
}
assert(!DyLibComponents.empty());
return ComputeLibsForComponents(DyLibComponents,
IsInDevelopmentTree,
GetComponentNames, nullptr, nullptr, DirSep);
}
int main(int argc, char **argv) {
std::vector<StringRef> Components;
bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
bool PrintSystemLibs = false, PrintSharedMode = false;
bool HasAnyOption = false;
bool IsInDevelopmentTree;
enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
std::string CurrentExecPrefix;
std::string ActiveObjRoot;
char const *build_mode = LLVM_BUILDMODE;
#if defined(CMAKE_CFG_INTDIR)
if (!(CMAKE_CFG_INTDIR[0] == '.' && CMAKE_CFG_INTDIR[1] == '\0'))
build_mode = CMAKE_CFG_INTDIR;
#endif
sys::fs::make_absolute(CurrentPath);
CurrentExecPrefix =
sys::path::parent_path(sys::path::parent_path(CurrentPath)).str();
if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
IsInDevelopmentTree = true;
DevelopmentTreeLayout = CMakeStyle;
ActiveObjRoot = LLVM_OBJ_ROOT;
} else if (sys::fs::equivalent(sys::path::parent_path(CurrentExecPrefix),
LLVM_OBJ_ROOT)) {
IsInDevelopmentTree = true;
DevelopmentTreeLayout = CMakeBuildModeStyle;
ActiveObjRoot = LLVM_OBJ_ROOT;
} else {
IsInDevelopmentTree = false;
DevelopmentTreeLayout = CMakeStyle; }
std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir,
ActiveCMakeDir;
std::string ActiveIncludeOption;
if (IsInDevelopmentTree) {
ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
ActivePrefix = CurrentExecPrefix;
switch (DevelopmentTreeLayout) {
case CMakeStyle:
ActiveBinDir = ActiveObjRoot + "/bin";
ActiveLibDir = ActiveObjRoot + "/lib" + LLVM_LIBDIR_SUFFIX;
ActiveCMakeDir = ActiveLibDir + "/cmake/llvm";
break;
case CMakeBuildModeStyle:
ActivePrefix = ActiveObjRoot;
ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
ActiveLibDir =
ActiveObjRoot + "/" + build_mode + "/lib" + LLVM_LIBDIR_SUFFIX;
ActiveCMakeDir =
ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX + "/cmake/llvm";
break;
}
ActiveIncludeOption =
("-I" + ActiveIncludeDir + " " + "-I" + ActiveObjRoot + "/include");
} else {
ActivePrefix = CurrentExecPrefix;
{
SmallString<256> Path(LLVM_INSTALL_INCLUDEDIR);
sys::fs::make_absolute(ActivePrefix, Path);
ActiveIncludeDir = std::string(Path.str());
}
{
SmallString<256> Path(LLVM_TOOLS_INSTALL_DIR);
sys::fs::make_absolute(ActivePrefix, Path);
ActiveBinDir = std::string(Path.str());
}
ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
{
SmallString<256> Path(LLVM_INSTALL_PACKAGE_DIR);
sys::fs::make_absolute(ActivePrefix, Path);
ActiveCMakeDir = std::string(Path.str());
}
ActiveIncludeOption = "-I" + ActiveIncludeDir;
}
StringRef SharedExt, SharedVersionedExt, SharedDir, SharedPrefix, StaticExt,
StaticPrefix, StaticDir = "lib";
std::string DirSep = "/";
const Triple HostTriple(Triple::normalize(LLVM_HOST_TRIPLE));
if (HostTriple.isOSWindows()) {
SharedExt = "dll";
SharedVersionedExt = LLVM_DYLIB_VERSION ".dll";
if (HostTriple.isOSCygMing()) {
SharedPrefix = "lib";
StaticExt = "a";
StaticPrefix = "lib";
} else {
StaticExt = "lib";
DirSep = "\\";
std::replace(ActiveObjRoot.begin(), ActiveObjRoot.end(), '/', '\\');
std::replace(ActivePrefix.begin(), ActivePrefix.end(), '/', '\\');
std::replace(ActiveBinDir.begin(), ActiveBinDir.end(), '/', '\\');
std::replace(ActiveLibDir.begin(), ActiveLibDir.end(), '/', '\\');
std::replace(ActiveCMakeDir.begin(), ActiveCMakeDir.end(), '/', '\\');
std::replace(ActiveIncludeOption.begin(), ActiveIncludeOption.end(), '/',
'\\');
}
SharedDir = ActiveBinDir;
StaticDir = ActiveLibDir;
} else if (HostTriple.isOSDarwin()) {
SharedExt = "dylib";
SharedVersionedExt = LLVM_DYLIB_VERSION ".dylib";
StaticExt = "a";
StaticDir = SharedDir = ActiveLibDir;
StaticPrefix = SharedPrefix = "lib";
} else {
SharedExt = "so";
SharedVersionedExt = LLVM_DYLIB_VERSION ".so";
StaticExt = "a";
StaticDir = SharedDir = ActiveLibDir;
StaticPrefix = SharedPrefix = "lib";
}
const bool BuiltDyLib = !!LLVM_ENABLE_DYLIB;
const bool BuiltSharedLibs = !!LLVM_ENABLE_SHARED;
bool DyLibExists = false;
const std::string DyLibName =
(SharedPrefix + "LLVM-" + SharedVersionedExt).str();
bool LinkDyLib = !!LLVM_LINK_DYLIB;
if (BuiltDyLib) {
std::string path((SharedDir + DirSep + DyLibName).str());
if (DirSep == "\\") {
std::replace(path.begin(), path.end(), '/', '\\');
}
DyLibExists = sys::fs::exists(path);
if (!DyLibExists) {
LinkDyLib = false;
}
}
LinkMode LinkMode =
(LinkDyLib || BuiltSharedLibs) ? LinkModeShared : LinkModeAuto;
auto GetComponentLibraryNameSlice = [&](const StringRef &Lib,
StringRef &Out) {
if (Lib.startswith("lib")) {
unsigned FromEnd;
if (Lib.endswith(StaticExt)) {
FromEnd = StaticExt.size() + 1;
} else if (Lib.endswith(SharedExt)) {
FromEnd = SharedExt.size() + 1;
} else {
FromEnd = 0;
}
if (FromEnd != 0) {
Out = Lib.slice(3, Lib.size() - FromEnd);
return true;
}
}
return false;
};
auto GetComponentLibraryFileName = [&](const StringRef &Lib,
const bool Shared) {
std::string LibFileName;
if (Shared) {
if (Lib == DyLibName) {
assert(Lib.endswith(SharedExt) && "DyLib is missing suffix");
LibFileName = std::string(Lib);
} else {
LibFileName = (SharedPrefix + Lib + "." + SharedExt).str();
}
} else {
LibFileName = (StaticPrefix + Lib + "." + StaticExt).str();
}
return LibFileName;
};
auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
auto LibFileName = GetComponentLibraryFileName(Name, Shared);
if (Shared) {
return (SharedDir + DirSep + LibFileName).str();
} else {
return (StaticDir + DirSep + LibFileName).str();
}
};
raw_ostream &OS = outs();
for (int i = 1; i != argc; ++i) {
StringRef Arg = argv[i];
if (Arg.startswith("-")) {
HasAnyOption = true;
if (Arg == "--version") {
OS << PACKAGE_VERSION << '\n';
} else if (Arg == "--prefix") {
OS << ActivePrefix << '\n';
} else if (Arg == "--bindir") {
OS << ActiveBinDir << '\n';
} else if (Arg == "--includedir") {
OS << ActiveIncludeDir << '\n';
} else if (Arg == "--libdir") {
OS << ActiveLibDir << '\n';
} else if (Arg == "--cmakedir") {
OS << ActiveCMakeDir << '\n';
} else if (Arg == "--cppflags") {
OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << '\n';
} else if (Arg == "--cflags") {
OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << '\n';
} else if (Arg == "--cxxflags") {
OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << '\n';
} else if (Arg == "--ldflags") {
OS << ((HostTriple.isWindowsMSVCEnvironment()) ? "-LIBPATH:" : "-L")
<< ActiveLibDir << ' ' << LLVM_LDFLAGS << '\n';
} else if (Arg == "--system-libs") {
PrintSystemLibs = true;
} else if (Arg == "--libs") {
PrintLibs = true;
} else if (Arg == "--libnames") {
PrintLibNames = true;
} else if (Arg == "--libfiles") {
PrintLibFiles = true;
} else if (Arg == "--components") {
std::vector<std::string> Components;
for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
continue;
Components.push_back(AvailableComponents[j].Name);
if (AvailableComponents[j].Library && !IsInDevelopmentTree) {
std::string path(
GetComponentLibraryPath(AvailableComponents[j].Library, false));
if (DirSep == "\\") {
std::replace(path.begin(), path.end(), '/', '\\');
}
if (DyLibExists && !sys::fs::exists(path)) {
Components =
GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
llvm::sort(Components);
break;
}
}
}
for (unsigned I = 0; I < Components.size(); ++I) {
if (I) {
OS << ' ';
}
OS << Components[I];
}
OS << '\n';
} else if (Arg == "--targets-built") {
OS << LLVM_TARGETS_BUILT << '\n';
} else if (Arg == "--host-target") {
OS << Triple::normalize(LLVM_DEFAULT_TARGET_TRIPLE) << '\n';
} else if (Arg == "--build-mode") {
OS << build_mode << '\n';
} else if (Arg == "--assertion-mode") {
#if defined(NDEBUG)
OS << "OFF\n";
#else
OS << "ON\n";
#endif
} else if (Arg == "--build-system") {
OS << LLVM_BUILD_SYSTEM << '\n';
} else if (Arg == "--has-rtti") {
OS << (LLVM_HAS_RTTI ? "YES" : "NO") << '\n';
} else if (Arg == "--shared-mode") {
PrintSharedMode = true;
} else if (Arg == "--obj-root") {
OS << ActivePrefix << '\n';
} else if (Arg == "--src-root") {
OS << LLVM_SRC_ROOT << '\n';
} else if (Arg == "--ignore-libllvm") {
LinkDyLib = false;
LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto;
} else if (Arg == "--link-shared") {
LinkMode = LinkModeShared;
} else if (Arg == "--link-static") {
LinkMode = LinkModeStatic;
} else if (Arg == "--help") {
usage(false);
} else {
usage();
}
} else {
Components.push_back(Arg);
}
}
if (!HasAnyOption)
usage();
if (LinkMode == LinkModeShared && !DyLibExists && !BuiltSharedLibs) {
WithColor::error(errs(), "llvm-config") << DyLibName << " is missing\n";
return 1;
}
if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs ||
PrintSharedMode) {
if (PrintSharedMode && BuiltSharedLibs) {
OS << "shared\n";
return 0;
}
if (Components.empty())
Components.push_back("all");
std::function<std::string(const StringRef &)>
GetComponentLibraryPathFunction = [&](const StringRef &Name) {
return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
};
std::vector<std::string> MissingLibs;
std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
Components,
IsInDevelopmentTree, false,
&GetComponentLibraryPathFunction, &MissingLibs, DirSep);
if (!MissingLibs.empty()) {
switch (LinkMode) {
case LinkModeShared:
if (LinkDyLib && !BuiltSharedLibs)
break;
for (auto &Lib : MissingLibs)
WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n";
return 1;
case LinkModeAuto:
if (DyLibExists) {
LinkMode = LinkModeShared;
break;
}
WithColor::error(errs(), "llvm-config")
<< "component libraries and shared library\n\n";
LLVM_FALLTHROUGH;
case LinkModeStatic:
for (auto &Lib : MissingLibs)
WithColor::error(errs(), "llvm-config") << "missing: " << Lib << "\n";
return 1;
}
} else if (LinkMode == LinkModeAuto) {
LinkMode = LinkModeStatic;
}
if (PrintSharedMode) {
std::unordered_set<std::string> FullDyLibComponents;
std::vector<std::string> DyLibComponents =
GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
for (auto &Component : DyLibComponents) {
FullDyLibComponents.insert(Component);
}
DyLibComponents.clear();
for (auto &Lib : RequiredLibs) {
if (!FullDyLibComponents.count(Lib)) {
OS << "static\n";
return 0;
}
}
FullDyLibComponents.clear();
if (LinkMode == LinkModeShared) {
OS << "shared\n";
return 0;
} else {
OS << "static\n";
return 0;
}
}
if (PrintLibs || PrintLibNames || PrintLibFiles) {
auto PrintForLib = [&](const StringRef &Lib) {
const bool Shared = LinkMode == LinkModeShared;
if (PrintLibNames) {
OS << GetComponentLibraryFileName(Lib, Shared);
} else if (PrintLibFiles) {
OS << GetComponentLibraryPath(Lib, Shared);
} else if (PrintLibs) {
if (HostTriple.isWindowsMSVCEnvironment()) {
OS << GetComponentLibraryPath(Lib, Shared);
} else {
StringRef LibName;
if (GetComponentLibraryNameSlice(Lib, LibName)) {
OS << "-l" << LibName;
} else {
OS << "-l" << Lib;
}
}
}
};
if (LinkMode == LinkModeShared && LinkDyLib) {
PrintForLib(DyLibName);
} else {
for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
auto Lib = RequiredLibs[i];
if (i)
OS << ' ';
PrintForLib(Lib);
}
}
OS << '\n';
}
if (PrintSystemLibs) {
OS << (LinkMode == LinkModeStatic ? LLVM_SYSTEM_LIBS : "") << '\n';
}
} else if (!Components.empty()) {
WithColor::error(errs(), "llvm-config")
<< "components given, but unused\n\n";
usage();
}
return 0;
}