From ee882db05077631c3d3136600c106c5ab4c4eeb6 Mon Sep 17 00:00:00 2001 From: Dicebot Date: Sat, 3 Sep 2016 16:33:44 +0300 Subject: [PATCH 1/3] Generate debug lib name automatically Deprecates -debuglib flag --- driver/main.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/driver/main.cpp b/driver/main.cpp index 0efa58ff407..54951fac573 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -99,12 +99,12 @@ static cl::opt static cl::opt debugLib( "debuglib", - cl::desc("Debug versions of default libraries (overrides previous)"), + cl::desc("(deprecated) Debug versions of default libraries"), cl::value_desc("lib1,lib2,..."), cl::ZeroOrMore); static cl::opt linkDebugLib( "link-debuglib", - cl::desc("Link with libraries specified in -debuglib, not -defaultlib"), + cl::desc("Link with debug versions of default libraries"), cl::ZeroOrMore); #if LDC_LLVM_VER >= 309 @@ -497,11 +497,30 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, deprecation( Loc(), "-nodefaultlib is deprecated, as " - "-defaultlib/-debuglib now override the existing list instead of " + "-defaultlib now overrides the existing list instead of " "appending to it. Please use the latter instead."); } else { // Parse comma-separated default library list. - std::stringstream libNames(linkDebugLib ? debugLib : defaultLib); + bool generatedDebugLib = false; + + if (debugLib.length() > 0) + { + // temporarily disabled to not affect expected test output + /* + deprecation( + Loc(), + "-debuglib is deprecated, as LDC generates names of " + "debug libraries automatically now by appending '-debug' " + "suffix to default ones" + ); + */ + } + else + generatedDebugLib = true; + + std::stringstream libNames(linkDebugLib && !generatedDebugLib + ? debugLib : defaultLib); + while (libNames.good()) { std::string lib; std::getline(libNames, lib, ','); @@ -509,9 +528,14 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, continue; } - char *arg = static_cast(mem.xmalloc(lib.size() + 3)); + size_t size = lib.size() + 3; + if (linkDebugLib && generatedDebugLib) + size += 6; + char *arg = static_cast(mem.xmalloc(size)); strcpy(arg, "-l"); strcpy(arg + 2, lib.c_str()); + if (linkDebugLib && generatedDebugLib) + strcpy(arg + size - 7, "-debug"); global.params.linkswitches->push(arg); } } From 27bbf40b7d4075344633bca17087d5d36f5af062 Mon Sep 17 00:00:00 2001 From: Dicebot Date: Sat, 3 Sep 2016 16:42:36 +0300 Subject: [PATCH 2/3] Make possible to choose shared or static lib -link-sharedlib will augment standard library names with '-shared' suffix and -link-sharedlib=false will cancel previously supplied -link-sharedlib --- driver/main.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/driver/main.cpp b/driver/main.cpp index 54951fac573..598154d3820 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -107,6 +107,17 @@ static cl::opt linkDebugLib( cl::desc("Link with debug versions of default libraries"), cl::ZeroOrMore); +static cl::opt linkSharedLib( + "link-sharedlib", + cl::desc("Link with shared versions of default libraries"), + cl::ZeroOrMore); + +static cl::opt staticFlag( + "static", + cl::desc( + "Create a statically linked binary, including all system dependencies"), + cl::ZeroOrMore); + #if LDC_LLVM_VER >= 309 static inline llvm::Optional getRelocModel() { if (mRelocModel.getNumOccurrences()) { @@ -493,6 +504,14 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, } } + if (linkSharedLib && staticFlag) + { + error( + Loc(), + "Can't use -link-sharedlib and -static together" + ); + } + if (noDefaultLib) { deprecation( Loc(), @@ -531,11 +550,16 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, size_t size = lib.size() + 3; if (linkDebugLib && generatedDebugLib) size += 6; + if (linkSharedLib) + size += 7; char *arg = static_cast(mem.xmalloc(size)); strcpy(arg, "-l"); strcpy(arg + 2, lib.c_str()); if (linkDebugLib && generatedDebugLib) - strcpy(arg + size - 7, "-debug"); + strcpy(arg + lib.length(), "-debug"); + if (linkSharedLib) + strcpy(arg + size - 8, "-shared"); + global.params.linkswitches->push(arg); } } From a71e4e1112dbeae9d49c796214f6c11a9a17d6f0 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 19 Mar 2017 19:33:14 +0100 Subject: [PATCH 3/3] Adapt Dicebot's prototype --- .travis.yml | 3 ++ driver/cl_options.cpp | 4 ++ driver/cl_options.h | 1 + driver/linker.cpp | 13 ++----- driver/main.cpp | 88 +++++++++++++++--------------------------- ldc2.conf.in | 3 +- ldc2_install.conf.in | 3 +- ldc2_phobos.conf.in | 3 +- runtime/CMakeLists.txt | 21 ++++++---- 9 files changed, 60 insertions(+), 79 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7b0b1b38283..78107faeba5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -96,6 +96,9 @@ script: if [[ "${OPTS}" == *-DMULTILIB?ON* ]]; then ninja -j2 phobos2-ldc-unittest-debug phobos2-ldc-unittest phobos2-ldc-unittest-debug_32 phobos2-ldc-unittest_32; ninja -j3 druntime-ldc-unittest-debug druntime-ldc-unittest druntime-ldc-unittest-debug_32 druntime-ldc-unittest_32; + elif [[ "${OPTS}" == *-DBUILD_SHARED_LIBS?ON* ]]; then + ninja -j2 phobos2-ldc-unittest-debug-shared phobos2-ldc-unittest-shared; + ninja -j3 druntime-ldc-unittest-debug-shared druntime-ldc-unittest-shared; else ninja -j2 phobos2-ldc-unittest-debug phobos2-ldc-unittest; ninja -j3 druntime-ldc-unittest-debug druntime-ldc-unittest; diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp index 9cac488403f..42469019f08 100644 --- a/driver/cl_options.cpp +++ b/driver/cl_options.cpp @@ -86,6 +86,10 @@ static cl::opt createSharedLib("shared", cl::desc("Create shared library (DLL)"), cl::ZeroOrMore, cl::location(global.params.dll)); +cl::opt staticFlag("static", cl::ZeroOrMore, + cl::desc("Create a statically linked binary, " + "including all system dependencies")); + static cl::opt verbose("v", cl::desc("Verbose"), cl::ZeroOrMore, cl::location(global.params.verbose)); diff --git a/driver/cl_options.h b/driver/cl_options.h index aa76b0b2ac2..7d4707d8c86 100644 --- a/driver/cl_options.h +++ b/driver/cl_options.h @@ -46,6 +46,7 @@ extern cl::list fileList; extern cl::list runargs; extern cl::opt invokedByLDMD; extern cl::opt compileOnly; +extern cl::opt staticFlag; extern cl::opt useDIP1000; extern cl::opt noAsm; extern cl::opt dontWriteObj; diff --git a/driver/linker.cpp b/driver/linker.cpp index e995048de1f..660b67bcb63 100644 --- a/driver/linker.cpp +++ b/driver/linker.cpp @@ -33,12 +33,6 @@ ////////////////////////////////////////////////////////////////////////////// -static llvm::cl::opt staticFlag( - "static", - llvm::cl::desc( - "Create a statically linked binary, including all system dependencies"), - llvm::cl::ZeroOrMore); - static llvm::cl::opt mscrtlib( "mscrtlib", llvm::cl::desc( @@ -323,7 +317,7 @@ static int linkObjToBinaryGcc(bool sharedLib) { args.push_back("-shared"); } - if (staticFlag) { + if (opts::staticFlag) { args.push_back("-static"); } @@ -499,8 +493,9 @@ static void addMscrtLibs(std::vector &args) { llvm::StringRef mscrtlibName = mscrtlib; if (mscrtlibName.empty()) { // default to static release variant - mscrtlibName = - staticFlag || staticFlag.getNumOccurrences() == 0 ? "libcmt" : "msvcrt"; + mscrtlibName = opts::staticFlag || opts::staticFlag.getNumOccurrences() == 0 + ? "libcmt" + : "msvcrt"; } args.push_back(("/DEFAULTLIB:" + mscrtlibName).str()); diff --git a/driver/main.cpp b/driver/main.cpp index 598154d3820..a9e6f027c59 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -99,24 +99,20 @@ static cl::opt static cl::opt debugLib( "debuglib", - cl::desc("(deprecated) Debug versions of default libraries"), - cl::value_desc("lib1,lib2,..."), cl::ZeroOrMore); + 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-debuglib"), + cl::value_desc("lib1,lib2,..."), cl::ZeroOrMore, cl::Hidden); -static cl::opt linkDebugLib( - "link-debuglib", - cl::desc("Link with debug versions of default libraries"), - cl::ZeroOrMore); - -static cl::opt linkSharedLib( - "link-sharedlib", - cl::desc("Link with shared versions of default libraries"), - cl::ZeroOrMore); +static cl::opt + linkDebugLib("link-debuglib", + cl::desc("Link with debug versions of default libraries"), + cl::ZeroOrMore); -static cl::opt staticFlag( - "static", - cl::desc( - "Create a statically linked binary, including all system dependencies"), - cl::ZeroOrMore); +static cl::opt + linkSharedLib("link-sharedlib", + cl::desc("Link with shared versions of default libraries"), + cl::ZeroOrMore); #if LDC_LLVM_VER >= 309 static inline llvm::Optional getRelocModel() { @@ -504,42 +500,25 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, } } - if (linkSharedLib && staticFlag) - { - error( - Loc(), - "Can't use -link-sharedlib and -static together" - ); + if (linkSharedLib && staticFlag) { + error(Loc(), "Can't use -link-sharedlib and -static together"); } + // default libraries if (noDefaultLib) { - deprecation( - Loc(), - "-nodefaultlib is deprecated, as " - "-defaultlib now overrides the existing list instead of " - "appending to it. Please use the latter instead."); + deprecation(Loc(), "-nodefaultlib is deprecated, as -defaultlib now " + "overrides the existing list instead of appending to " + "it. Please use the latter instead."); } else { - // Parse comma-separated default library list. - bool generatedDebugLib = false; - - if (debugLib.length() > 0) - { - // temporarily disabled to not affect expected test output - /* - deprecation( - Loc(), - "-debuglib is deprecated, as LDC generates names of " - "debug libraries automatically now by appending '-debug' " - "suffix to default ones" - ); - */ - } - else - generatedDebugLib = true; - - std::stringstream libNames(linkDebugLib && !generatedDebugLib - ? debugLib : defaultLib); + const bool addDebugSuffix = + (linkDebugLib && debugLib.getNumOccurrences() == 0); + const bool addSharedSuffix = + linkSharedLib || (linkSharedLib.getNumOccurrences() == 0 && + global.params.dll && !staticFlag); + // Parse comma-separated default library list. + std::stringstream libNames(linkDebugLib && !addDebugSuffix ? debugLib + : defaultLib); while (libNames.good()) { std::string lib; std::getline(libNames, lib, ','); @@ -547,18 +526,15 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles, continue; } - size_t size = lib.size() + 3; - if (linkDebugLib && generatedDebugLib) - size += 6; - if (linkSharedLib) - size += 7; + const size_t size = + lib.size() + 3 + (addDebugSuffix ? 6 : 0) + (addSharedSuffix ? 7 : 0); char *arg = static_cast(mem.xmalloc(size)); strcpy(arg, "-l"); strcpy(arg + 2, lib.c_str()); - if (linkDebugLib && generatedDebugLib) - strcpy(arg + lib.length(), "-debug"); - if (linkSharedLib) - strcpy(arg + size - 8, "-shared"); + if (addDebugSuffix) + strcpy(arg + 2 + lib.length(), "-debug"); + if (addSharedSuffix) + strcpy(arg + size - 8, "-shared"); global.params.linkswitches->push(arg); } diff --git a/ldc2.conf.in b/ldc2.conf.in index f5a9b9290a3..7605478fffb 100644 --- a/ldc2.conf.in +++ b/ldc2.conf.in @@ -9,7 +9,6 @@ default: switches = [ "-I@RUNTIME_DIR@/src", "-L-L@PROJECT_BINARY_DIR@/../lib@LIB_SUFFIX@", @MULTILIB_ADDITIONAL_PATH@@SHARED_LIBS_RPATH@ - "-defaultlib=druntime-ldc", - "-debuglib=druntime-ldc-debug"@ADDITIONAL_DEFAULT_LDC_SWITCHES@ + "-defaultlib=druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@ ]; }; diff --git a/ldc2_install.conf.in b/ldc2_install.conf.in index c73b7424522..07caa5f1aee 100644 --- a/ldc2_install.conf.in +++ b/ldc2_install.conf.in @@ -10,7 +10,6 @@ default: "-I@INCLUDE_INSTALL_DIR@/ldc", "-I@INCLUDE_INSTALL_DIR@", "-L-L@CMAKE_INSTALL_LIBDIR@", @MULTILIB_ADDITIONAL_INSTALL_PATH@ - "-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@ ]; }; diff --git a/ldc2_phobos.conf.in b/ldc2_phobos.conf.in index 02e87e444fc..b0d04512d0a 100644 --- a/ldc2_phobos.conf.in +++ b/ldc2_phobos.conf.in @@ -11,7 +11,6 @@ default: "-I@PROFILERT_DIR@/d", "-I@PHOBOS2_DIR@", "-L-L@CMAKE_BINARY_DIR@/lib@LIB_SUFFIX@", @MULTILIB_ADDITIONAL_PATH@@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@ ]; }; diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 66f34efb3ae..6ab0a58f523 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -49,10 +49,7 @@ if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF") 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() get_directory_property(PROJECT_PARENT_DIR DIRECTORY ${PROJECT_SOURCE_DIR} PARENT_DIRECTORY) @@ -206,6 +203,14 @@ if((${CMAKE_SYSTEM_NAME} MATCHES "Linux") OR (${CMAKE_SYSTEM_NAME} MATCHES "Free 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-sharedlib\"") +elseif(${BUILD_SHARED_LIBS} STREQUAL "OFF") + set(ADDITIONAL_DEFAULT_LDC_SWITCHES ",\n \"-link-sharedlib=false\"") +endif() + configure_file(${PROJECT_PARENT_DIR}/${CONFIG_NAME}.conf.in ${PROJECT_BINARY_DIR}/../bin/${LDC_EXE}.conf) # Prepare the config files we are going to install later in bin. configure_file(${PROJECT_PARENT_DIR}/${LDC_EXE}_install.conf.in ${PROJECT_BINARY_DIR}/../bin/${LDC_EXE}_install.conf) @@ -667,8 +672,8 @@ function(build_test_runners name_suffix path_suffix is_shared) add_test(NAME build-druntime-test-runner${target_suffix} COMMAND ${LDC_EXE_FULL} -of${PROJECT_BINARY_DIR}/druntime-test-runner${target_suffix}${CMAKE_EXECUTABLE_SUFFIX} - -defaultlib= -debuglib= - ${tmpflags} ${RUNTIME_DIR}/src/test_runner.d + ${tmpflags} -defaultlib= + ${RUNTIME_DIR}/src/test_runner.d ) set_tests_properties(build-druntime-test-runner${target_suffix} PROPERTIES DEPENDS build-druntime-ldc-unittest${target_suffix}) @@ -680,8 +685,8 @@ function(build_test_runners name_suffix path_suffix is_shared) add_test(NAME build-phobos2-test-runner${target_suffix} COMMAND ${LDC_EXE_FULL} -of${PROJECT_BINARY_DIR}/phobos2-test-runner${target_suffix}${CMAKE_EXECUTABLE_SUFFIX} - -defaultlib=${libarg} -debuglib=${libarg} - ${tmpflags} ${RUNTIME_DIR}/src/test_runner.d + ${tmpflags} -defaultlib=${libarg} -link-debuglib=false -link-sharedlib=false + ${RUNTIME_DIR}/src/test_runner.d ) set_tests_properties(build-phobos2-test-runner${target_suffix} PROPERTIES DEPENDS "build-druntime-ldc-unittest${target_suffix};build-phobos2-ldc-unittest${target_suffix}"