chore: add lsinc for more efficient makescript

This commit is contained in:
TopchetoEU 2023-01-26 16:46:50 +02:00
parent 2461ed1860
commit fafd2ad864
No known key found for this signature in database
GPG Key ID: 5ED5FFB2A3F5DB21
5 changed files with 123 additions and 7 deletions

4
.gitignore vendored
View File

@ -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

View File

@ -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'...

9
scripts/ls.mak Normal file
View File

@ -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 $@

View File

@ -1,3 +0,0 @@
$(lsproj): $(src)/lsproj.cc
$(call mkdir,$(dir $@))
$(CXX) $^ -o $@

109
src/lsinc.cc Normal file
View File

@ -0,0 +1,109 @@
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
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<fs::path> &res) {
static std::set<fs::path> 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<fs::path> 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;
}