Makefile 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. CODEC_DIR = src/codec
  23. # External sources compiled in core object.
  24. EXT_SRC ::= $(wildcard ext/log/src/*.c) \
  25. $(wildcard ext/hashmap/*.c) \
  26. $(wildcard ext/tpl/src/*.c)
  27. # External headers of libraries compiled in core.
  28. EXT_H ::= $(wildcard ext/log/src/*.h) \
  29. $(wildcard ext/tpl/src/*.h) \
  30. $(wildcard ext/hashmap/*.h)
  31. LSUP_SRC = $(wildcard src/*.c)
  32. SRC = $(EXT_SRC) $(LSUP_SRC)
  33. TEST_SRC = $(wildcard test/*.c) test.c
  34. EXT_OBJ ::= $(EXT_SRC:.c=.o)
  35. # TODO This is extremely convoluted, simplify if possible.
  36. CODEC_SRC ::= $(wildcard $(CODEC_DIR)/codec_*.c)
  37. CODEC_REL_SRC ::= $(CODEC_SRC:$(CODEC_DIR)/%=%)
  38. ALL_CODEC_REL_SRC ::= $(CODEC_REL_SRC) $(CODEC_REL_SRC:codec_%=parser_%) \
  39. $(CODEC_REL_SRC:codec_%=grammar_%)
  40. CODEC_SRC = $(ALL_CODEC_REL_SRC:%=$(CODEC_DIR)/%)
  41. CODEC_OBJ = $(CODEC_SRC:.c=.o)
  42. CODEC_DBG_OBJ = $(CODEC_SRC:.c=_dbg.o)
  43. OBJ = $(EXT_OBJ) $(LSUP_SRC:.c=.o)
  44. DBG_OBJ = $(EXT_OBJ) $(LSUP_SRC:.c=_dbg.o)
  45. DEPLIBS = libxxhash liblmdb
  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: $(DEPLIBS) codec $(LIBS) ## Compile main library (static and dynamic linking).
  64. debug: $(DEPLIBS) 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) $(CFLAGS) $(DBG_CFLAGS) -c $^ -o $@
  80. # Codecs in a subfolder.
  81. .PHONY: parsers
  82. parsers: ## Make intermediate parser sources for development.
  83. $(MAKE) -C $(CODEC_DIR) parsers
  84. .PHONY: codec
  85. codec:
  86. $(MAKE) -C $(CODEC_DIR) codec
  87. .PHONY: codec_dbg
  88. codec_dbg:
  89. $(MAKE) -C $(CODEC_DIR) debug
  90. # Ext libraries.
  91. .PHONY: libxxhash
  92. libxxhash:
  93. $(MAKE) -C $(XXHASH_DIR)
  94. .PHONY: liblmdb
  95. liblmdb:
  96. $(MAKE) -C $(MDB_DIR)
  97. install: lib ## Install library and dependencies to $PREFIX. May require sudo.
  98. @echo "Installing library files in $(PREFIX)."
  99. cd $(MDB_DIR); PREFIX=$(PREFIX) make install
  100. cd $(XXHASH_DIR); PREFIX=$(PREFIX) make install
  101. mkdir -p $(DESTDIR)$(libdir)
  102. mkdir -p $(DESTDIR)$(includedir)
  103. cp liblsuprdf.{a,so} $(DESTDIR)$(libdir) && \
  104. cp include/*.h $(EXT_H) $(DESTDIR)$(includedir)
  105. debug_install: install debug ## Install standard and debug libraries.
  106. @echo "Installing debug library files in $(PREFIX)."
  107. cp liblsuprdf_dbg.{a,so} $(DESTDIR)$(libdir)
  108. .PHONY: clean ## Clean up artifacts, including language parsers.
  109. clean:
  110. rm -f src/*.[aod] ./*[aod] src/codec/*[aod]
  111. rm -f include/codec/grammar_*.h
  112. rm -f src/codec/{grammar,parser}_*.c
  113. .PHONY: deepclean ## Clean up external libraries.
  114. deepclean: clean
  115. cd $(MDB_DIR); make clean
  116. cd $(XXHASH_DIR); make clean
  117. .PHONY: uninstall ## Uninstall library (not the dependencies).
  118. uninstall:
  119. rm -f $(DESTDIR)$(libdir)/liblsuprdf*
  120. rm -rf $(DESTDIR)$(includedir)
  121. rm -f bin/test*
  122. # For testing, use debug symbols.
  123. bin/test: debug $(TEST_SRC)
  124. $(CC) $(CFLAGS) $(DBG_CFLAGS) -Itest $(LDFLAGS) -llsuprdf_dbg \
  125. test.c -o bin/test
  126. .PHONY: test
  127. test: bin/test ## Run a test suite.
  128. @echo "Using libraries: "; ldd bin/test
  129. exec bin/test
  130. .PHONY: gdb_test
  131. gdb_test: bin/test ## Run a test suite within gdb.
  132. @echo "Using libraries: "; ldd bin/test
  133. exec gdb bin/test
  134. lint:
  135. splint \
  136. $(INCLUDE) -Itest \
  137. -DUINT_MAX=0xFFFFFFFFUL \
  138. -nullpass \
  139. -posix-lib \
  140. test.c
  141. .PHONY: memcheck
  142. memcheck:
  143. valgrind \
  144. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  145. --log-file=$(VALGRIND_DUMP) \
  146. ./bin/test
  147. @echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  148. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  149. # Performance test application. Essentially the profiling code without debug.
  150. bin/profile: debug profile.c
  151. $(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -llsuprdf_dbg \
  152. profile.c -o bin/profile
  153. # Performance test application. Essentially the profiling code without debug.
  154. bin/perftest: lib profile.c
  155. $(CC) $(CFLAGS) -g $(LDFLAGS) -llsuprdf profile.c -o bin/perftest
  156. .PHONY: perftest
  157. perftest: bin/perftest ## Run a performance test by creating, inserting and looking up triples.
  158. bin/perftest
  159. .PHONY: profile
  160. profile: bin/perftest ## Run a profiling session. Output can be inspected with KCachegrind.
  161. LSUP_MDB_MAPSIZE=800000 valgrind --tool=callgrind \
  162. --callgrind-out-file="$(CALLGRIND_DUMP)" bin/perftest 1000
  163. @echo "Profile dump written at $(CALLGRIND_DUMP)"
  164. .PHONY: pytest
  165. pytest: ## Run a test suite for the Python package.
  166. pip3 install --user .
  167. python3 test/cpython_test.py
  168. # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
  169. depgraph: $(LSUP_SRC) $(CODEC_SRC) include/* include/codec/* ## Build a visual dependency graph of the code.
  170. cinclude2dot --merge=module --include=$(DEPS) \
  171. --exclude='test|ext' >| $(DOCS)/dev/deps.dot
  172. dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf