From 0697f410b05085cdaf767d5b6fd3fea056ec5bd3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 8 Mar 2023 12:53:25 +0100 Subject: Add support for mold, add optimization flags for linking step --- src/CmakeModule.cpp | 4 +++- src/FileUtil.cpp | 10 ++++++++++ src/Linker.cpp | 4 ++++ src/main.cpp | 22 ++++++++++++++-------- 4 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/CmakeModule.cpp b/src/CmakeModule.cpp index e3b3918..063cced 100644 --- a/src/CmakeModule.cpp +++ b/src/CmakeModule.cpp @@ -239,7 +239,9 @@ namespace sibs } } - if(!config.linker.empty()) + if(config.linker == "mold") + cflags += TINYDIR_STRING(" -B/usr/lib/mold/"); + else if(!config.linker.empty()) cflags += TINYDIR_STRING(" -fuse-ld=") + toFileString(config.linker); cxxflags = cflags; diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp index e1142ec..02acfa4 100644 --- a/src/FileUtil.cpp +++ b/src/FileUtil.cpp @@ -141,6 +141,11 @@ namespace sibs else return FileType::FILE_NOT_FOUND; } + + bool fileExists(const _tinydir_char_t *path) { + struct stat64 fileStat; + return stat64(path, &fileStat) == 0; + } Result getFileLastModifiedTime(const _tinydir_char_t *path) { @@ -163,6 +168,11 @@ namespace sibs else return FileType::FILE_NOT_FOUND; } + + bool fileExists(const _tinydir_char_t *path) { + struct _stat64i32 fileStat; + return _wstat(path, &fileStat) == 0; + } Result getFileLastModifiedTime(const _tinydir_char_t *path) { diff --git a/src/Linker.cpp b/src/Linker.cpp index 1546909..d03e3a4 100644 --- a/src/Linker.cpp +++ b/src/Linker.cpp @@ -46,4 +46,8 @@ namespace sibs bool is_lld_linker_installed() { return is_linker_installed("ld.lld"); } + + bool is_mold_linker_installed() { + return is_linker_installed("ld.mold") && fileExists("/usr/lib/mold/ld"); + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c67ac68..aa06549 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -129,7 +129,7 @@ static void usage() static void usageBuild(bool run) { - printf("Usage: sibs %s [project_path] [--debug|--release] [--sanitize=(address|undefined|leak|thread|none)] [--linker=linker] [--platform ]\n\n", run ? "run" : "build"); + printf("Usage: sibs %s [project_path] [--debug|--release] [--sanitize=(address|undefined|leak|thread|none)] [--linker=lld|gold|mold] [--platform ]\n\n", run ? "run" : "build"); printf("%s a sibs project\n\n", run ? "Build and run" : "Build"); printf("Options:\n"); printf(" project_path The directory containing a project.conf file - Optional (default: current directory)\n"); @@ -138,7 +138,7 @@ static void usageBuild(bool run) printf(" --platform The platform to build for - Optional (default: the running platform)\n"); printf(" --lto Use link-time optimization. May increase compile times - Optional (default: not used)\n"); printf(" --debug-symbols Include debug symbols in release mode - Optional (default: not used)\n"); - printf(" --linker=linker The linker to use. \"lld\" or \"gold\" is the default linker when using gcc\n"); + printf(" --linker=linker The linker to use. \"lld\", \"gold\" or \"mold\". \"gold\" - Optional (the compile automatically chooses best option)\n"); if(run) { printf(" --args A list of arguments to run the program with\n"); } else { @@ -173,7 +173,7 @@ static void usageNew() static void usageTest() { - printf("Usage: sibs test [project_path] [--debug|--release] [--sanitize=(address|undefined|leak|thread|none)] [--linker=linker] [--file ...|--all-files]\n\n"); + printf("Usage: sibs test [project_path] [--debug|--release] [--sanitize=(address|undefined|leak|thread|none)] [--linker=lld|gold|mold] [--file ...|--all-files]\n\n"); printf("Build and run tests for a sibs project\n\n"); printf("Options:\n"); printf(" project_path The directory containing a project.conf file - Optional (default: current directory)\n"); @@ -182,7 +182,7 @@ static void usageTest() printf(" --sanitize=option Add runtime address/undefined behavior sanitization. Program can be up to 3 times slower and use 10 times as much RAM. Ignored if compiler doesn't support sanitization - Optional (default: none)\n"); printf(" --file Specify file to test, path to test file should be defined after this. Can be defined multiple times to test multiple files - Optional (default: not used), Only applicable for Zig\n"); printf(" --all-files Test all files - Optional (default: not used), Only applicable for Zig\n"); - printf(" --linker=linker The linker to use. \"lld\" or \"gold\" is the default linker when using gcc\n"); + printf(" --linker=linker The linker to use. \"lld\", \"gold\" or \"mold\". \"gold\" - Optional (the compile automatically chooses best option)\n"); printf(" --skip-compile Skip compilation. This can be used to generate a compile_commands.json file without compiling. Note that the compile_commands.json can miss files that are generated when this option is used. This option also skips running the tests\n"); printf(" --args A list of arguments to run the program with\n"); printf("Examples:\n"); @@ -664,16 +664,22 @@ static int buildProject(int argc, const _tinydir_char_t **argv, bool run) Compiler compiler = Compiler::MSVC; #endif - // TODO: Make mold the default linker if it's installed and the installed gcc version supports it with -fuse-ld= option if(linker.empty()) { // Static object files compiled with gcc are not compatible with lld (llvm linker) // and we dont know which compiler was used to compile to code so we disable automatic // use of lld by default. The user can still force lld with --linker=lld, if they are // sure that they used clang to compile the code. - if(!use_lto && optimizationLevel != OPT_LEV_DEBUG && !include_debug_symbols_in_release && is_lld_linker_installed()) - linker = "lld"; - else if(is_gold_linker_installed()) + // TODO: Detect if linker has change and recompile everything (that was compiled with a different linker). + if(!use_lto && optimizationLevel != OPT_LEV_DEBUG && !include_debug_symbols_in_release) { + if(is_mold_linker_installed()) + linker = "mold"; + else if(is_lld_linker_installed()) + linker = "lld"; + else if(is_gold_linker_installed()) + linker = "gold"; + } else if(is_gold_linker_installed()) { linker = "gold"; + } } SibsConfig sibsConfig(compiler, projectPath, optimizationLevel, false); -- cgit v1.2.3