From: Mikko Rasa Date: Sun, 8 Jan 2023 12:53:07 +0000 (+0200) Subject: Add import functionality for setup modules X-Git-Url: http://git.tdb.fi/?p=libs%2Fgame.git;a=commitdiff_plain;h=84516f86642ec6fae7f908b29649c75d2606e221 Add import functionality for setup modules Also install setup definition files so definitions from libraries can be used. --- diff --git a/tools/builder-plugin/gamesetupdefinitions.cpp b/tools/builder-plugin/gamesetupdefinitions.cpp index a6abd23..cf4f6ce 100644 --- a/tools/builder-plugin/gamesetupdefinitions.cpp +++ b/tools/builder-plugin/gamesetupdefinitions.cpp @@ -1,7 +1,57 @@ #include "gamesetupdefinitions.h" +#include +#include +#include +#include +#include +using namespace std; using namespace Msp; GameSetupDefinitions::GameSetupDefinitions(Builder &b, const Component &c, const FS::Path &p): SourceFile(b, c, p) -{ } +{ + install_location = "include"; +} + +void GameSetupDefinitions::find_dependencies() +{ + if(!mtime) + return; + + vector imports; + + Cache &cache = package->get_cache(); + if(mtimeget_build_info_for_path(path); + for(const string &i: imports) + if(Target *imp = builder.get_vfs().find_header(i+".mgs", generator, binfo.incpath)) + add_dependency(*imp); +} + +vector GameSetupDefinitions::parse_imports(const FS::Path &fn) +{ + vector imports; + + IO::BufferedFile in_file(fn.str()); + DataFile::Parser parser(in_file, fn.str()); + while(parser) + { + DataFile::Statement st = parser.parse(); + if(st.keyword=="import" && st.args.size()==1) + imports.push_back(st.args.front().get()); + } + + return imports; +} diff --git a/tools/builder-plugin/gamesetupdefinitions.h b/tools/builder-plugin/gamesetupdefinitions.h index 090b412..f9e84fb 100644 --- a/tools/builder-plugin/gamesetupdefinitions.h +++ b/tools/builder-plugin/gamesetupdefinitions.h @@ -9,6 +9,10 @@ public: GameSetupDefinitions(Builder &, const Component &, const Msp::FS::Path &); const char *get_type() const override { return "GameSetupDefinitions"; } + +private: + void find_dependencies() override; + static std::vector parse_imports(const Msp::FS::Path &); }; #endif diff --git a/tools/builder-plugin/gamesetupgenerator.cpp b/tools/builder-plugin/gamesetupgenerator.cpp index 3302c21..e11355e 100644 --- a/tools/builder-plugin/gamesetupgenerator.cpp +++ b/tools/builder-plugin/gamesetupgenerator.cpp @@ -52,6 +52,12 @@ ExternalTask::Arguments GameSetupGenerator::_run(const CSourceFile &out_src, FS: ExternalTask::Arguments argv; argv.push_back(tool.get_executable()->get_path().str()); + BuildInfo binfo; + out_src.collect_build_info(binfo); + + for(const FS::Path &i: binfo.incpath) + argv.push_back("-I"+i.str()); + for(const Target *d: out_src.get_dependencies()) if(const GameSetupDefinitions *defs = dynamic_cast(d)) argv.push_back(FS::relative(defs->get_path(), work_dir).str()); diff --git a/tools/setupgen/setupgen.cpp b/tools/setupgen/setupgen.cpp index 083fc8a..571396f 100644 --- a/tools/setupgen/setupgen.cpp +++ b/tools/setupgen/setupgen.cpp @@ -1,6 +1,8 @@ #include "setupgen.h" +#include #include #include +#include #include #include #include @@ -11,10 +13,15 @@ using namespace Msp; SetupGen::SetupGen(int argc, char **argv) { + vector import_dirs; + GetOpt getopt; + getopt.add_option('I', "importdir", import_dirs, GetOpt::REQUIRED_ARG); getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG); getopt.add_argument("input_file", in_fn, GetOpt::REQUIRED_ARG); getopt(argc, argv); + + import_path.insert(import_path.end(), import_dirs.begin(), import_dirs.end()); } int SetupGen::main() @@ -192,6 +199,7 @@ void SetupGen::Loader::init_actions() add("component", &Loader::struct_def, Struct::COMPONENT); add("entity", &Loader::struct_def, Struct::ENTITY); add("enum", &Loader::enum_def); + add("import", &Loader::import); add("namespace", &Loader::name_space); } @@ -203,6 +211,20 @@ void SetupGen::Loader::enum_def(const DataFile::Symbol &n) type.set_enum(*mod.enums.emplace_back(make_unique(move(en)))); } +void SetupGen::Loader::import(const string &n) +{ + string fn = n+".mgs"; + FS::Path full_path; + if(!ranges::any_of(obj.import_path, [&full_path, &fn](const FS::Path &p){ + full_path = p/fn; + return FS::exists(full_path); + })) + throw IO::file_not_found(fn); + + obj.load(full_path); + obj.headers.insert(n+".h"); +} + void SetupGen::Loader::name_space(const string &ns) { mod.name_space = ns; diff --git a/tools/setupgen/setupgen.h b/tools/setupgen/setupgen.h index e4c1b33..1ff2b8d 100644 --- a/tools/setupgen/setupgen.h +++ b/tools/setupgen/setupgen.h @@ -35,12 +35,14 @@ private: void init_actions() override; void enum_def(const Msp::DataFile::Symbol &); + void import(const std::string &); void name_space(const std::string &); void struct_def(Struct::Kind, const Msp::DataFile::Symbol &); }; std::string in_fn; std::string out_fn; + std::vector import_path; std::list modules; std::map types; std::vector> enums;