Makefile 6.2 KB

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