From d0734fbafa08a46da7efbb6d7efb69ad2de46e28 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Wed, 8 Jan 2025 12:45:52 +0100 Subject: [PATCH] Migrate to latest project structure --- Makefile | 311 ++++++++++++++++++++++++++++------ tagger.1 => man/man1/tagger.1 | 0 premake.make | 31 +++- src/main.cpp | 14 +- src/precomp.hpp | 127 ++++++++++++++ src/version.cpp | 19 +++ src/version.hpp | 8 + tests/Makefile | 52 +----- tests/postmake.make | 6 + tests/premake.make | 5 + tests/src/precomp.hpp | 1 + 11 files changed, 464 insertions(+), 110 deletions(-) rename tagger.1 => man/man1/tagger.1 (100%) create mode 100644 src/precomp.hpp create mode 100644 src/version.cpp create mode 100644 src/version.hpp mode change 100644 => 120000 tests/Makefile create mode 100644 tests/postmake.make create mode 100644 tests/premake.make create mode 120000 tests/src/precomp.hpp diff --git a/Makefile b/Makefile index 7781647..032d08b 100644 --- a/Makefile +++ b/Makefile @@ -1,79 +1,294 @@ include premake.make -BIN := $(shell basename $$(pwd)) -COMMIT != git log 2>/dev/null | sed -e '1s/^commit //;q' +# git commit hash and version for this build +COMMIT-HASH != git log 2>/dev/null | sed -e '1s/^commit //;q' +COMMIT := "static const char* commit = \"$(COMMIT-HASH)\";" +VERSION := "static const char* version = \"$(MAJOR).$(MINOR).$(PATCH)\";" -MANSECTION ?= 1 -MANPAGE := $(BIN).$(MANSECTION) - -SRCS := $(notdir $(wildcard src/*.cpp)) -OBJS := $(SRCS:.cpp=.o) -DEPS := $(SRCS:.cpp=.dep) - -BUILDDIR := build/intermediates/ +# some important install locations PREFIX ?= /usr/local BINDIR ?= $(PREFIX)/bin -MANDIR ?= $(PREFIX)/man/man CONFIGDIR ?= $(PREFIX)/etc -DATADIR ?= $(PREFIX)/share -DOCDIR ?= $(DATADIR)/$(BIN)/doc +INCLUDEDIR ?= $(PREFIX)/include +LIBDIR ?= $(PREFIX)/lib +MANDIR ?= $(PREFIX)/man +DATADIR ?= $(PREFIX)/share/$(PROJ) +DOCDIR ?= $(DATADIR)/doc +# setup naming and versioning for library product +ifeq ($(UNAME_S), Darwin) + LINKERNAME := $(PROJ).dylib + SONAME := $(PROJ).$(MAJOR).dylib + REALNAME := $(LINKERNAME) +else ifeq ($(UNAME_S), OpenBSD) + REALNAME := $(PROJ).so.$(MAJOR).$(MINOR) +else ifeq ($(UNAME_S), Linux) + LINKERNAME := $(PROJ).so + SONAME := $(LINKERNAME).$(MAJOR) + REALNAME := $(SONAME).$(MINOR).$(PATCH) +endif +STATICLIB := $(PROJ).a + +# select default compiler CXX ?= g++ -RM := /bin/rm -rf -INSTALL := /usr/bin/install -c +# setup compiler flags and build config CXXFLAGS += -Wshadow -Wall -Wpedantic -Wextra -Wno-unused-parameter -CXXFLAGS += -g3 -std=c++20 -ifeq ($(DEBUG),1) - CXXFLAGS += -D DEBUG -O0 +CXXFLAGS += -g3 -fPIC +ifeq ($(DEBUG), 1) + CXXFLAGS += -DDEBUG -O0 CONFIG := debug else - CXXFLAGS += -D NDEBUG -O3 + CXXFLAGS += -DNDEBUG -O3 CONFIG := release endif -OUTDIR := build/$(CONFIG)/ -vpath %.cpp src -vpath %.dep $(BUILDDIR) -vpath %.o $(BUILDDIR) +# setup build locations +OUTDIR := build/$(CONFIG) +BUILDDIR := build/obj +BIN := $(OUTDIR)/$(PROJ) -.PHONY: all clean install prebuild test dist-clean +# define sources and derived files +# allow for extra sources defined in premake.make +SRCS += $(notdir $(wildcard src/*.cpp)) +OBJS := $(addprefix $(BUILDDIR)/, $(SRCS:.cpp=.o)) +DEPS := $(addprefix $(BUILDDIR)/, $(SRCS:.cpp=.dep)) +HDRS ?= $(filter-out src/precomp.hpp, $(wildcard src/*.hpp)) +MANS := $(addprefix $(PREFIX)/, $(wildcard man/man*/*)) -all: prebuild $(OUTDIR)$(BIN) +# if project supports plugins, link to libdl where needed +ifdef PLUGINS +ifeq ($(UNAME_S),Darwin) + LDLIBS += -ldl +else ifeq ($(UNAME_S),Linux) + LDLIBS += -ldl +endif +endif -prebuild: src/commit.inc - @mkdir -p $(BUILDDIR) $(OUTDIR) - sed -i.bak -e '1s/".*"/"$(COMMIT)"/' src/commit.inc - @rm src/commit.inc.bak +# pattern rules =========================================================== -src/commit.inc: - echo 'const char* commit = "";' > $@ +$(BUILDDIR)/%.o: src/%.cpp +ifeq ($(PRECOMPILE), 1) + $(CXX) $(CXXFLAGS) -o $@ -include precomp.hpp -MMD -MP -MT $@ -MF $(BUILDDIR)/$*.dep -c $< +else + $(CXX) $(CXXFLAGS) -o $@ -MMD -MP -MT $@ -MF $(BUILDDIR)/$*.dep -c $< +endif -$(OUTDIR)$(BIN): $(OBJS) - $(CXX) -o $(OUTDIR)$(BIN) $(LDFLAGS) $(addprefix $(BUILDDIR),$(OBJS)) $(LDLIBS) - @ln -sf $(OUTDIR)$(BIN) $(BIN) +%.dep: ; -%.o %.dep: %.cpp - $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.dep -c $< - @mv $@ $*.dep $(BUILDDIR) +$(MANDIR)/man1/%: man/man1/% + install -m 0644 $< $@ --include $(addprefix $(BUILDDIR), $(DEPS)) +$(MANDIR)/man3/%: man/man3/% + install -m 0644 $< $@ + +$(MANDIR)/man5/%: man/man5/% + install -m 0644 $< $@ + +# =========================================================== pattern rules + +# targets ================================================================= + +.PHONY: all commit-hash version plugins test clean dist-clean install-plugins \ +install install-data uninstall-data uninstall-plugins uninstall + +# main target +ifeq ($(PRODUCT), tool) +all: $(BUILDDIR) $(OUTDIR) commit-hash version plugins $(BIN) +else ifeq ($(PRODUCT), lib) +all: $(BUILDDIR) $(OUTDIR) commit-hash version plugins $(OUTDIR)/$(REALNAME) $(OUTDIR)/$(STATICLIB) +endif + +$(BUILDDIR): + mkdir -p $@ + +$(OUTDIR): + mkdir -p $@ + +# product build rules +ifeq ($(PRODUCT), tool) # -------------------------------------------- tool + +ifeq ($(PRECOMPILE), 1) +$(BIN): precomp.hpp.gch $(OBJS) +else +$(BIN): $(OBJS) +endif + $(CXX) -o $(BIN) $(LDFLAGS) $(OBJS) $(LDLIBS) + @ln -sf $(BIN) $(PROJ) + +endif # -------------------------------------------------------------- tool + +ifeq ($(PRODUCT), lib) # ---------------------------------------------- lib + +ifeq ($(PRECOMPILE), 1) +$(OUTDIR)/$(REALNAME): precomp.hpp.gch $(OBJS) +else +$(OUTDIR)/$(REALNAME): $(OBJS) +endif +ifeq ($(UNAME_S),Darwin) + $(CXX) -dynamiclib -o $(OUTDIR)/$(REALNAME) -install_name $(LIBDIR)/$(REALNAME) \ +-current_version $(MAJOR) -compatibility_version $(MINOR) $(LDFLAGS) $(OBJS) $(LDLIBS) +else ifeq ($(UNAME_S),OpenBSD) + $(CXX) -g -shared -Wl,-soname,$(REALNAME) -o $(OUTDIR)/$(REALNAME) $(LDFLAGS) $(OBJS) $(LDLIBS) +else ifeq ($(UNAME_S),Linux) + $(CXX) -g -shared -Wl,-soname,$(SONAME) -o $(OUTDIR)/$(REALNAME) $(LDFLAGS) $(OBJS) $(LDLIBS) +endif + +$(OUTDIR)/$(STATICLIB): $(OBJS) + ar r $(OUTDIR)/$(STATICLIB) $(OBJS) + +ifeq ($(GENERATELIBHEADER),1) +$(OUTDIR)/$(PROJ).hpp: $(HDRS) + @echo updating $(OUTDIR)/$(PROJ).hpp + @cp /dev/null $(OUTDIR)/$(PROJ).hpp + @for h in $(HDRS); \ + do \ + sed '/@exclude/d' $$h >> $(OUTDIR)/$(PROJ).hpp; \ + done +endif + +endif # --------------------------------------------------------------- lib + +# get generated dependencies, if any +-include $(DEPS) + +commit-hash: + @if ! echo $(COMMIT) | diff -q src/commit.inc - >/dev/null 2>&1 ;\ + then \ + echo $(COMMIT) > src/commit.inc ;\ + fi + +version: + @if ! echo $(VERSION) | diff -q src/version.inc - >/dev/null 2>&1 ;\ + then \ + echo $(VERSION) > src/version.inc ;\ + fi + +$(BUILDDIR)/version.o: src/version.inc src/commit.inc + +precomp.hpp.gch: src/precomp.hpp + $(CXX) $< $(CXXFLAGS) -o $@ + +plugins: +ifdef PLUGINS + @for plug in $(PLUGINS) ;\ + do \ + $(MAKE) -C $$plug ;\ + done +endif test: $(MAKE) -C tests && tests/tests clean: - $(RM) $(BUILDDIR) - $(MAKE) -C tests clean + rm -rf $(BUILDDIR) tests/build tests/tests tests/src/commit.inc tests/src/version.inc +ifdef PLUGINS + @for plug in $(PLUGINS) ;\ + do \ + $(MAKE) -C $$plug clean ;\ + done +endif -dist-clean: - $(RM) build $(BIN) src/commit.inc - $(MAKE) -C tests clean +dist-clean: clean + rm -rf build $(PROJ) src/commit.inc src/version.inc precomp.hpp.gch -install: - $(INSTALL) -d $(BINDIR) - $(INSTALL) $(OUTDIR)$(BIN) $(BINDIR) - $(INSTALL) -d $(MANDIR)$(MANSECTION) - $(INSTALL) -m 0644 $(MANPAGE) $(MANDIR)$(MANSECTION) +$(MANDIR)/man1: + install -m 0755 -d $@ +$(MANDIR)/man3: + install -m 0755 -d $@ + +$(MANDIR)/man5: + install -m 0755 -d $@ + +$(DATADIR): + install -m 0755 -d $@ + +# (un)install targets + +ifdef DATAFILES +install-data: $(DATADIR) + install -m 0644 $(DATAFILES) $(DATADIR) +else +install-data: +endif + +uninstall-data: + rm -rf $(DATADIR) + +install-plugins: +ifdef PLUGINS + @for plug in $(PLUGINS) ;\ + do \ + $(MAKE) -C $$plug install ;\ + done +endif + +uninstall-plugins: +ifdef PLUGINS + @for plug in $(PLUGINS) ;\ + do \ + $(MAKE) -C $$plug uninstall ;\ + done +endif + +ifeq ($(PRODUCT), tool) # -------------------------------------------- tool + +$(BINDIR): + install -m 0755 -d $@ + +install: $(MANDIR)/man1 $(MANDIR)/man3 $(MANDIR)/man5 $(BINDIR) $(MANS) install-data install-plugins + install -m 0755 $(BIN) $(BINDIR) + +uninstall: uninstall-data uninstall-plugins + rm -f $(BINDIR)/$(PROJ) $(MANS) + +endif # -------------------------------------------------------------- tool + +ifeq ($(PRODUCT), lib) # ---------------------------------------------- lib + +$(LIBDIR): + install -m 0755 -d $@ + +$(INCLUDEDIR): + install -m 0755 -d $@ + +$(INCLUDEDIR)/$(PROJ): + install -m 0755 -d $@ + +ifeq ($(GENERATELIBHEADER), 1) +install: $(MANDIR)/man1 $(MANDIR)/man3 $(MANDIR)/man5 $(LIBDIR) $(INCLUDEDIR) $(MANS) $(OUTDIR)/$(PROJ).hpp install-data install-plugins + install -m 0644 $(OUTDIR)/$(PROJ).hpp $(INCLUDEDIR) +else +install: $(MANDIR)/man1 $(MANDIR)/man3 $(MANDIR)/man5 $(LIBDIR) $(INCLUDEDIR) $(MANS) $(INCLUDEDIR)/$(PROJ) install-data install-plugins + install -m 0644 $(HDRS) $(INCLUDEDIR)/$(PROJ) +endif + install -m 0644 $(OUTDIR)/$(REALNAME) $(LIBDIR) + install -m 0644 $(OUTDIR)/$(STATICLIB) $(LIBDIR) +ifeq ($(UNAME_S), Darwin) + cd $(LIBDIR) && ln -sf $(REALNAME) $(SONAME) +else ifeq ($(UNAME_S), Linux) +ifeq ($(USER), root) + ldconfig +else + cd $(LIBDIR) && ln -sf $(REALNAME) $(SONAME) +endif + cd $(LIBDIR) && ln -sf $(SONAME) $(LINKERNAME) +else ifeq ($(UNAME_S), OpenBSD) + ldconfig -R +endif + +uninstall: uninstall-data uninstall-plugins + rm -rf $(INCLUDEDIR)/$(PROJ).hpp $(INCLUDEDIR)/$(PROJ) $(MANS) $(LIBDIR)/$(STATICLIB) $(LIBDIR)/$(REALNAME) $(LIBDIR)/$(SONAME) $(LIBDIR)/$(LINKERNAME) +ifeq ($(UNAME_S), Linux) + ldconfig +else ifeq ($(UNAME_S), OpenBSD) + ldconfig -R +endif + +endif # --------------------------------------------------------------- lib + +# ================================================================= targets + +# project-specific targets go in here -include postmake.make diff --git a/tagger.1 b/man/man1/tagger.1 similarity index 100% rename from tagger.1 rename to man/man1/tagger.1 diff --git a/premake.make b/premake.make index 59ab771..1e3c839 100644 --- a/premake.make +++ b/premake.make @@ -1,2 +1,31 @@ +# Define linker flags here, like: -lsqlite3 -lpthread LDLIBS := -lscstring -lscerror -lscterm -MANSECTION := 1 + +# Dir name becomes product name. +PROJ := $(shell basename $$(pwd)) + +# Find out what platform we're on, e.g. Darwin, Linux, OpenBSD. +UNAME_S := $(shell uname -s) + +# We will build a tool, not a library. +PRODUCT := tool + +# Single source of truth for version. +MAJOR := 1 +MINOR := 0 +PATCH := 1 + +# Specify desired C++ standard for this project. +CXXFLAGS += -std=c++20 + +# Set to 1 if you want a precompiled header for the C++ Standard Library. +PRECOMPILE := 0 + +# List of extra files to be installed in DATADIR (/usr/local/share/$(PROJ) by default) +DATAFILES := + +# Define plugin sub-projects here. Assumption here is a directory "plugins" +# containing plugin sub-projects, each in its own directory. +# Rename and/or repeat accordingly. +PLUGINS := $(wildcard plugins/*) +MAKE += --no-print-directory diff --git a/src/main.cpp b/src/main.cpp index 24cd4ea..1e999f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,7 @@ #include // project -#include "commit.inc" +#include "version.hpp" std::vector additions; std::string change; @@ -104,14 +104,6 @@ static void print_help() { std::cout << " -i, --into into new value\n"; } -static void print_version() { - std::cout << "tagger version 1.0"; - if (commit[0] != '\0') { - std::cout << ", build " << commit; - } - std::cout << std::endl; -} - int main(int argc, char* argv[]) { try { int opt_char, opt_val; @@ -132,7 +124,9 @@ int main(int argc, char* argv[]) { case 0: { // handle long-only options here switch (opt_val) { - case 1: print_version(); return EXIT_SUCCESS; + case 1: + std::cout << tagger_version() << '\n'; + return EXIT_SUCCESS; } break; } diff --git a/src/precomp.hpp b/src/precomp.hpp new file mode 100644 index 0000000..182bcb4 --- /dev/null +++ b/src/precomp.hpp @@ -0,0 +1,127 @@ +// C++98 (first official C++ standard) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if (__cplusplus >= 201103L) // C++11 +#include +#include +#include +#include +#include +#include // deprecated in C++17, removed in C++26 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif // C++11 + +#if (__cplusplus >= 201402L) // C++14 +#include +#endif // C++14 + +#if (__cplusplus >= 201703L) // C++17 +#include +#include +#include +#include +#include +#include +#include +#include +#endif // C++17 + +#if (__cplusplus >= 202002L) // C++20 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +//#include not yet supported by clang 16 +//#include not yet supported by clang 17 +#include +#endif // C++20 + +#if (__cplusplus >= 202302L) // C++23 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif // C++23 + +#if (__cplusplus > 202302L) // C++26 +#include +#include +#include +#include +#include +#include +#endif // C++26 diff --git a/src/version.cpp b/src/version.cpp new file mode 100644 index 0000000..2470899 --- /dev/null +++ b/src/version.cpp @@ -0,0 +1,19 @@ +#include "version.hpp" +#include + +std::string tagger_version() { +#include "version.inc" +#include "commit.inc" + std::ostringstream oss; + oss << "tagger version " << version; +#ifdef DEBUG + oss << " DEBUG"; +#endif + oss << '\n'; + if (commit[0] != '\0') { + oss << "build " << commit << ", "; + } + oss << __DATE__ << ", " << __TIME__ << '\n'; + oss << "(c) Bob Polis, all rights reserved"; + return oss.str(); +} diff --git a/src/version.hpp b/src/version.hpp new file mode 100644 index 0000000..d4fca0e --- /dev/null +++ b/src/version.hpp @@ -0,0 +1,8 @@ +#ifndef _TAGGER_VERSION_H_ +#define _TAGGER_VERSION_H_ + +#include + +std::string tagger_version(); + +#endif // _TAGGER_VERSION_H_ diff --git a/tests/Makefile b/tests/Makefile deleted file mode 100644 index 0762090..0000000 --- a/tests/Makefile +++ /dev/null @@ -1,51 +0,0 @@ -include ../premake.make -LDLIBS += -lboost_unit_test_framework - -BIN := $(shell basename $$(pwd)) - -SRCS := $(notdir $(wildcard src/*.cpp)) -SRCS += $(notdir $(filter-out ../src/main.cpp,$(wildcard ../src/*.cpp))) -OBJS := $(SRCS:.cpp=.o) -DEPS := $(SRCS:.cpp=.d) - -BUILDDIR := build/intermediates/ - -CXX ?= g++ -RM := /bin/rm -rf - -CXXFLAGS += -Wshadow -Wall -Wpedantic -Wextra -Wno-unused-parameter -CXXFLAGS += -g3 -std=c++20 -I../src -ifeq ($(DEBUG),1) - CXXFLAGS += -D DEBUG -O0 - CONFIG := debug -else - CXXFLAGS += -D NDEBUG -O3 - CONFIG := release -endif -OUTDIR := build/$(CONFIG)/ - -vpath %.cpp src ../src -vpath %.d $(BUILDDIR) -vpath %.o $(BUILDDIR) - -.PHONY: all clean prebuild - -all: prebuild $(OUTDIR)$(BIN) - -prebuild: - @mkdir -p $(BUILDDIR) $(OUTDIR) - -$(OUTDIR)$(BIN): $(OBJS) - $(CXX) $(addprefix $(BUILDDIR),$(OBJS)) $(LDFLAGS) $(LDLIBS) -o $(OUTDIR)$(BIN) - @ln -sf $(OUTDIR)$(BIN) $(BIN) - -%.o %.d: %.cpp - $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< - @mv $@ $*.d $(BUILDDIR) - --include $(addprefix $(BUILDDIR), $(DEPS)) - -clean: - $(RM) build $(BIN) - --include postmake.make diff --git a/tests/Makefile b/tests/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/tests/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/tests/postmake.make b/tests/postmake.make new file mode 100644 index 0000000..39e713a --- /dev/null +++ b/tests/postmake.make @@ -0,0 +1,6 @@ +$(BUILDDIR)/%.o: ../src/%.cpp +ifeq ($(PRECOMPILE), 1) + $(CXX) $(CXXFLAGS) -o $@ -include precomp.hpp -MMD -MP -MT $@ -MF $(BUILDDIR)/$*.dep -c $< +else + $(CXX) $(CXXFLAGS) -o $@ -MMD -MP -MT $@ -MF $(BUILDDIR)/$*.dep -c $< +endif diff --git a/tests/premake.make b/tests/premake.make new file mode 100644 index 0000000..d5cfa47 --- /dev/null +++ b/tests/premake.make @@ -0,0 +1,5 @@ +include ../premake.make +LDLIBS += -lboost_unit_test_framework +CXXFLAGS += -I../src +SRCS := $(notdir $(filter-out ../src/main.cpp,$(wildcard ../src/*.cpp))) +PRODUCT := tool diff --git a/tests/src/precomp.hpp b/tests/src/precomp.hpp new file mode 120000 index 0000000..821bca7 --- /dev/null +++ b/tests/src/precomp.hpp @@ -0,0 +1 @@ +../../src/precomp.hpp \ No newline at end of file