Makefile 2.8 KB

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