Makefile 7.1 KB

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