diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/generated_sources_test.cc | 55 | ||||
| -rw-r--r-- | test/suite/ctor_files/ctor.cc.generated | 39 | ||||
| -rw-r--r-- | test/suite/ctor_files/ctor.cc.generated2 | 86 | ||||
| -rw-r--r-- | test/suite/test.cc | 496 |
4 files changed, 435 insertions, 241 deletions
diff --git a/test/generated_sources_test.cc b/test/generated_sources_test.cc index 1ea7538..8c46983 100644 --- a/test/generated_sources_test.cc +++ b/test/generated_sources_test.cc @@ -14,6 +14,7 @@ public: GeneratedSourcesTest() { uTEST(GeneratedSourcesTest::test_custom_output); + uTEST(GeneratedSourcesTest::test_many_to_one_output); } void setup() @@ -74,6 +75,60 @@ public: } uASSERT(found); } + + void test_many_to_one_output() + { + using namespace std::string_literals; + + ctor::reg( + [](const ctor::settings&) + { + return ctor::build_configurations{ + { + .target = "test1", + .sources = { + {"foo.cc", ctor::source_type::generated} + } + }, + { + .target = "foo.cc", + .sources = { + {"bar.x"}, + {"bar.y"}, + }, + .function = [](const std::vector<std::string>& input, + const std::string& output, + const ctor::build_configuration& config, + const ctor::settings& settings) + { + return 0; + } + }, + }; + }); + ctor::settings settings{}; + auto tasks = getTasks(settings); + for(auto task : tasks) + { + uASSERT(task->registerDepTasks(tasks) == 0); + } + uASSERT_EQUAL(4u, tasks.size()); + bool found{false}; + for(const auto& task : tasks) + { + if(task->target() == "test1") + { + auto deps_test1 = task->getDependsTasks(); + uASSERT_EQUAL(1u, deps_test1.size()); + auto deps_foo_o = deps_test1[0]->getDependsTasks(); + uASSERT_EQUAL(1u, deps_foo_o.size()); + uASSERT_EQUAL("test/bar.x"s, deps_foo_o[0]->source()); + found = true; + } + } + uASSERT(found); + } + }; // Registers the fixture into the 'registry' diff --git a/test/suite/ctor_files/ctor.cc.generated b/test/suite/ctor_files/ctor.cc.generated index 5f82fd4..5bc2940 100644 --- a/test/suite/ctor_files/ctor.cc.generated +++ b/test/suite/ctor_files/ctor.cc.generated @@ -4,6 +4,7 @@ #include <ctor.h> #include <filesystem> #include <iostream> +#include <fstream> namespace { @@ -41,6 +42,44 @@ ctor::build_configurations ctorConfigs(const ctor::settings& settings) return 0; } }, + + { + .target = "many_to_one", + .sources = { + {"many_to_one.cc", ctor::source_type::generated} + } + }, + { + .target = "many_to_one.cc", + .sources = { + {"foo.cc", ctor::source_type::generated}, + {"hello.cc"}, + }, + .function = [](const std::vector<std::string>& input, + const std::string& output, + const ctor::build_configuration& config, + const ctor::settings& settings) + { + std::cout << "Output: " << output << '\n'; + std::ofstream ofs(output); + bool comment{true}; + for(const auto& input_file : input) + { + std::cout << "Input: " << input_file << '\n'; + std::ifstream ifs(input_file); + std::string line; + while(std::getline(ifs, line)) + { + ofs << line << '\n'; + } + if(comment) ofs << "/*\n"; + comment = false; + } + ofs << "*/\n"; + return 0; + } + }, + }; } } diff --git a/test/suite/ctor_files/ctor.cc.generated2 b/test/suite/ctor_files/ctor.cc.generated2 new file mode 100644 index 0000000..c78489f --- /dev/null +++ b/test/suite/ctor_files/ctor.cc.generated2 @@ -0,0 +1,86 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include <ctor.h> +#include <filesystem> +#include <iostream> +#include <fstream> + +namespace +{ +ctor::build_configurations ctorConfigs(const ctor::settings& settings) +{ + return + { + { + .target = "world", + .sources = { + { "world.cc", ctor::source_type::generated }, + }, + }, + { + .target = "foo", + .sources = { + { "foo.cc", ctor::source_type::generated }, + }, + }, + { + .target = "this_is_unused", + .sources = { + {"hello.cc", ctor::output_file{"world.cc"}}, + {"hello.cc", ctor::output_file{"foo.cc"}}, + }, + .function = [](const std::string& input, + const std::string& output, + const ctor::build_configuration& config, + const ctor::settings& settings) + { + namespace fs = std::filesystem; + std::cout << "Input: " << input << '\n'; + std::cout << "Output: " << output << '\n'; + fs::copy_file(input, output, fs::copy_options::overwrite_existing); + return 0; + } + }, + + { + .target = "many_to_one", + .sources = { + {"many_to_one.cc", ctor::source_type::generated} + } + }, + { + .target = "many_to_one.cc", + .sources = { + {"hello.cc"}, + }, + .function = [](const std::vector<std::string>& input, + const std::string& output, + const ctor::build_configuration& config, + const ctor::settings& settings) + { + std::cout << "Output: " << output << '\n'; + std::ofstream ofs(output); + bool comment{true}; + for(const auto& input_file : input) + { + std::cout << "Input: " << input_file << '\n'; + std::ifstream ifs(input_file); + std::string line; + while(std::getline(ifs, line)) + { + ofs << line << '\n'; + } + if(comment) ofs << "/*\n"; + comment = false; + } + ofs << "*/\n"; + return 0; + } + }, + + }; +} +} + +REG(ctorConfigs); diff --git a/test/suite/test.cc b/test/suite/test.cc index 0ab1299..bb62d9d 100644 --- a/test/suite/test.cc +++ b/test/suite/test.cc @@ -14,28 +14,45 @@ using namespace std::chrono_literals; -std::vector<std::string> tokenize(const std::string& str) -{ - std::vector<std::string> tokens; - std::stringstream ss(str); - std::string token; - while(getline(ss, token, ' ')) - { - tokens.push_back(token); - } - return tokens; -} - int fail(int value = 1, const std::source_location location = std::source_location::current()) { std::cout << "*** Failure at line " << location.line() << '\n'; - return value; + exit(value); } const std::string ctor_exe{"./ctor"}; const std::string obj_ext{".o"}; +void run_ctor(const std::vector<std::string>& args, + const std::source_location location = std::source_location::current()) +{ + ctor::settings settings{.verbose = 2}; + auto ret = execute(settings, ctor_exe, args); + if(ret != 0) + { + fail(ret, location); + } +} + +void assert_not_exists(const std::string& path, + const std::source_location location = std::source_location::current()) +{ + if(!std::filesystem::exists(path)) + { + fail(1, location); + } +} + +void assert_exists(const std::string& path, + const std::source_location location = std::source_location::current()) +{ + if(std::filesystem::exists(path)) + { + fail(1, location); + } +} + void copy_config(std::string cfg) { std::cout << "** ctor_files/ctor.cc." + cfg + "\n"; @@ -48,6 +65,50 @@ void copy_config(std::string cfg) } } +class Tracker +{ +public: + Tracker(const std::string& file_) : file(file_) + { + capture(); // capture initial value + } + + void capture() + { + changed(); + } + + bool changed() + { + auto tmp = readFile(file); + auto content_changed = tmp != content; + content = tmp; + return content_changed; + } + +private: + std::string file; + std::string content; +}; + +void assert_not_changed(Tracker& tracker, + const std::source_location location = std::source_location::current()) +{ + if(tracker.changed()) + { + fail(1, location); + } +} + +void assert_changed(Tracker& tracker, + const std::source_location location = std::source_location::current()) +{ + if(!tracker.changed()) + { + fail(1, location); + } +} + int main() { ctor::settings settings{}; @@ -65,291 +126,244 @@ int main() std::string LDFLAGS; get_env("LDFLAGS", LDFLAGS); - auto cxx_prog = locate(CXX, paths); - // Wipe the board std::filesystem::remove_all(BUILDDIR); std::filesystem::remove("configuration.cc"); std::filesystem::remove("config.h"); - std::filesystem::remove("ctor"); + std::filesystem::remove(ctor_exe); ////////////////////////////////////////////////////////////////////////////// - copy_config("base"); - - // Compile bootstrap binary - std::vector<std::string> args = - {"-pthread", "-std=c++20", "-L", CTORDIR, "-lctor", "-I", "../../src", - "ctor.cc", "-o", ctor_exe}; - - // TODO: add support for quoted strings with spaces - if(!CXXFLAGS.empty()) + // bootstrap { - auto tokens = tokenize(CXXFLAGS); - for(const auto& token : tokens) + auto cxx_prog = locate(CXX, paths); + + std::vector<std::string> args = + {"-pthread", "-std=c++20", "-L", CTORDIR, "-lctor", "-I", "../../src", + "ctor.cc", "-o", ctor_exe}; + if(!CXXFLAGS.empty()) { - args.push_back(token); + auto tokens = argsplit(CXXFLAGS); + for(const auto& token : tokens) + { + args.push_back(token); + } } - } - if(!LDFLAGS.empty()) - { - auto tokens = tokenize(LDFLAGS); - for(const auto& token : tokens) + if(!LDFLAGS.empty()) { - args.push_back(token); + auto tokens = argsplit(LDFLAGS); + for(const auto& token : tokens) + { + args.push_back(token); + } } - } - auto ret = execute(settings, cxx_prog, args); - if(ret != 0) - { - return fail(ret); + // Compile bootstrap binary + copy_config("base"); + auto ret = execute(settings, cxx_prog, args); + if(ret != 0) + { + fail(ret); + } } - ////////////////////////////////////////////////////////////////////////////// + // check if source file changes are tracked { - // No build files should have been created yet - if(std::filesystem::exists(BUILDDIR)) - { - return fail(); - } + // No build files should have been created yet + assert_exists(BUILDDIR); - // capture ctor binary before configure is called - auto ctor_bin = readFile(ctor_exe); - args = {"configure", "--ctor-includedir", "../../src", - "--ctor-libdir=" + CTORDIR, "--build-dir=" + BUILDDIR}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + // capture ctor binary before configure is called + Tracker ctor_bin(ctor_exe); + run_ctor( + {"configure", "--ctor-includedir", "../../src", + "--ctor-libdir=" + CTORDIR, "--build-dir=" + BUILDDIR}); - // ctor should be rebuilt at this point, so binary should have changed - auto ctor_bin2 = readFile(ctor_exe); - if(ctor_bin == ctor_bin2) - { - return fail(); - } + // ctor should be rebuilt at this point, so binary should have changed + assert_changed(ctor_bin); - // configuration.cc should have been generated now - if(!std::filesystem::exists("configuration.cc")) - { - return fail(); - } - if(!std::filesystem::exists("config.h")) - { - return fail(); - } + // configuration.cc should have been generated now + assert_not_exists("configuration.cc"); + assert_not_exists("config.h"); - // Shouldn't compile anything yet - only configure - if(std::filesystem::exists(BUILDDIR + "/hello-hello_cc" + obj_ext)) - { - return fail(); - } + // Shouldn't compile anything yet - only configure + assert_exists(BUILDDIR + "/hello-hello_cc" + obj_ext); - ctor_bin = readFile(ctor_exe); + ctor_bin.capture(); - // Run normally to build project - args = {"-v"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + // Run normally to build project + run_ctor({"-v"}); - // Compiled object should now exist - if(!std::filesystem::exists(BUILDDIR + "/hello-hello_cc" + obj_ext)) - { - return fail(); - } + // Compiled object should now exist + assert_not_exists(BUILDDIR + "/hello-hello_cc" + obj_ext); - // ctor should not have been rebuilt, so binary should be the same - ctor_bin2 = readFile(ctor_exe); - if(ctor_bin != ctor_bin2) - { - return fail(); - } + // ctor should not have been rebuilt, so binary should be the same + assert_not_changed(ctor_bin); - std::this_thread::sleep_for(1100ms); + std::this_thread::sleep_for(1100ms); - auto time = - std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); - std::filesystem::last_write_time("hello.cc", time + 1s); - std::this_thread::sleep_for(1100ms); + auto time = + std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); + std::filesystem::last_write_time("hello.cc", time + 1s); + std::this_thread::sleep_for(1100ms); - // Run normally to rebuild hello.cc - args = {"-v"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + // Run normally to rebuild hello.cc + run_ctor({"-v"}); - // Object file should have been recompiled - auto time2 = - std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); - if(time == time2) - { - return fail(); - } + // Object file should have been recompiled + auto time2 = + std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); + if(time == time2) + { + fail(); + } } ////////////////////////////////////////////////////////////////////////////// - { - // Replace -DFOO with -DBAR in foo external.cxxflags - copy_config("bar"); - auto configuration_cc_bin = readFile("configuration.cc"); - auto ctor_bin = readFile(ctor_exe); - auto time = - std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); - std::this_thread::sleep_for(1100ms); - - // Run normally to reconfigure, rebuild ctor and rebuild hello.cc - args = {"-v"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + // check if flags change trigger rebuild + { + // Replace -DFOO with -DBAR in foo external.cxxflags + copy_config("bar"); + Tracker configuration_cc_bin("configuration.cc"); + Tracker ctor_bin(ctor_exe); + auto time = + std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); + std::this_thread::sleep_for(1100ms); + + // Run normally to reconfigure, rebuild ctor and rebuild hello.cc + run_ctor({"-v"}); + + auto time2 = + std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); + if(time == time2) + { + fail(); + } - auto time2 = - std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); - if(time == time2) - { - return fail(); + assert_changed(configuration_cc_bin); + assert_changed(ctor_bin); } - auto configuration_cc_bin2 = readFile("configuration.cc"); - if(configuration_cc_bin == configuration_cc_bin2) + ////////////////////////////////////////////////////////////////////////////// + // check if included files in ctor.cc is tracked for changes { - return fail(); - } + copy_config("multi"); + Tracker configuration_cc_bin("configuration.cc"); + Tracker ctor_bin(ctor_exe); + auto time = + std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); + std::this_thread::sleep_for(1100ms); - auto ctor_bin2 = readFile(ctor_exe); - if(ctor_bin == ctor_bin2) - { - return fail(); - } + // Run normally to reconfigure, rebuild ctor and rebuild hello.cc + run_ctor({"-v"}); + + auto time2 = + std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); + if(time == time2) + { + fail(); + } + + assert_changed(configuration_cc_bin); + assert_changed(ctor_bin); + + // now touching foobar.h, should retrigger re-configuration + time = std::filesystem::last_write_time(ctor_exe); + std::filesystem::last_write_time("foobar.h", time + 1s); + std::this_thread::sleep_for(1100ms); + + // Run normally to reconfigure, rebuild ctor and rebuild hello.cc + run_ctor({"-v"}); + + time2 = std::filesystem::last_write_time(ctor_exe); + if(time == time2) + { + fail(); + } } ////////////////////////////////////////////////////////////////////////////// + // generated part1: one-to-one generation { - copy_config("multi"); - auto configuration_cc_bin = readFile("configuration.cc"); - auto ctor_bin = readFile(ctor_exe); - auto time = - std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); - std::this_thread::sleep_for(1100ms); - - // Run normally to reconfigure, rebuild ctor and rebuild hello.cc - args = {"-v"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + copy_config("generated"); + std::filesystem::remove(BUILDDIR + "/world.cc"); + std::filesystem::remove(BUILDDIR + "/foo.cc"); - auto time2 = - std::filesystem::last_write_time(BUILDDIR + "/hello-hello_cc" + obj_ext); - if(time == time2) - { - return fail(); - } + Tracker configuration_cc_bin("configuration.cc"); + Tracker ctor_bin(ctor_exe); + std::this_thread::sleep_for(1100ms); - auto configuration_cc_bin2 = readFile("configuration.cc"); - if(configuration_cc_bin == configuration_cc_bin2) - { - return fail(); - } + // Run normally to reconfigure, rebuild ctor and build world.cc + run_ctor({"-v", "world"}); - auto ctor_bin2 = readFile(ctor_exe); - if(ctor_bin == ctor_bin2) - { - return fail(); - } + assert_changed(configuration_cc_bin); + assert_changed(ctor_bin); - // now touching foobar.h, should retrigger re-configuration - time = std::filesystem::last_write_time(ctor_exe); - std::filesystem::last_write_time("foobar.h", time + 1s); - std::this_thread::sleep_for(1100ms); + // foo.cc should not be generated at this point + assert_exists(BUILDDIR+"/foo.cc"); - // Run normally to reconfigure, rebuild ctor and rebuild hello.cc - args = {"-v"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + auto time_w = std::filesystem::last_write_time(BUILDDIR + "/world.cc"); + auto time_wo = + std::filesystem::last_write_time(BUILDDIR + "/" + BUILDDIR+ + "/world-world_cc" + obj_ext); + std::this_thread::sleep_for(1100ms); - time2 = std::filesystem::last_write_time(ctor_exe); - if(time == time2) - { - return fail(); - } + // now touching hello.cc, should trigger regeneration of world.cc and + // rebuild + std::filesystem::last_write_time("hello.cc", time_w + 1s); + run_ctor({"-v", "world"}); + + auto time_w2 = std::filesystem::last_write_time(BUILDDIR + "/world.cc"); + auto time_wo2 = + std::filesystem::last_write_time(BUILDDIR + "/" + BUILDDIR + + "/world-world_cc" + obj_ext); + if(time_w == time_w2) + { + fail(); + } + if(time_wo == time_wo2) + { + fail(); + } } ////////////////////////////////////////////////////////////////////////////// + // generated part2: many-to-one generation { - copy_config("generated"); - std::filesystem::remove(BUILDDIR + "/world.cc"); - std::filesystem::remove(BUILDDIR + "/foo.cc"); + std::filesystem::remove(BUILDDIR + "/world.cc"); + std::filesystem::remove(BUILDDIR + "/foo.cc"); + std::filesystem::remove(BUILDDIR + "/many_to_one.cc"); - auto configuration_cc_bin = readFile("configuration.cc"); - auto ctor_bin = readFile(ctor_exe); - std::this_thread::sleep_for(1100ms); + run_ctor({"-v", "many_to_one"}); - // Run normally to reconfigure, rebuild ctor and build world.cc - args = {"-v", "world"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + // world.cc should not be generated at this point + assert_exists(BUILDDIR+"/world.cc"); - auto configuration_cc_bin2 = readFile("configuration.cc"); - if(configuration_cc_bin == configuration_cc_bin2) - { - return fail(); - } + // foo.cc should have been generated at this point + assert_not_exists(BUILDDIR+"/foo.cc"); - auto ctor_bin2 = readFile(ctor_exe); - if(ctor_bin == ctor_bin2) - { - return fail(); - } + // many_to_one.cc should have been generated + assert_not_exists(BUILDDIR+"/many_to_one.cc"); - // foo.cc should not be generated at this point - if(std::filesystem::exists(BUILDDIR+"/foo.cc")) - { - return fail(); - } + auto time = std::filesystem::last_write_time(BUILDDIR + "/many_to_one.cc"); + std::this_thread::sleep_for(1100ms); - auto time_w = std::filesystem::last_write_time(BUILDDIR + "/world.cc"); - auto time_wo = - std::filesystem::last_write_time(BUILDDIR + "/" + BUILDDIR+ - "/world-world_cc" + obj_ext); - std::this_thread::sleep_for(1100ms); + Tracker ctor_bin(ctor_exe); - // now touching hello.cc, should trigger regeneration of world.cc and rebuild - std::filesystem::last_write_time("hello.cc", time_w + 1s); - args = {"-v", "world"}; - ret = execute(settings, ctor_exe, args); - if(ret != 0) - { - return fail(ret); - } + // remove "foo.cc" from sources list, so it only contains hello.cc + copy_config("generated2"); - auto time_w2 = std::filesystem::last_write_time(BUILDDIR + "/world.cc"); - auto time_wo2 = - std::filesystem::last_write_time(BUILDDIR + "/" + BUILDDIR + - "/world-world_cc" + obj_ext); - if(time_w == time_w2) - { - return fail(); - } - if(time_wo == time_wo2) - { - return fail(); - } + // rebuild + run_ctor({"-v", "many_to_one"}); + + // Verify that the change resulted in a ctor rebuild + assert_changed(ctor_bin); + + // Verify that source-list change triggered a new build of many_to_one.cc + auto time2 = + std::filesystem::last_write_time(BUILDDIR + "/many_to_one.cc"); + if(time == time2) + { + fail(); + } } return 0; |
