github.com/moontrade/mdbx-go@v0.4.0/GNUmakefile (about) 1 # This makefile is for GNU Make 3.80 or above, and nowadays provided 2 # just for compatibility and preservation of traditions. 3 # 4 # Please use CMake in case of any difficulties or 5 # problems with this old-school's magic. 6 # 7 ################################################################################ 8 # 9 # Basic internal definitios. For a customizable variables and options see below. 10 # 11 $(info // The GNU Make $(MAKE_VERSION)) 12 SHELL := $(shell env bash -c 'echo $$BASH') 13 MAKE_VERx3 := $(shell printf "%3s%3s%3s" $(subst ., ,$(MAKE_VERSION))) 14 make_lt_3_81 := $(shell expr "$(MAKE_VERx3)" "<" " 3 81") 15 ifneq ($(make_lt_3_81),0) 16 $(error Please use GNU Make 3.81 or above) 17 endif 18 make_ge_4_1 := $(shell expr "$(MAKE_VERx3)" ">=" " 4 1") 19 SRC_PROBE_C := $(shell [ -f mdbx.c ] && echo mdbx.c || echo src/osal.c) 20 SRC_PROBE_CXX := $(shell [ -f mdbx.c++ ] && echo mdbx.c++ || echo src/mdbx.c++) 21 UNAME := $(shell uname -s 2>/dev/null || echo Unknown) 22 23 define cxx_filesystem_probe 24 int main(int argc, const char*argv[]) { 25 mdbx::filesystem::path probe(argv[0]); 26 if (argc != 1) throw mdbx::filesystem::filesystem_error(std::string("fake"), std::error_code()); 27 return mdbx::filesystem::is_directory(probe.relative_path()); 28 } 29 endef 30 # 31 ################################################################################ 32 # 33 # Use `make options` to list the available libmdbx build options. 34 # 35 # Note that the defaults should already be correct for most platforms; 36 # you should not need to change any of these. Read their descriptions 37 # in README and source code (see src/options.h) if you do. 38 # 39 40 # install sandbox 41 DESTDIR ?= 42 INSTALL ?= install 43 # install prefixes (inside sandbox) 44 prefix ?= /usr/local 45 mandir ?= $(prefix)/man 46 # lib/bin suffix for multiarch/biarch, e.g. '.x86_64' 47 suffix ?= 48 49 # toolchain 50 CC ?= gcc 51 CXX ?= g++ 52 CFLAGS_EXTRA ?= 53 LD ?= ld 54 55 # build options 56 MDBX_BUILD_OPTIONS ?=-DNDEBUG=1 57 MDBX_BUILD_TIMESTAMP ?=$(shell date +%Y-%m-%dT%H:%M:%S%z) 58 59 # probe and compose common compiler flags with variable expansion trick (seems this work two times per session for GNU Make 3.81) 60 CFLAGS ?= $(strip $(eval CFLAGS := -std=gnu11 -O2 -g -Wall -Werror -Wextra -Wpedantic -ffunction-sections -fPIC -fvisibility=hidden -pthread -Wno-error=attributes $$(shell for opt in -fno-semantic-interposition -Wno-unused-command-line-argument -Wno-tautological-compare; do [ -z "$$$$($(CC) '-DMDBX_BUILD_FLAGS="probe"' $$$${opt} -c $(SRC_PROBE_C) -o /dev/null >/dev/null 2>&1 || echo failed)" ] && echo "$$$${opt} "; done)$(CFLAGS_EXTRA))$(CFLAGS)) 61 62 # choosing C++ standard with variable expansion trick (seems this work two times per session for GNU Make 3.81) 63 CXXSTD ?= $(eval CXXSTD := $$(shell for std in gnu++23 c++23 gnu++2b c++2b gnu++20 c++20 gnu++2a c++2a gnu++17 c++17 gnu++1z c++1z gnu++14 c++14 gnu++1y c++1y gnu+11 c++11 gnu++0x c++0x; do $(CXX) -std=$$$${std} -c $(SRC_PROBE_CXX) -o /dev/null 2>probe4std-$$$${std}.err >/dev/null && echo "-std=$$$${std}" && exit; done))$(CXXSTD) 64 CXXFLAGS ?= $(strip $(CXXSTD) $(filter-out -std=gnu11,$(CFLAGS))) 65 66 # libraries and options for linking 67 EXE_LDFLAGS ?= -pthread 68 ifneq ($(make_ge_4_1),1) 69 # don't use variable expansion trick as workaround for bugs of GNU Make before 4.1 70 LIBS ?= $(shell $(uname2libs)) 71 LDFLAGS ?= $(shell $(uname2ldflags)) 72 LIB_STDCXXFS ?= $(shell echo '$(cxx_filesystem_probe)' | cat mdbx.h++ - | sed $$'1s/\xef\xbb\xbf//' | $(CXX) -x c++ $(CXXFLAGS) -Wno-error - -Wl,--allow-multiple-definition -lstdc++fs $(LIBS) $(LDFLAGS) $(EXE_LDFLAGS) -o /dev/null 2>probe4lstdfs.err >/dev/null && echo '-Wl,--allow-multiple-definition -lstdc++fs') 73 else 74 # using variable expansion trick to avoid repeaded probes 75 LIBS ?= $(eval LIBS := $$(shell $$(uname2libs)))$(LIBS) 76 LDFLAGS ?= $(eval LDFLAGS := $$(shell $$(uname2ldflags)))$(LDFLAGS) 77 LIB_STDCXXFS ?= $(eval LIB_STDCXXFS := $$(shell echo '$$(cxx_filesystem_probe)' | cat mdbx.h++ - | sed $$$$'1s/\xef\xbb\xbf//' | $(CXX) -x c++ $(CXXFLAGS) -Wno-error - -Wl,--allow-multiple-definition -lstdc++fs $(LIBS) $(LDFLAGS) $(EXE_LDFLAGS) -o /dev/null 2>probe4lstdfs.err >/dev/null && echo '-Wl,--allow-multiple-definition -lstdc++fs'))$(LIB_STDCXXFS) 78 endif 79 80 ################################################################################ 81 82 define uname2sosuffix 83 case "$(UNAME)" in 84 Darwin*|Mach*) echo dylib;; 85 CYGWIN*|MINGW*|MSYS*|Windows*) echo dll;; 86 *) echo so;; 87 esac 88 endef 89 90 define uname2ldflags 91 case "$(UNAME)" in 92 CYGWIN*|MINGW*|MSYS*|Windows*) 93 echo '-Wl,--gc-sections,-O1'; 94 ;; 95 *) 96 $(LD) --help 2>/dev/null | grep -q -- --gc-sections && echo '-Wl,--gc-sections,-z,relro,-O1'; 97 $(LD) --help 2>/dev/null | grep -q -- -dead_strip && echo '-Wl,-dead_strip'; 98 ;; 99 esac 100 endef 101 102 # TIP: try add the'-Wl, --no-as-needed,-lrt' for ability to built with modern glibc, but then use with the old. 103 define uname2libs 104 case "$(UNAME)" in 105 CYGWIN*|MINGW*|MSYS*|Windows*) 106 echo '-lm -lntdll -lwinmm'; 107 ;; 108 *SunOS*|*Solaris*) 109 echo '-lm -lkstat -lrt'; 110 ;; 111 *Darwin*|OpenBSD*) 112 echo '-lm'; 113 ;; 114 *) 115 echo '-lm -lrt'; 116 ;; 117 esac 118 endef 119 120 SO_SUFFIX := $(shell $(uname2sosuffix)) 121 HEADERS := mdbx.h mdbx.h++ 122 LIBRARIES := libmdbx.a libmdbx.$(SO_SUFFIX) 123 TOOLS := mdbx_stat mdbx_copy mdbx_dump mdbx_load mdbx_chk mdbx_drop 124 MANPAGES := mdbx_stat.1 mdbx_copy.1 mdbx_dump.1 mdbx_load.1 mdbx_chk.1 mdbx_drop.1 125 TIP := // TIP: 126 127 .PHONY: all help options lib libs tools clean install uninstall check_buildflags_tag tools-static 128 .PHONY: install-strip install-no-strip strip libmdbx mdbx show-options lib-static lib-shared 129 130 ifeq ("$(origin V)", "command line") 131 MDBX_BUILD_VERBOSE := $(V) 132 endif 133 ifndef MDBX_BUILD_VERBOSE 134 MDBX_BUILD_VERBOSE := 0 135 endif 136 137 ifeq ($(MDBX_BUILD_VERBOSE),1) 138 QUIET := 139 HUSH := 140 $(info $(TIP) Use `make V=0` for quiet.) 141 else 142 QUIET := @ 143 HUSH := >/dev/null 144 $(info $(TIP) Use `make V=1` for verbose.) 145 endif 146 147 all: show-options $(LIBRARIES) $(TOOLS) 148 149 help: 150 @echo " make all - build libraries and tools" 151 @echo " make help - print this help" 152 @echo " make options - list build options" 153 @echo " make lib - build libraries, also lib-static and lib-shared" 154 @echo " make tools - build the tools" 155 @echo " make tools-static - build the tools with statically linking with system libraries and compiler runtime" 156 @echo " make clean " 157 @echo " make install " 158 @echo " make uninstall " 159 @echo "" 160 @echo " make strip - strip debug symbols from binaries" 161 @echo " make install-no-strip - install explicitly without strip" 162 @echo " make install-strip - install explicitly with strip" 163 @echo "" 164 @echo " make bench - run ioarena-benchmark" 165 @echo " make bench-couple - run ioarena-benchmark for mdbx and lmdb" 166 @echo " make bench-triplet - run ioarena-benchmark for mdbx, lmdb, sqlite3" 167 @echo " make bench-quartet - run ioarena-benchmark for mdbx, lmdb, rocksdb, wiredtiger" 168 @echo " make bench-clean - remove temp database(s) after benchmark" 169 170 show-options: 171 @echo " MDBX_BUILD_OPTIONS = $(MDBX_BUILD_OPTIONS)" 172 @echo " MDBX_BUILD_TIMESTAMP = $(MDBX_BUILD_TIMESTAMP)" 173 @echo '$(TIP) Use `make options` to listing available build options.' 174 @echo " CC =`which $(CC)` | `$(CC) --version | head -1`" 175 @echo " CFLAGS =$(CFLAGS)" 176 @echo " CXXFLAGS =$(CXXFLAGS)" 177 @echo " LDFLAGS =$(LDFLAGS) $(LIB_STDCXXFS) $(LIBS) $(EXE_LDFLAGS)" 178 @echo '$(TIP) Use `make help` to listing available targets.' 179 180 options: 181 @echo " INSTALL =$(INSTALL)" 182 @echo " DESTDIR =$(DESTDIR)" 183 @echo " prefix =$(prefix)" 184 @echo " mandir =$(mandir)" 185 @echo " suffix =$(suffix)" 186 @echo "" 187 @echo " CC =$(CC)" 188 @echo " CFLAGS_EXTRA =$(CFLAGS_EXTRA)" 189 @echo " CFLAGS =$(CFLAGS)" 190 @echo " CXX =$(CXX)" 191 @echo " CXXSTD =$(CXXSTD)" 192 @echo " CXXFLAGS =$(CXXFLAGS)" 193 @echo "" 194 @echo " LD =$(LD)" 195 @echo " LDFLAGS =$(LDFLAGS)" 196 @echo " EXE_LDFLAGS =$(EXE_LDFLAGS)" 197 @echo " LIBS =$(LIBS)" 198 @echo "" 199 @echo " MDBX_BUILD_OPTIONS = $(MDBX_BUILD_OPTIONS)" 200 @echo " MDBX_BUILD_TIMESTAMP = $(MDBX_BUILD_TIMESTAMP)" 201 @echo "" 202 @echo "## Assortment items for MDBX_BUILD_OPTIONS:" 203 @echo "## Note that the defaults should already be correct for most platforms;" 204 @echo "## you should not need to change any of these. Read their descriptions" 205 @echo "## in README and source code (see mdbx.c) if you do." 206 @grep -h '#ifndef MDBX_' mdbx.c | grep -v BUILD | uniq | sed 's/#ifndef / /' 207 208 lib libs libmdbx mdbx: libmdbx.a libmdbx.$(SO_SUFFIX) 209 210 tools: $(TOOLS) 211 tools-static: $(addsuffix .static,$(TOOLS)) $(addsuffix .static-lto,$(TOOLS)) 212 213 strip: all 214 @echo ' STRIP libmdbx.$(SO_SUFFIX) $(TOOLS)' 215 $(TRACE )strip libmdbx.$(SO_SUFFIX) $(TOOLS) 216 217 clean: 218 @echo ' REMOVE ...' 219 $(QUIET)rm -rf $(TOOLS) mdbx_test @* *.[ao] *.[ls]o *.$(SO_SUFFIX) *.dSYM *~ tmp.db/* \ 220 *.gcov *.log *.err src/*.o test/*.o mdbx_example dist \ 221 config.h src/config.h src/version.c *.tar* buildflags.tag \ 222 mdbx_*.static mdbx_*.static-lto 223 224 MDBX_BUILD_FLAGS =$(strip $(MDBX_BUILD_OPTIONS) $(CXXSTD) $(CFLAGS) $(LDFLAGS) $(LIBS)) 225 check_buildflags_tag: 226 $(QUIET)if [ "$(MDBX_BUILD_FLAGS)" != "$$(cat buildflags.tag 2>&1)" ]; then \ 227 echo -n " CLEAN for build with specified flags..." && \ 228 $(MAKE) IOARENA=false CXXSTD= -s clean >/dev/null && echo " Ok" && \ 229 echo '$(MDBX_BUILD_FLAGS)' > buildflags.tag; \ 230 fi 231 232 buildflags.tag: check_buildflags_tag 233 234 lib-static libmdbx.a: mdbx-static.o mdbx++-static.o 235 @echo ' AR $@' 236 $(QUIET)$(AR) rcs $@ $? $(HUSH) 237 238 lib-shared libmdbx.$(SO_SUFFIX): mdbx-dylib.o mdbx++-dylib.o 239 @echo ' LD $@' 240 $(QUIET)$(CXX) $(CXXFLAGS) $^ -pthread -shared $(LDFLAGS) $(LIB_STDCXXFS) $(LIBS) -o $@ 241 242 243 ################################################################################ 244 # Amalgamated source code, i.e. distributed after `make dist` 245 MAN_SRCDIR := man1/ 246 247 config.h: buildflags.tag mdbx.c $(lastword $(MAKEFILE_LIST)) 248 @echo ' MAKE $@' 249 $(QUIET)(echo '#define MDBX_BUILD_TIMESTAMP "$(MDBX_BUILD_TIMESTAMP)"' \ 250 && echo "#define MDBX_BUILD_FLAGS \"$$(cat buildflags.tag)\"" \ 251 && echo '#define MDBX_BUILD_COMPILER "$(shell (LC_ALL=C $(CC) --version || echo 'Please use GCC or CLANG compatible compiler') | head -1)"' \ 252 && echo '#define MDBX_BUILD_TARGET "$(shell set -o pipefail; (LC_ALL=C $(CC) -v 2>&1 | grep -i '^Target:' | cut -d ' ' -f 2- || (LC_ALL=C $(CC) --version | grep -qi e2k && echo E2K) || echo 'Please use GCC or CLANG compatible compiler') | head -1)"' \ 253 ) >$@ 254 255 mdbx-dylib.o: config.h mdbx.c mdbx.h $(lastword $(MAKEFILE_LIST)) 256 @echo ' CC $@' 257 $(QUIET)$(CC) $(CFLAGS) $(MDBX_BUILD_OPTIONS) '-DMDBX_CONFIG_H="config.h"' -DLIBMDBX_EXPORTS=1 -c mdbx.c -o $@ 258 259 mdbx-static.o: config.h mdbx.c mdbx.h $(lastword $(MAKEFILE_LIST)) 260 @echo ' CC $@' 261 $(QUIET)$(CC) $(CFLAGS) $(MDBX_BUILD_OPTIONS) '-DMDBX_CONFIG_H="config.h"' -ULIBMDBX_EXPORTS -c mdbx.c -o $@ 262 263 mdbx++-dylib.o: config.h mdbx.c++ mdbx.h mdbx.h++ $(lastword $(MAKEFILE_LIST)) 264 @echo ' CC $@' 265 $(QUIET)$(CXX) $(CXXFLAGS) $(MDBX_BUILD_OPTIONS) '-DMDBX_CONFIG_H="config.h"' -DLIBMDBX_EXPORTS=1 -c mdbx.c++ -o $@ 266 267 mdbx++-static.o: config.h mdbx.c++ mdbx.h mdbx.h++ $(lastword $(MAKEFILE_LIST)) 268 @echo ' CC $@' 269 $(QUIET)$(CXX) $(CXXFLAGS) $(MDBX_BUILD_OPTIONS) '-DMDBX_CONFIG_H="config.h"' -ULIBMDBX_EXPORTS -c mdbx.c++ -o $@ 270 271 mdbx_%: mdbx_%.c mdbx-static.o 272 @echo ' CC+LD $@' 273 $(QUIET)$(CC) $(CFLAGS) $(MDBX_BUILD_OPTIONS) '-DMDBX_CONFIG_H="config.h"' $^ $(EXE_LDFLAGS) $(LIBS) -o $@ 274 275 mdbx_%.static: mdbx_%.c mdbx-static.o 276 @echo ' CC+LD $@' 277 $(QUIET)$(CC) $(CFLAGS) $(MDBX_BUILD_OPTIONS) '-DMDBX_CONFIG_H="config.h"' $^ $(EXE_LDFLAGS) -static -Wl,--strip-all -o $@ 278 279 mdbx_%.static-lto: mdbx_%.c config.h mdbx.c mdbx.h 280 @echo ' CC+LD $@' 281 $(QUIET)$(CC) $(CFLAGS) -Os -flto $(MDBX_BUILD_OPTIONS) '-DLIBMDBX_API=' '-DMDBX_CONFIG_H="config.h"' \ 282 $< mdbx.c $(EXE_LDFLAGS) $(LIBS) -static -Wl,--strip-all -o $@ 283 284 285 install: $(LIBRARIES) $(TOOLS) $(HEADERS) 286 @echo ' INSTALLING...' 287 $(QUIET)mkdir -p $(DESTDIR)$(prefix)/bin$(suffix) && \ 288 $(INSTALL) -p $(EXE_INSTALL_FLAGS) $(TOOLS) $(DESTDIR)$(prefix)/bin$(suffix)/ && \ 289 mkdir -p $(DESTDIR)$(prefix)/lib$(suffix)/ && \ 290 $(INSTALL) -p $(EXE_INSTALL_FLAGS) $(filter-out libmdbx.a,$(LIBRARIES)) $(DESTDIR)$(prefix)/lib$(suffix)/ && \ 291 mkdir -p $(DESTDIR)$(prefix)/lib$(suffix)/ && \ 292 $(INSTALL) -p libmdbx.a $(DESTDIR)$(prefix)/lib$(suffix)/ && \ 293 mkdir -p $(DESTDIR)$(prefix)/include/ && \ 294 $(INSTALL) -p -m 444 $(HEADERS) $(DESTDIR)$(prefix)/include/ && \ 295 mkdir -p $(DESTDIR)$(mandir)/man1/ && \ 296 $(INSTALL) -p -m 444 $(addprefix $(MAN_SRCDIR), $(MANPAGES)) $(DESTDIR)$(mandir)/man1/ 297 298 install-strip: EXE_INSTALL_FLAGS = -s 299 install-strip: install 300 301 install-no-strip: EXE_INSTALL_FLAGS = 302 install-no-strip: install 303 304 uninstall: 305 @echo ' UNINSTALLING/REMOVE...' 306 $(QUIET)rm -f $(addprefix $(DESTDIR)$(prefix)/bin$(suffix)/,$(TOOLS)) \ 307 $(addprefix $(DESTDIR)$(prefix)/lib$(suffix)/,$(LIBRARIES)) \ 308 $(addprefix $(DESTDIR)$(prefix)/include/,$(HEADERS)) \ 309 $(addprefix $(DESTDIR)$(mandir)/man1/,$(MANPAGES)) 310 311 ################################################################################ 312 # Benchmarking by ioarena 313 314 ifeq ($(origin IOARENA),undefined) 315 IOARENA := $(shell \ 316 (test -x ../ioarena/@BUILD/src/ioarena && echo ../ioarena/@BUILD/src/ioarena) || \ 317 (test -x ../../@BUILD/src/ioarena && echo ../../@BUILD/src/ioarena) || \ 318 (test -x ../../src/ioarena && echo ../../src/ioarena) || which ioarena 2>&- || \ 319 (echo false && echo '$(TIP) Clone and build the https://github.com/pmwkaa/ioarena.git within a neighbouring directory for availability of benchmarking.' >&2)) 320 endif 321 NN ?= 25000000 322 BENCH_CRUD_MODE ?= nosync 323 324 bench-clean: 325 @echo ' REMOVE bench-*.txt _ioarena/*' 326 $(QUIET)rm -rf bench-*.txt _ioarena/* 327 328 re-bench: bench-clean bench 329 330 ifeq ($(or $(IOARENA),false),false) 331 bench bench-quartet bench-triplet bench-couple: 332 $(QUIET)echo 'The `ioarena` benchmark is required.' >&2 && \ 333 echo 'Please clone and build the https://github.com/pmwkaa/ioarena.git within a neighbouring `ioarena` directory.' >&2 && \ 334 false 335 336 else 337 338 .PHONY: bench bench-clean bench-couple re-bench bench-quartet bench-triplet 339 340 define bench-rule 341 bench-$(1)_$(2).txt: $(3) $(IOARENA) $(lastword $(MAKEFILE_LIST)) 342 @echo ' RUNNING ioarena for $1/$2...' 343 $(QUIET)LD_LIBRARY_PATH="./:$$$${LD_LIBRARY_PATH}" \ 344 $(IOARENA) -D $(1) -B crud -m $(BENCH_CRUD_MODE) -n $(2) \ 345 | tee $$@ | grep throughput && \ 346 LD_LIBRARY_PATH="./:$$$${LD_LIBRARY_PATH}" \ 347 $(IOARENA) -D $(1) -B get,iterate -m $(BENCH_CRUD_MODE) -r 4 -n $(2) \ 348 | tee -a $$@ | grep throughput \ 349 || mv -f $$@ $$@.error 350 351 endef 352 353 $(eval $(call bench-rule,mdbx,$(NN),libmdbx.$(SO_SUFFIX))) 354 355 $(eval $(call bench-rule,sophia,$(NN))) 356 $(eval $(call bench-rule,leveldb,$(NN))) 357 $(eval $(call bench-rule,rocksdb,$(NN))) 358 $(eval $(call bench-rule,wiredtiger,$(NN))) 359 $(eval $(call bench-rule,forestdb,$(NN))) 360 $(eval $(call bench-rule,lmdb,$(NN))) 361 $(eval $(call bench-rule,nessdb,$(NN))) 362 $(eval $(call bench-rule,sqlite3,$(NN))) 363 $(eval $(call bench-rule,ejdb,$(NN))) 364 $(eval $(call bench-rule,vedisdb,$(NN))) 365 $(eval $(call bench-rule,dummy,$(NN))) 366 bench: bench-mdbx_$(NN).txt 367 bench-quartet: bench-mdbx_$(NN).txt bench-lmdb_$(NN).txt bench-rocksdb_$(NN).txt bench-wiredtiger_$(NN).txt 368 bench-triplet: bench-mdbx_$(NN).txt bench-lmdb_$(NN).txt bench-sqlite3_$(NN).txt 369 bench-couple: bench-mdbx_$(NN).txt bench-lmdb_$(NN).txt 370 371 # $(eval $(call bench-rule,debug,10)) 372 # .PHONY: bench-debug 373 # bench-debug: bench-debug_10.txt 374 375 endif