first commit

This commit is contained in:
Bob Polis 2021-12-11 23:46:06 +01:00
commit d9f9688020
6 changed files with 256 additions and 0 deletions

104
Makefile Normal file
View File

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

1
premake.make Normal file
View File

@ -0,0 +1 @@
LDLIBS := -lscerror -lm

57
src/utils.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "utils.hpp"
#include <libscerror.hpp>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
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();
}

35
src/utils.hpp Normal file
View File

@ -0,0 +1,35 @@
#ifndef UTILS_H_
#define UTILS_H_
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
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_

51
tests/Makefile Normal file
View File

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

8
tests/src/main.cpp Normal file
View File

@ -0,0 +1,8 @@
#define BOOST_TEST_MODULE My Test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test)
{
BOOST_TEST(1 == 1);
}