uthread/Makefile
2025-03-14 19:28:49 +01:00

118 lines
2.6 KiB
Makefile

build_dir?=build
src_dir?=src
tst_dir?=tst
install_dir?=install
# Comment out the bins that doesn't need to be compiled
bins+=01-main
bins+=02-switch
bins+=03-equity
bins+=11-join
bins+=12-join-main
bins+=21-create-many
bins+=22-create-many-recursive
bins+=23-create-many-once
bins+=31-switch-many
bins+=32-switch-many-join
bins+=33-switch-many-cascade
bins+=51-fibonacci
bins+=61-mutex
bins+=62-mutex
bins+=71-preemption
bins+=81-deadlock
bins_target=$(addprefix ${build_dir}/,${bins})
install_bins_targets=$(addprefix ${install_dir}/bin/,${bins})
valgrind_targets=$(addprefix valgrind_,${bins})
check_argv?=8 255
check_targets=$(addprefix check_,${bins})
src_dirs=$(sort $(dir $(wildcard ${src_dir}/**/)))
includes=$(patsubst %,-I%,${src_dirs})
CFLAGS+=-g -O0 ${includes} -fPIC
LDFLAGS+=
ifdef _USE_PTHREAD
LDFLAGS+=-lpthread
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)
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:
true
.PHONY: valgrind
valgrind: ${valgrind_targets}
.PHONY: ${valgrind_targets}
${valgrind_targets}: valgrind_%: ${build_dir}/%
valgrind $^ --leak-check=full --show-reachable=yes --track-origins=yes
.PHONY: build
build: ${bins_target} ${build_dir}/libthread.so ${build_dir}/libthread.a
.PHONY: pthreads
pthreads:
$(MAKE) _USE_PTHREAD=. ${pthread_targets}
touch .lastpthread
.PHONY: check
check: ${check_targets}
.PHONY: ${check_targets}
${check_targets}: check_%: ${build_dir}/%
$^ ${check_argv}
${bins_target}: ${build_dir}/%: ${objs} ${build_dir}/${tst_dir}/%.o
${CC} -o $@ $^ ${CFLAGS} ${LDFLAGS}
${build_dir}/libthread.so: ${objs}
${CC} -o $@ -shared $^ ${CFLAGS} ${LDFLAGS}
${build_dir}/libthread.a: ${objs}
ar rcs $@ $^
${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: clean
clean:
${RM} -r build