Makefile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/uthash/src -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/uthash/src/*.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. cd $(MDB_DIR); PREFIX=$(PREFIX) make install
  94. cd $(XXHASH_DIR); PREFIX=$(PREFIX) make install
  95. mkdir -p $(DESTDIR)$(libdir)
  96. mkdir -p $(DESTDIR)$(includedir)
  97. cp liblsuprdf.{a,so} $(DESTDIR)$(libdir) && \
  98. cp include/*.h $(EXT_H) $(DESTDIR)$(includedir)
  99. debug_install: install debug ## Install standard and debug libraries.
  100. cp liblsuprdf_dbg.{a,so} $(DESTDIR)$(libdir)
  101. .PHONY: clean ## Clean up artifacts.
  102. clean:
  103. rm -rf src/*.[aod] ./*[aod] src/codec/*[aod]
  104. .PHONY: deepclean ## Clean up artifacts in external libraries as well.
  105. deepclean: clean
  106. cd $(MDB_DIR); make clean
  107. cd $(XXHASH_DIR); make clean
  108. .PHONY: uninstall ## Uninstall library (not the dependencies).
  109. uninstall:
  110. rm -f $(DESTDIR)$(libdir)/liblsuprdf*
  111. rm -rf $(DESTDIR)$(includedir)
  112. rm -f bin/test*
  113. # For testing, use debug symbols.
  114. bin/test: debug $(TEST_SRC)
  115. $(CC) $(CFLAGS) $(DBG_CFLAGS) -Itest $(LDFLAGS) -llsuprdf_dbg \
  116. test.c -o bin/test
  117. .PHONY: test
  118. test: bin/test ## Run a test suite.
  119. @echo "Using libraries: "; ldd bin/test
  120. exec bin/test
  121. .PHONY: gdb_test
  122. gdb_test: bin/test ## Run a test suite within gdb.
  123. @echo "Using libraries: "; ldd bin/test
  124. exec gdb bin/test
  125. lint:
  126. splint \
  127. $(INCLUDE) -Itest \
  128. -DUINT_MAX=0xFFFFFFFFUL \
  129. -nullpass \
  130. -posix-lib \
  131. test.c
  132. .PHONY: memcheck
  133. memcheck:
  134. valgrind \
  135. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  136. --log-file=$(VALGRIND_DUMP) \
  137. ./bin/test
  138. echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  139. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  140. # Performance test application. Essentially the profiling code without debug.
  141. bin/profile: debug profile.c
  142. $(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -llsuprdf_dbg \
  143. profile.c -o bin/profile
  144. # Performance test application. Essentially the profiling code without debug.
  145. bin/perftest: lib profile.c
  146. $(CC) $(CFLAGS) -g $(LDFLAGS) -llsuprdf profile.c -o bin/perftest
  147. .PHONY: perftest
  148. perftest: bin/perftest ## Run a performance test by creating, inserting and looking up triples.
  149. bin/perftest
  150. .PHONY: profile
  151. profile: bin/perftest ## Run a profiling session. Output can be inspected with KCachegrind.
  152. LSUP_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  153. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/perftest 1000
  154. @echo "Profile dump written at $(CALLGRIND_DUMP)"
  155. .PHONY: pytest
  156. pytest: ## Run a test suite for the Python package.
  157. pip3 install --user .
  158. python3 test/cpython_test.py
  159. # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
  160. depgraph: src/* include/* ## Build a visual dependency graph of the code.
  161. cinclude2dot --merge=module --include=$(DEPS) \
  162. --exclude='test|ext' >| $(DOCS)/dev/deps.dot
  163. dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf