Makefile 7.2 KB

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