Makefile 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # NOTE: only tested in GNU/Linux.
  2. ## Binary dependencies.
  3. CC = gcc
  4. AR = ar
  5. VALGRIND_DUMP = /tmp/lsr_valgrind.log
  6. ## Paths.
  7. PREFIX ?= /usr/local
  8. bindir = $(PREFIX)/bin
  9. libdir = $(PREFIX)/lib
  10. includedir = $(PREFIX)/include/lsup
  11. outdir = bin
  12. INCLUDE = -I. -Iinclude -I$(includedir)
  13. # Link all ext libraries statically.
  14. CFLAGS += -std=gnu11 -Wall -fPIC -MMD $(INCLUDE)
  15. DBG_CFLAGS = -Itest -O0 -g3 -DDEBUG
  16. LDFLAGS = -L. -L$(outdir) -L$(libdir) -llsuprdf
  17. DBG_LDFLAGS = -L. -L$(outdir) -L$(libdir) -llsuprdf_dbg
  18. SRC = $(wildcard src/*.c)
  19. OBJ = $(SRC:.c=.o)
  20. DBG_OBJ = $(SRC:.c=_dbg.o)
  21. TEST_SRC = $(wildcard test/*.c) test.c
  22. STATIC_LIB = $(outdir)/liblsuprepo.a
  23. DYN_LIB = $(outdir)/liblsuprepo.so
  24. STATIC_DBG_LIB = $(outdir)/liblsuprepo_dbg.a
  25. DYN_DBG_LIB = $(outdir)/liblsuprepo_dbg.so
  26. LIBS = $(STATIC_LIB) $(DYN_LIB)
  27. DBG_LIBS = $(STATIC_DBG_LIB) $(DYN_DBG_LIB)
  28. ## Environment.
  29. export LD_LIBRARY_PATH = $(outdir):$(libdir)
  30. ## Rules.
  31. .DEFAULT_GOAL := lib
  32. # Extract all rule comment into a help message.
  33. .PHONY: help
  34. help:
  35. @echo "Command overview:"; echo; \
  36. grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
  37. | sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1::\3/p' \
  38. | column -t -s '::'
  39. lib: $(LIBS)
  40. debug: $(DBG_LIBS)
  41. $(STATIC_LIB): $(OBJ)
  42. $(AR) rs $@ $^
  43. $(DYN_LIB): $(OBJ)
  44. $(CC) -shared $(LDFLAGS) -o $@ $^
  45. $(STATIC_DBG_LIB): $(DBG_OBJ)
  46. $(AR) rs $@ $^
  47. $(DYN_DBG_LIB): $(DBG_OBJ)
  48. $(CC) -shared $(LDFLAGS) -o $@ $^
  49. # Debug objects.
  50. %_dbg.o: %.c
  51. $(CC) $(CFLAGS) $(DBG_CFLAGS) $(LDFLAGS) -c $^ -o $@
  52. install: lib ## Install default libraries to $PREFIX. May require sudo.
  53. mkdir -p $(DESTDIR)$(libdir)
  54. mkdir -p $(DESTDIR)$(includedir)
  55. cp $(LIBS) $(DESTDIR)$(libdir) && \
  56. cp include/*.h $(EXT_H) $(DESTDIR)$(includedir)
  57. debug_install: install debug ## Install default and debug libraries.
  58. cp $(DBG_LIBS) $(DESTDIR)$(libdir)
  59. .PHONY: clean
  60. clean:
  61. rm -rf ./*.[aod] src/*.[aod]
  62. rm -f $(LIBS) bin/test*
  63. .PHONY: uninstall
  64. uninstall:
  65. rm -f $(DESTDIR)$(libdir)/liblsuprepo.*
  66. rm -rf $(DESTDIR)$(includedir)
  67. # For testing, use debug symbols.
  68. bin/test: debug $(TEST_SRC)
  69. $(CC) $(CFLAGS) $(DBG_CFLAGS) -Itest $(DBG_LDFLAGS) -llsuprepo_dbg \
  70. test.c -o bin/test
  71. .PHONY: test
  72. test: bin/test ## Run a test suite.
  73. exec bin/test
  74. .PHONY: gdb_test
  75. gdb_test: bin/test ## Run a test suite within gdb.
  76. exec gdb bin/test
  77. .PHONY: memcheck
  78. memcheck:
  79. valgrind \
  80. --leak-check=full --show-leak-kinds=all --track-origins=yes \
  81. --log-file=$(VALGRIND_DUMP) \
  82. ./bin/test
  83. @echo "Memcheck complete. Valgrind log is at $(VALGRIND_DUMP)"
  84. memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
  85. bin/profile: debug profile.c
  86. $(CC) $(CFLAGS) -g -DTESTING profile.c $(DBG_LDFLAGS) \
  87. -llsuprepo_dbg -o bin/profile
  88. .PHONY: profile
  89. profile: bin/profile
  90. exec valgrind --tool=callgrind --callgrind-out-file="$(CALLGRIND_DUMP)" \
  91. bin/profile 10000
  92. @echo "Profile dump written at $(CALLGRIND_DUMP)"
  93. .PHONY: pytest
  94. py_test:
  95. pip3 install --user .
  96. python3 test/cpython_test.py