From 3af995320685507cb54e60507257948dbf28ae12 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Fri, 2 Jul 2021 17:59:45 +0200 Subject: [PATCH] first commit --- Makefile | 101 ++++++++++++++++++++++++++++++++++++++++++++ premake.make | 1 + src/config_file.cpp | 22 ++++++++++ src/config_file.hpp | 34 +++++++++++++++ tests/Makefile | 51 ++++++++++++++++++++++ tests/src/main.cpp | 8 ++++ 6 files changed, 217 insertions(+) create mode 100644 Makefile create mode 100644 premake.make create mode 100644 src/config_file.cpp create mode 100644 src/config_file.hpp create mode 100644 tests/Makefile create mode 100644 tests/src/main.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e453519 --- /dev/null +++ b/Makefile @@ -0,0 +1,101 @@ +include premake.make + +LIBNAME := $(shell basename $$(pwd)) +MAJOR := 1 +MINOR := 0.0 + +UNAME_S := $(shell uname -s) + +ifeq ($(UNAME_S),Darwin) + LINKERNAME := $(LIBNAME).dylib + SONAME := $(LIBNAME).$(MAJOR).dylib + REALNAME := $(LINKERNAME) +else + LINKERNAME := $(LIBNAME).so + SONAME := $(LINKERNAME).$(MAJOR) + REALNAME := $(SONAME).$(MINOR) +endif + +BUILDDIR := build/intermediates/ +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +CONFIGDIR ?= $(PREFIX)/etc +INCLUDEDIR ?= $(PREFIX)/include +LIBDIR ?= $(PREFIX)/lib +DATADIR ?= $(PREFIX)/share +MANDIR ?= $(DATADIR)/man +DOCDIR ?= $(DATADIR)/$(LIBNAME)/doc + +SRCS := $(notdir $(wildcard src/*.cpp)) +OBJS := $(SRCS:.cpp=.o) +DEPS := $(SRCS:.cpp=.d) +HDRS := $(wildcard src/*.hpp) + +CXX ?= g++ + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -fPIC +ifeq ($(DEBUG),1) + CXXFLAGS += -D DEBUG -O0 + CONFIG := debug +else + CXXFLAGS += -D NDEBUG -O3 + CONFIG := release +endif +OUTDIR := build/$(CONFIG)/ + +RM := /bin/rm -rf +INSTALL := /usr/bin/install -c + +vpath %.cpp src +vpath %.d $(BUILDDIR) +vpath %.o $(BUILDDIR) + +.PHONY: all clean install prebuild test + +all: prebuild $(OUTDIR)$(REALNAME) + +prebuild: + @mkdir -p $(BUILDDIR) $(OUTDIR) + +$(OUTDIR)$(REALNAME): $(OBJS) $(DEPS) +ifeq ($(UNAME_S),Darwin) + $(CXX) -dynamiclib -o $(OUTDIR)$(REALNAME) -current_version $(MAJOR) \ + -compatibility_version $(MINOR) $(LDFLAGS) $(LDLIBS) $(addprefix $(BUILDDIR),$(OBJS)) +else + $(CXX) -g -shared -Wl,-soname,$(SONAME) -o $(OUTDIR)$(REALNAME) $(LDFLAGS) $(LDLIBS) $(addprefix $(BUILDDIR),$(OBJS)) +endif + +%.o: %.cpp %.d + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + @mv $@ $*.d $(BUILDDIR) + +-include $(BUILDDIR)*.d + +%.d: ; + +build/$(LIBNAME).hpp: $(HDRS) + @echo updating build/$(LIBNAME).hpp + @cp /dev/null build/$(LIBNAME).hpp + @for h in $(HDRS); \ + do \ + cat $$h >> build/$(LIBNAME).hpp; \ + done + +test: + $(MAKE) -C tests && tests/tests + +clean: + $(RM) build + $(MAKE) -C tests clean + +install: $(OUTDIR)$(REALNAME) build/$(LIBNAME).hpp + $(INSTALL) -d $(LIBDIR) + $(INSTALL) -m 644 $(OUTDIR)$(REALNAME) $(LIBDIR) + $(INSTALL) -d $(INCLUDEDIR) + $(INSTALL) -m 644 build/$(LIBNAME).hpp $(INCLUDEDIR) +ifeq ($(UNAME_S),Darwin) + cd $(LIBDIR) && ln -sf $(REALNAME) $(SONAME) +else + ldconfig + cd $(LIBDIR) && ln -sf $(SONAME) $(LINKERNAME) +endif diff --git a/premake.make b/premake.make new file mode 100644 index 0000000..0328966 --- /dev/null +++ b/premake.make @@ -0,0 +1 @@ +LDLIBS := -lm -lscstring diff --git a/src/config_file.cpp b/src/config_file.cpp new file mode 100644 index 0000000..7d44c50 --- /dev/null +++ b/src/config_file.cpp @@ -0,0 +1,22 @@ +#include "config_file.hpp" +#include + +sc::config_file::config_file(const std::string& prog_name, bool has_dir) + : _has_dir {has_dir}, _prog_name {prog_name} +{ + std::string cfg_path; + _root = getenv("HOME"); + _root += "/.config/"; + if (has_dir) { + _root += prog_name + "/"; + cfg_path = _root + "config"; // sensible default + // TODO maybe also look for .conf or rc here. + } else { + cfg_path = _root + prog_name + "rc"; // standard "run control" file + } + _cfg = sc::parse_ini_file(cfg_path); +} + +std::string sc::config_file::path_for_file_name(const std::string& file_name) { + return _root + file_name; +} diff --git a/src/config_file.hpp b/src/config_file.hpp new file mode 100644 index 0000000..80f127f --- /dev/null +++ b/src/config_file.hpp @@ -0,0 +1,34 @@ +#ifndef SC_CONFIG_HPP_ +#define SC_CONFIG_HPP_ + +#include +#include +#include + +namespace sc { + + class config_file { + public: + config_file(const std::string& prog_name, bool has_dir = false); + + std::string path_for_file_name(const std::string& file_name); + + template + T get(const std::string& key, const T& default_value) { + auto it = _cfg.find(key); + if (it == _cfg.end()) { + return default_value; + } + return sc::from_string(it->second); + } + + private: + bool _has_dir {false}; + std::string _prog_name; + std::string _root; + std::map _cfg; + }; + +} + +#endif // SC_CONFIG_HPP_ diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..ae065a3 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,51 @@ +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 +INSTALL := /usr/bin/install -c + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -pthread -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) $(DEPS) + $(CXX) $(addprefix $(BUILDDIR),$(OBJS)) $(LDFLAGS) $(LDLIBS) -o $(OUTDIR)$(BIN) + @ln -sf $(OUTDIR)$(BIN) $(BIN) + +%.o: %.cpp %.d + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + @mv $@ $*.d $(BUILDDIR) + +-include $(BUILDDIR)*.d + +%.d: ; + +clean: + $(RM) build $(BIN) diff --git a/tests/src/main.cpp b/tests/src/main.cpp new file mode 100644 index 0000000..533ab2a --- /dev/null +++ b/tests/src/main.cpp @@ -0,0 +1,8 @@ +#define BOOST_TEST_MODULE My Test +#define BOOST_TEST_DYN_LINK +#include + +BOOST_AUTO_TEST_CASE(first_test) +{ + BOOST_TEST(1 == 1); +}