From 1eaf818aa9339f29d08cf79601836c0ea763c622 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 9 May 2024 16:50:36 +0200 Subject: Use specified includepath --- Builder.cpp | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'Builder.cpp') diff --git a/Builder.cpp b/Builder.cpp index 57f731e..e74809b 100644 --- a/Builder.cpp +++ b/Builder.cpp @@ -179,6 +179,16 @@ Builder::Builder(const pt::ptree& ptree, const std::string& target): // to prevent creation of .d files in clean() } +// given a compiled compile unit (*.o), find its source file +fs::path Builder::get_compile_unit_source_from_object(const fs::path& path) { + std::vector source_files{dependencies_of(path)}; + auto it{std::find_if(source_files.begin(), source_files.end(), is_compile_unit_source_by_extension)}; + if (it == source_files.end()) { + throw std::runtime_error(fmt::format("No source file found for {}", path.string())); + } + return *it; +} + std::vector Builder::dependencies_of(const fs::path& p) const { try { @@ -197,10 +207,26 @@ std::vector Builder::link_libs_of(const fs::path& p) const } } -std::vector Builder::include_paths_of(const fs::path& p) const +fs::path Builder::target_from_object(const fs::path& object) const +{ + for (const auto& target: _all_targets) { + const auto& deps{dependencies_of(target)}; + auto it{std::find(deps.cbegin(), deps.cend(), object)}; + if (it != deps.cend()) { + return target; + } + } + + throw std::runtime_error("No target found which depends on object "s + object.string()); +} + +std::vector Builder::include_paths_of_object(const fs::path& object) const { + fs::path target{target_from_object(object)}; + + // include paths from target try { - return _include_paths.at(p); + return _include_paths.at(target); } catch (const std::out_of_range& ex) { return {}; // empty by default } @@ -288,13 +314,8 @@ void Builder::build_file(const fs::path& p) { if (p.extension() == ".o") { // compile - std::vector source_files{dependencies_of(p)}; - auto it{std::find_if(source_files.begin(), source_files.end(), is_compile_unit_source_by_extension)}; - if (it == source_files.end()) { - throw std::runtime_error(fmt::format("No source file found for {}", p.string())); - } - //std::cout << "DEBUG: " << (*_include_paths.begin()).first << " " << (*_include_paths.begin()).second.size() << " " << p.string() << std::endl; - command = _lang.getCompileCommand(p, *it, include_paths_of(p)); + fs::path source_file{get_compile_unit_source_from_object(p)}; + command = _lang.getCompileCommand(p, source_file, include_paths_of_object(p)); } else { // link std::vector objects{dependencies_of(p)}; -- cgit v1.2.3