Commit 632ae061 authored by RomainFeron's avatar RomainFeron
Browse files

Better makefile

parent c8b7db83
......@@ -2,58 +2,87 @@
CC = g++
OPTCFLAGS = -O2
CFLAGS = -Wall -Wno-maybe-uninitialized -std=c++11 $(OPTCFLAGS)
LDFLAGS = -pthread -static-libstdc++ -lz
LDFLAGS = -pthread -lstdc++ -lz -L$(INCLUDE)/bwa -lbwa
# Directory organisation
BASEDIR = .
BIN = $(BASEDIR)/bin
BUILD = $(BASEDIR)/build
LIBBUILD = $(BASEDIR)/build
INCLUDE = $(BASEDIR)/include
SRC = $(BASEDIR)/src
CPP = $(wildcard $(SRC)/*.cpp) $(wildcard $(SRC)/*/*.cpp)
LIBCPP = $(wildcard $(INCLUDE)/*/*.cpp)
BWAC = $(wildcard $(INCLUDE)/bwa/*.c)
CPP = $(wildcard $(SRC)/*.cpp)
# Target
TARGET = radsex
# Get number of parallel jobs
MAKE_PID := $(shell echo $$PPID)
JOBS := $(shell ps T | sed -n 's/.*$(MAKE_PID).*$(MAKE).* \(-j\|--jobs\) *\([0-9][0-9]*\).*/\2/p')
ifeq ($(JOBS),)
JOBS := 1
endif
# Variables
# Object files inferred from cpp files
OBJS = $(addprefix $(BUILD)/, $(notdir $(CPP:.cpp=.o)))
LIBOBJS = $(LIBCPP:.cpp=.o)
BWAOBJS = $(BWAC:.c=.o)
# Rules
# Target
TARGETS = $(BIN)/radsex
# Declare phony targets (i.e. targets which are not files)
.PHONY: all clean clean-bwa clean-kfun clean-all rebuild rebuild-all
all: init BWA $(TARGET)
# Main rule
all: $(BUILD) $(BIN) $(TARGETS)
print-%: ; @echo $* = $($*)
# Build directory
$(BUILD):
if [ ! -e $@ ]; then \
mkdir $@; \
fi;
BWA:
@cd $(INCLUDE)/bwa && $(MAKE)
# Bin directory
$(BIN):
if [ ! -e $@ ]; then \
mkdir $@; \
fi;
$(TARGET): $(OBJS) $(LIBOBJS) $(BWAOBJS) $(INCLUDE)/bwa/libbwa.a
$(CC) $(CFLAGS) -I $(INCLUDE) -L $(INCLUDE)/bwa -lbwa -o $(BIN)/$(TARGET) $^ $(LDFLAGS)
# Build BWA
$(INCLUDE)/bwa/libbwa.a:
(cd $(INCLUDE)/bwa && $(MAKE) -j $(JOBS))
$(BUILD)/%.o: $(SRC)/%.cpp
$(CC) $(CFLAGS) -I $(INCLUDE) -c -o $@ $^
# Clean BWA
clean-bwa:
(cd $(INCLUDE)/bwa && $(MAKE) clean)
$(LIBBUILD)/%.o: $(INCLUDE)/*/%.cpp
# Build kfun
$(INCLUDE)/kfun/kfun.o: $(INCLUDE)/kfun/kfun.cpp
$(CC) $(CFLAGS) -I $(INCLUDE) -c -o $@ $^
# Clean BWA
clean-kfun:
(rm $(INCLUDE)/kfun/kfun.o)
# Clean RADSex files
clean:
@rm -rf $(BUILD)/*.o
@rm -rf $(BIN)/$(TARGET)
rm -rf $(BUILD)/*.o
rm -rf $(BIN)/*
# Clean all files
clean-all: clean clean-bwa clean-kfun
# Rebuild RADSex only
rebuild:
$(MAKE) clean
$(MAKE) -j $(JOBS)
clean-all: clean
@rm -rf $(INCLUDE)/*/*.o
@cd $(INCLUDE)/bwa && $(MAKE) clean
# Rebuild RADSex and dependencies
rebuild-all:
$(MAKE) clean-all
$(MAKE) -j $(JOBS)
rebuild: clean $(TARGET)
# Linking
$(BIN)/radsex: $(OBJS) $(INCLUDE)/kfun/kfun.o
$(CC) $(CFLAGS) -I $(INCLUDE) -o $(BIN)/radsex $^ $(LDFLAGS)
rebuild-all: clean-all BWA $(TARGET)
# Build a single object file. Added libs as dependency so they are built before object files
$(BUILD)/%.o: $(SRC)/%.cpp $(INCLUDE)/bwa/libbwa.a
$(CC) $(CFLAGS) -I $(INCLUDE) -c -o $@ $<
init:
@mkdir -p $(BUILD) $(BUILD)
@mkdir -p $(BIN) $(BIN)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment