2 Commits 11f463ecc5 ... 45821b8d65

Author SHA1 Message Date
  Stefano Cossu 45821b8d65 Compile & install debug symbols separately; make help. 2 years ago
  Stefano Cossu 60c74b22d0 Remove untracked content. 2 years ago
4 changed files with 88 additions and 47 deletions
  1. 84 33
      Makefile
  2. 4 12
      README.md
  3. 0 1
      ext/log
  4. 0 1
      ext/tpl

+ 84 - 33
Makefile

@@ -13,7 +13,7 @@ PARSER = lemon
 PREFIX = /usr/local
 bindir = $(PREFIX)/bin
 libdir = $(PREFIX)/lib
-includedir = $(PREFIX)/include/lsup_rdf
+includedir = $(PREFIX)/include/lsup
 MDB_DIR = ext/openldap/libraries/liblmdb
 XXHASH_DIR = ext/xxHash
 CALLGRIND_DUMP = /tmp/lsup_callgrind.%p.out
@@ -22,17 +22,16 @@ INCLUDE_BASE = . -Iinclude -I$(MDB_DIR) -I$(XXHASH_DIR) \
 	-Iext/tpl/src -Iext/uthash/src -Iext/log/src
 INCLUDE = -I$(INCLUDE_BASE)
 CFLAGS += -Wall -fPIC -MMD -DLOG_USE_COLOR $(INCLUDE)
-TEST_CFLAGS = -Itest -O0 -g3 -DDEBUG
+DBG_CFLAGS = -Itest -O0 -g3 -DDEBUG
 # NOTE: -luuid is a Linux system library. Other OS's might need a different
 # link or a non-system library built.
-LDFLAGS = -L$(MDB_DIR) -L$(XXHASH_DIR) \
-		  -llmdb -lxxhash
+LDFLAGS = -L. -L$(libdir) -llmdb -lxxhash -luuid
 
 CODEC_DIR = src/codec
