From 0bb8fff4d481f5076639d28b55daf76dd2b2ea0f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 7 Jan 2018 12:04:20 +0100 Subject: Add c/c++ language version option, compile c/cc files with c compiler --- backend/ninja/Ninja.cpp | 129 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 112 insertions(+), 17 deletions(-) (limited to 'backend') diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp index 4e94fcf..6d871fd 100644 --- a/backend/ninja/Ninja.cpp +++ b/backend/ninja/Ninja.cpp @@ -159,6 +159,57 @@ namespace backend } return result; } + + string getLanguageVersionFlag(Compiler compiler, CVersion cVersion) + { + switch (compiler) + { + case Compiler::GCC: + { + switch(cVersion) + { + case CVersion::C89: return "-std=c89 -pedantic"; + case CVersion::C99: return "-std=c99 -pedantic"; + case CVersion::C11: return "-std=c11 -pedantic"; + } + } + case Compiler::MSVC: + { + // TODO: Is it possible to specify c version in msvc? + return ""; + } + } + assert(false); + return ""; + } + + string getLanguageVersionFlag(Compiler compiler, CPPVersion cppVersion) + { + switch (compiler) + { + case Compiler::GCC: + { + switch(cppVersion) + { + case CPPVersion::CPP11: return "-std=c++11 -pedantic"; + case CPPVersion::CPP14: return "-std=c++14 -pedantic"; + case CPPVersion::CPP17: return "-std=c++17 -pedantic"; + } + } + case Compiler::MSVC: + { + switch(cppVersion) + { + // Use /Za flag? + case CPPVersion::CPP11: return "/std=c++11"; + case CPPVersion::CPP14: return "/std=c++14"; + case CPPVersion::CPP17: return "/std=c++17"; + } + } + } + assert(false); + return ""; + } const char* getObjectFileExtension(Compiler compiler) { @@ -296,6 +347,12 @@ namespace backend return GlobalLib::getLibs(globalLibDependencies, config, globalLibDir, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallback, globalIncludeDirCallback); } + + enum class SourceFileLanguage + { + C, + CPP + }; Result Ninja::build(const SibsConfig &config, const _tinydir_char_t *savePath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallback, GlobalIncludeDirCallbackFunc globalIncludeDirCallback) { @@ -429,17 +486,6 @@ namespace backend } #endif - string compilerName; - switch (config.getCompiler()) - { - case Compiler::GCC: - compilerName = "ccache c++"; - break; - case Compiler::MSVC: - compilerName = "cl.exe"; - break; - } - string buildJob; switch(libraryType) { @@ -454,6 +500,9 @@ namespace backend result += "rule cpp_BUILD_EXEC\n"; result += " command = ccache c++ $ARGS -o $out $in $LINK_ARGS $aliasing\n\n"; + + result += "rule c_COMPILER\n"; + result += " command = ccache cc $ARGS -c $in -o $out\n\n"; break; } case Compiler::MSVC: @@ -463,6 +512,9 @@ namespace backend result += "rule cpp_BUILD_EXEC\n"; result += " command = cl.exe $ARGS $in /Fe$out $LINK_ARGS\n\n"; + + result += "rule c_COMPILER\n"; + result += " command = cl.exe $ARGS /c $in /Fo$out\n\n"; break; } } @@ -484,6 +536,9 @@ namespace backend result += config.getPackageName(); result += ".a"; result += " $in\n\n"; + + result += "rule c_COMPILER\n"; + result += " command = ccache cc $ARGS -c -fPIC $in -o $out\n\n"; break; } case Compiler::MSVC: @@ -493,6 +548,9 @@ namespace backend result += "rule cpp_BUILD_STATIC\n"; result += " command = lib.exe /OUT:$out $in\n\n"; + + result += "rule c_COMPILER\n"; + result += " command = cl.exe $ARGS /c $in /Fo$out\n\n"; break; } } @@ -512,6 +570,9 @@ namespace backend // --whole-archive result += "rule cpp_BUILD_DYNAMIC\n"; result += " command = ccache c++ $in -shared -o $out $LINK_ARGS $aliasing\n\n"; + + result += "rule c_COMPILER\n"; + result += " command = ccache cc $ARGS -c -fPIC $in -o $out\n\n"; break; } case Compiler::MSVC: @@ -524,6 +585,9 @@ namespace backend result += "rule cpp_BUILD_DYNAMIC\n"; result += " command = lib.exe /OUT:$out $in\n\n"; + + result += "rule c_COMPILER\n"; + result += " command = cl.exe $ARGS /c $in /Fo$out\n\n"; break; } } @@ -580,27 +644,58 @@ namespace backend objectNames.reserve(sourceFiles.size()); for(const string &sourceFile : sourceFiles) { - string sourceFileLanguage = "c++"; - if(endsWith(sourceFile, ".c")) - sourceFileLanguage = "c"; + SourceFileLanguage sourceFileLanguage = SourceFileLanguage::CPP; + if(endsWith(sourceFile, ".c") || endsWith(sourceFile, ".cc")) + sourceFileLanguage = SourceFileLanguage::C; + + const char *buildTarget; + switch(sourceFileLanguage) + { + case SourceFileLanguage::C: + buildTarget = "c_COMPILER"; + break; + case SourceFileLanguage::CPP: + buildTarget = "cpp_COMPILER"; + break; + } + //string sourceFileEncoded = sourceFile; //replace(sourceFileEncoded, '/', '@'); string objectName = config.getPackageName() + "@exe/" + sourceFile; objectName += getObjectFileExtension(config.getCompiler()); result += "build "; result += objectName; - result += ": cpp_COMPILER ../../"; + result += ": "; + result += buildTarget; + result += " ../../"; result += sourceFile; result += "\n"; result += " ARGS = $globalIncDir "; if(!defines.empty()) result += defines; + + result += " "; + switch(sourceFileLanguage) + { + case SourceFileLanguage::C: + result += getLanguageVersionFlag(config.getCompiler(), config.getCversion()); + break; + case SourceFileLanguage::CPP: + result += getLanguageVersionFlag(config.getCompiler(), config.getCppVersion()); + break; + } + switch (config.getCompiler()) { case Compiler::GCC: { // -Werror - result += " '-I" + config.getPackageName() + "@exe' " + cflags + " '-I..' -Wall -Wextra -Werror=return-type -fexceptions -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' '-Wnon-virtual-dtor' " + optimizationFlags; + result += " '-I" + config.getPackageName() + "@exe' " + cflags + " '-I..' -Wall -Wextra -Werror=return-type -fdiagnostics-show-option '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Winvalid-pch' -fstack-protector " + optimizationFlags; + if(sourceFileLanguage == SourceFileLanguage::CPP) + { + result += " -fexceptions -Wnon-virtual-dtor"; + } + switch (config.getOptimizationLevel()) { case OPT_LEV_DEBUG: @@ -616,7 +711,7 @@ namespace backend { result += " "; result += optimizationFlags; - result += " /EHsc "; + result += " /EHsc /W3 "; result += cflags; switch (config.getOptimizationLevel()) { -- cgit v1.2.3