diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-19 18:00:48 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-19 18:19:44 +0200 |
commit | 69528ba012ac2238f63464fb84e7623f3088603a (patch) | |
tree | c587fb15494d448a572e28ad6156aa9bb196ac17 /task_cc.cc | |
parent | f1fda74cb76600b746cc3239f3ebf2f69ce2bf53 (diff) |
Add compilation database generation.
Diffstat (limited to 'task_cc.cc')
-rw-r--r-- | task_cc.cc | 103 |
1 files changed, 64 insertions, 39 deletions
@@ -175,44 +175,7 @@ int TaskCC::runInner() return 1; } - std::string comp = compiler(); - auto compiler_flags = flags(); - - std::vector<std::string> args; - args.push_back("-MMD"); - - if(std::filesystem::path(config.target).extension() == ".so") - { - // Add -fPIC arg to all contained object files - args.push_back("-fPIC"); - } - - args.push_back("-c"); - args.push_back(std::string(sourceFile)); - args.push_back("-o"); - args.push_back(std::string(targetFile)); - - for(const auto& flag : compiler_flags) - { - // Is arg an added include path? - if(flag.substr(0, 2) == "-I") - { - std::string include_path = flag.substr(2); - include_path.erase(0, include_path.find_first_not_of(' ')); - std::filesystem::path path(include_path); - - // Is it relative? - if(path.is_relative()) - { - path = (sourceDir / path).lexically_normal(); - std::string new_include_path = "-I" + path.string(); - args.push_back(new_include_path); - continue; - } - } - - args.push_back(flag); - } + auto args = getCompilerArgs(); { // Write flags to file. std::ofstream flagsStream(flagsFile); @@ -225,7 +188,8 @@ int TaskCC::runInner() sourceFile.lexically_normal().string() << " => " << targetFile.lexically_normal().string() << "\n"; } - return execute(comp, args, settings.verbose > 0); + + return execute(compiler(), args, settings.verbose > 0); } int TaskCC::clean() @@ -261,6 +225,24 @@ std::string TaskCC::target() const return targetFile; } +std::string TaskCC::toJSON() const +{ + std::string json; + json += "\t{\n"; + json += "\t\t\"directory\": \"" + sourceDir.string() + "\",\n"; + json += "\t\t\"file\": \"" + sourceFile.lexically_normal().string() + "\",\n"; + json += "\t\t\"output\": \"" + targetFile.string() + "\",\n"; + json += "\t\t\"arguments\": [ \"" + compiler() + "\""; + auto args = getCompilerArgs(); + for(const auto& arg : args) + { + json += ", \"" + arg + "\""; + } + json += " ]\n"; + json += "\t}"; + return json; +} + std::vector<std::string> TaskCC::flags() const { if(std::string(sourceFile.extension()) == ".c") @@ -292,3 +274,46 @@ std::string TaskCC::compiler() const } return "/usr/bin/g++"; } + +std::vector<std::string> TaskCC::getCompilerArgs() const +{ + auto compiler_flags = flags(); + + std::vector<std::string> args; + args.push_back("-MMD"); + + if(std::filesystem::path(config.target).extension() == ".so") + { + // Add -fPIC arg to all contained object files + args.push_back("-fPIC"); + } + + args.push_back("-c"); + args.push_back(std::string(sourceFile)); + args.push_back("-o"); + args.push_back(std::string(targetFile)); + + for(const auto& flag : compiler_flags) + { + // Is arg an added include path? + if(flag.substr(0, 2) == "-I") + { + std::string include_path = flag.substr(2); + include_path.erase(0, include_path.find_first_not_of(' ')); + std::filesystem::path path(include_path); + + // Is it relative? + if(path.is_relative()) + { + path = (sourceDir / path).lexically_normal(); + std::string new_include_path = "-I" + path.string(); + args.push_back(new_include_path); + continue; + } + } + + args.push_back(flag); + } + + return args; +} |