build_dir?=build src_dir?=src tst_dir?=tst install_dir?=install # Comment out the bins that doesn't need to be compiled bins+=$(wildcard ${tst_dir}/*.c) all_bins=01-main 02-switch 03-equity 11-join 12-join-main 21-create-many 22-create-many-recursive 23-create-many-once 31-switch-many 32-switch-many-join 33-switch-many-cascade 51-fibonacci 61-mutex 62-mutex 63-mutex-equity 64-mutex-join 71-preemption 81-deadlock bins_target=$(patsubst %.c,${build_dir}/%,${bins}) install_bins_targets=$(patsubst %.c,${install_dir}/bin/%,${bins}) valgrind_targets=$(addprefix valgrind_,${all_bins}) check_argv?=20 255 check_targets=$(addprefix check_,${all_bins}) src_dirs=$(sort $(dir $(wildcard ${src_dir}/**/))) includes=$(patsubst %,-I%,${src_dirs}) CFLAGS+=${includes} -fPIC LDFLAGS+= ifdef USE_PTHREAD LDFLAGS+=-lpthread CFLAGS+=-DUSE_PTHREAD endif ifndef USE_DEBUG CFLAGS+=-O3 -DNDEBUG -DNTRACE else CFLAGS+=-g -O0 endif # Tests if the last compilation was with pthread ifeq ("$(wildcard .lastpthread)","") all_targets:=build pthread_targets:=clean build else pthread_targets:=build all_targets:=clean build endif srcs+=$(wildcard ${src_dir}/*.c ${src_dir}/**/*.c) headers+=$(wildcard ${src_dir}/*.h ${src_dir}/**/*.h) objs+=$(patsubst %.c,${build_dir}/%.o,${srcs}) .PHONY: all all: ${all_targets} ${RM} .lastpthread # Set that the last build was without pthread .PHONY: install install: build ${install_bins_targets} ${install_dir}/lib/libthread.so ${install_dir}/lib/libthread.a ${install_dir}/lib/libthread.so: ${build_dir}/libthread.so @mkdir -p $(dir $@) cp $^ $@ ${install_dir}/lib/libthread.a: ${build_dir}/libthread.a @mkdir -p $(dir $@) cp $^ $@ ${install_bins_targets}: ${install_dir}/bin/%: ${build_dir}/% @mkdir -p $(dir $@) install $^ $@ .PHONY: graphs graphs: cd graphs && bash generate_graphs.sh .PHONY: valgrind valgrind: ${valgrind_targets} .PHONY: ${valgrind_targets} ${valgrind_targets}: valgrind_%: ${build_dir}/${tst_dir}/% valgrind $^ ${check_argv} --leak-check=full --show-reachable=yes --track-origins=yes .PHONY: build build: ${bins_target} ${build_dir}/libthread.so ${build_dir}/libthread.a .PHONY: debug debug: $(MAKE) USE_DEBUG=. build .PHONY: pthreads pthreads: $(MAKE) USE_PTHREAD=. ${pthread_targets} touch .lastpthread .PHONY: check check: ${check_targets} .PHONY: ${check_targets} ${check_targets}: check_%: ${build_dir}/${tst_dir}/% $^ ${check_argv} ${bins_target}: ${build_dir}/%: ${objs} ${build_dir}/%.o ${CC} -o $@ $^ ${CFLAGS} ./lib/libmimalloc ${LDFLAGS} ${build_dir}/51-fibonacci: ${build_dir}/src/thread/thread_fibo.o ${build_dir}/${tst_dir}/51-fibonacci.o ${CC} -o $@ $^ ${CFLAGS} ./lib/libmimalloc ${LDFLAGS} ${build_dir}/libthread.so: ${objs} ${CC} -o $@ -shared $^ ${CFLAGS} ${LDFLAGS} ${build_dir}/libthread.a: ${objs} ar rcs $@ $^ ${build_dir}/%_fibo.o: %.c @mkdir -p $(dir $@) ${CC} -o $@ -c $^ -DFIBO_STRAT ${CFLAGS} ${build_dir}/%.o: %.c @mkdir -p $(dir $@) ${CC} -o $@ -c $^ ${CFLAGS} .PHONY: compile_flags.txt compile_flags.txt: (echo "${CFLAGS} ${LDFLAGS}" | sed 's/ /\n/g') > compile_flags.txt .PHONY: format format: clang-format -i ${srcs} ${headers} .PHONY: check_format check_format: clang-format --dry-run --Werror ${srcs} ${headers} .PHONY: clean clean: ${RM} -r build install