From 9fb0030c7a3db272a2aef9e4f9f7cfc75a50a216 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 2 Nov 2021 23:32:14 +0000 Subject: [PATCH 1/4] Compiles for static + shared libraries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Future small blessing − 末小吉 --- .gitignore | 2 + Makefile | 130 ++++++++++++++++++++++++++++++++++++++++----------- src/main.cpp | 43 ----------------- 3 files changed, 105 insertions(+), 70 deletions(-) delete mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore index 46cdddc..304249c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ obj/ vgcore.* *-release *-debug +*.so* +*.a diff --git a/Makefile b/Makefile index 86d7d15..97788a1 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,39 @@ -# Generic C and C++ Makefile project template -# Contains targets for `release', `debug', and `clean'. +# pipec - Piping IPC -PROJECT=rng +PROJECT=rngxx AUTHOR=Avril (Flanchan) -SRC_C = $(wildcard src/*.c) $(wildcard src/rng/*.c) -SRC_CXX = $(wildcard src/*.cpp) $(wildcard src/rng/*.cpp) +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION=$(VERSION_MAJOR).$(VERSION_MINOR) + +ifeq ($(PREFIX),) + PREFIX := /usr/local +endif + +SRC_C = $(wildcard src/*.c) +SRC_CXX = $(wildcard src/*.cpp) INCLUDE=include -COMMON_FLAGS+= -W -Wall -pedantic -fno-strict-aliasing $(addprefix -I,$(INCLUDE)) -OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ +COMMON_FLAGS= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing "-D_VERSION=$(VERSION)" $(addprefix -I,$(INCLUDE)) + +TARGET_CPU?=native +OPT_FLAGS?= $(addprefix -march=,$(TARGET_CPU)) -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -fno-stack-check -CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors +CXX_OPT_FLAGS?= $(OPT_FLAGS) -CFLAGS += $(COMMON_FLAGS) --std=gnu17 -CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -LDFLAGS += +# Static analyzer currently generates false positives for C++, enable it only for C +CFLAGS += $(COMMON_FLAGS) --std=gnu17 -fanalyzer +CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -felide-constructors +LDFLAGS += STRIP=strip -RELEASE_COMMON_FLAGS+= # -Werror -DEBUG_COMMON_FLAGS+= +RELEASE_COMMON_FLAGS+= -Werror +DEBUG_COMMON_FLAGS+= -fanalyzer ifneq ($(TARGET_SPEC_FLAGS),no) RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) @@ -50,10 +60,33 @@ OBJ = $(OBJ_C) $(OBJ_CXX) # Phonies .PHONY: release -release: | dirs $(PROJECT)-release +release: | dirs + $(MAKE) lib$(PROJECT).a + @$(MAKE) clean-rebuild >> /dev/null + @$(MAKE) dirs >> /dev/null + $(MAKE) lib$(PROJECT).so .PHONY: debug -debug: | dirs $(PROJECT)-debug +debug: | dirs + $(MAKE) lib$(PROJECT)-debug.a + @$(MAKE) clean-rebuild >> /dev/null + @$(MAKE) dirs >> /dev/null + $(MAKE) lib$(PROJECT)-debug.so + +# Rebuild both release and debug targets from scratch +.PHONY: all +all: | clean + @$(MAKE) release + @$(MAKE) clean-rebuild + @$(MAKE) debug + +.PHONY: install +.PHONY: uninstall + +.PHONY: test +test: + @rm -f $(PROJECT)-test + @$(MAKE) $(PROJECT)-test # Targets @@ -66,22 +99,65 @@ obj/c/%.o: %.c obj/cxx/%.o: %.cpp $(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS) -$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) -$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) -$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS) -$(PROJECT)-release: $(OBJ) - $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) +lib$(PROJECT)-release.a: CFLAGS+= $(RELEASE_CFLAGS) +lib$(PROJECT)-release.a: CXXFLAGS += $(RELEASE_CXXFLAGS) +lib$(PROJECT)-release.a: LDFLAGS += $(RELEASE_LDFLAGS) +lib$(PROJECT)-release.a: $(OBJ) + ar rcs $@ $^ + ranlib $@ + +lib$(PROJECT)-debug.a: CFLAGS+= $(DEBUG_CFLAGS) +lib$(PROJECT)-debug.a: CXXFLAGS += $(DEBUG_CXXFLAGS) +lib$(PROJECT)-debug.a: LDFLAGS += $(DEBUG_LDFLAGS) +lib$(PROJECT)-debug.a: $(OBJ) + ar rcs $@ $^ + ranlib $@ + +lib$(PROJECT)-release.so: CFLAGS+= $(RELEASE_CFLAGS) -fPIC +lib$(PROJECT)-release.so: CXXFLAGS += $(RELEASE_CXXFLAGS) -fPIC +lib$(PROJECT)-release.so: LDFLAGS += $(RELEASE_LDFLAGS) +lib$(PROJECT)-release.so: $(OBJ) + $(CXX) -shared $^ $(CXXFLAGS) -o $@ $(LDFLAGS) $(STRIP) $@ -$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) -$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) -$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS) -$(PROJECT)-debug: $(OBJ) - $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) +lib$(PROJECT)-debug.so: CFLAGS+= $(DEBUG_CFLAGS) -fPIC +lib$(PROJECT)-debug.so: CXXFLAGS += $(DEBUG_CXXFLAGS) -fPIC +lib$(PROJECT)-debug.so: LDFLAGS += $(DEBUG_LDFLAGS) +lib$(PROJECT)-debug.so: $(OBJ) + $(CXX) -shared $^ $(CXXFLAGS) -o $@ $(LDFLAGS) + +lib$(PROJECT).a: lib$(PROJECT)-release.a + ln -f $< $@ + +lib$(PROJECT).so: LDFLAGS+= -Wl,-soname,lib$(PROJECT).so.$(VERSION_MAJOR) +lib$(PROJECT).so: lib$(PROJECT)-release.so + ln -f $< $@.$(VERSION) + ln -sf $@.$(VERSION) $@.$(VERSION_MAJOR) + ln -sf $@.$(VERSION_MAJOR) $@ clean-rebuild: rm -rf obj clean: clean-rebuild - rm -f $(PROJECT)-{release,debug,pgo} - + rm -f lib$(PROJECT){,-{release,debug,pgo}}.{a,so{,.*}} + rm -f $(PROJECT)-test + +install: + install -d $(DESTDIR)$(PREFIX)/lib/ + install -m 644 lib$(PROJECT).a $(DESTDIR)$(PREFIX)/lib/ + install -s -m 755 lib$(PROJECT).so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/ + ln -sf lib$(PROJECT).so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).so.$(VERSION_MAJOR) + ln -sf lib$(PROJECT).so.$(VERSION_MAJOR) $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).so + install -d $(DESTDIR)$(PREFIX)/include/ + install -m 644 $(wildcard $(INCLUDE)/*.*) $(DESTDIR)$(PREFIX)/include/ + install -d $(DESTDIR)$(PREFIX)/include/$(PROJECT)/ + install -m 644 $(wildcard $(INCLUDE)/$(PROJECT)/*.*) $(DESTDIR)$(PREFIX)/include/$(PROJECT)/ + +uninstall: + -rm $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).{a,so{,.*}} + cd $(INCLUDE) && find . -type f | xargs -I {} rm "$(DESTDIR)$(PREFIX)/include/{}" + -rmdir $(DESTDIR)$(PREFIX)/include/$(PROJECT) + +$(PROJECT)-test: lib$(PROJECT).so + $(CXX) $(CXXFLAGS) src/test/*.cpp -o $@ -l:$< -lfmt -Wl,-flto -Wl,-O3 $(LDFLAGS) + -valgrind ./$@ diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index af9f001..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include - -#include - -using namespace rng; - -void r(Random& r) -{ - u8 bytes[32]; - r.next_bytes(bytes); - u32 n = r.next(); - f64 f = r.next(); - f = r.next_f64(); - std::array nn = r.next>(); -} - -void rng_test() -{ - crand _r(123); - Random& r = _r; - printf("%lu %lu %lu\n", r.next_u64(), r.next_u64(), r.next_u64()); - printf("%d %d %d\n", r.next_i32(), r.next_i32(), r.next_i32()); - printf("%u %u %u\n", r.next_u32(), r.next_u32(), r.next_u32()); - - union { - volatile u64 u; - u8 b[sizeof(u64)]; - std::array a; - } thing = {0}; - - r.next_bytes(thing.b, sizeof(u64)); - printf("chaos: %lu, %lu, %lu\n", thing.u, (r.next_bytes(thing.a), thing.u), (r.next_bytes(thing.b), thing.u)); - - // TODO: these aren't implemented yet in the base Random huh... - printf("---\n%u %d %d %u\n", r.next_u32(10, 20), r.next_i32(10, 20), r.next_i32(10), r.next_u32(10)); -} -int main() -{ - rng_test(); - - return 0; -} From df0fbadf3b88a70aae0c0c7db86cd2244938cb26 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 2 Nov 2021 23:37:17 +0000 Subject: [PATCH 2/4] Reorganised /include MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Future curse − 末凶 --- Makefile | 2 +- include/rng.h | 2 +- include/{ => rng}/common.h | 6 +++--- src/internal/common.h | 1 + {include => src/internal}/mem.h | 0 {include => src/internal}/range.h | 0 6 files changed, 6 insertions(+), 5 deletions(-) rename include/{ => rng}/common.h (96%) create mode 120000 src/internal/common.h rename {include => src/internal}/mem.h (100%) rename {include => src/internal}/range.h (100%) diff --git a/Makefile b/Makefile index 97788a1..274e2d4 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ endif SRC_C = $(wildcard src/*.c) SRC_CXX = $(wildcard src/*.cpp) -INCLUDE=include +INCLUDE=include src/internal COMMON_FLAGS= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing "-D_VERSION=$(VERSION)" $(addprefix -I,$(INCLUDE)) diff --git a/include/rng.h b/include/rng.h index 1df12e8..7b193b0 100644 --- a/include/rng.h +++ b/include/rng.h @@ -6,7 +6,7 @@ #include -#include "common.h" +#include "rng/common.h" #define CTOR_COPY(name) name(const name& copy) #define CTOR_MOVE(name) name(name&& move) diff --git a/include/common.h b/include/rng/common.h similarity index 96% rename from include/common.h rename to include/rng/common.h index bcc1d01..cc49478 100644 --- a/include/common.h +++ b/include/rng/common.h @@ -1,5 +1,5 @@ -#ifndef _INTS_H -#define _INTS_H +#ifndef _RNG__COMMON_H +#define _RNG__COMMON_H #ifdef __cplusplus extern "C" { @@ -69,4 +69,4 @@ typedef uintptr_t ptr_t; } #endif -#endif /* _INTS_H */ +#endif /* _RNG__COMMON_H */ diff --git a/src/internal/common.h b/src/internal/common.h new file mode 120000 index 0000000..25c7947 --- /dev/null +++ b/src/internal/common.h @@ -0,0 +1 @@ +../../include/rng/common.h \ No newline at end of file diff --git a/include/mem.h b/src/internal/mem.h similarity index 100% rename from include/mem.h rename to src/internal/mem.h diff --git a/include/range.h b/src/internal/range.h similarity index 100% rename from include/range.h rename to src/internal/range.h From 54dfb6459685fe3649c6ed983f30a5966de30f01 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 2 Nov 2021 23:41:15 +0000 Subject: [PATCH 3/4] Renamed API header & API header location /include MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Future small blessing − 末小吉 --- include/{rng.h => rngxx.h} | 2 +- include/{rng => rngxx}/common.h | 0 include/{rng => rngxx}/crand.h | 0 src/internal/common.h | 2 +- src/rng.cpp | 2 +- 5 files changed, 3 insertions(+), 3 deletions(-) rename include/{rng.h => rngxx.h} (99%) rename include/{rng => rngxx}/common.h (100%) rename include/{rng => rngxx}/crand.h (100%) diff --git a/include/rng.h b/include/rngxx.h similarity index 99% rename from include/rng.h rename to include/rngxx.h index 7b193b0..8944e1e 100644 --- a/include/rng.h +++ b/include/rngxx.h @@ -6,7 +6,7 @@ #include -#include "rng/common.h" +#include "rngxx/common.h" #define CTOR_COPY(name) name(const name& copy) #define CTOR_MOVE(name) name(name&& move) diff --git a/include/rng/common.h b/include/rngxx/common.h similarity index 100% rename from include/rng/common.h rename to include/rngxx/common.h diff --git a/include/rng/crand.h b/include/rngxx/crand.h similarity index 100% rename from include/rng/crand.h rename to include/rngxx/crand.h diff --git a/src/internal/common.h b/src/internal/common.h index 25c7947..df8e6f3 120000 --- a/src/internal/common.h +++ b/src/internal/common.h @@ -1 +1 @@ -../../include/rng/common.h \ No newline at end of file +../../include/rngxx/common.h \ No newline at end of file diff --git a/src/rng.cpp b/src/rng.cpp index 62f467a..333ede1 100644 --- a/src/rng.cpp +++ b/src/rng.cpp @@ -1,5 +1,5 @@ -#include +#include #include constexpr const static util::range SAMPLE_RANGE { 0.0, 1.0 }; From 5b7983154c3eb91d51caaa5444435defa190e5af Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 2 Nov 2021 23:44:26 +0000 Subject: [PATCH 4/4] Fix internal includes being installed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Curse − 凶 --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 274e2d4..42404e7 100644 --- a/Makefile +++ b/Makefile @@ -14,9 +14,10 @@ endif SRC_C = $(wildcard src/*.c) SRC_CXX = $(wildcard src/*.cpp) -INCLUDE=include src/internal +INCLUDE=include +INCLUDE_INTERNAL=src/internal -COMMON_FLAGS= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing "-D_VERSION=$(VERSION)" $(addprefix -I,$(INCLUDE)) +COMMON_FLAGS= -W -Wall -Wextra -Wstrict-aliasing -fno-strict-aliasing "-D_VERSION=$(VERSION)" $(addprefix -I,$(INCLUDE)) $(addprefix -I,$(INCLUDE_INTERNAL)) TARGET_CPU?=native OPT_FLAGS?= $(addprefix -march=,$(TARGET_CPU)) -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \