Makefile 6.9 KB

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