# Mostly-header library for threaded-programming sync primitives & helpers `fx::[shared::]{Mutex,CondVar}`, `fx::Queue`, & namespace `fx::signal`. # # Likely prefer static-linking. Non-header elements are required for `fx::error` & certain required features of `fx::signal::` # # Contains targets for `release', `debug', and `clean'. PROJECT=fx-sync AUTHOR=Avril (Flanchan) DESCRIPTION=sync module of libfx VERSION=0.1.1 SRC = src SRC_C = $(shell find $(SRC)/ -type f -name \*.c -and -not -path $(SRC)/test/\* ) SRC_CXX = $(shell find -O2 $(SRC)/ -type f \( -name \*.cpp -or -name \*.cxx \) -and -not -path $(SRC)/test/\* ) INCLUDE=include # If PCH should be auto-included for all TUs, set to 1. INCLUDE_PCH_GLOBAL?=0 # Files to be auto-included by all TUs (after PCH global, if `INCLUDE_PCH_GLOBAL == 1`.) INCLUDE_GLOBAL= # Link to these libraries dynamicalls SHARED_LIBS= # Link to these libraries statically STATIC_LIBS= # Prefix for un/install targets, if it's blank, ensure it is set (default to `/usr/local` instead of `/usr`.) ifeq ($(PREFIX),) PREFIX := /usr/local endif # Prefix for un/install target headers (default: all of `$(INCLUDE)`) # Make blank for default install to PREFIX/include # TODO: Do we want to turn this to `fx/sync/` instead of `fx-sync/`? What if `fx` is taken in usr/include..? (XXX: Or if other fx-* projects have things of the same name (esp. error.hh which is currently not standardised...?) Leave it as fx-sync for now i think...) INCLUDE_PREFIX := $(PROJECT)/ # Default archivers AR?=ar RANLIB?=ranlib # Default linker LINKER?=gold # Use gcc-{ar,ranlib} when using gcc ifeq ($(CXX),g++) AR:=gcc-ar RANLIB:=gcc-ranlib endif # Pre-compile these headers PCH_HEADERS+= # PCH_HEADERS depend on these header files PCH_INCLUDES+= #TODO: Add in the configurable opt levels from `work/map-rc/Makefile` # Compile-time default program features (see `Features application` below.) FEATURES?= # Build constants CONSTANTS+=_GNU_SOURCE __API_EXPORT__=__attribute__\(\(\visibility\(\"default\"\)\)\) # Default visibility ("default" / "", "hidden", "protected", "internal".) # If not exported, symbols must be manually exported. SYMBOL_VISIBILITY?=default # Reduce binary size by removing inline address identity & define symbols relevant only to exported symbols # # NOTE: This requires either `SYMBOL_VISIBILITY=hidden` or manual non-exported symbol hiding to be effective. # Only disable this if `inline`s need address identity. STRICT_EXPORT?=yes # Testing # Can be {debug,release}.{a,so} TARGET?=debug.a TEST_LDFLAGS+= -lfmt -lstdc++ -Wl,-z -Wl,now -Wl,-z -Wl,relro TEST_CFLAGS+= -Og -g -fwhole-program override __COMMA=, override __VERSION_SPLIT:= $(subst ., ,$(VERSION)) override __VERSION_REVISION:=$(word 3,$(__VERSION_SPLIT)) 0 VERSION_MAJOR:= $(word 1,$(__VERSION_SPLIT)) VERSION_MINOR:= $(word 2,$(__VERSION_SPLIT)) VERSION_BUGFIX:= $(word 3,$(__VERSION_SPLIT)) VERSION_REVISION:= $(word 2,$(subst r, ,$(__VERSION_REVISION))) override __VERSION_SPLIT:= MAJOR:$(word 1,$(__VERSION_SPLIT)) MINOR:$(word 2,$(__VERSION_SPLIT)) BUGFIX:$(word 1,$(subst r, ,$(__VERSION_REVISION))) REVISION:$(word 2,$(subst r, ,$(__VERSION_REVISION))) REVISION_STRING:$(word 3,$(__VERSION_SPLIT)) COMMON_FLAGS+= -W -Wall COMMON_FLAGS+= $(addprefix -D,$(CONSTANTS)) COMMON_FLAGS+= -pipe -Wstrict-aliasing -fno-strict-aliasing $(addprefix -I,$(INCLUDE)) COMMON_FLAGS+= $(addprefix -D_VERSION_,$(subst :,=,$(__VERSION_SPLIT))) '-D_PROJECT_VERSION="$(VERSION)"' '-D_PROJECT_AUTHOR="$(AUTHOR)"' '-D_PROJECT_NAME="$(PROJECT)"' COMMON_FLAGS+= $(addprefix -fvisibility=,$(SYMBOL_VISIBILITY)) ifeq ($(STRICT_EXPORT),yes) COMMON_FLAGS+= -fvisibility-inlines-hidden -ffunction-sections -fdata-sections endif # PCH targets for `%_p.hh` -> `%_p.hh.gch`, add `-include $(PCH_HEADERS)` to general `COMMON_FLAGS`, and add $(PCH_OUT) as a requirement for all `%.c/pp` targets as well. (where `PCH_OUT= $(addsuffix .gch,$(PCH_HEADERS))`.) # Target arch & CPU. Set to blank for generic ARCH?=native CPU?=native # Enable OpenMP and loop parallelisation? (dyn-links to openmp, allows C++STL algorithms to use parallel execution mode when provided, enables auto-parallelise optimisation of loops in `release` build only.) # # To enable in CXX without auto-optimisation, set to `maybe` instead of `yes`. (__NOTE__: When set to `maybe`, if C++ parallel execution modes are used (from the `` header,) you will need to manually add `tbb` to `SHARED_LIBS`. `yes` and `force` add `-ltbb` automatically alongside `-fopenmp`.) # To enable throughout, *regardless of* release-only optimisations, set to `force`, (`yes` only enables to always in CXX, and only enables it for CC in release builds.) # To *only ever* enable in release builds, *regardless of optimisations*, set to `release` (which acts as `yes` but only in release builds.) PARALLEL?=no # Set exception mode to: # - Empty - Exceptions are left as the default for CXX, and nothing about them is communicated to CC either. # - 'debug' (default) - Same as above, but `-fexceptions` is passed to CC in debug builds only. (XXX: Is this useful, to not pass it to CXX as well in debug...?) # - 'yes' - Pass `-fexceptions` to CC. # - 'no' - Pass `-fno-exceptions` to CXX. # - 'force' - Pass `-fexceptions` to CC and CXX always. # - 'never' - Pass `-fno-exceptions` to CC and CXX always. EXCEPTIONS?=force # Enable CPU-specific features CPU_FLAGS?= BINFLAGS+= DEBUG_BINFLAGS+= RELEASE_BINFLAGS+= -fuse-linker-plugin OPT_FLAGS?= -fgraphite \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -fno-stack-check # Static and shared common flags SHARED_FLAGS+=-fPIC SHARED_RELEASE_FLAGS+= SHARED_DEBUG_FLAGS+= STATIC_FLAGS+= STATIC_RELEASE_FLAGS+=-ffat-lto-objects STATIC_DEBUG_FLAGS+= # Features application ## Tell program which features are enabled via `FEATURE_`. override __FEATURES=$(shell echo "$(FEATURES)" | tr '[:lower:]' '[:upper:]' | tr '-' '_') COMMON_FLAGS+=$(addsuffix =1,$(addprefix -DFEATURE_,$(__FEATURES))) ## Specific feature additions # ## Example: #ifneq (,$(findstring fast-math,$(FEATURES))) # # fast-math feature: Apply `-ffast-math` # COMMON_FLAGS+=-ffast-math #endif # Add linker ifneq ($(LINKER),) COMMON_FLAGS+=-fuse-ld=$(LINKER) endif # Arch and optimisation ifneq ($(ARCH),) OPT_FLAGS+= $(addprefix -march=,$(ARCH)) endif ifneq ($(CPU),) OPT_FLAGS+= $(addprefix -mtune=,$(CPU)) endif # Add parallelisation flags override __PARALLEL_OPT_FLAGS=-floop-parallelize-all -ftree-parallelize-loops=4 ifeq ($(PARALLEL),yes) # NOTE: `-fopenmp` adds parallelised STL algorithms by default, so we want C++ (debug & release) TUs to always be compiled with it # XXX: Should we add this in for all `COMMON_FLAGS` to enable to C TUs too? Will this make the interop / debugging any better to *only* enable it in C for release builds?? COMMON_FLAGS+= -fopenmp -DPARALLEL=1 OPT_FLAGS+= $(__PARALLEL_OPT_FLAGS) # For C++ `std::execution` parallel execution models. LDFLAGS+= -ltbb -lgomp endif ifeq ($(PARALLEL),maybe) # Define `PARALLEL` constant, but to 0. So `#ifdef` and `#if` can distinguish. CFLAGS+= -DPARALLEL=0 # Add openmp to C & CXX, without adding loop auto-parallelise optimisation CXXFLAGS+= -fopenmp -DPARALLEL=1 -DPARALLEL_CXX endif ifeq ($(PARALLEL),force) # Add openmp to C & CXX COMMON_FLAGS+= -fopenmp -DPARALLEL=1 -DPARALLEL_FORCE OPT_FLAGS+= $(__PARALLEL_OPT_FLAGS) # For C++ `std::execution` parallel execution models. LDFLAGS+= -ltbb -lgomp endif ifeq ($(PARALLEL),release) # Enable OMP & parallel opt in `release` mode only # NOTE: Adds in non-opt flags *after* target-spec below. OPT_FLAGS+= $(__PARALLEL_OPT_FLAGS) endif COMMON_FLAGS+=$(addprefix -m,$(CPU_FLAGS)) CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors COMMON_STD=23 CSTD?=gnu$(COMMON_STD) CXXSTD?=gnu++$(COMMON_STD) # Build Options STRIP=strip # XXX: Is `-fno-plt` actually helpful here? RELEASE_COMMON_FLAGS+= -fno-plt -fno-bounds-check DEBUG_COMMON_FLAGS+= -ggdb -gz -ftrapv -fbounds-check ifneq ($(TARGET_SPEC_FLAGS),no) RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto DEBUG_CFLAGS?= -Og DEBUG_CXXFLAGS?= -Og DEBUG_LDFLAGS?= endif ifeq ($(PARALLEL),release) # Add openmp to C & CXX *only* in release mode. RELEASE_COMMON_FLAGS+= -fopenmp -DPARALLEL=1 RELEASE_LDFLAGS+= -ltbb -lgomp endif # Set exception modes (See def `EXCEPTIONS`.) ifneq ($(EXCEPTIONS),) ifeq ($(EXCEPTIONS),debug) # XXX (see above) Should we change to `DEBUG_COMMON_FLAGS` here to pass to CXX as well? DEBUG_CFLAGS+=-fexceptions endif ifeq ($(EXCEPTIONS),yes) CFLAGS+=-fexceptions endif ifeq ($(EXCEPTIONS),no) CXXFLAGS+=-fno-exceptions endif ifeq ($(EXCEPTIONS),force) COMMON_FLAGS+=-fexceptions endif ifeq ($(EXCEPTIONS),never) COMMON_FLAGS+=-fno-exceptions endif endif DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) -fasynchronous-unwind-tables RELEASE_CFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS) RELEASE_CXXFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS) # Objects OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o)) OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o)) OBJ = $(OBJ_C) $(OBJ_CXX) # Pre-compiled header objects PCH_HEADERS+=$(shell find -O2 $(INCLUDE) -type f -name \*_p.hh -or -name \*_p.h) PCH_OUTPUT_LOCATION= PCH_OUTPUT=$(addprefix $(PCH_OUTPUT_LOCATION),$(addsuffix .gch,$(PCH_HEADERS))) PCH_BUILD_COMMON_FLAGS+=-H #-D_PCH_BUILD=1 PCH_BUILD_CFLAGS+= -x c-header $(PCH_BUILD_COMMON_FLAGS) PCH_BUILD_CXXFLAGS+= -x c++-header $(PCH_BUILD_COMMON_FLAGS) PCH_CHEADERS=$(filter %.h,$(PCH_HEADERS)) PCH_CXXHEADERS=$(filter %.hh,$(PCH_HEADERS)) PCH_USE_COMMON_FLAGS+=-Winvalid-pch ifeq ($(INCLUDE_PCH_GLOBAL),1) # Globally included PCH_USE_CFLAGS+= $(addprefix -include ,$(realpath $(PCH_CHEADERS))) PCH_USE_CXXFLAGS+= $(addprefix -include ,$(realpath $(PCH_CXXHEADERS))) else # Seperately include them ifneq ($(PCH_OUTPUT_LOCATION),) override PCH_OUTPUT_LOCATION:=$(dir $(PCH_OUTPUT_LOCATION)) # XXX: We don't wan't to be using PCH_OUTPUT_LOCATION really... COMMON_FLAGS:= $(addprefix -I,$(dir $(shell find -O3 $(PCH_OUTPUT_LOCATION) -mindepth 1 -type d -printf %p/ ))) $(COMMON_FLAGS) endif endif COMMON_FLAGS+=$(addprefix -include ,$(INCLUDE_GLOBAL)) PCH_USE_CFLAGS+= $(PCH_USE_COMMON_FLAGS) PCH_USE_CXXFLAGS+= $(PCH_USE_COMMON_FLAGS) # Compiler Flags CFLAGS += $(COMMON_FLAGS) --std=$(CSTD) CXXFLAGS += $(COMMON_FLAGS) --std=$(CXXSTD) LDFLAGS += $(addsuffix .a,$(addprefix -l:lib,$(STATIC_LIBS))) $(addprefix -l,$(SHARED_LIBS)) # PGO (unused for lib targets) PROF_FLAGS= -D_PGO_GEN -fprofile-generate PGO_OBJ_C= $(addprefix prof/c/,$(SRC_C:.c=.o)) PGO_OBJ_CXX= $(addprefix prof/cxx/,$(SRC_CXX:.c=.o)) PGO_OBJ= $(PGO_OBJ_C) $(PGO_OBJ_CXX) PROF_ITERATIONS=10 PROF_LOCATION?=/tmp/$(PROJECT)-pgo PROF_LARGE_BOUND= 10240 PROF_SMALL_BOUND= 1024 # Phonies # XXX: This doesn't force them to run in series for some reason? .PHONY: release release: | dirs $(MAKE) lib$(PROJECT).a @$(MAKE) clean-rebuild >> /dev/null @$(MAKE) dirs >> /dev/null $(MAKE) lib$(PROJECT).so .PHONY: 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: test test: $(PROJECT)-test -strace ./$< -valgrind ./$< # Targets .PHONY: pch pch: | dirs $(PROJECT)-pch @echo 'WARNING: When building target $(PROJECT)-pch, the PCH file(s) will not be built with the default auto-opts enabled by targets of kind `debug` or `release`. This should only be used when full manually overriding all compile and linker flags' >&2 $(PROJECT)-pch: $(PCH_OUTPUT) @echo "" @echo 'PCH: $(PCH_OUTPUT)' '<-' '$(PCH_HEADERS)' @echo " with: $(PCH_INCLUDES)" @echo 'C: $(PCH_CHEADERS)' @echo 'C++: $(PCH_CXXHEADERS)' # Invoking target `pch` itself is not necissary, they are build automatically where they are used. dirs: @mkdir -p $(addprefix {obj$(__COMMA)prof}/c{$(__COMMA)xx}/,$(shell find $(SRC)/ -type d)) $(PCH_OUTPUT_LOCATION)%.hh.gch: CXXFLAGS+=$(PCH_BUILD_CXXFLAGS) $(PCH_OUTPUT_LOCATION)%.hh.gch: %.hh $(PCH_INCLUDES) @mkdir -p $(dir $@) $(CXX) $(CXXFLAGS) -o $@ -c $< $(PCH_OUTPUT_LOCATION)%.h.gch: CFLAGS+=$(PCH_BUILD_CFLAGS) $(PCH_OUTPUT_LOCATION)%.h.gch: %.h $(PCH_INCLUDES) @mkdir -p $(dir $@) $(CC) $(CFLAGS) -o $@ -c $< obj/c/%.o: CFLAGS+= $(PCH_USE_CFLAGS) obj/c/%.o: %.c $(PCH_OUTPUT) $(CC) -c $< $(CFLAGS) -o $@ obj/cxx/%.o: CXXFLAGS+= $(PCH_USE_CXXFLAGS) obj/cxx/%.o: %.cpp $(PCH_OUTPUT) $(CXX) -c $< $(CXXFLAGS) -o $@ prof/c/%.o: CFLAGS+= $(PCH_USE_CFLAGS) prof/c/%.o: %.c $(PCH_OUTPUT) $(CC) -c $< $(CFLAGS) -o $@ $(PROF_FLAGS) #$(LDFLAGS) prof/cxx/%.o: CXXFLAGS+= $(PCH_USE_CXXFLAGS) prof/cxx/%.o: %.cpp $(PCH_OUTPUT) $(CXX) -c $< $(CXXFLAGS) -o $@ $(PROF_FLAGS) #$(LDFLAGS) lib$(PROJECT)-release.a: CFLAGS+= $(RELEASE_CFLAGS) $(STATIC_FLAGS) $(STATIC_RELEASE_FLAGS) lib$(PROJECT)-release.a: CXXFLAGS += $(RELEASE_CXXFLAGS) $(STATIC_FLAGS) $(STATIC_RELEASE_FLAGS) lib$(PROJECT)-release.a: LDFLAGS += $(RELEASE_LDFLAGS) lib$(PROJECT)-release.a: $(OBJ) $(AR) rcs $@ $^ $(RANLIB) $@ lib$(PROJECT)-debug.a: CFLAGS+= $(DEBUG_CFLAGS) $(STATIC_FLAGS) $(STATIC_DEBUG_FLAGS) lib$(PROJECT)-debug.a: CXXFLAGS += $(DEBUG_CXXFLAGS) $(STATIC_FLAGS) $(STATIC_DEBUG_FLAGS) lib$(PROJECT)-debug.a: LDFLAGS += $(DEBUG_LDFLAGS) lib$(PROJECT)-debug.a: $(OBJ) $(AR) rcs $@ $^ $(RANLIB) $@ lib$(PROJECT)-release.so: CFLAGS+= $(RELEASE_CFLAGS) $(SHARED_FLAGS) $(SHARED_RELEASE_FLAGS) lib$(PROJECT)-release.so: CXXFLAGS += $(RELEASE_CXXFLAGS) $(SHARED_FLAGS) $(SHARED_RELEASE_FLAGS) lib$(PROJECT)-release.so: LDFLAGS += $(RELEASE_LDFLAGS) lib$(PROJECT)-release.so: BINFLAGS += $(RELEASE_BINFLAGS) lib$(PROJECT)-release.so: $(OBJ) $(CXX) -shared $^ $(BINFLAGS) $(CXXFLAGS) -o $@ $(LDFLAGS) $(STRIP) $@ lib$(PROJECT)-debug.so: CFLAGS+= $(DEBUG_CFLAGS) $(SHARED_FLAGS) $(SHARED_DEBUG_FLAGS) lib$(PROJECT)-debug.so: CXXFLAGS += $(DEBUG_CXXFLAGS) $(SHARED_FLAGS) $(SHARED_DEBUG_FLAGS) lib$(PROJECT)-debug.so: LDFLAGS += $(DEBUG_LDFLAGS) lib$(PROJECT)-debug.so: BINFLAGS += $(DEBUG_BINFLAGS) lib$(PROJECT)-debug.so: $(OBJ) $(CXX) -shared $^ $(BINFLAGS) $(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-source: find -O2 {obj,prof}/ -type f -exec rm {} + clean-rebuild: clean-source find $(INCLUDE) $(PCH_OUTPUT_LOCATION) -type f -name \*.gch -exec rm {} + clean: clean-rebuild rm -f lib$(PROJECT){,-{release,debug,pgo}}.{a,so{,.*}} rm -f $(PROJECT)-test clean-full: clean rm -rf {obj,prof} 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/$(INCLUDE_PREFIX) install -m 644 $(wildcard $(INCLUDE)/*.*) $(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX) uninstall: -rm $(DESTDIR)$(PREFIX)/lib/lib$(PROJECT).{a,so{,.*}} cd $(INCLUDE) && find . -type f | xargs -I {} rm "$(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX){}" [[ -d "$(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX)" ]] && \ rmdir $(DESTDIR)$(PREFIX)/include/$(INCLUDE_PREFIX) || : $(PROJECT)-test: lib$(PROJECT)-$(TARGET) $(CXX) $(CXXFLAGS) $(TEST_CFLAGS) $(TEST_CXXFLAGS) src/test/*.cpp -o $@ -l:$< $(LDFLAGS) $(TEST_LDFLAGS)