Makefile 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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=$(BUILDDIR)/%.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 EXT_SRC: $(EXT_SRC))
  59. $(info EXT_OBJ: $(EXT_OBJ))
  60. $(info OBJ: $(OBJ))
  61. $(info DBG_OBJ: $(DBG_OBJ))
  62. #$(info LIBS: $(LIBS))
  63. #$(info DBG_LIBS: $(DBG_LIBS))
  64. # LDD for Linux, otool -L for OSX.
  65. ifeq (, $(shell which ldd))
  66. LDD := otool -L
  67. else
  68. LDD := ldd
  69. endif
  70. # For visual dep graph.
  71. DEPS := $(shell echo "${INCLUDE_BASE}" | sed -e 's/ -I/,/g'),include/codec
  72. DOCS = docs
  73. ## Environment.
  74. # Tests need the freshly compiled libs.
  75. export LD_LIBRARY_PATH = $(OUTDIR):$(libdir)
  76. ## Rules.
  77. .DEFAULT_GOAL := lib
  78. # Extract all rule comments into a help message.
  79. .PHONY: help
  80. help:
  81. @echo "Command overview:"; echo; \
  82. grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
  83. | sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1|\3/p' \
  84. | column -t -s '|'
  85. .PHONY: lib
  86. lib: codec $(LIBS) ## Compile main library (static and dynamic linking).
  87. .PHONY: debug
  88. debug: codec_dbg $(DBG_LIBS) ## Compile main library with debug symbols.
  89. # Static library.
  90. $(STATIC_LIB): $(OBJ)
  91. $(AR) rs $@ $^ $(CODEC_OBJ)
  92. # Dynamic library.
  93. $(DYN_LIB): $(OBJ)
  94. $(CC) -shared $(LDFLAGS) -o $@ $^ $(CODEC_OBJ)
  95. # Static debug library.
  96. $(STATIC_DBG_LIB): $(DBG_OBJ)
  97. $(AR) rs $@ $^ $(CODEC_DBG_OBJ)
  98. # Dynamic debug library.
  99. $(DYN_DBG_LIB): $(DBG_OBJ)
  100. $(CC) -shared $(LDFLAGS) -o $@ $^ $(CODEC_DBG_OBJ)
  101. # External libraries.
  102. $(BUILDDIR)/ext/%.o: ext/%.c
  103. mkdir -p $(dir $@)
  104. $(CC) $(CFLAGS) -c $^ -o $@
  105. # Standard objects.
  106. $(BUILDDIR)/%.o: src/%.c
  107. $(CC) $(CFLAGS) -c $^ -o $@
  108. # Debug objects.
  109. $(BUILDDIR)/%_dbg.o: src/%.c
  110. $(CC) $(DBG_CFLAGS) -c $^ -o $@
  111. # Codecs in a subfolder.
  112. .PHONY: codec
  113. codec: $(PARSER)
  114. mkdir -p $(BUILDDIR) && \
  115. $(MAKE) -C $(CODEC_DIR) codec
  116. .PHONY: codec_dbg
  117. codec_dbg: $(PARSER)
  118. mkdir -p $(BUILDDIR) && \
  119. $(MAKE) -C $(CODEC_DIR) debug
  120. # Build the parser executable.
  121. $(PARSER): $(LEMON_SRC)
  122. $(CC) $^ -o $@
  123. install: lib ## Install library and dependencies to $PREFIX. May require sudo.
  124. @echo "Installing library files in $(PREFIX)."
  125. mkdir -p $(libdir)
  126. mkdir -p $(INCLUDEDIR)/lsup
  127. cp $(LIBS) $(libdir) && \
  128. cp -r include/lsup/* $(EXT_H) $(INCLUDEDIR)/lsup && \
  129. cp include/*.h $(INCLUDEDIR)
  130. debug_install: debug ## Install debug libraries.
  131. @echo "Installing debug library files in $(PREFIX)."
  132. cp $(DBG_LIBS) $(libdir)
  133. .PHONY: clean
  134. clean: ## Clean up artifacts, including language parsers.
  135. rm -rf $(BUILDDIR)
  136. rm -f bin/*
  137. rm -f $(LIBS) $(DBG_LIBS)
  138. rm -f include/codec/grammar_*.h
  139. rm -f src/codec/grammar_*.c src/codec/parser_*.c
  140. .PHONY: uninstall ## Uninstall library (not the dependencies).
  141. uninstall:
  142. rm -f $(libdir)/liblsuprdf*
  143. rm -rf $(INCLUDEDIR)/lsup*
  144. rm -f bin/test*
  145. # For testing, use debug symbols.
  146. bin/test: debug $(TEST_SRC)
  147. $(CC) $(DBG_CFLAGS) $(LDFLAGS) -llsuprdf_dbg \
  148. test.c -o bin/test
  149. .PHONY: test
  150. test: bin/test ## Run a test suite.
  151. @echo "Using libraries: "; $(LDD) bin/test
  152. exec bin/test
  153. .PHONY: gdb_test
  154. gdb_test: bin/test ## Run a test suite within gdb.
  155. @echo "Using libraries: "; $(LDD) bin/test
  156. exec gdb bin/test
  157. lint:
  158. splint \
  159. $(INCLUDE) -Itest \
  160. -DUINT_MAX=0xFFFFFFFFUL \
  161. -nullpass \
  162. -posix-lib \
  163. test.c
  164. .PHONY: memcheck
  165. memcheck:
  166. valgrind \
  167. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  168. --log-file=$(VALGRIND_DUMP) \
  169. ./bin/test || true
  170. @echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  171. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  172. # Profiling application.
  173. bin/profile: debug profile.c
  174. $(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -llsuprdf_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) -llsuprdf 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: bin/profile ## Run a profiling session on a limited set of high-volume commands.
  184. LSUP_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  185. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/profile 1000
  186. @echo "Profile dump written at $(CALLGRIND_DUMP) . Open it with "\
  187. "qcachegrind, kcachegrind, etc."
  188. .PHONY: test_profile
  189. test_profile: bin/test ## Run profiling on the standard test suite.
  190. LSUP_MDB_MAPSIZE=800000 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. LSUP_MDB_MAPSIZE=80000000 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: $(LSUP_SRC) include/* include/lsup/* include/lsup/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