Skip to content

Commit

Permalink
Add -link-defaultlib-shared and rename -link-debuglib (ldc-developers…
Browse files Browse the repository at this point in the history
…#2443)

... to -link-defaultlib-debug (with alias for backwards compatibility).

-link-defaultlib-shared is to be used for switching between static and
shared default libs to be linked with.

It defaults to true when generating shared libraries (if shared
druntime/Phobos are supported for the target, i.e., not for Windows).
This is a benign breaking change!
  • Loading branch information
kinke authored Jan 12, 2018
1 parent 3e54312 commit 1572c91
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 39 deletions.
5 changes: 5 additions & 0 deletions driver/cl_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ cl::opt<std::string>
"'-deps' alone prints module dependencies "
"(imports/file/version/debug/lib)"));

cl::opt<cl::boolOrDefault>
staticFlag("static", llvm::cl::ZeroOrMore,
llvm::cl::desc("Create a statically linked binary, including "
"all system dependencies"));

cl::opt<bool> m32bits("m32", cl::desc("32 bit target"), cl::ZeroOrMore);

cl::opt<bool> m64bits("m64", cl::desc("64 bit target"), cl::ZeroOrMore);
Expand Down
1 change: 1 addition & 0 deletions driver/cl_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extern cl::opt<std::string> moduleDeps;
extern cl::opt<std::string> cacheDir;
extern cl::list<std::string> linkerSwitches;
extern cl::list<std::string> ccSwitches;
extern cl::opt<cl::boolOrDefault> staticFlag;

extern cl::opt<bool> m32bits;
extern cl::opt<bool> m64bits;
Expand Down
9 changes: 2 additions & 7 deletions driver/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@

//////////////////////////////////////////////////////////////////////////////

static llvm::cl::opt<llvm::cl::boolOrDefault>
staticFlag("static", llvm::cl::ZeroOrMore,
llvm::cl::desc("Create a statically linked binary, including "
"all system dependencies"));

#if LDC_WITH_LLD
static llvm::cl::opt<bool>
useInternalLinker("link-internally", llvm::cl::ZeroOrMore, llvm::cl::Hidden,
Expand Down Expand Up @@ -134,10 +129,10 @@ int linkObjToBinary() {
createDirectoryForFileOrFail(gExePath);

if (global.params.targetTriple->isWindowsMSVCEnvironment()) {
return linkObjToBinaryMSVC(gExePath, useInternalLinker, staticFlag);
return linkObjToBinaryMSVC(gExePath, useInternalLinker, opts::staticFlag);
}

return linkObjToBinaryGcc(gExePath, useInternalLinker, staticFlag);
return linkObjToBinaryGcc(gExePath, useInternalLinker, opts::staticFlag);
}

//////////////////////////////////////////////////////////////////////////////
Expand Down
62 changes: 49 additions & 13 deletions driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,34 @@ static cl::list<std::string, StringsAdapter>
cl::value_desc("directory"), cl::location(impPathsStore),
cl::Prefix);

static cl::OptionCategory
defaultLibsCategory("Libraries linked with by default");

static cl::opt<std::string>
defaultLib("defaultlib", cl::ZeroOrMore, cl::value_desc("lib1,lib2,..."),
cl::desc("Default libraries to link with (overrides previous)"));
cl::desc("Default libraries to link with (overrides previous)"),
cl::cat(defaultLibsCategory));

static cl::opt<std::string> debugLib(
"debuglib", cl::ZeroOrMore, cl::value_desc("lib1,lib2,..."),
cl::desc("Debug versions of default libraries (overrides previous)"));

static cl::opt<bool> linkDebugLib(
"link-debuglib", cl::ZeroOrMore,
cl::desc("Link with libraries specified in -debuglib, not -defaultlib"));
"debuglib", cl::ZeroOrMore, cl::Hidden, cl::value_desc("lib1,lib2,..."),
cl::desc("Debug versions of default libraries (overrides previous). If the "
"option is omitted, LDC will append -debug to the -defaultlib "
"names when linking with -link-defaultlib-debug"),
cl::cat(defaultLibsCategory));

