Makefile 7.1 KB

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