Skip to content

Commit

Permalink
Fix llvm-config to emit the linker flag for the combined shared obj…
Browse files Browse the repository at this point in the history
…ect built by autoconfig/make instead of the individual components.

Summary:
When LLVM is configured to build shared libraries, CMake builds each component as it's own shared object, while autoconfig/make builds them statically and then links them all together to create a single shared object. This change adds compile time config flags to `llvm-config` so it can know whether LLVM's components are separated or not and act accordingly.

This fixes `llvm-config` instead of fixing the makefiles to behave like CMake because, AIUI, LLVM's autoconfig/make build system is on the way out anyway.

This change only affects `llvm-config` from builds that use autoconfig/make.

Reviewers: jfb

Subscribers: echristo, dschuff, llvm-commits

Differential Revision: http://reviews.llvm.org/D11392

llvm-svn: 243297
  • Loading branch information
jfbastien committed Jul 27, 2015
1 parent 83934d3 commit ba70e9e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions llvm/bindings/ocaml/Makefile.ocaml
Expand Up @@ -277,6 +277,8 @@ uninstall-local:: uninstall-deplibs

build-deplibs: $(OutputLibs)

$(OcamlDir)/%.so: $(LibDir)/%.so
$(Verb) ln -sf $< $@
$(OcamlDir)/%.a: $(LibDir)/%.a
$(Verb) ln -sf $< $@

Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-config/BuildVariables.inc.in
Expand Up @@ -26,3 +26,5 @@
#define LLVM_LIBDIR_SUFFIX "@LLVM_LIBDIR_SUFFIX@"
#define LLVM_TARGETS_BUILT "@LLVM_TARGETS_BUILT@"
#define LLVM_SYSTEM_LIBS "@LLVM_SYSTEM_LIBS@"
#define BUILD_SHARED_LIBS "@BUILD_SHARED_LIBS@"
#define WAS_BUILT_WITH_CMAKE "@WAS_BUILT_WITH_CMAKE@"
1 change: 1 addition & 0 deletions llvm/tools/llvm-config/CMakeLists.txt
Expand Up @@ -20,6 +20,7 @@ set(LLVM_CXXFLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_
set(LLVM_LDFLAGS "${CMAKE_CXX_LINK_FLAGS}")
set(LLVM_BUILDMODE ${CMAKE_BUILD_TYPE})
set(LLVM_SYSTEM_LIBS ${SYSTEM_LIBS})
set(WAS_BUILT_WITH_CMAKE "ON")
string(REPLACE ";" " " LLVM_TARGETS_BUILT "${LLVM_TARGETS_TO_BUILD}")
configure_file(${BUILDVARIABLES_SRCPATH} ${BUILDVARIABLES_OBJPATH} @ONLY)

Expand Down
6 changes: 6 additions & 0 deletions llvm/tools/llvm-config/Makefile
Expand Up @@ -65,6 +65,12 @@ $(ObjDir)/BuildVariables.inc: $(BUILDVARIABLES_SRCPATH) Makefile $(ObjDir)/.dir
>> temp.sed
$(Verb) $(ECHO) 's/@LLVM_TARGETS_BUILT@/$(subst /,\/,$(TARGETS_TO_BUILD))/' \
>> temp.sed
$(if $(filter-out $(ENABLE_SHARED),0),\
$(Verb) $(ECHO) 's/@BUILD_SHARED_LIBS@/ON/',\
$(Verb) $(ECHO) 's/@BUILD_SHARED_LIBS@/OFF/') \
>> temp.sed
$(Verb) $(ECHO) 's/@WAS_BUILT_WITH_CMAKE@/OFF/' \
>> temp.sed
$(Verb) $(SED) -f temp.sed < $< > $@
$(Verb) $(RM) temp.sed

Expand Down
43 changes: 41 additions & 2 deletions llvm/tools/llvm-config/llvm-config.cpp
Expand Up @@ -349,6 +349,30 @@ int main(int argc, char **argv) {
/*IncludeNonInstalled=*/IsInDevelopmentTree);

if (PrintLibs || PrintLibNames || PrintLibFiles) {
// If LLVM was built as a shared library, there will be only one thing
// that users should link against.
const bool IsSharedLib = (std::strcmp(BUILD_SHARED_LIBS, "ON") == 0);
const bool WasBuiltWithCMake = (std::strcmp(WAS_BUILT_WITH_CMAKE, "ON") == 0);
// CMake correctly builds components as separate shared libraries, however
// autoconfig/make builds components a static libraries and then links
// them all together to form a single shared library. Thus, only when
// `WAS_BUILT_WITH_CMAKE` is `OFF` and `BUILD_SHARED_LIBS` is `ON` do we
// override `RequiredLibs` with the single library name.
if (IsSharedLib && !WasBuiltWithCMake) {
RequiredLibs.clear();
std::string Name = "libLLVM-" PACKAGE_VERSION;
const Triple HostTriple(LLVM_DEFAULT_TARGET_TRIPLE);
if (HostTriple.isOSWindows()) {
Name += ".dll";
} else if (HostTriple.isOSDarwin()) {
Name += ".dylib";
} else {
// default to linux' ext:
Name += ".so";
}
RequiredLibs.push_back(Name);
}

for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
StringRef Lib = RequiredLibs[i];
if (i)
Expand All @@ -360,8 +384,23 @@ int main(int argc, char **argv) {
OS << ActiveLibDir << '/' << Lib;
} else if (PrintLibs) {
// If this is a typical library name, include it using -l.
if (Lib.startswith("lib") && Lib.endswith(".a")) {
OS << "-l" << Lib.slice(3, Lib.size()-2);
if (Lib.startswith("lib")) {
size_t FromEnd = 0;
if (Lib.endswith(".a")) {
FromEnd = 2;
} else if (Lib.endswith(".so")) {
FromEnd = 3;
} else if (Lib.endswith(".dylib")) {
FromEnd = 6;
} else {
FromEnd = 0;
}

if (FromEnd != 0) {
OS << "-l" << Lib.slice(3, Lib.size() - FromEnd);
} else {
OS << "-l:" << Lib;
}
continue;
}

Expand Down

0 comments on commit ba70e9e

Please sign in to comment.