Makefile 2.8 KB

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