Makefile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # NOTE: only tested in GNU/Linux.
  2. SHELL := /bin/bash
  3. ## Binary dependencies.
  4. CC = gcc
  5. AR = ar
  6. # Global switches.
  7. # Compile with debug symbols. Libraries are suffixed with _dbg and can coexist
  8. # with non-debug libraries.
  9. DEBUG ?= 0
  10. # Install in a user-owned directory provided as LOCAL_PREFIX or a default
  11. # (~/.local), instead of a system directory.
  12. LOCAL ?= 0
  13. ## Paths.
  14. LOCAL_PREFIX ?= $(HOME)/.local
  15. ifneq ($(LOCAL), 0)
  16. PREFIX ?= $(LOCAL_PREFIX)
  17. else
  18. PREFIX ?= /usr/local
  19. endif
  20. bindir := $(PREFIX)/bin
  21. libdir := $(PREFIX)/lib
  22. INCLUDEDIR := $(PREFIX)/include
  23. BUILDDIR := ./build
  24. OUTDIR := ./bin
  25. TMPDIR ?= /tmp
  26. VALGRIND_DUMP := $(TMPDIR)/volksdata_valgrind.log
  27. CALLGRIND_DUMP := $(TMPDIR)/volksdata_callgrind.out
  28. MASSIF_DUMP := $(TMPDIR)/volksdata_massif.out
  29. INCLUDE_BASE := . -Iinclude -Iext/hashmap -Iext/log/src
  30. INCLUDE := -I$(INCLUDE_BASE)
  31. _CFLAGS = -std=gnu11 -Wall -fPIC $(INCLUDE)
  32. ifneq ($(DEBUG), 0)
  33. CFLAGS = $(_CFLAGS) -Itest -O0 -ggdb -DDEBUG
  34. else
  35. CFLAGS = $(_CFLAGS) -O3
  36. endif
  37. $(info CFLAGS: $(CFLAGS))
  38. # NOTE: -luuid is a Linux system library. Other OS's might need a different
  39. # link or a non-system library built.
  40. LDFLAGS := -L$(OUTDIR) -L$(libdir) -L. -llmdb -lxxhash -luuid
  41. PARSER = bin/lemon
  42. LEMON_SRC = ext/sqlite/tool/lemon.c
  43. CODEC_DIR = src/codec
  44. # External sources compiled in core object.
  45. EXT_SRC := $(wildcard ext/log/src/*.c) \
  46. $(wildcard ext/hashmap/*.c)
  47. # External headers of libraries compiled in core.
  48. EXT_H := $(wildcard ext/log/src/*.h) \
  49. $(wildcard ext/hashmap/*.h)
  50. VOLK_SRC = $(wildcard src/*.c)
  51. SRC = $(EXT_SRC) $(VOLK_SRC)
  52. TEST_SRC = $(wildcard test/*.c) test.c
  53. EXT_OBJ := $(EXT_SRC:%.c=$(BUILDDIR)/%.o)
  54. # TODO This is extremely convoluted, simplify if possible.
  55. CODEC_SRC := $(wildcard $(CODEC_DIR)/codec_*.c)
  56. CODEC_REL_SRC := $(CODEC_SRC:$(CODEC_DIR)/%=%)
  57. ALL_CODEC_REL_SRC := $(CODEC_REL_SRC) $(CODEC_REL_SRC:codec_%=parser_%) \
  58. $(CODEC_REL_SRC:codec_%=grammar_%)
  59. CODEC_SRC = $(ALL_CODEC_REL_SRC:%=$(CODEC_DIR)/%)
  60. ifneq ($(DEBUG), 0)
  61. CODEC_OBJ = $(patsubst $(CODEC_DIR)/%.c, $(BUILDDIR)/%_dbg.o, $(CODEC_SRC))
  62. else
  63. CODEC_OBJ = $(patsubst $(CODEC_DIR)/%.c, $(BUILDDIR)/%.o, $(CODEC_SRC))
  64. endif
  65. ifneq ($(DEBUG), 0)
  66. STATIC_LIB = $(OUTDIR)/libvolksdata_dbg.a
  67. LOCAL_OBJ = $(VOLK_SRC:src/%.c=$(BUILDDIR)/%_dbg.o)
  68. else
  69. STATIC_LIB = $(OUTDIR)/libvolksdata.a
  70. LOCAL_OBJ = $(VOLK_SRC:src/%.c=$(BUILDDIR)/%.o)
  71. endif
  72. OBJ = $(EXT_OBJ) $(LOCAL_OBJ)
  73. DYN_LIB = $(STATIC_LIB:.a=.so)
  74. LIBS = $(STATIC_LIB) $(DYN_LIB)
  75. $(info EXT_SRC: $(EXT_SRC))
  76. $(info EXT_OBJ: $(EXT_OBJ))
  77. $(info OBJ: $(OBJ))
  78. $(info LIBS: $(LIBS))
  79. # LDD for Linux, otool -L for OSX.
  80. ifeq (, $(shell which ldd))
  81. LDD := otool -L
  82. else
  83. LDD := ldd
  84. endif
  85. # For visual dep graph.
  86. DEPS := $(shell echo "${INCLUDE_BASE}" | sed -e 's/ -I/,/g'),include/codec
  87. DOCS = docs
  88. ## Environment.
  89. # Tests need the freshly compiled libs.
  90. export LD_LIBRARY_PATH = $(OUTDIR):$(libdir)
  91. ## Rules.
  92. .DEFAULT_GOAL := lib
  93. # Extract all rule comments into a help message.
  94. .PHONY: help
  95. help:
  96. @echo "Command overview:"; echo; \
  97. grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
  98. | SED -N 'S/^\(.*\): \(.*\)##\(.*\)/\1|\3/P' \
  99. | column -t -s '|'
  100. .PHONY: lib
  101. lib: codec $(LIBS) ## Compile main library (static and dynamic linking).
  102. # Static library.
  103. $(STATIC_LIB): $(OBJ)
  104. $(AR) rs $@ $^ $(CODEC_OBJ)
  105. # Dynamic library.
  106. $(DYN_LIB): $(OBJ)
  107. $(CC) -shared $(LDFLAGS) -o $@ $^ $(CODEC_OBJ)
  108. # External libraries.
  109. $(BUILDDIR)/ext/%.o: ext/%.c
  110. mkdir -p $(dir $@)
  111. $(CC) $(CFLAGS) -c $^ -o $@
  112. # Standard objects.
  113. $(BUILDDIR)/%.o: src/%.c
  114. $(CC) $(CFLAGS) -c $^ -o $@
  115. # Debug objects.
  116. $(BUILDDIR)/%_dbg.o: src/%.c
  117. $(CC) $(CFLAGS) -c $^ -o $@
  118. # Codecs in a subfolder.
  119. .PHONY: codec
  120. codec: $(PARSER)
  121. mkdir -p $(BUILDDIR) && \
  122. $(MAKE) -C $(CODEC_DIR) codec DEBUG=$(DEBUG)
  123. # Build the parser executable.
  124. $(PARSER): $(LEMON_SRC)
  125. $(CC) $^ -o $@
  126. install: lib ## Install library and dependencies to $PREFIX. May require sudo.
  127. @echo "Installing library files in $(PREFIX)."
  128. mkdir -p $(libdir)
  129. mkdir -p $(INCLUDEDIR)/volksdata
  130. cp $(LIBS) $(libdir) && \
  131. cp -r include/volksdata/* $(EXT_H) $(INCLUDEDIR)/volksdata && \
  132. cp include/*.h $(INCLUDEDIR)
  133. .PHONY: clean
  134. clean: DEBUG = 0
  135. clean: ## Clean up artifacts, including language parsers.
  136. rm -rf $(BUILDDIR)
  137. rm -f bin/*
  138. rm -f include/codec/grammar_*.h
  139. rm -f src/codec/grammar_*.c src/codec/parser_*.c
  140. .PHONY: uninstall ## Uninstall library (not the dependencies).
  141. uninstall:
  142. rm -f $(libdir)/libvolksdata*
  143. rm -rf $(INCLUDEDIR)/volksdata*
  144. rm -f bin/test*
  145. # For testing, use debug symbols.
  146. bin/test: override DEBUG = 1
  147. bin/test: lib $(TEST_SRC)
  148. $(CC) $(CFLAGS) $(LDFLAGS) -lvolksdata_dbg \
  149. test.c -o bin/test
  150. .PHONY: test
  151. test: bin/test ## Run a test suite.
  152. @echo "Using libraries: "; $(LDD) bin/test
  153. exec bin/test
  154. .PHONY: gdb_test
  155. gdb_test: bin/test ## Run a test suite within gdb.
  156. @echo "Using libraries: "; $(LDD) bin/test
  157. exec gdb bin/test
  158. lint:
  159. splint \
  160. $(INCLUDE) -Itest \
  161. -DUINT_MAX=0xFFFFFFFFUL \
  162. -nullpass \
  163. -posix-lib \
  164. test.c
  165. .PHONY: memcheck
  166. memcheck:
  167. valgrind \
  168. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  169. --log-file=$(VALGRIND_DUMP) \
  170. ./bin/test || true
  171. @echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  172. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  173. # Profiling application.
  174. bin/profile: DEBUG = 1
  175. bin/profile: lib profile.c
  176. $(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -lvolksdata_dbg \
  177. profile.c -o bin/profile
  178. # Performance test application. Essentially the profiling code without debug.
  179. bin/perftest: DEBUG = 0
  180. bin/perftest: lib profile.c
  181. $(CC) $(CFLAGS) -g $(LDFLAGS) -lvolksdata profile.c -o bin/perftest
  182. .PHONY: perftest
  183. perftest: bin/perftest ## Run a performance test by creating, inserting and looking up triples.
  184. bin/perftest
  185. .PHONY: profile
  186. profile: bin/profile ## Run a profiling session on a limited set of high-volume commands.
  187. VOLK_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  188. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/profile 1000
  189. @echo "Profile dump written at $(CALLGRIND_DUMP) . Open it with "\
  190. "qcachegrind, kcachegrind, etc."
  191. .PHONY: test_profile
  192. test_profile: bin/test ## Run profiling on the standard test suite.
  193. VOLK_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  194. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/test
  195. @echo "Profile dump written at $(CALLGRIND_DUMP) . Open it with "\
  196. "qcachegrind, kcachegrind, etc."
  197. .PHONY: footprint
  198. footprint: bin/perftest ## Measure memory footprint by generating and storing 100K triples.
  199. VOLK_MDB_MAPSIZE=80000000 valgrind --tool=massif \
  200. --massif-out-file=$(MASSIF_DUMP) bin/perftest 100000
  201. @echo "Memory stats file written at $(MASSIF_DUMP). Open it with "\
  202. "massif-visualizer or similar."
  203. # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
  204. depgraph: $(VOLK_SRC) include/* include/volksdata/* include/volksdata/codec/* ## Build a visual dependency graph of the code.
  205. cinclude2dot --merge=module --include=$(DEPS) \
  206. --exclude='test|ext' >| $(DOCS)/dev/deps.dot
  207. dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf