From e73ee708292aac55070701ff0157db4eab6c06f4 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 27 Sep 2021 21:32:17 +0200 Subject: Add folder hierarchy in build folder corresponding to that of the source folders and store build-files in corresponding locations. --- src/task_ar.cc | 13 +++++++++---- src/task_ar.h | 3 ++- src/task_cc.cc | 3 +++ src/task_ld.cc | 12 ++++++++---- src/task_ld.h | 3 ++- src/task_so.cc | 16 +++++++++++----- src/task_so.h | 3 ++- src/tasks.cc | 7 ++++--- test/source_type_test.cc | 2 +- test/tasks_test.cc | 20 ++++++++++---------- 10 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/task_ar.cc b/src/task_ar.cc index d767912..84cda1b 100644 --- a/src/task_ar.cc +++ b/src/task_ar.cc @@ -30,13 +30,17 @@ std::string readFile(const std::string &fileName) TaskAR::TaskAR(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector& objects) + const std::vector& objects, + const std::string& sourcePath) : Task(config) , config(config) , settings(settings) { - targetFile = settings.builddir; - targetFile /= target; + std::filesystem::path base = settings.builddir; + base /= sourcePath; + std::filesystem::create_directories(base); + + targetFile = base / target; for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -51,7 +55,7 @@ TaskAR::TaskAR(const BuildConfiguration& config, depFiles.push_back(depFile); } - flagsFile = settings.builddir / targetFile.stem(); + flagsFile = base / targetFile.stem(); flagsFile += ".flags"; target_type = TargetType::StaticLibrary; @@ -59,6 +63,7 @@ TaskAR::TaskAR(const BuildConfiguration& config, for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); + // TODO: Use task languages instead if(sourceFile.extension().string() != ".c") { source_language = Language::Cpp; diff --git a/src/task_ar.h b/src/task_ar.h index abdc3ae..f2873c7 100644 --- a/src/task_ar.h +++ b/src/task_ar.h @@ -20,7 +20,8 @@ public: TaskAR(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector& objects); + const std::vector& objects, + const std::string& sourcePath); bool dirtyInner() override; diff --git a/src/task_cc.cc b/src/task_cc.cc index eb361cb..dfda75d 100644 --- a/src/task_cc.cc +++ b/src/task_cc.cc @@ -129,6 +129,9 @@ TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, sourceFile /= source.file; std::filesystem::path base = settings.builddir; + base /= sourceFile.parent_path(); + std::filesystem::create_directories(base); + base /= config.target; base += "-"; base += sourceFile.stem(); diff --git a/src/task_ld.cc b/src/task_ld.cc index 7710bb0..10b8bce 100644 --- a/src/task_ld.cc +++ b/src/task_ld.cc @@ -29,7 +29,8 @@ std::string readFile(const std::string &fileName) TaskLD::TaskLD(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector& objects) + const std::vector& objects, + const std::string& sourcePath) : Task(config) , config(config) , settings(settings) @@ -40,8 +41,11 @@ TaskLD::TaskLD(const BuildConfiguration& config, target_type = TargetType::Executable; } - targetFile = settings.builddir; - targetFile /= target; + std::filesystem::path base = settings.builddir; + base /= sourcePath; + std::filesystem::create_directories(base); + + targetFile = base / target; for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -56,7 +60,7 @@ TaskLD::TaskLD(const BuildConfiguration& config, depFiles.push_back(depFile); } - flagsFile = settings.builddir / targetFile.stem(); + flagsFile = base / targetFile.stem(); flagsFile += ".flags"; source_language = Language::C; diff --git a/src/task_ld.h b/src/task_ld.h index 730975a..a86e49b 100644 --- a/src/task_ld.h +++ b/src/task_ld.h @@ -20,7 +20,8 @@ public: TaskLD(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector& objects); + const std::vector& objects, + const std::string& sourcePath); bool dirtyInner() override; diff --git a/src/task_so.cc b/src/task_so.cc index 863b80a..ce6e868 100644 --- a/src/task_so.cc +++ b/src/task_so.cc @@ -14,7 +14,8 @@ namespace { std::string readFile(const std::string &fileName) { - std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); + std::ifstream ifs(fileName.c_str(), + std::ios::in | std::ios::binary | std::ios::ate); std::ifstream::pos_type fileSize = ifs.tellg(); ifs.seekg(0, std::ios::beg); @@ -29,13 +30,17 @@ std::string readFile(const std::string &fileName) TaskSO::TaskSO(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector& objects) + const std::vector& objects, + const std::string& sourcePath) : Task(config) , config(config) , settings(settings) { - targetFile = settings.builddir; - targetFile /= target; + std::filesystem::path base = settings.builddir; + base /= sourcePath; + std::filesystem::create_directories(base); + + targetFile = base / target; for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -50,7 +55,7 @@ TaskSO::TaskSO(const BuildConfiguration& config, depFiles.push_back(depFile); } - flagsFile = settings.builddir / targetFile.stem(); + flagsFile = base / targetFile.stem(); flagsFile += ".flags"; target_type = TargetType::DynamicLibrary; @@ -58,6 +63,7 @@ TaskSO::TaskSO(const BuildConfiguration& config, for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); + // TODO: Use task languages instead if(sourceFile.extension().string() != ".c") { source_language = Language::Cpp; diff --git a/src/task_so.h b/src/task_so.h index 1e65694..09aa205 100644 --- a/src/task_so.h +++ b/src/task_so.h @@ -20,7 +20,8 @@ public: TaskSO(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector& objects); + const std::vector& objects, + const std::string& sourcePath); bool dirtyInner() override; diff --git a/src/tasks.cc b/src/tasks.cc index a52b0be..254404e 100644 --- a/src/tasks.cc +++ b/src/tasks.cc @@ -91,23 +91,24 @@ std::list> taskFactory(const BuildConfiguration& config, case TargetType::StaticLibrary: tasks.emplace_back(std::make_shared(config, settings, config.target, - objects)); + objects, sourceDir)); break; case TargetType::DynamicLibrary: + // TODO: Use C++20 starts_with if(targetFile.stem().string().substr(0, 3) != "lib") { std::cerr << "Dynamic library target must have 'lib' prefix\n"; exit(1); } tasks.emplace_back(std::make_shared(config, settings, config.target, - objects)); + objects, sourceDir)); break; case TargetType::Executable: case TargetType::UnitTest: tasks.emplace_back(std::make_shared(config, settings, config.target, - objects)); + objects, sourceDir)); break; case TargetType::Object: diff --git a/test/source_type_test.cc b/test/source_type_test.cc index 2da1ac4..47d820d 100644 --- a/test/source_type_test.cc +++ b/test/source_type_test.cc @@ -29,7 +29,7 @@ class TestableTaskCC { public: TestableTaskCC(const Source& source) - : TaskCC({}, {}, {}, source) + : TaskCC({}, {}, "build", source) {} Language language() const diff --git a/test/tasks_test.cc b/test/tasks_test.cc index d6515b8..612d899 100644 --- a/test/tasks_test.cc +++ b/test/tasks_test.cc @@ -100,29 +100,29 @@ public: auto tasks = getTasks(settings); uASSERT_EQUAL(6u, tasks.size()); auto task = tasks.begin(); - uASSERT_EQUAL("target1-foo_cc.o"s, (*task)->target()); + uASSERT_EQUAL("test/target1-foo_cc.o"s, (*task)->target()); task++; - uASSERT_EQUAL("target1-bar_c.o"s, (*task)->target()); + uASSERT_EQUAL("test/target1-bar_c.o"s, (*task)->target()); task++; - uASSERT_EQUAL("target1"s, (*task)->target()); + uASSERT_EQUAL("test/target1"s, (*task)->target()); task++; - uASSERT_EQUAL("target2"s, (*task)->target()); + uASSERT_EQUAL("test/target2"s, (*task)->target()); task++; - uASSERT_EQUAL("target3"s, (*task)->target()); + uASSERT_EQUAL("test/target3"s, (*task)->target()); task++; - uASSERT_EQUAL("target4"s, (*task)->target()); + uASSERT_EQUAL("test/target4"s, (*task)->target()); } { auto tasks = getTasks(settings, {"target1", "target3"}); uASSERT_EQUAL(4u, tasks.size()); auto task = tasks.begin(); - uASSERT_EQUAL("target1-foo_cc.o"s, (*task)->target()); + uASSERT_EQUAL("test/target1-foo_cc.o"s, (*task)->target()); task++; - uASSERT_EQUAL("target1-bar_c.o"s, (*task)->target()); + uASSERT_EQUAL("test/target1-bar_c.o"s, (*task)->target()); task++; - uASSERT_EQUAL("target1"s, (*task)->target()); + uASSERT_EQUAL("test/target1"s, (*task)->target()); task++; - uASSERT_EQUAL("target3"s, (*task)->target()); + uASSERT_EQUAL("test/target3"s, (*task)->target()); } { auto tasks = getTasks(settings, {"no-such-target"}); -- cgit v1.2.3