static cl::opt<bool> linkDefaultLibDebug(
"link-defaultlib-debug", cl::ZeroOrMore,
cl::desc("Link with debug versions of default libraries"),
cl::cat(defaultLibsCategory));
static cl::alias _linkDebugLib("link-debuglib", cl::Hidden,
cl::aliasopt(linkDefaultLibDebug),
cl::desc("Alias for -link-defaultlib-debug"),
cl::cat(defaultLibsCategory));

static cl::opt<bool> linkDefaultLibShared(
"link-defaultlib-shared", cl::ZeroOrMore,
cl::desc("Link with shared versions of default libraries"),
cl::cat(defaultLibsCategory));

// This function exits the program.
void printVersion(llvm::raw_ostream &OS) {
Expand Down Expand Up @@ -485,23 +502,42 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles,
}
}

// default libraries
if (noDefaultLib) {
deprecation(Loc(), "-nodefaultlib is deprecated, as -defaultlib/-debuglib "
"now override the existing list instead of appending to "
deprecation(Loc(), "-nodefaultlib is deprecated, as -defaultlib now "
"overrides the existing list instead of appending to "
"it. Please use the latter instead.");
} else if (!global.params.betterC) {
if (linkDefaultLibShared && staticFlag == cl::BOU_TRUE) {
error(Loc(), "Can't use -link-defaultlib-shared and -static together");
}

const bool addDebugSuffix =
(linkDefaultLibDebug && debugLib.getNumOccurrences() == 0);
// Default to shared default libs for DLLs compiled without -static.
const bool addSharedSuffix =
linkDefaultLibShared ||
(linkDefaultLibShared.getNumOccurrences() == 0 && global.params.dll &&
staticFlag != cl::BOU_TRUE);

// Parse comma-separated default library list.
std::stringstream libNames(linkDebugLib ? debugLib : defaultLib);
std::stringstream libNames(
linkDefaultLibDebug && !addDebugSuffix ? debugLib : defaultLib);
while (libNames.good()) {
std::string lib;
std::getline(libNames, lib, ',');
if (lib.empty()) {
continue;
}

char *arg = static_cast<char *>(mem.xmalloc(lib.size() + 3));
strcpy(arg, "-l");
strcpy(arg + 2, lib.c_str());
std::ostringstream os;
os << "-l" << lib;
if (addDebugSuffix)
os << "-debug";
if (addSharedSuffix)
os << "-shared";

char *arg = mem.xstrdup(os.str().c_str());
global.params.linkswitches->push(arg);
}
}
Expand Down
3 changes: 1 addition & 2 deletions ldc2.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ default:
// default switches injected before all explicit command-line switches
switches = [
"-I@RUNTIME_DIR@/src",@SHARED_LIBS_RPATH@
"-defaultlib=druntime-ldc",
"-debuglib=druntime-ldc-debug"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
"-defaultlib=druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
];
// default switches appended after all explicit command-line switches
post-switches = [
Expand Down
3 changes: 1 addition & 2 deletions ldc2_install.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ default:
switches = [
"-I@INCLUDE_INSTALL_DIR@/ldc",
"-I@INCLUDE_INSTALL_DIR@",
"-defaultlib=phobos2-ldc,druntime-ldc",
"-debuglib=phobos2-ldc-debug,druntime-ldc-debug"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
"-defaultlib=phobos2-ldc,druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
];
// default switches appended after all explicit command-line switches
post-switches = [
Expand Down
3 changes: 1 addition & 2 deletions ldc2_phobos.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ default:
"-I@PROFILERT_DIR@/d",
"-I@JITRT_DIR@/d",
"-I@PHOBOS2_DIR@",@SHARED_LIBS_RPATH@
"-defaultlib=phobos2-ldc,druntime-ldc",
"-debuglib=phobos2-ldc-debug,druntime-ldc-debug"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
"-defaultlib=phobos2-ldc,druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
];
// default switches appended after all explicit command-line switches
post-switches = [
Expand Down
16 changes: 10 additions & 6 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ set(BUILD_BC_LIBS OFF CACHE BOOL "Buil
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include/d CACHE PATH "Path to install D modules to")
set(BUILD_SHARED_LIBS AUTO CACHE STRING "Whether to build the runtime as a shared library (ON|OFF|BOTH)")
set(D_FLAGS -w CACHE STRING "Runtime D compiler flags, separated by ';'")
set(D_FLAGS_DEBUG -g;-link-debuglib CACHE STRING "Runtime D compiler flags (debug libraries), separated by ';'")
set(D_FLAGS_DEBUG -g;-link-defaultlib-debug CACHE STRING "Runtime D compiler flags (debug libraries), separated by ';'")
set(D_FLAGS_RELEASE -O3;-release CACHE STRING "Runtime D compiler flags (release libraries), separated by ';'")
set(COMPILE_ALL_D_FILES_AT_ONCE ON CACHE BOOL "Compile all D files for a lib in a single command line instead of separately")
set(RT_ARCHIVE_WITH_LDC AUTO CACHE STRING "Whether to archive the static runtime libs via LDC instead of CMake archiver")
Expand Down Expand Up @@ -113,11 +113,7 @@ if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
if(NOT SHARED_LIBS_SUPPORTED)
message(FATAL_ERROR "Shared libraries (BUILD_SHARED_LIBS) are only supported on Linux, macOS and FreeBSD for the time being.")
endif()

# Only use the `-shared` lib suffix if static libs are generated too
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "ON")
set(SHARED_LIB_SUFFIX "-shared")
endif()
set(SHARED_LIB_SUFFIX "-shared")
endif()

# Auto-detect C system libraries
Expand Down Expand Up @@ -270,6 +266,14 @@ if("${TARGET_SYSTEM}" MATCHES "Linux|FreeBSD")
endif()
endif()

# Only have either shared or static libs?
# Then explicitly default to linking against them via default LDC switch.
if(${BUILD_SHARED_LIBS} STREQUAL "ON")
set(ADDITIONAL_DEFAULT_LDC_SWITCHES ",\n \"-link-defaultlib-shared\"")
elseif(${BUILD_SHARED_LIBS} STREQUAL "OFF")
set(ADDITIONAL_DEFAULT_LDC_SWITCHES ",\n \"-link-defaultlib-shared=false\"")
endif()

# Only generate the config files if this CMake project is embedded in the LDC CMake project.
if(LDC_EXE)
if(PHOBOS2_DIR)
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/exception_stack_trace.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %ldc -g -disable-fp-elim -link-debuglib %s -of=%t%exe
// RUN: %ldc -g -disable-fp-elim -link-defaultlib-debug %s -of=%t%exe
// RUN: %t%exe | FileCheck %s

void bar()
Expand Down
4 changes: 2 additions & 2 deletions tests/d2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ string(REGEX REPLACE "[^0-9]*([0-9]+[0-9.]*).*" "\\1" GDB_VERSION "${GDB_VERSION
# Would like to specify the "-release" flag for release builds, but some of the
# tests (e.g. 'testdstress') depend on contracts and invariants being active.
# Need a solution integrated with d_do_test.
add_testsuite("-debug" "-g -link-debuglib" "${gdb_flags}" ${host_model})
add_testsuite("-debug" "-g -link-defaultlib-debug" "${gdb_flags}" ${host_model})
add_testsuite("" -O "OFF" ${host_model})

if(MULTILIB AND host_model EQUAL 64)
# Also test in 32 bit mode on x86_64 multilib builds.
add_testsuite("-debug_32" "-g -link-debuglib" "${gdb_flags}" 32)
add_testsuite("-debug_32" "-g -link-defaultlib-debug" "${gdb_flags}" 32)
add_testsuite("_32" -O "OFF" 32)
endif()
8 changes: 4 additions & 4 deletions tests/driver/mscrtlib.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// REQUIRES: Windows

// RUN: %ldc -mscrtlib=libcmt -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=libcmtd -link-debuglib -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=msvcrt -link-debuglib -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=msvcrtd -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=libcmt -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=libcmtd -link-defaultlib-debug -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=msvcrt -link-defaultlib-debug -run "%S\..\d2\dmd-testsuite\runnable\eh.d"
// RUN: %ldc -mscrtlib=msvcrtd -run "%S\..\d2\dmd-testsuite\runnable\eh.d"

0 comments on commit 1572c91

Please sign in to comment.