From 44479895f325cbbc283553dcb10b29a0af3b480b Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 4 May 2024 14:43:48 +0200 Subject: Added yscan --- Builder.cpp | 8 ++------ Makefile | 15 +++++++++++---- YMakefile | 9 +++++++++ file.cpp | 24 ++++++++++++++++++++++++ file.h | 8 ++++++++ ymake.cpp | 3 +-- yscan-main.cpp | 6 ++++++ yscan.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ yscan.h | 4 ++++ 9 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 file.cpp create mode 100644 file.h create mode 100644 yscan-main.cpp create mode 100644 yscan.cpp create mode 100644 yscan.h diff --git a/Builder.cpp b/Builder.cpp index 1f6fbb2..58176d4 100644 --- a/Builder.cpp +++ b/Builder.cpp @@ -1,5 +1,7 @@ #include "Builder.h" +#include "file.h" + #include #include #include @@ -107,12 +109,6 @@ namespace { return deps_from_depfile(depfile); } - // type of file can be built from dependencies - bool is_buildable_by_extension(const fs::path& p) { - fs::path ext{p.extension()}; - return ext.empty() || ext == ".o"; - } - std::unordered_map> get_dependencies(const pt::ptree& ptree) { std::unordered_map> dependencies; dependencies.emplace(get_target(ptree), get_objects(ptree)); diff --git a/Makefile b/Makefile index c0a8132..4a8438a 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,13 @@ PROJECTNAME=ymake -SRC=main.cpp ymake.cpp Builder.cpp ProcessRunner.cpp - +SRC=main.cpp ymake.cpp Builder.cpp ProcessRunner.cpp file.cpp OBJ=$(SRC:.cpp=.o) -all: $(PROJECTNAME) +YSCAN=yscan +YSCAN_SRC=yscan-main.cpp yscan.cpp file.cpp +YSCAN_OBJ=$(YSCAN_SRC:.cpp=.o) + +all: $(PROJECTNAME) $(YSCAN) LDLIBS += -lfmt -lreichwein CXXFLAGS += -std=c++17 @@ -12,15 +15,19 @@ CXXFLAGS += -std=c++17 $(PROJECTNAME): $(OBJ) $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@ +$(YSCAN): $(YSCAN_OBJ) + $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@ + %.o: %.cpp $(CXX) $(CXXFLAGS) -c $< -o $@ install: mkdir -p $(DESTDIR)/usr/bin cp $(PROJECTNAME) $(DESTDIR)/usr/bin/ + cp $(YSCAN) $(DESTDIR)/usr/bin/ clean: - -rm -f *.o $(PROJECTNAME) + -rm -f *.o $(PROJECTNAME) $(YSCAN) -dh_clean deb: diff --git a/YMakefile b/YMakefile index 2881c10..e0bcbc0 100644 --- a/YMakefile +++ b/YMakefile @@ -6,4 +6,13 @@ ProcessRunner.cpp ymake.cpp + + yscan + yscan.cpp + yscan-main.cpp + + + test-ymake + test-ymake.cpp + diff --git a/file.cpp b/file.cpp new file mode 100644 index 0000000..823d723 --- /dev/null +++ b/file.cpp @@ -0,0 +1,24 @@ +#include "file.h" + +#include + +namespace fs = std::filesystem; + +const fs::path YMakefile{"YMakefile"}; + +// type of file can be built from dependencies +bool is_buildable_by_extension(const fs::path& p) { + fs::path ext{p.extension()}; + return ext.empty() || ext == ".o"; +} + +namespace { + std::unordered_set compile_unit_source_types{".cpp", ".c"}; +} + +// type of file is source of compile unit (no included types like headers) +bool is_compile_unit_source_by_extension(const fs::path& p) { + fs::path ext{p.extension()}; + return compile_unit_source_types.find(ext) != compile_unit_source_types.end(); +} + diff --git a/file.h b/file.h new file mode 100644 index 0000000..9570266 --- /dev/null +++ b/file.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +extern const std::filesystem::path YMakefile; + +bool is_buildable_by_extension(const std::filesystem::path& p); +bool is_compile_unit_source_by_extension(const std::filesystem::path& p); diff --git a/ymake.cpp b/ymake.cpp index ad46d3c..62ddeb1 100644 --- a/ymake.cpp +++ b/ymake.cpp @@ -1,5 +1,6 @@ #include "ymake.h" +#include "file.h" #include "Builder.h" #include @@ -30,8 +31,6 @@ namespace pt = boost::property_tree; using namespace std::string_literals; namespace { - const fs::path YMakefile{"YMakefile"}; - void usage() { std::cout << "Usage: ymake " << std::endl; diff --git a/yscan-main.cpp b/yscan-main.cpp new file mode 100644 index 0000000..f0d2930 --- /dev/null +++ b/yscan-main.cpp @@ -0,0 +1,6 @@ +#include "yscan.h" + +int main(int argc, char* argv[]) +{ + return yscan(argc, argv); +} diff --git a/yscan.cpp b/yscan.cpp new file mode 100644 index 0000000..8dad6bf --- /dev/null +++ b/yscan.cpp @@ -0,0 +1,55 @@ +#include "yscan.h" + +#include "file.h" + +#include +#include +#include + +#include + +#include + +namespace fs = std::filesystem; +using namespace std::string_literals; + +int yscan(int argc, char* argv[]) +{ + try { + std::string name {fs::current_path().stem().string()}; + + std::string sources; + std::string test_sources; + for (const auto& i: fs::directory_iterator(".")) { + fs::path p{i.path().filename()}; + if (is_compile_unit_source_by_extension(p)) { + if (p.string().substr(0, 4) == "test") { + test_sources += " " + p.string() + "\n"; + } else { + sources += " " + p.string() + "\n"; + } + } + } + + if (!test_sources.empty()) { + test_sources = fmt::format(R"( + {} +{} +)", "test-"s + name, test_sources); + } + + std::string contents {R"( + + {} +{} +{} +)"}; + contents = fmt::format(contents, name, sources, test_sources); + std::cout << contents << std::endl; + } catch (const std::exception& ex) { + std::cerr << "yscan: " << ex.what() << std::endl; + return 1; + } + return 0; +} + diff --git a/yscan.h b/yscan.h new file mode 100644 index 0000000..66571c5 --- /dev/null +++ b/yscan.h @@ -0,0 +1,4 @@ +#pragma once + +int yscan(int argc, char* argv[]); + -- cgit v1.2.3