Makefile 5.7 KB

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