First attempt at makefile cleanup make-cleanup
authorDamiano Boppart <damiano.boppart@pep.security>
Sat, 15 Dec 2018 17:48:57 +0100
branchmake-cleanup
changeset 85c318ce6c21ed
parent 83 3a04c442363f
child 86 8773b3febc6f
child 90 886b3d76f403
First attempt at makefile cleanup
Makefile
Makefile.conf
     1.1 --- a/Makefile	Thu Nov 08 12:20:13 2018 +0100
     1.2 +++ b/Makefile	Sat Dec 15 17:48:57 2018 +0100
     1.3 @@ -1,49 +1,77 @@
     1.4 -# this file is under GNU GPL 3.0, see LICENSE.txt
     1.5 +# Copyright 2018, pEp Foundation
     1.6 +# This file is part of lib pEp Adapter
     1.7 +# This file may be used under the terms of the GNU General Public License version 3
     1.8  # see LICENSE.txt
     1.9  
    1.10  include Makefile.conf
    1.11 --include local.conf
    1.12  
    1.13 -CXXFLAGS += -I$(HOME)/include -std=c++14 -O0 -g
    1.14 +ifneq ($(wildcard local.conf),)
    1.15 +    $(info ================================================)
    1.16 +    $(info Overrides in \`local.conf\` are used.)
    1.17 +    $(info ================================================)
    1.18 +endif
    1.19 +
    1.20 +ifdef BUILD_CONFIG
    1.21 +    $(info ================================================)
    1.22 +    $(info Overrides in \`$(BUILD_CONFIG)\` are used.)
    1.23 +    $(info ================================================)
    1.24 +endif
    1.25  
    1.26  SOURCE=$(wildcard *.cc)
    1.27  HEADERS=$(wildcard *.hh *.hxx)
    1.28  OBJECTS=$(subst .cc,.o,$(SOURCE))
    1.29  WITHOUT_TESTS=$(patsubst test%.o,,$(OBJECTS))
    1.30 +TARGET=libpEpAdapter.a
    1.31  
    1.32 +EXTRA_LIB_PATHS=.:
    1.33 +ifdef ENGINE_LIB
    1.34 +	EXTRA_LIB_PATHS:=$(EXTRA_LIB_PATHS)$(patsubst -L%,%,$(ENGINE_LIB)):
    1.35 +endif
    1.36 +
    1.37 +# Remove trailing ':'
    1.38 +EXTRA_LIB_PATHS:=$(EXTRA_LIB_PATHS::=)
    1.39 +
    1.40 +ifeq ($(BUILD_FOR),Darwin)
    1.41 +    LIBPATH=DYLD_LIBRARY_PATH
    1.42 +else
    1.43 +    LIBPATH=LD_LIBRARY_PATH
    1.44 +endif
    1.45 +
    1.46 +$(TEST_CMD_PFX)=$(LIBPATH)=$(EXTRA_LIB_PATHS)
    1.47 +
    1.48 +
    1.49 +.PHONY: all
    1.50  all: $(TARGET)
    1.51  
    1.52 +# Rule copied from make's built-in rules
    1.53  %.o: %.cc %.hh
    1.54 -	$(CXX) $(CXXFLAGS) -c $<
    1.55 +	$(COMPILE.cc) $(OUTPUT_OPTION) $<
    1.56  
    1.57  $(TARGET): $(WITHOUT_TESTS)
    1.58  	ar -rc $@ $^
    1.59  
    1.60 -.PHONY: clean distclean test install uninstall
    1.61 +.PHONY: clean
    1.62 +clean:
    1.63 +	rm -f $(TARGET) $(OBJECTS) *.a test_adapter test_library lib
    1.64  
    1.65 -clean:
    1.66 -	rm -f $(TARGET) $(OBJECTS) *.a test_adapter lib
    1.67 -
    1.68 +.PHONY: distclean
    1.69  distclean: clean
    1.70  	rm -Rf .gnupg .pEp_management*
    1.71  
    1.72 +# $$(pwd) will return the incorrect directory, if make is run with `make -C ...`.
    1.73 +.PHONY: test
    1.74  test: test_adapter test_library
    1.75 -ifeq ($(HOME),$(PREFIX))
    1.76 -	-ln -fs $$HOME/lib
    1.77 -endif
    1.78 -	HOME=$$(pwd) ./test_adapter
    1.79 +	 $(TEST_CMD_PFX) HOME=$$(pwd) ./test_adapter
    1.80  
    1.81 -test_adapter: test_adapter.o $(TARGET)
    1.82 -	$(CXX) -o $@ -L$(PEP)/lib -lpEpEngine -L. -lpEpAdapter $<
    1.83 +test_%: test_%.o $(TARGET)
    1.84  
    1.85 -test_library: test_library.o $(TARGET)
    1.86 -	$(CXX) -o $@ -L$(PEP)/lib -lpEpEngine -L. -lpEpAdapter $<
    1.87 +.PHONY: install
    1.88 +install: $(TARGET)
    1.89 +	-mkdir -p $(PREFIX)/include/pEp
    1.90 +	cp $(HEADERS) $(PREFIX)/include/pEp/
    1.91 +	cp $(TARGET) $(PREFIX)/lib/
    1.92  
    1.93 -install: $(TARGET)
    1.94 -	-mkdir -p $(PEP)/include/pEp
    1.95 -	cp $(HEADERS) $(PEP)/include/pEp/
    1.96 -	cp $(TARGET) $(PEP)/lib/
    1.97 -
    1.98 +.PHONY: uninstall
    1.99  uninstall:
   1.100 -	cd $(PEP)/include && rm -f $(HEADERS)
   1.101 -	cd $(PEP)/lib && rm -f $(TARGET)
   1.102 +	cd $(PREFIX)/include/pEp && rm -f $(HEADERS)
   1.103 +	cd $(PREFIX)/lib && rm -f $(TARGET)
     2.1 --- a/Makefile.conf	Thu Nov 08 12:20:13 2018 +0100
     2.2 +++ b/Makefile.conf	Sat Dec 15 17:48:57 2018 +0100
     2.3 @@ -1,7 +1,49 @@
     2.4 -# this file is under GNU GPL 3.0, see LICENSE.txt
     2.5 +# Copyright 2018, pEp Foundation
     2.6 +# This file is part of lib pEp Adapter
     2.7 +# This file may be used under the terms of the GNU General Public License version 3
     2.8  # see LICENSE.txt
     2.9  
    2.10 -TARGET=libpEpAdapter.a
    2.11 +######### Header #########
    2.12 +HERE:=$(dir $(lastword $(MAKEFILE_LIST)))
    2.13 +
    2.14 +
    2.15 +######### General #########
    2.16 +BUILD_FOR:=$(shell uname)
    2.17 +
    2.18  PREFIX=$(HOME)
    2.19 -PEP=$(HOME)
    2.20  
    2.21 +
    2.22 +######### C and C++ #########
    2.23 +LDFLAGS=$(ENGINE_LIB)
    2.24 +
    2.25 +LDLIBS=-lstdc++ -lpEpEngine -lpEpAdapter
    2.26 +
    2.27 +
    2.28 +######### C++ #########
    2.29 +ifeq ($(BUILD_FOR),Linux)
    2.30 +    CXX=g++ -std=c++14
    2.31 +else ifeq ($(BUILD_FOR),Darwin)
    2.32 +    CXX=clang -std=c++14
    2.33 +endif
    2.34 +
    2.35 +ifeq ($(BUILD_FOR),Linux)
    2.36 +    CXXFLAGS=-fdiagnostics-color=always -O0 -g $(ENGINE_INC)
    2.37 +else ifeq ($(BUILD_FOR),Darwin)
    2.38 +    CXXFLAGS=-fcolor-diagnostics -O0 -g $(ENGINE_INC)
    2.39 +endif
    2.40 +
    2.41 +
    2.42 +######### Engine #########
    2.43 +ENGINE_LIB=-L$(HOME)/code/engine/build/lib
    2.44 +#ENGINE_LIB=-L$(HOME)/local/lib
    2.45 +
    2.46 +ENGINE_INC=-I$(HOME)/code/engine/build/include
    2.47 +#ENGINE_INC=-I$(HOME)/local/inc
    2.48 +
    2.49 +
    2.50 +######### Footer #########
    2.51 +-include $(HERE)/local.conf
    2.52 +
    2.53 +ifdef BUILD_CONFIG
    2.54 +    include $(BUILD_CONFIG)
    2.55 +endif