#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/config.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdlib>
#include <set>
#include <vector>
using namespace llvm;
#include "BuildVariables.inc"
#include "LibraryDependencies.inc"
static void VisitComponent(StringRef Name,
const StringMap<AvailableComponent*> &ComponentMap,
std::set<AvailableComponent*> &VisitedComponents,
std::vector<StringRef> &RequiredLibs,
bool IncludeNonInstalled) {
AvailableComponent *AC = ComponentMap.lookup(Name);
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);
}
if (AC->Library)
RequiredLibs.push_back(AC->Library);
}
void ComputeLibsForComponents(const std::vector<StringRef> &Components,
std::vector<StringRef> &RequiredLibs,
bool IncludeNonInstalled) {
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);
}
std::reverse(RequiredLibs.begin(), RequiredLibs.end());
}
void usage() {
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\
--version Print LLVM version.\n\
--prefix Print the installation prefix.\n\
--src-root Print the source root LLVM was built from.\n\
--obj-root Print the object root used to build LLVM.\n\
--bindir Directory containing LLVM executables.\n\
--includedir Directory containing LLVM headers.\n\
--libdir Directory containing LLVM libraries.\n\
--cppflags C preprocessor flags for files that include LLVM headers.\n\
--cflags C compiler flags for files that include LLVM headers.\n\
--cxxflags C++ compiler flags for files that include LLVM headers.\n\
--ldflags Print Linker flags.\n\
--system-libs System Libraries needed to link against LLVM components.\n\
--libs Libraries needed to link against LLVM components.\n\
--libnames Bare library names for in-tree builds.\n\
--libfiles Fully qualified library filenames for makefile depends.\n\
--components List of all possible components.\n\
--targets-built List of all targets currently built.\n\
--host-target Target triple used to configure LLVM.\n\
--build-mode Print build mode of LLVM tree (e.g. Debug or Release).\n\
--assertion-mode Print assertion mode of LLVM tree (ON or OFF).\n\
Typical components:\n\
all All LLVM libraries (default).\n\
engine Either a native JIT or a bitcode interpreter.\n";
exit(1);
}
std::string GetExecutablePath(const char *Argv0) {
void *P = (void*) (intptr_t) GetExecutablePath;
return llvm::sys::fs::getMainExecutable(Argv0, P);
}
int main(int argc, char **argv) {
std::vector<StringRef> Components;
bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;
bool PrintSystemLibs = false;
bool HasAnyOption = false;
bool IsInDevelopmentTree;
enum { MakefileStyle, 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,
Twine(LLVM_OBJ_ROOT) + "/" + build_mode)) {
IsInDevelopmentTree = true;
DevelopmentTreeLayout = MakefileStyle;
if (sys::path::filename(LLVM_OBJ_ROOT) == "BuildTools") {
ActiveObjRoot = sys::path::parent_path(LLVM_OBJ_ROOT);
} else {
ActiveObjRoot = LLVM_OBJ_ROOT;
}
} else if (sys::fs::equivalent(CurrentExecPrefix, LLVM_OBJ_ROOT)) {
IsInDevelopmentTree = true;
DevelopmentTreeLayout = CMakeStyle;
ActiveObjRoot = LLVM_OBJ_ROOT;
} else if (sys::fs::equivalent(CurrentExecPrefix,
Twine(LLVM_OBJ_ROOT) + "/bin")) {
IsInDevelopmentTree = true;
DevelopmentTreeLayout = CMakeBuildModeStyle;
ActiveObjRoot = LLVM_OBJ_ROOT;
} else {
IsInDevelopmentTree = false;
DevelopmentTreeLayout = MakefileStyle; }
std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir;
std::string ActiveIncludeOption;
if (IsInDevelopmentTree) {
ActiveIncludeDir = std::string(LLVM_SRC_ROOT) + "/include";
ActivePrefix = CurrentExecPrefix;
switch (DevelopmentTreeLayout) {
case MakefileStyle:
ActivePrefix = ActiveObjRoot;
ActiveBinDir = ActiveObjRoot + "/" + build_mode + "/bin";
ActiveLibDir = ActiveObjRoot + "/" + build_mode + "/lib";
break;
case CMakeStyle:
ActiveBinDir = ActiveObjRoot + "/bin";
ActiveLibDir = ActiveObjRoot + "/lib";
break;
case CMakeBuildModeStyle:
ActivePrefix = ActiveObjRoot;
ActiveBinDir = ActiveObjRoot + "/bin/" + build_mode;
ActiveLibDir = ActiveObjRoot + "/lib/" + build_mode;
break;
}
ActiveIncludeOption = ("-I" + ActiveIncludeDir + " " +
"-I" + ActiveObjRoot + "/include");
} else {
ActivePrefix = CurrentExecPrefix;
ActiveIncludeDir = ActivePrefix + "/include";
ActiveBinDir = ActivePrefix + "/bin";
ActiveLibDir = ActivePrefix + "/lib";
ActiveIncludeOption = "-I" + ActiveIncludeDir;
}
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 == "--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 << "-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") {
for (unsigned j = 0; j != array_lengthof(AvailableComponents); ++j) {
if (!AvailableComponents[j].IsInstalled && !IsInDevelopmentTree)
continue;
OS << ' ';
OS << AvailableComponents[j].Name;
}
OS << '\n';
} else if (Arg == "--targets-built") {
OS << LLVM_TARGETS_BUILT << '\n';
} else if (Arg == "--host-target") {
OS << 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 == "--obj-root") {
OS << ActivePrefix << '\n';
} else if (Arg == "--src-root") {
OS << LLVM_SRC_ROOT << '\n';
} else {
usage();
}
} else {
Components.push_back(Arg);
}
}
if (!HasAnyOption)
usage();
if (PrintLibs || PrintLibNames || PrintLibFiles || PrintSystemLibs) {
if (Components.empty())
Components.push_back("all");
std::vector<StringRef> RequiredLibs;
ComputeLibsForComponents(Components, RequiredLibs,
IsInDevelopmentTree);
for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
StringRef Lib = RequiredLibs[i];
if (i)
OS << ' ';
if (PrintLibNames) {
OS << Lib;
} else if (PrintLibFiles) {
OS << ActiveLibDir << '/' << Lib;
} else if (PrintLibs) {
if (Lib.startswith("lib") && Lib.endswith(".a")) {
OS << "-l" << Lib.slice(3, Lib.size()-2);
continue;
}
OS << ActiveLibDir << '/' << Lib;
}
}
OS << '\n';
if (PrintSystemLibs)
OS << LLVM_SYSTEM_LIBS << '\n';
} else if (!Components.empty()) {
errs() << "llvm-config: error: components given, but unused\n\n";
usage();
}
return 0;
}