From 8e8dda19284c2e3767aa7b97fb29edad56344aae Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 20 Sep 2018 21:00:42 +0200 Subject: Use cflags includes in zig build, ignore sibs-build dir for source files --- .vscode/settings.json | 3 +- backend/BackendUtils.cpp | 2 +- backend/ninja/Ninja.cpp | 106 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 92 insertions(+), 19 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 30413be..a06c734 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -59,5 +59,6 @@ "hash_map": "cpp", "valarray": "cpp", "unordered_set": "cpp" - } + }, + "C_Cpp.configurationWarnings": "Disabled" } \ No newline at end of file diff --git a/backend/BackendUtils.cpp b/backend/BackendUtils.cpp index b790978..26608c7 100644 --- a/backend/BackendUtils.cpp +++ b/backend/BackendUtils.cpp @@ -79,7 +79,7 @@ namespace backend string filePathUtf8 = toUtf8(pathNative.c_str()); ninjaProject->addTestSourceDir(filePathUtf8.c_str()); } - else if(recursive && !directoryToIgnore(pathNative, sibsConfig.getIgnoreDirs())) + else if(recursive && !directoryToIgnore(pathNative, sibsConfig.getIgnoreDirs()) && !_tinydir_strcmp(pathNative.c_str(), TINYDIR_STRING("sibs-build"))) { FileString projectConfPath = file->path; #if OS_FAMILY == OS_FAMILY_WINDOWS diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp index 9d5dc42..ca88f40 100644 --- a/backend/ninja/Ninja.cpp +++ b/backend/ninja/Ninja.cpp @@ -365,9 +365,9 @@ namespace backend if(config.getCompiler() == Compiler::GCC && config.getSanitize()) { return { - ninja::NinjaArg("-fno-omit-frame-pointer"), - ninja::NinjaArg("-fsanitize=address"), - ninja::NinjaArg("-fsanitize=undefined") + ninja::NinjaArg::createRaw("-fno-omit-frame-pointer"), + ninja::NinjaArg::createRaw("-fsanitize=address"), + ninja::NinjaArg::createRaw("-fsanitize=undefined") }; } return {}; @@ -384,19 +384,19 @@ namespace backend case OPT_LEV_DEBUG: { return { - ninja::NinjaArg("-Og"), - ninja::NinjaArg("-g3"), - ninja::NinjaArg("-D_FORTIFY_SOURCE=2"), - ninja::NinjaArg("-D_GLIBCXX_ASSERTIONS"), - ninja::NinjaArg("-fasynchronous-unwind-tables") + ninja::NinjaArg::createRaw("-Og"), + ninja::NinjaArg::createRaw("-g3"), + ninja::NinjaArg::createRaw("-D_FORTIFY_SOURCE=2"), + ninja::NinjaArg::createRaw("-D_GLIBCXX_ASSERTIONS"), + ninja::NinjaArg::createRaw("-fasynchronous-unwind-tables") }; } case OPT_LEV_RELEASE: { return { - ninja::NinjaArg("-O3"), - ninja::NinjaArg("-g0"), - ninja::NinjaArg("-DNDEBUG") + ninja::NinjaArg::createRaw("-O3"), + ninja::NinjaArg::createRaw("-g0"), + ninja::NinjaArg::createRaw("-DNDEBUG") }; } } @@ -409,16 +409,16 @@ namespace backend case OPT_LEV_DEBUG: { return { - ninja::NinjaArg("/Od"), - ninja::NinjaArg("/MTd") + ninja::NinjaArg::createRaw("/Od"), + ninja::NinjaArg::createRaw("/MTd") }; } case OPT_LEV_RELEASE: { return { - ninja::NinjaArg("/Ox"), - ninja::NinjaArg("/MT"), - ninja::NinjaArg("/DNDEBUG") + ninja::NinjaArg::createRaw("/Ox"), + ninja::NinjaArg::createRaw("/MT"), + ninja::NinjaArg::createRaw("/DNDEBUG") }; } } @@ -459,6 +459,74 @@ namespace backend return cIncludes; } + static vector extractIncludesFromCFlag(Compiler compiler, const ninja::NinjaArg &cflag) + { + vector result; + string includeStartStr; + + switch (compiler) + { + case Compiler::GCC: + { + includeStartStr = "-I"; + break; + } + case Compiler::MSVC: + { + includeStartStr = "/I"; + } + default: + assert(false); + break; + } + + size_t index = 0; + while(index != string::npos) + { + char endChar = ' '; + size_t includeStartIndex = cflag.arg.find(includeStartStr, index); + if(includeStartIndex == string::npos) + break; + else if(includeStartIndex > 0) + { + if(cflag.arg[includeStartIndex - 1] == '"' || cflag.arg[includeStartIndex - 1] == '\'') + endChar = cflag.arg[includeStartIndex - 1]; + } + includeStartIndex += includeStartStr.length(); + size_t includeEndIndex = cflag.arg.find(endChar, includeStartIndex); + index = includeEndIndex; + + if(includeEndIndex == string::npos) + includeEndIndex = cflag.arg.length(); + + result.push_back(ninja::NinjaArg::createRaw(cflag.arg.substr(includeStartIndex, includeEndIndex - includeStartIndex))); + } + + return result; + } + + static vector extractIncludesFromCFlags(Compiler compiler, const vector &cflags) + { + vector result; + for(const ninja::NinjaArg &cflag : cflags) + { + vector subCFlags = extractIncludesFromCFlag(compiler, cflag); + result.insert(result.end(), subCFlags.begin(), subCFlags.end()); + } + return result; + } + + static vector convertCFlagsIncludesToZigIncludes(Compiler compiler, const vector &cflags) + { + vector result; + result = extractIncludesFromCFlags(compiler, cflags); + for(ninja::NinjaArg &include : result) + { + include.arg = "-isystem \"" + include.arg + "\""; + } + return result; + } + Result Ninja::build(const SibsConfig &config, const _tinydir_char_t *savePath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback, GlobalIncludeDirCallbackFunc globalIncludeDirCallback) { if (!sourceFiles.empty()) @@ -739,7 +807,10 @@ namespace backend compileCppRule->depFile = "$out.d"; // TODO: Specify -mconsole or -mwindows for windows. - // TODO: Convert sibs defines to const variables in a zig file that other zig files can include (like a config file) + // TODO: Convert sibs defines to const variables in a zig file that other zig files can include (like a config file). + + // TODO: Find a way to do this more efficiently + vector cflagsIncludes = convertCFlagsIncludesToZigIncludes(config.getCompiler(), cflags); vector compileZigArgs = { ninja::NinjaArg::createRaw("zig build-obj"), ninja::NinjaArg::createRaw("$in"), @@ -749,6 +820,7 @@ namespace backend ninja::NinjaArg::createRaw("--library c"), // TODO: Remove this if project does not depend on c libraries or project only has .zig files ninja::NinjaArg::createRaw("$globalIncDirZig") }; + compileZigArgs.insert(compileZigArgs.end(), cflagsIncludes.begin(), cflagsIncludes.end()); if(config.getOptimizationLevel() == sibs::OPT_LEV_RELEASE) { -- cgit v1.2.3