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 ++++++++++++++++++++++++++++++--------- Builder.h | 4 +++- LanguageSettings.cpp | 2 +- 3 files changed, 34 insertions(+), 11 deletions(-) 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)}; diff --git a/Builder.h b/Builder.h index b167e65..42c5af4 100644 --- a/Builder.h +++ b/Builder.h @@ -24,11 +24,13 @@ public: private: std::unordered_map> get_dependencies(const boost::property_tree::ptree& ptree) const; std::vector dependencies_of(const std::filesystem::path& p) const; - std::vector include_paths_of(const std::filesystem::path& p) const; + std::vector include_paths_of_object(const std::filesystem::path& p) const; + std::filesystem::path target_from_object(const std::filesystem::path& object) const; std::vector link_libs_of(const std::filesystem::path& p) const; bool is_outdated(const std::filesystem::path& p) const; bool is_outdated(const std::filesystem::path& p, const std::vector &dependencies) const; std::vector make_depfile_from(const std::filesystem::path& p) const; + std::filesystem::path get_compile_unit_source_from_object(const std::filesystem::path& path); void build_file(const std::filesystem::path& p); void build_filelist(); diff --git a/LanguageSettings.cpp b/LanguageSettings.cpp index 57a550d..1d243d5 100644 --- a/LanguageSettings.cpp +++ b/LanguageSettings.cpp @@ -73,7 +73,7 @@ std::string LanguageSettings::getCompileCommand(const std::filesystem::path& tar const std::vector& includepaths) const { std::string includes{std::accumulate(includepaths.begin(), includepaths.end(), std::string{}, - [](const std::string& sum, const fs::path& p){ return sum + " " + p.string();})}; + [](const std::string& sum, const fs::path& p){ return sum + " -I" + p.string();})}; // compile: $(CXX) $(CXXFLAGS) -c $< -o $@ return fmt::format("{}{}{} -c {} -o {}", -- cgit v1.2.3