Makefile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/*_grammar.c) \
  26. $(wildcard src/codec/*_parser.c)
  27. CODEC_OBJ = $(CODEC_SRC:.c=.o)
  28. CODEC_DBG_OBJ = $(CODEC_SRC:.c=_dbg.o)
  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. OBJ = $(EXT_OBJ) $(CODEC_OBJ) $(LSUP_SRC:.c=.o)
  42. DBG_OBJ = $(EXT_OBJ) $(CODEC_DBG_OBJ) $(LSUP_SRC:.c=_dbg.o)
  43. DEPLIBS = libxxhash liblmdb
  44. LIBS = liblsuprdf.a liblsuprdf.so
  45. DBG_LIBS = liblsuprdf_dbg.a liblsuprdf_dbg.so
  46. # For visual dep graph.
  47. DEPS := $(shell echo "${INCLUDE_BASE}" | sed -e 's/ -I/,/g')
  48. DOCS = docs
  49. ## Environment.
  50. # Tests need the freshly compiled libs.
  51. export LD_LIBRARY_PATH = .:$(libdir)
  52. ## Rules.
  53. .DEFAULT_GOAL := lib
  54. # Extract all rule comment into a help message.
  55. .PHONY: help
  56. help:
  57. @echo "Command overview:"; echo; \
  58. grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
  59. | sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1|\3/p' \
  60. | column -t -s '|'
  61. lib: $(DEPLIBS) $(LIBS) ## Compile main library (static and dynamic linking).
  62. debug: $(DEPLIBS) $(DBG_LIBS) ## Compile main library with debug symbols.
  63. # Static library.
  64. liblsuprdf.a: $(OBJ)
  65. $(AR) rs $@ $^
  66. # Dynamic library.
  67. liblsuprdf.so: $(OBJ)
  68. $(CC) -shared $(LDFLAGS) -o $@ $^
  69. # Static debug library.
  70. liblsuprdf_dbg.a: $(DBG_OBJ)
  71. $(AR) rs $@ $^
  72. # Dynamic debug library.
  73. liblsuprdf_dbg.so: $(DBG_OBJ)
  74. $(CC) -shared $(LDFLAGS) -o $@ $^
  75. # Debug objects.
  76. %_dbg.o: %.c
  77. $(CC) $(CFLAGS) $(DBG_CFLAGS) $(LDFLAGS) -c $^ -o $@
  78. # Codecs.
  79. $(CODEC_OBJ): $(CODEC_SRC)
  80. # Parser C sources.
  81. %_parser.c: %_lexer.re %_grammar.c
  82. $(LEXER) $< -o $@ -T --case-ranges
  83. # Parser generators.
  84. %_grammar.c: %_grammar.y
  85. $(PARSER) $< -q -m -T$(CODEC_DIR)/lempar.c -d$(CODEC_DIR)
  86. # Ext libraries.
  87. .PHONY: libxxhash
  88. libxxhash:
  89. $(MAKE) -C $(XXHASH_DIR)
  90. .PHONY: liblmdb
  91. liblmdb:
  92. $(MAKE) -C $(MDB_DIR)
  93. install: lib ## Install library and dependencies to $PREFIX. May require sudo.
  94. @echo "Installing library files in $(PREFIX)."
  95. cd $(MDB_DIR); PREFIX=$(PREFIX) make install
  96. cd $(XXHASH_DIR); PREFIX=$(PREFIX) make install
  97. mkdir -p $(DESTDIR)$(libdir)
  98. mkdir -p $(DESTDIR)$(includedir)
  99. cp liblsuprdf.{a,so} $(DESTDIR)$(libdir) && \
  100. cp include/*.h $(EXT_H) $(DESTDIR)$(includedir)
  101. debug_install: install debug ## Install standard and debug libraries.
  102. @echo "Installing debug library files in $(PREFIX)."
  103. cp liblsuprdf_dbg.{a,so} $(DESTDIR)$(libdir)
  104. .PHONY: clean ## Clean up artifacts.
  105. clean:
  106. rm -rf src/*.[aod] ./*[aod] src/codec/*[aod]
  107. .PHONY: deepclean ## Clean up artifacts in external libraries as well.
  108. deepclean: clean
  109. cd $(MDB_DIR); make clean
  110. cd $(XXHASH_DIR); make clean
  111. .PHONY: uninstall ## Uninstall library (not the dependencies).
  112. uninstall:
  113. rm -f $(DESTDIR)$(libdir)/liblsuprdf*
  114. rm -rf $(DESTDIR)$(includedir)
  115. rm -f bin/test*
  116. # For testing, use debug symbols.
  117. bin/test: debug $(TEST_SRC)
  118. $(CC) $(CFLAGS) $(DBG_CFLAGS) -Itest $(LDFLAGS) -llsuprdf_dbg \
  119. test.c -o bin/test
  120. .PHONY: test
  121. test: bin/test ## Run a test suite.
  122. @echo "Using libraries: "; ldd bin/test
  123. exec bin/test
  124. .PHONY: gdb_test
  125. gdb_test: bin/test ## Run a test suite within gdb.
  126. @echo "Using libraries: "; ldd bin/test
  127. exec gdb bin/test
  128. lint:
  129. splint \
  130. $(INCLUDE) -Itest \
  131. -DUINT_MAX=0xFFFFFFFFUL \
  132. -nullpass \
  133. -posix-lib \
  134. test.c
  135. .PHONY: memcheck
  136. memcheck:
  137. valgrind \
  138. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  139. --log-file=$(VALGRIND_DUMP) \
  140. ./bin/test
  141. @echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  142. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  143. # Performance test application. Essentially the profiling code without debug.
  144. bin/profile: debug profile.c
  145. $(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -llsuprdf_dbg \
  146. profile.c -o bin/profile
  147. # Performance test application. Essentially the profiling code without debug.
  148. bin/perftest: lib profile.c
  149. $(CC) $(CFLAGS) -g $(LDFLAGS) -llsuprdf profile.c -o bin/perftest
  150. .PHONY: perftest
  151. perftest: bin/perftest ## Run a performance test by creating, inserting and looking up triples.
  152. bin/perftest
  153. .PHONY: profile
  154. profile: bin/perftest ## Run a profiling session. Output can be inspected with KCachegrind.
  155. LSUP_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  156. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/perftest 1000
  157. @echo "Profile dump written at $(CALLGRIND_DUMP)"
  158. .PHONY: pytest
  159. pytest: ## Run a test suite for the Python package.
  160. pip3 install --user .
  161. python3 test/cpython_test.py
  162. # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
  163. depgraph: src/* include/* ## Build a visual dependency graph of the code.
  164. cinclude2dot --merge=module --include=$(DEPS) \
  165. --exclude='test|ext' >| $(DOCS)/dev/deps.dot
  166. dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf