Makefile 6.6 KB

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