Makefile 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # NOTE: only tested in GNU/Linux.
  2. ## Binary dependencies.
  3. CC = gcc
  4. AR = ar
  5. LEXER = re2c
  6. PARSER = lemon
  7. ## Paths.
  8. PREFIX ?= /usr/local
  9. bindir := $(PREFIX)/bin
  10. libdir := $(PREFIX)/lib
  11. includedir = $(PREFIX)/include/lsup
  12. MDB_DIR = ext/openldap/libraries/liblmdb
  13. XXHASH_DIR = ext/xxHash
  14. VALGRIND_DUMP = /tmp/lsup_valgrind.log
  15. CALLGRIND_DUMP = /tmp/lsup_callgrind.out
  16. INCLUDE_BASE = . -Iinclude -I$(MDB_DIR) -I$(XXHASH_DIR) \
  17. -Iext/tpl/src -Iext/hashmap -Iext/log/src
  18. INCLUDE = -I$(INCLUDE_BASE)
  19. CFLAGS += -Wall -fPIC -MMD -DLOG_USE_COLOR $(INCLUDE)
  20. DBG_CFLAGS = -Itest -O0 -g3 -DDEBUG
  21. # NOTE: -luuid is a Linux system library. Other OS's might need a different
  22. # link or a non-system library built.
  23. LDFLAGS = -L. -L$(libdir) -llmdb -lxxhash -luuid
  24. CODEC_DIR = src/codec
  25. CODEC_SRC = $(wildcard src/codec_*.c)
  26. CODEC_OBJ = $(CODEC_SRC:.c=.o)
  27. CODEC_DBG_OBJ = $(CODEC_SRC:.c=_dbg.o)
  28. PARSER_SRC = $(CODEC_SRC:src/codec_%=src/codec/parser_%)
  29. PARSER_OBJ = $(PARSER_SRC:.c=.o)
  30. # External sources compiled in core object.
  31. EXT_SRC = $(wildcard ext/log/src/*.c) \
  32. $(wildcard ext/hashmap/*.c) \
  33. $(wildcard ext/tpl/src/*.c)
  34. # External headers of libraries compiled in core.
  35. EXT_H = $(wildcard ext/log/src/*.h) \
  36. $(wildcard ext/tpl/src/*.h) \
  37. $(wildcard ext/hashmap/*.h)
  38. LSUP_SRC = $(wildcard src/*.c)
  39. SRC = $(EXT_SRC) $(LSUP_SRC)
  40. TEST_SRC = $(wildcard test/*.c) test.c
  41. EXT_OBJ = $(EXT_SRC:.c=.o)
  42. OBJ = $(EXT_OBJ) $(PARSER_OBJ) $(CODEC_OBJ) $(LSUP_SRC:.c=.o)
  43. DBG_OBJ = $(EXT_OBJ) $(PARSER_OBJ) $(CODEC_DBG_OBJ) $(LSUP_SRC:.c=_dbg.o)
  44. DEPLIBS = libxxhash liblmdb
  45. LIBS = liblsuprdf.a liblsuprdf.so
  46. DBG_LIBS = liblsuprdf_dbg.a liblsuprdf_dbg.so
  47. # For visual dep graph.
  48. DEPS := $(shell echo "${INCLUDE_BASE}" | sed -e 's/ -I/,/g')
  49. DOCS = docs
  50. ## Environment.
  51. # Tests need the freshly compiled libs.
  52. export LD_LIBRARY_PATH = .:$(libdir)
  53. ## Rules.
  54. .DEFAULT_GOAL := lib
  55. # Extract all rule comments into a help message.
  56. .PHONY: help
  57. help:
  58. @echo "Command overview:"; echo; \
  59. grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
  60. | sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1|\3/p' \
  61. | column -t -s '|'
  62. lib: $(DEPLIBS) $(LIBS) ## Compile main library (static and dynamic linking).
  63. debug: $(DEPLIBS) $(DBG_LIBS) ## Compile main library with debug symbols.
  64. # Static library.
  65. liblsuprdf.a: $(OBJ)
  66. $(AR) rs $@ $^
  67. # Dynamic library.
  68. liblsuprdf.so: $(OBJ)
  69. $(CC) -shared $(LDFLAGS) -o $@ $^
  70. # Static debug library.
  71. liblsuprdf_dbg.a: $(DBG_OBJ)
  72. $(AR) rs $@ $^
  73. # Dynamic debug library.
  74. liblsuprdf_dbg.so: $(DBG_OBJ)
  75. $(CC) -shared $(LDFLAGS) -o $@ $^
  76. # Debug objects.
  77. %_dbg.o: %.c
  78. $(CC) $(CFLAGS) $(DBG_CFLAGS) $(LDFLAGS) -c $^ -o $@
  79. .PHONY: parsers
  80. parsers: $(PARSER_SRC) ## Make intermediate parser sources for development.
  81. # Codecs.
  82. # Parser C sources.
  83. $(CODEC_DIR)/parser_%.c: $(CODEC_DIR)/lexer_%.re $(CODEC_DIR)/grammar_%.c $(CODEC_DIR)/grammar_%.h src/codec.c
  84. $(LEXER) $< -o $@ -T --case-ranges
  85. .SECONDARY: $(CODEC_DIR)/grammar_%.c
  86. # Parser generators.
  87. $(CODEC_DIR)/grammar_%.c $(CODEC_DIR)/grammar_%.h: $(CODEC_DIR)/grammar_%.y
  88. $(PARSER) $< -q -T$(CODEC_DIR)/lempar.c -d$(CODEC_DIR)
  89. # Ext libraries.
  90. .PHONY: libxxhash
  91. libxxhash:
  92. $(MAKE) -C $(XXHASH_DIR)
  93. .PHONY: liblmdb
  94. liblmdb:
  95. $(MAKE) -C $(MDB_DIR)
  96. install: lib ## Install library and dependencies to $PREFIX. May require sudo.
  97. @echo "Installing library files in $(PREFIX)."
  98. cd $(MDB_DIR); PREFIX=$(PREFIX) make install
  99. cd $(XXHASH_DIR); PREFIX=$(PREFIX) make install
  100. mkdir -p $(DESTDIR)$(libdir)
  101. mkdir -p $(DESTDIR)$(includedir)
  102. cp liblsuprdf.{a,so} $(DESTDIR)$(libdir) && \
  103. cp include/*.h $(EXT_H) $(DESTDIR)$(includedir)
  104. debug_install: install debug ## Install standard and debug libraries.
  105. @echo "Installing debug library files in $(PREFIX)."
  106. cp liblsuprdf_dbg.{a,so} $(DESTDIR)$(libdir)
  107. .PHONY: clean ## Clean up artifacts.
  108. clean:
  109. rm -f src/*.[aod] ./*[aod] src/codec/*[aod]
  110. .PHONY: deepclean ## Clean up external libraries and language parsers as well.
  111. deepclean: clean
  112. rm -f src/codec/grammar_*.{c,h}
  113. rm -f src/codec/parser_*.c
  114. cd $(MDB_DIR); make clean
  115. cd $(XXHASH_DIR); make clean
  116. .PHONY: uninstall ## Uninstall library (not the dependencies).
  117. uninstall:
  118. rm -f $(DESTDIR)$(libdir)/liblsuprdf*
  119. rm -rf $(DESTDIR)$(includedir)
  120. rm -f bin/test*
  121. # For testing, use debug symbols.
  122. bin/test: debug $(TEST_SRC)
  123. $(CC) $(CFLAGS) $(DBG_CFLAGS) -Itest $(LDFLAGS) -llsuprdf_dbg \
  124. test.c -o bin/test
  125. .PHONY: test
  126. test: bin/test ## Run a test suite.
  127. @echo "Using libraries: "; ldd bin/test
  128. exec bin/test
  129. .PHONY: gdb_test
  130. gdb_test: bin/test ## Run a test suite within gdb.
  131. @echo "Using libraries: "; ldd bin/test
  132. exec gdb bin/test
  133. lint:
  134. splint \
  135. $(INCLUDE) -Itest \
  136. -DUINT_MAX=0xFFFFFFFFUL \
  137. -nullpass \
  138. -posix-lib \
  139. test.c
  140. .PHONY: memcheck
  141. memcheck:
  142. valgrind \
  143. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  144. --log-file=$(VALGRIND_DUMP) \
  145. ./bin/test
  146. @echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  147. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  148. # Performance test application. Essentially the profiling code without debug.
  149. bin/profile: debug profile.c
  150. $(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -llsuprdf_dbg \
  151. profile.c -o bin/profile
  152. # Performance test application. Essentially the profiling code without debug.
  153. bin/perftest: lib profile.c
  154. $(CC) $(CFLAGS) -g $(LDFLAGS) -llsuprdf profile.c -o bin/perftest
  155. .PHONY: perftest
  156. perftest: bin/perftest ## Run a performance test by creating, inserting and looking up triples.
  157. bin/perftest
  158. .PHONY: profile
  159. profile: bin/perftest ## Run a profiling session. Output can be inspected with KCachegrind.
  160. LSUP_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  161. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/perftest 1000
  162. @echo "Profile dump written at $(CALLGRIND_DUMP)"
  163. .PHONY: pytest
  164. pytest: ## Run a test suite for the Python package.
  165. pip3 install --user .
  166. python3 test/cpython_test.py
  167. # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
  168. depgraph: src/* include/* ## Build a visual dependency graph of the code.
  169. cinclude2dot --merge=module --include=$(DEPS) \
  170. --exclude='test|ext' >| $(DOCS)/dev/deps.dot
  171. dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf