Makefile 6.8 KB

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