-#CODEC_SRC = $(wildcard src/codec/*_parser.c)
 CODEC_SRC = $(wildcard src/codec/*_grammar.c) \
 	  		$(wildcard src/codec/*_parser.c)
 CODEC_OBJ = $(CODEC_SRC:.c=.o)
+CODEC_DBG_OBJ = $(CODEC_SRC:.c=_dbg.o)
 
 # External sources compiled in core object.
 EXT_SRC = $(wildcard ext/log/src/*.c) \
@@ -43,35 +42,74 @@ EXT_H = $(wildcard ext/log/src/*.h) \
 	  	$(wildcard ext/tpl/src/*.h) \
 	  	$(wildcard ext/uthash/src/*.h)
 
-SRC = $(EXT_SRC) $(wildcard src/*.c)
+LSUP_SRC = $(wildcard src/*.c)
+SRC = $(EXT_SRC) $(LSUP_SRC)
 TEST_SRC = $(wildcard test/*.c) test.c
 
-EXT_OBJ = $(MDB_DIR)/{mdb,midl}.o $(XXHASH_DIR)/xxhash.o
-OBJ = $(SRC:.c=.o) $(CODEC_OBJ)
+OBJ = $(LSUP_SRC:.c=.o) $(CODEC_OBJ)
+EXT_OBJ = $(EXT_SRC:.c=.o) 
+DBG_OBJ = $(EXT_OBJ) $(CODEC_DBG_OBJ) $(LSUP_SRC:.c=_dbg.o)
 
 DEPLIBS = libxxhash liblmdb
 LIBS = liblsuprdf.a liblsuprdf.so
+DBG_LIBS = liblsuprdf_dbg.a liblsuprdf_dbg.so
 
 # For visual dep graph.
 DEPS := $(shell echo "${INCLUDE_BASE}" | sed -e 's/ -I/,/g')
 DOCS = docs
 
 
+## Environment.
+
+export LD_LIBRARY_PATH = $(libdir):.
+
+
 ## Rules.
 
-all: $(DEPLIBS) $(LIBS)
+.DEFAULT_GOAL := lib
+
+# Extract all rule comment into a help message.
+.PHONY: help
+help:
+	@echo "Command overview:"; echo; \
+		grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
+		| sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1::\3/p' \
+		| column -t  -s '::'
+	
+
+lib: $(DEPLIBS) $(LIBS) ## Compile main library (static and dynamic linking).
 
+
+debug: $(DEPLIBS) $(DBG_LIBS) ## Compile main library with debug symbols.
+
+# Static library.
 liblsuprdf.a: $(OBJ)
-	$(AR) rs $@ $(EXT_OBJ) $^
+	$(AR) rs $@ $^
 
+# Dynamic library.
 liblsuprdf.so: $(OBJ)
 	$(CC) -shared $(LDFLAGS) -o $@ $^
 
+# Static debug library.
+liblsuprdf_dbg.a: $(DBG_OBJ)
+	$(AR) rs $@ $^
+
+# Dynamic debug library.
+liblsuprdf_dbg.so: $(DBG_OBJ)
+	$(CC) -shared $(LDFLAGS) -o $@ $^
+
+# Debug objects.
+%_dbg.o: %.c
+	$(CC) $(CFLAGS) $(DBG_CFLAGS) $(LDFLAGS) -c $^ -o $@
+
+# Codecs.
 $(CODEC_OBJ): $(CODEC_SRC)
 
+# Parser C sources.
 %_parser.c: %_lexer.re %_grammar.c
 	$(LEXER) $< -o $@ -T --case-ranges
 
+# Parser generators.
 %_grammar.c: %_grammar.y
 	$(PARSER) $< -q -m -T$(CODEC_DIR)/lempar.c -d$(CODEC_DIR)
 
@@ -85,39 +123,52 @@ libxxhash:
 liblmdb:
 	$(MAKE) -C $(MDB_DIR)
 
-install: all
+
+install: lib ## Install library and dependencies to $PREFIX. May require sudo.
+	cd $(MDB_DIR); PREFIX=$(PREFIX) make install
+	cd $(XXHASH_DIR); PREFIX=$(PREFIX) make install
 	mkdir -p $(DESTDIR)$(libdir)
 	mkdir -p $(DESTDIR)$(includedir)
-	cp liblsuprdf.* $(DESTDIR)$(libdir) && \
+	cp liblsuprdf.{a,so} $(DESTDIR)$(libdir) && \
 		cp include/*.h $(EXT_H) $(DESTDIR)$(includedir)
+
+
+debug_install: install debug ## Install standard and debug libraries.
+	cp liblsuprdf_dbg.{a,so} $(DESTDIR)$(libdir)
 	
-.PHONY: clean
+
+.PHONY: clean ## Clean up artifacts.
 clean:
 	rm -rf src/*.[aod] ./*[aod] src/codec/*[aod]
 
-.PHONY: deepclean
+
+.PHONY: deepclean ## Clean up artifacts in external libraries as well.
 deepclean: clean
 	cd $(MDB_DIR); make clean
 	cd $(XXHASH_DIR); make clean
 
-.PHONY: uninstall
+
+.PHONY: uninstall ## Uninstall library (not the dependencies).
 uninstall:
-	rm -f $(DESTDIR)$(libdir)/liblsuprdf.*
+	rm -f $(DESTDIR)$(libdir)/liblsuprdf*
 	rm -rf $(DESTDIR)$(includedir)
+	rm -f bin/test*
 
-bin/test: $(OBJ) $(TEST_SRC) $(DEPLIBS)
-	$(CC) \
-		$(CFLAGS) $(TEST_CFLAGS) $(SRC) $(CODEC_SRC) test.c -L. $(LDFLAGS) \
-		-o bin/test
+# For testing, use debug symbols.
+bin/test: debug $(TEST_SRC)
+	$(CC) $(CFLAGS) $(DBG_CFLAGS) -Itest $(LDFLAGS) -llsuprdf_dbg \
+		test.c -o bin/test
 
-.PHONY: debug
-debug: bin/test
-	exec gdb bin/test
 
 .PHONY: test
-test: bin/test
+test: bin/test ## Run a test suite.
 	exec bin/test
 
+
+.PHONY: gdb_test
+gdb_test: bin/test ## Run a test suite within gdb.
+	exec gdb bin/test
+
 lint:
 	splint \
 		$(INCLUDE) -Itest \
@@ -136,31 +187,31 @@ memcheck:
 	echo "Memcheck complete. Valgrind log is at /tmp/lsup_valgrind.log"
 
 
-memtest: test memcheck
+memtest: bin/test memcheck ## Run a test suite using Valgrind. Output to separate file.
 
 
-bin/profile: $(OBJ) $(DEPLIBS)
-	$(CC) \
-		$(CFLAGS) -g -DTESTING $(SRC) $(CODEC_SRC) profile.c -L. $(LDFLAGS) \
-		-o bin/profile
+# Profiling application.
+bin/profile: debug $(TEST_SRC)
+	$(CC) $(CFLAGS) -g -DTESTING $(LDFLAGS) -lliblsuprdf_dbg \
+		profile.c -o bin/profile
 
 
 .PHONY: profile
-profile: bin/profile
+profile: bin/profile ## Run a profiling session. Output can be inspected with KCachegrind.
 	exec valgrind --tool=callgrind --callgrind-out-file="$(CALLGRIND_DUMP)" \
 		bin/profile 10000 && \
 		echo "Profile dump written at $(CALLGRIND_DUMP)"
 
 
 .PHONY: pytest
-py_test:
+pytest: ## Run a test suite for the Python package.
 	pip3 install --user . && \
 	python3 test/cpython_test.py
 
 
-# Build a visual dependency graph.
+
 # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
-depgraph: src/* include/*
+depgraph: src/* include/* ## Build a visual dependency graph of the code.
 	cinclude2dot --merge=module --include=$(DEPS) \
 		--exclude='test|ext' >| $(DOCS)/dev/deps.dot
 	dot $(DOCS)/dev/deps.dot -Tpdf >| $(DOCS)/dev/deps.pdf

+ 4 - 12
README.md

@@ -89,18 +89,10 @@ of features as a standalone library:
   [Graphviz](https://graphviz.org/) for generating dependency graph (optional).
 
 
-### Build actions
-
-- `make` to build the library as static and dynamic objects.
-- `make install` to install in a default location (`/usr/local`).
-- `make test` to compile into `./bin/test` and run a test suite.
-- `make memtest` same as `make test` but run the tests using Valgrind. Output
-  Valgrind results to a logfile.
-- `python setup.py bulid` or `pip install --user .`, or similar Python build
-  methods to build and install the `lsup_rdf` Python module.
-- `make pytest` to build the Python module and run a test suite on it.
-- `make depgraph` to create a PDF showing an `#include` dependency graph for
-  development purposes. .
+### `make` commands
+
+The default `make` command compiles the library. Enter `make help` to get an
+overview of the other available commands.
 
 ### Compile-Time Constants
 

+ 0 - 1
ext/log

@@ -1 +0,0 @@
-Subproject commit f9ea34994bd58ed342d2245cd4110bb5c6790153

+ 0 - 1
ext/tpl

@@ -1 +0,0 @@
-Subproject commit f8138ad393f4b1985c916029ab6d703e4e7a1c4c