From f03efd68efd04c32920723b0a6e7d560865698be Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:52:05 +0800 Subject: [PATCH] Makefile keep track of variables Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- .gitignore | 1 + Makefile | 58 +++++++++++----------------------- mk/config.mk | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 40 deletions(-) create mode 100644 mk/config.mk diff --git a/.gitignore b/.gitignore index 49f1271f6..db9ffe367 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ .vscode .idea test/bin/ +test/obj/ diff --git a/Makefile b/Makefile index 33cae997c..6b1189920 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,14 @@ # SPDX-License-Identifier: Apache-2.0 -Q ?= @ -QEMU = qemu-aarch64 -CROSS_PREFIX ?= -CC := $(CROSS_PREFIX)gcc -INCLUDE_FIPS202 = -I fips202 -INCLUDE_MLKEM = -I mlkem -INCLUDE_RANDOM = -I randombytes -INCLUDE_NISTRANDOM = -I test/nistrng -CFLAGS += -Wall -Wextra -Wpedantic -Werror -Wmissing-prototypes -Wredundant-decls \ - -Wshadow -Wpointer-arith -Wno-unknown-pragmas -O3 -fomit-frame-pointer -pedantic \ - ${INCLUDE_MLKEM} ${INCLUDE_FIPS202} $(ARCH_FLAGS) - -HOST_PLATFORM := $(shell uname -s)-$(shell uname -m) -ifeq ($(HOST_PLATFORM),Linux-x86_64) - CFLAGS += -static -endif +.PHONY: all mlkem kat nistkat clean -CYCLES ?= NO +all: mlkem bench kat nistkat -ifeq ($(CYCLES),PMU) - CFLAGS += -DPMU_CYCLES -endif +include mk/config.mk -ifeq ($(CYCLES),PERF) - CFLAGS += -DPERF_CYCLES -endif +INCLUDE_RANDOM = -I randombytes +INCLUDE_NISTRANDOM = -I test/nistrng CFLAGS_RANDOMBYTES = ${CFLAGS} ${INCLUDE_RANDOM} CFLAGS_NISTRANDOMBYTES = ${CFLAGS} ${INCLUDE_NISTRANDOM} @@ -45,11 +27,6 @@ HEADERSKECCAKRANDOM = $(HEADERSKECCAK) randombytes/randombytes.h HEADERNISTKATS = $(HEADERSKECCAK) test/nistrng/aes.h test/nistrng/randombytes.h HEADERSBENCH = $(HEADERSKECCAKRANDOM) test/hal.h - -.PHONY: all mlkem kat nistkat clean - -all: mlkem bench kat nistkat - mlkem: \ test/bin/test_kyber512 \ test/bin/test_kyber768 \ @@ -70,62 +47,62 @@ kat: \ test/bin/gen_KAT768 \ test/bin/gen_KAT1024 -test/bin/test_kyber512: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) +test/bin/test_kyber512: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/test_kyber768: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) +test/bin/test_kyber768: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/test_kyber1024: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) +test/bin/test_kyber1024: test/test_kyber.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/bench_kyber512: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) +test/bin/bench_kyber512: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESBENCH) $< -o $@ -test/bin/bench_kyber768: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) +test/bin/bench_kyber768: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESBENCH) $< -o $@ -test/bin/bench_kyber1024: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) +test/bin/bench_kyber1024: test/bench_kyber.c $(SOURCESBENCH) $(HEADERSBENCH) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESBENCH) $< -o $@ -test/bin/gen_KAT512: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) +test/bin/gen_KAT512: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=2 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/gen_KAT768: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) +test/bin/gen_KAT768: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=3 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/gen_KAT1024: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) +test/bin/gen_KAT1024: test/gen_KAT.c $(SOURCESKECCAKRANDOM) $(HEADERSKECCAKRANDOM) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_RANDOMBYTES) -DKYBER_K=4 $(SOURCESKECCAKRANDOM) $< -o $@ -test/bin/gen_NISTKAT512: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) +test/bin/gen_NISTKAT512: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=2 $(SOURCESNISTKATS) $< -o $@ -test/bin/gen_NISTKAT768: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) +test/bin/gen_NISTKAT768: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=3 $(SOURCESNISTKATS) $< -o $@ -test/bin/gen_NISTKAT1024: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) +test/bin/gen_NISTKAT1024: test/gen_NISTKAT.c $(SOURCESNISTKATS) $(HEADERNISTKATS) $(CONFIG) $(Q)echo " CC $@" $(Q)[ -d $(@D) ] || mkdir -p $(@D) $(CC) $(CFLAGS_NISTRANDOMBYTES) -DKYBER_K=4 $(SOURCESNISTKATS) $< -o $@ @@ -138,3 +115,4 @@ emulate: clean: -$(RM) -rf *.gcno *.gcda *.lcov *.o *.so -$(RM) -rf test/bin + -$(RM) -rf test/obj diff --git a/mk/config.mk b/mk/config.mk new file mode 100644 index 000000000..9d8736aa7 --- /dev/null +++ b/mk/config.mk @@ -0,0 +1,88 @@ +# SPDX-License-Identifier: Apache-2.0 +ifndef _CONFIG +_CONFIG := + +SRCDIR := $(CURDIR) + +############## +# GCC config # +############## +CROSS_PREFIX ?= +CC := $(CROSS_PREFIX)gcc +CPP := $(CROSS_PREFIX)cpp +AR := $(CROSS_PREFIX)ar +LD := $(CC) +OBJCOPY := $(CROSS_PREFIX)objcopy +SIZE := $(CROSS_PREFIX)size + +################# +# Common config # +################# +CFLAGS += \ + $(ARCH_FLAGS) \ + -Wall \ + -Wextra \ + -Wpedantic \ + -Werror \ + -Wmissing-prototypes \ + -Wredundant-decls \ + -Wshadow \ + -Wpointer-arith \ + -Wno-unknown-pragmas \ + -O3 \ + -fomit-frame-pointer \ + -pedantic \ + -I mlkem \ + -I fips202 \ + $(CPPFLAGS) + +################## +# Some Variables # +################## +Q ?= @ +QEMU = qemu-aarch64 + +HOST_PLATFORM := $(shell uname -s)-$(shell uname -m) +ifeq ($(HOST_PLATFORM),Linux-x86_64) + CFLAGS += -static +endif + +CYCLES ?= NO + +ifeq ($(CYCLES),PMU) + CFLAGS += -DPMU_CYCLES +endif + +ifeq ($(CYCLES),PERF) + CFLAGS += -DPERF_CYCLES +endif + +############################## +# Include retained variables # +############################## + +RETAINED_VARS := + +CONFIG := test/obj/.config.mk + +-include $(CONFIG) + +$(CONFIG): + @echo " GEN $@" + $(Q)[ -d $(@D) ] || mkdir -p $(@D) + @echo "# These variables are retained and can't be changed without a clean" > $@ + @$(foreach var,$(RETAINED_VARS),echo "$(var) := $($(var))" >> $@; echo "LAST_$(var) := $($(var))" >> $@;) + +RETAINED_VARS += CYCLES + +define VAR_CHECK +ifneq ($$(origin LAST_$(1)),undefined) +ifneq "$$($(1))" "$$(LAST_$(1))" +$$(info Variable $(1) changed, forcing rebuild!) +.PHONY: $(CONFIG) +endif +endif +endef + +$(foreach VAR,$(RETAINED_VARS),$(eval $(call VAR_CHECK,$(VAR)))) +endif