Makefile 7.0 KB

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