diff --git a/src/common/compilation_database.cc b/src/common/compilation_database.cc index 3a8dc6c4..3e762e89 100644 --- a/src/common/compilation_database.cc +++ b/src/common/compilation_database.cc @@ -94,9 +94,12 @@ void compilation_database::adjust_compilation_database( #if !defined(_WIN32) if (config().query_driver && !config().query_driver().empty()) { for (auto &compile_command : commands) { + auto argv0 = config().query_driver() == "." + ? compile_command.CommandLine.at(0) + : config().query_driver(); + util::query_driver_output_extractor extractor{ - config().query_driver(), - guess_language_from_filename(compile_command.Filename)}; + argv0, guess_language_from_filename(compile_command.Filename)}; extractor.execute(); @@ -109,6 +112,12 @@ void compilation_database::adjust_compilation_database( compile_command.CommandLine.insert( compile_command.CommandLine.begin() + 1, system_header_args.begin(), system_header_args.end()); + + if (!extractor.target().empty()) { + compile_command.CommandLine.insert( + compile_command.CommandLine.begin() + 1, + fmt::format("--target={}", extractor.target())); + } } } #endif diff --git a/src/util/query_driver_output_extractor.cc b/src/util/query_driver_output_extractor.cc index 2f31085b..73245817 100644 --- a/src/util/query_driver_output_extractor.cc +++ b/src/util/query_driver_output_extractor.cc @@ -43,6 +43,7 @@ void query_driver_output_extractor::execute() system_include_paths_.clear(); extract_system_include_paths(driver_output); + extract_target(driver_output); if (system_include_paths_.empty()) { throw query_driver_no_paths(fmt::format( @@ -55,6 +56,20 @@ void query_driver_output_extractor::execute() fmt::join(system_include_paths_, ",")); } +void query_driver_output_extractor::extract_target(const std::string &output) +{ + std::istringstream f(output); + std::string line; + + while (std::getline(f, line)) { + line = trim(line); + if (util::starts_with(line, std::string{"Target: "})) { + target_ = line.substr(strlen("Target: ")); + break; + } + } +} + void query_driver_output_extractor::extract_system_include_paths( const std::string &output) { @@ -84,4 +99,8 @@ query_driver_output_extractor::system_include_paths() const return system_include_paths_; } +const std::string &query_driver_output_extractor::target() const +{ + return target_; +} } // namespace clanguml::util \ No newline at end of file diff --git a/src/util/query_driver_output_extractor.h b/src/util/query_driver_output_extractor.h index 041289a1..5b08df87 100644 --- a/src/util/query_driver_output_extractor.h +++ b/src/util/query_driver_output_extractor.h @@ -35,13 +35,18 @@ public: void execute(); + void extract_target(const std::string &output); + void extract_system_include_paths(const std::string &output); const std::vector &system_include_paths() const; + const std::string &target() const; + private: const std::string command_; const std::string language_; + std::string target_; std::vector system_include_paths_; }; } // namespace clanguml::util \ No newline at end of file diff --git a/tests/test_compilation_database.cc b/tests/test_compilation_database.cc index b1ed87a9..aba0085e 100644 --- a/tests/test_compilation_database.cc +++ b/tests/test_compilation_database.cc @@ -39,6 +39,7 @@ TEST_CASE("Test compilation_database should work", "[unit-test]") using clanguml::common::model::access_t; using clanguml::common::model::relationship_t; using clanguml::util::contains; + using std::filesystem::path; auto cfg = clanguml::config::load("./test_compilation_database_data/config.yml"); @@ -51,12 +52,19 @@ TEST_CASE("Test compilation_database should work", "[unit-test]") auto all_files = db->getAllFiles(); REQUIRE(all_files.size() == 3); - REQUIRE(all_files.at(0) == - "src/class_diagram/generators/json/class_diagram_generator.cc"); - REQUIRE(all_files.at(1) == - "src/class_diagram/generators/plantuml/" - "class_diagram_generator.cc"); - REQUIRE(all_files.at(2) == "src/class_diagram/model/class.cc"); + REQUIRE(contains(all_files, + path("src/class_diagram/generators/json/class_diagram_generator.cc") + .make_preferred() + .string())); + REQUIRE(contains(all_files, + path("src/class_diagram/generators/plantuml/" + "class_diagram_generator.cc") + .make_preferred() + .string())); + REQUIRE(contains(all_files, + path("src/class_diagram/model/class.cc") + .make_preferred() + .string())); auto ccs = db->getAllCompileCommands(); diff --git a/tests/test_query_driver_output_extractor.cc b/tests/test_query_driver_output_extractor.cc index a928cb9b..361b27f5 100644 --- a/tests/test_query_driver_output_extractor.cc +++ b/tests/test_query_driver_output_extractor.cc @@ -61,9 +61,13 @@ COLLECT_GCC_OPTIONS='-E' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64' extractor.extract_system_include_paths(output); + extractor.extract_target(output); + std::vector expected = { "/usr/lib/gcc/x86_64-linux-gnu/11/include", "/usr/local/include", "/usr/include/x86_64-linux-gnu", "/usr/include"}; REQUIRE(extractor.system_include_paths() == expected); + + REQUIRE(extractor.target() == "x86_64-linux-gnu"); } \ No newline at end of file