Makefile 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # NOTE: only tested in GNU/Linux.
  2. SHELL := /bin/bash
  3. ## Binary dependencies.
  4. CC = gcc
  5. AR = ar
  6. ## Paths.
  7. PREFIX ?= /usr/local
  8. bindir := $(PREFIX)/bin
  9. libdir := $(PREFIX)/lib
  10. includedir = $(PREFIX)/include/lsup
  11. outdir = ./bin
  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 -Iext/tpl/src -Iext/hashmap -Iext/log/src
  16. INCLUDE := -I$(INCLUDE_BASE)
  17. _CFLAGS := -std=gnu11 -Wall -fPIC -MMD $(INCLUDE)
  18. CFLAGS = $(_CFLAGS) -O3
  19. DBG_CFLAGS = $(_CFLAGS) -Itest -O0 -g3 -DDEBUG
  20. #$(info CFLAGS: $(CFLAGS))
  21. #$(info DBG_CFLAGS: $(DBG_CFLAGS))
  22. # NOTE: -luuid is a Linux system library. Other OS's might need a different
  23. # link or a non-system library built.
  24. LDFLAGS := -L$(libdir) -L$(outdir) -L. -llmdb -lxxhash -luuid
  25. PARSER = bin/lemon
  26. LEMON_SRC = ext/sqlite/tool/lemon.c
  27. CODEC_DIR = src/codec
  28. # External sources compiled in core object.
  29. EXT_SRC := $(wildcard ext/log/src/*.c) \
  30. $(wildcard ext/hashmap/*.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/hashmap/*.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. # TODO This is extremely convoluted, simplify if possible.
  41. CODEC_SRC := $(wildcard $(CODEC_DIR)/codec_*.c)
  42. CODEC_REL_SRC := $(CODEC_SRC:$(CODEC_DIR)/%=%)
  43. ALL_CODEC_REL_SRC := $(CODEC_REL_SRC) $(CODEC_REL_SRC:codec_%=parser_%) \
  44. $(CODEC_REL_SRC:codec_%=grammar_%)
  45. CODEC_SRC = $(ALL_CODEC_REL_SRC:%=$(CODEC_DIR)/%)
  46. CODEC_OBJ = $(CODEC_SRC:.c=.o)
  47. CODEC_DBG_OBJ = $(CODEC_SRC:.c=_dbg.o)
  48. OBJ = $(EXT_OBJ) $(LSUP_SRC:.c=.o)
  49. DBG_OBJ = $(EXT_OBJ) $(LSUP_SRC:.c=_dbg.o)
  50. STATIC_LIB = $(outdir)/liblsuprdf.a
  51. DYN_LIB = $(outdir)/liblsuprdf.so
  52. STATIC_DBG_LIB = $(outdir)/liblsuprdf_dbg.a
  53. DYN_DBG_LIB = $(outdir)/liblsuprdf_dbg.so
  54. LIBS = $(STATIC_LIB) $(DYN_LIB)
  55. DBG_LIBS = $(STATIC_DBG_LIB) $(DYN_DBG_LIB)
  56. # LDD for Linux, otool -L for OSX.
  57. ifeq (, $(shell which ldd))
  58. LDD := otool -L
  59. else
  60. LDD := ldd
  61. endif
  62. # For visual dep graph.
  63. DEPS := $(shell echo "${INCLUDE_BASE}" | sed -e 's/ -I/,/g'),include/codec
  64. DOCS = docs
  65. ## Environment.
  66. # Tests need the freshly compiled libs.
  67. export LD_LIBRARY_PATH = $(outdir):$(libdir)
  68. ## Rules.
  69. .DEFAULT_GOAL := lib
  70. # Extract all rule comments into a help message.
  71. .PHONY: help
  72. help:
  73. @echo "Command overview:"; echo; \
  74. grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
  75. | sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1|\3/p' \
  76. | column -t -s '|'
  77. .PHONY: lib
  78. lib: codec $(LIBS) ## Compile main library (static and dynamic linking).
  79. .PHONY: debug
  80. debug: codec_dbg $(DBG_LIBS) ## Compile main library with debug symbols.
  81. # Static library.
  82. $(STATIC_LIB): $(OBJ)
  83. $(AR) rs $@ $^ $(CODEC_OBJ)
  84. # Dynamic library.
  85. $(DYN_LIB): $(OBJ)
  86. $(CC) -shared $(LDFLAGS) -o $@ $^ $(CODEC_OBJ)
  87. # Static debug library.
  88. $(STATIC_DBG_LIB): $(DBG_OBJ)
  89. $(AR) rs $@ $^ $(CODEC_DBG_OBJ)
  90. # Dynamic debug library.
  91. $(DYN_DBG_LIB): $(DBG_OBJ)
  92. $(CC) -shared $(LDFLAGS) -o $@ $^ $(CODEC_DBG_OBJ)
  93. # Debug objects.
  94. %_dbg.o: %.c
  95. $(CC) $(DBG_CFLAGS) -c $^ -o $@
  96. # Codecs in a subfolder.
  97. .PHONY: codec
  98. codec: $(PARSER)
  99. $(MAKE) -C $(CODEC_DIR) codec
  100. .PHONY: codec_dbg
  101. codec_dbg: $(PARSER)
  102. $(MAKE) -C $(CODEC_DIR) debug
  103. # Build the parser executable.
  104. $(PARSER): $(LEMON_SRC)
  105. $(CC) $^ -o $@
  106. install: lib ## Install library and dependencies to $PREFIX. May require sudo.
  107. @echo "Installing library files in $(PREFIX)."
  108. mkdir -p $(DESTDIR)$(libdir)
  109. mkdir -p $(DESTDIR)$(includedir)
  110. cp $(LIBS) $(DESTDIR)$(libdir) && \
  111. cp -r include/*.h include/codec $(EXT_H) $(DESTDIR)$(includedir)
  112. debug_install: install debug ## Install standard and debug libraries.
  113. @echo "Installing debug library files in $(PREFIX)."
  114. cp $(DBG_LIBS) $(DESTDIR)$(libdir)
  115. .PHONY: clean
  116. clean: ## Clean up artifacts, including language parsers.
  117. rm -f ./*.[aod] bin/* src/*.[aod] src/codec/*.[aod] src/codec/*.out
  118. rm -rf build/ dist/ lsup_rdf.egg-info/
  119. rm -f $(LIBS) $(DBG_LIBS)
  120. rm -f include/codec/grammar_*.h
  121. rm -f src/codec/grammar_*.c src/codec/parser_*.c
  122. rm -rf venv/
  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. # Profiling application.
  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/profile 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. # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
  178. depgraph: $(LSUP_SRC) $(CODEC_SRC) include/* include/codec/* ## Build a visual dependency graph of the code.
  179. cinclude2dot --merge=module --include=$(DEPS) \
  180. --exclude='test|ext' >| $(DOCS)/dev/deps.dot
  181. dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf