From fafd2ad864a293d9da63f2e1cd5d3d793616f254 Mon Sep 17 00:00:00 2001 From: topchetoeu <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 26 Jan 2023 16:46:50 +0200 Subject: [PATCH] chore: add lsinc for more efficient makescript --- .gitignore | 4 +- scripts/common.mak | 5 ++- scripts/ls.mak | 9 ++++ scripts/lsproj.mak | 3 -- src/lsinc.cc | 109 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 scripts/ls.mak delete mode 100644 scripts/lsproj.mak create mode 100644 src/lsinc.cc diff --git a/.gitignore b/.gitignore index 0e936b5..715003a 100644 --- a/.gitignore +++ b/.gitignore @@ -19,11 +19,11 @@ !src/*/**/*.h !src/*/**/*.hh !src/*.proj -!src/lsproj.cc +!src/ls*.cc !scripts !scripts/common.mak -!scripts/lsproj.mak +!scripts/ls.mak !scripts/install.bat !scripts/uninstall.bat diff --git a/scripts/common.mak b/scripts/common.mak index c9091f3..e5f8973 100644 --- a/scripts/common.mak +++ b/scripts/common.mak @@ -1,7 +1,8 @@ export lsproj = $(bin)/lsproj$(exe) +export lsinc = $(bin)/lsinc$(exe) export flags += "-I$(inc)" -D$(OS) -DPPC_VERSION_MAJOR=$(version-major) -DPPC_VERSION_MINOR=$(version-minor) -DPPC_VERSION_BUILD=$(version-build) -$(shell make -f scripts/lsproj.mak lsproj=$(lsproj) src=$(src) $(lsproj)) +$(shell make -f scripts/ls.mak lsinc=$(lsinc) lsproj=$(lsproj) src=$(src) "flags=$(flags)" all) rwildcard=$(foreach d, $(wildcard $(1:=/*)),\ $(call rwildcard,$d,$2)\ @@ -52,7 +53,7 @@ $(bin)/lib$(lib)%$(so): $$(call frdeps,$$*) $$(call binaries,$$*) $(CXX) -shared -fPIC $(flags) $(call binaries,$*) -o $@ $(ldflags) $(call ldeps,$*) -L$(bin) "-I$(inc)" echo Compiling library '$(notdir $@)'... -$(bin)/tmp/%.o: $(src)/%.cc $(headers) +$(bin)/tmp/%.o: $(src)/%.cc $(shell $(lsinc) $(inc) $(src)/%.cc) $(call mkdir,$(dir $@)) $(CXX) -fPIC -c $(flags) $< -o $@ echo - Compiling '$*.cc'... diff --git a/scripts/ls.mak b/scripts/ls.mak new file mode 100644 index 0000000..0d4e125 --- /dev/null +++ b/scripts/ls.mak @@ -0,0 +1,9 @@ +all: $(lsproj) $(lsinc) + +$(lsproj): $(src)/lsproj.cc + $(call mkdir,$(dir $@)) + $(CXX) $(flags) $^ -o $@ + +$(lsinc): $(src)/lsinc.cc + $(call mkdir,$(dir $@)) + $(CXX) $(flags) $^ -o $@ \ No newline at end of file diff --git a/scripts/lsproj.mak b/scripts/lsproj.mak deleted file mode 100644 index 6d49d5a..0000000 --- a/scripts/lsproj.mak +++ /dev/null @@ -1,3 +0,0 @@ -$(lsproj): $(src)/lsproj.cc - $(call mkdir,$(dir $@)) - $(CXX) $^ -o $@ \ No newline at end of file diff --git a/src/lsinc.cc b/src/lsinc.cc new file mode 100644 index 0000000..8bb4ebb --- /dev/null +++ b/src/lsinc.cc @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std::string_literals; +namespace fs = std::filesystem; + +static inline void ltrim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); +} +static inline void rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), s.end()); +} +static inline void trim(std::string &s) { + rtrim(s); + ltrim(s); +} +static bool read_line(std::istream &str, std::string &res) { + if (str.eof()) return false; + std::getline(str, res); + trim(res); + return true; +} + +fs::path find_file(fs::path original, fs::path include) { + if (fs::exists(original)) return original; + if (fs::exists(include / original)) return include / original; + return original; +} + +bool get_import(std::string line, std::string &import) { + if (line.rfind("#include") != 0) return false; + line = line.substr(8); + trim(line); + if (line.length() < 3) throw "Invalid import syntax."s; + if (line[0] == '<') return false; + if (line[0] == '"') { + if (line[line.length() - 1] != '"') throw "Invalid import syntax."s; + import = line.substr(1, line.length() - 2); + return true; + } + + throw "Invalid import syntax."s; +} + +void get_imports(fs::path file, fs::path include, std::set &res) { + static std::set parents = { }; + + if (!parents.emplace(file).second) { + throw "Circular dependency encountered."s; + } + + std::ifstream f(file.string()); + if (f.is_open() && !f.eof()) { + std::string line; + + while (read_line(f, line)) { + std::string import; + if (get_import(line, import)) { + auto child = find_file(import, include); + res.emplace(child); + get_imports(child, include, res); + } + } + } + + parents.erase(file); +} + +int main(int argc, char **argv) { + try { + argc--; + argv++; + + if (argc == 0) { + throw "Incorrect usage. Syntax: [inc-path] [files...]."s; + return -1; + } + + fs::path inc(argv[0]); + std::set res; + + for (int i = 1; i < argc; i++) { + fs::path file(argv[i]); + get_imports(file, inc, res); + } + + for (auto &i : res) { + std::cout << i.string() << "\n"; + } + + + return 0; + } + catch (std::string err) { + std::cerr << "Error: " << err << std::endl; + } + + return -1; +} \ No newline at end of file