From 69528ba012ac2238f63464fb84e7623f3088603a Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Jun 2021 18:00:48 +0200 Subject: Add compilation database generation. --- task_cc.cc | 103 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 39 deletions(-) (limited to 'task_cc.cc') diff --git a/task_cc.cc b/task_cc.cc index b5cc768..6c7e3b7 100644 --- a/task_cc.cc +++ b/task_cc.cc @@ -175,44 +175,7 @@ int TaskCC::runInner() return 1; } - std::string comp = compiler(); - auto compiler_flags = flags(); - - std::vector 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 TaskCC::flags() const { if(std::string(sourceFile.extension()) == ".c") @@ -292,3 +274,46 @@ std::string TaskCC::compiler() const } return "/usr/bin/g++"; } + +std::vector TaskCC::getCompilerArgs() const +{ + auto compiler_flags = flags(); + + std::vector 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; +} -- cgit v1.2.3