Skip to content

Commit

Permalink
Makefile keep track of variables (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Thing-han, Lim <[email protected]>
  • Loading branch information
potsrevennil authored Jun 26, 2024
1 parent 6c0791e commit ac842f8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.vscode
.idea
test/bin/
test/obj/
58 changes: 18 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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 \
Expand All @@ -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 $@
Expand All @@ -138,3 +115,4 @@ emulate:
clean:
-$(RM) -rf *.gcno *.gcda *.lcov *.o *.so
-$(RM) -rf test/bin
-$(RM) -rf test/obj
88 changes: 88 additions & 0 deletions mk/config.mk
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ac842f8

Please sign in to comment.