commit d9f96880205c23a891b6d64533fd1b8b6d337883 Author: Bob Polis Date: Sat Dec 11 23:46:06 2021 +0100 first commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0add70b --- /dev/null +++ b/Makefile @@ -0,0 +1,104 @@ +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 +STATICLIB := $(LIBNAME).a + +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 += -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) $(OUTDIR)$(STATICLIB) + +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 Makefile + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + @mv $@ $*.d $(BUILDDIR) + +-include $(BUILDDIR)*.d + +%.d: ; + +$(OUTDIR)$(STATICLIB): $(OBJS) + ar r $(OUTDIR)$(STATICLIB) $(addprefix $(BUILDDIR),$(OBJS)) + +$(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) $(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..e6ec4a5 --- /dev/null +++ b/premake.make @@ -0,0 +1 @@ +LDLIBS := -lscerror -lm diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..8ff37ca --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,57 @@ +#include "utils.hpp" +#include +#include +#include +#include +#include + +using namespace sc; + +term::term() { + if (isatty(STDOUT_FILENO)) { + throw_if_min1(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)); + } +} + +int term::rows() const { + return ws.ws_row; +} + +int term::cols() const { + return ws.ws_col; +} + +void term::progress(double cur, double total) const { + // use unicode to make nice bar + const char* bar[8] = { + u8"\u258F", u8"\u258E", u8"\u258D", u8"\u258C", + u8"\u258B", u8"\u258A", u8"\u2589", u8"\u2588" + }; + int barwidth = cols() - 5; + int maxsteps = barwidth * 8; + int steps = round(cur * maxsteps / total); + int perc = round(100 * cur / total); + std::cerr << '\r' << std::setw(3) << perc << "% "; + for (int i = 0; i < steps / 8; ++i) { + std::cerr << bar[7]; + } + if (steps % 8) { + std::cerr << bar[steps % 8 - 1]; + } +} + +void term::hide_cursor() const { + std::cerr << "\x1b[?25l"; +} + +void term::show_cursor() const { + std::cerr << "\x1b[?25h"; +} + +cursor_hider::cursor_hider() { + t.hide_cursor(); +} + +cursor_hider::~cursor_hider() { + t.show_cursor(); +} diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..f4c8853 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,35 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include +#include +#include + +namespace sc { + + class term { + struct winsize ws; + + public: + term(); + + int rows() const; + int cols() const; + + void progress(double cur, double total) const; + + void hide_cursor() const; + void show_cursor() const; + }; + + class cursor_hider { + term t; + + public: + cursor_hider(); + ~cursor_hider(); + }; + +} + +#endif // UTILS_H_ diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..7e524f5 --- /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 += -Wshadow -Wall -Wpedantic -Wextra -g -std=c++17 -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); +}