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