first commit

This commit is contained in:
Bob Polis 2021-10-06 10:55:22 +02:00
commit 087e31219c
6 changed files with 228 additions and 0 deletions

100
Makefile Normal file
View File

@ -0,0 +1,100 @@
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 Makefile
$(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $<
@mv $@ $*.d $(BUILDDIR)
-include $(BUILDDIR)*.d
%.d: ;
$(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 := -lm

23
src/random.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "random.hpp"
sc::random sc::random::singleton {};
bool sc::random::random_bool() {
std::uniform_int_distribution<int> dist {0, 1};
return dist(_reng) == 1;
}
int sc::random::random_int(int from, int to) {
std::uniform_int_distribution<int> dist {from, to};
return dist(_reng);
}
double sc::random::random_double() {
std::uniform_real_distribution<double> dist {};
return dist(_reng);
}
double sc::random::random_double(double from, double to) {
std::uniform_real_distribution<double> dist {from, to};
return dist(_reng);
}

45
src/random.hpp Normal file
View File

@ -0,0 +1,45 @@
#ifndef RANDOM_H_
#define RANDOM_H_
#include <random>
namespace sc {
class random {
public:
static random& instance() { return singleton; }
// forbid copying
random(const random&) = delete;
random& operator=(const random&) = delete;
// forbid moving
random(random&&) = delete;
random& operator=(random&&) = delete;
std::default_random_engine& engine() { return _reng; }
bool random_bool();
int random_int(int from, int to);
double random_double();
double random_double(double from, double to);
template <typename T, typename Iter>
T choice(Iter beg, Iter end) {
std::uniform_int_distribution<int> dist {0, end - beg - 1};
return beg + dist(_reng);
}
private:
static random singleton;
random() = default;
~random() = default;
std::random_device _rdev {};
std::default_random_engine _reng {_rdev()};
};
}
#endif // RANDOM_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 := $(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)

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);
}