Skip to content

Commit

Permalink
Refactor makefile with libopencm3 module gcc and configuration depend…
Browse files Browse the repository at this point in the history
…encies (#35)

* libopencm3 nix cleanup

Signed-off-by: Thing-han, Lim <[email protected]>

* refactor with libopencm3 module gcc

Signed-off-by: Thing-han, Lim <[email protected]>

* refactor common gcc config for different platform

Signed-off-by: Thing-han, Lim <[email protected]>

* refactor RNG makefile config

Signed-off-by: Thing-han, Lim <[email protected]>

* add makefile config dependency

Signed-off-by: Thing-han, Lim <[email protected]>

* merge ci tests since there is no need for make clean between different configuration

Signed-off-by: Thing-han, Lim <[email protected]>

* move opencm3 configs inside mk/opencm3.mk

Signed-off-by: Thing-han, Lim <[email protected]>

* remove obj/libhal.a whenever dependencies changed

Signed-off-by: Thing-han, Lim <[email protected]>

* add platform as test script argument

Signed-off-by: Thing-han, Lim <[email protected]>

* add shell completion for the tests script

Signed-off-by: Thing-han, Lim <[email protected]>

* update ci

Signed-off-by: Thing-han, Lim <[email protected]>

* fix make nistkat in tests script

Signed-off-by: Thing-han, Lim <[email protected]>

---------

Signed-off-by: Thing-han, Lim <[email protected]>
  • Loading branch information
potsrevennil authored May 13, 2024
1 parent 18597f6 commit 48d5029
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 195 deletions.
22 changes: 6 additions & 16 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
script: |
astyle --version
arm-none-eabi-gcc --version
qemu-system-arm --version
- name: Lint
shell: nix develop .#ci -c bash -e {0}
run: |
Expand All @@ -25,17 +26,7 @@ jobs:
shell: nix develop .#ci -c bash -e {0}
run: |
make
emulate_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup nix
uses: ./.github/actions/setup-nix
with:
script: |
arm-none-eabi-gcc --version
qemu-system-arm --version
make RNG=NISTKAT nistkat
- name: Build for emulation on QEMU
shell: nix develop .#ci -c bash -e {0}
run: |
Expand All @@ -47,15 +38,15 @@ jobs:
id: func_test
shell: nix develop .#ci -c bash -e {0}
run: |
tests func -e -v
tests func -e -v stm32f4discovery
- name: Speed test
id: speed_test
shell: nix develop .#ci -c bash -e {0}
if: |
success()
|| steps.func_test.conclusion == 'failure'
run: |
tests speed -e -v
tests speed -e -v stm32f4discovery
- name: Stack test
id: stack_test
shell: nix develop .#ci -c bash -e {0}
Expand All @@ -64,7 +55,7 @@ jobs:
|| steps.func_test.conclusion == 'failure'
|| steps.speed_test.conclusion == 'failure'
run: |
tests stack -e -v
tests stack -e -v stm32f4discovery
- name: Nistkat test
shell: nix develop .#ci -c bash -e {0}
if: |
Expand All @@ -73,5 +64,4 @@ jobs:
|| steps.speed_test.conclusion == 'failure'
|| steps.stack_test.conclusion == 'failure'
run: |
make clean
tests nistkat -e -v
tests nistkat -e -v stm32f4discovery
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ nistkat: $(foreach scheme,$(KEM_SCHEMES),$(scheme)-nistkat)

emulate%: PLATFORM = mps2-an386
emulate%: NTESTS = 10
emulate%: override RNG = NOTRAND

emulate\ nistkat: KATRNG=NIST
emulate\ nistkat:
$(Q)$(MAKE) PLATFORM=$(PLATFORM) NTESTS=$(NTESTS) KATRNG=$(KATRNG) nistkat
emulate\ nistkat: override RNG = NISTKAT

# emulate for test, speed, stack
emulate%:
$(Q)$(MAKE) PLATFORM=$(PLATFORM) NTESTS=$(NTESTS) $*
$(Q)$(MAKE) PLATFORM=$(PLATFORM) NTESTS=$(NTESTS) RNG=$(RNG) $*

emulate\ run: PLATFORM=mps2-an386
emulate\ run:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ For example,
- `make [PLATFORM=<PLATFORM_NAME>] test` assembles all binaries for functional tests.
- `make [PLATFORM=<PLATFORM_NAME>] speed` assembles all binaries for speed benchmarking
- `make [PLATFORM=<PLATFORM_NAME>] stack` assembles all binaries for stack benchmarking
- `make [PLATFORM=<PLATFORM_NAME>] KATRNG=NIST nistkat` assembles all binaries for nistkat
- `make [PLATFORM=<PLATFORM_NAME>] RNG=NISTKAT nistkat` assembles all binaries for nistkat
- `make [PLATFORM=<PLATFORM_NAME>] (all)` assembles all the above targets for all parameter sets.

- `make emulate` build binaries for emulating `mps2-an386` on `QEMU` of test, speed, stack
Expand Down
3 changes: 1 addition & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
qemu # 8.1.5
libopencm3


yq
python311Packages.pyserial # 3.5
python311Packages.click
];
in
{
packages.default = libopencm3;
devShells.default = with pkgs; mkShellNoCC {
packages = core ++ [
direnv
Expand All @@ -53,6 +51,7 @@
shellHook = ''
export OPENCM3_DIR=${libopencm3}
export PATH=$PWD/scripts:$PWD/scripts/ci:$PATH
eval "$(_TESTS_COMPLETE=source tests)"
'';
};

Expand Down
75 changes: 74 additions & 1 deletion mk/config.mk
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
# SPDX-License-Identifier: Apache-2.0
ifndef _CONFIG
_CONFIG :=

SRCDIR := $(CURDIR)

# GCC config
CROSS_PREFIX ?= arm-none-eabi
CC := $(CROSS_PREFIX)-gcc
CPP := $(CROSS_PREFIX)-cpp
AR := $(CROSS_PREFIX)-ar
LD := $(CC)
OBJCOPY := $(CROSS_PREFIX)-objcopy
SIZE := $(CROSS_PREFIX)-size

##############################
# Include retained variables #
##############################

RETAINED_VARS :=

CONFIG := 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))" >> $@;)

###############
# Some Macros #
###############
objs = $(addprefix obj/,$(addsuffix .o,$(1)))

PLATFORM ?= stm32f4discovery
KATRNG ?=
RNG ?= HAL

RETAINED_VARS += PLATFORM RNG

# RNG config
ifeq ($(RNG),HAL)
LIBHAL_SRC = hal/randombytes.c
else ifeq ($(RNG),NOTRAND)
LIBHAL_SRC = hal/notrandombytes.c
else ifeq ($(RNG),NISTKAT)
LIBHAL_SRC = \
test/common/nistkatrng.c \
test/common/aes.c
CPPFLAGS += -Itest/common
endif

LDLIBS += -lhal
LIBDEPS += obj/libhal.a

# HAL config

# Common config
include mk/$(PLATFORM).mk

CFLAGS += \
Expand All @@ -21,11 +69,36 @@ CFLAGS += \
$(CPPFLAGS)

LDFLAGS += \
--specs=nosys.specs \
-Wl,--wrap=_sbrk \
-Wl,--wrap=_open \
-Wl,--wrap=_close \
-Wl,--wrap=_isatty \
-Wl,--wrap=_kill \
-Wl,--wrap=_lseek \
-Wl,--wrap=_read \
-Wl,--wrap=_write \
-Wl,--wrap=_fstat \
-Wl,--wrap=_getpid \
-ffreestanding \
-Lobj \
-Wl,--gc-sections

NTESTS ?= 1
RETAINED_VARS += NTESTS

KEM_SCHEMES=mlkem512 mlkem768 mlkem1024
# mlkem k
KEM_PARAMS=2 3 4

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
47 changes: 6 additions & 41 deletions mk/mps2-an386.mk
Original file line number Diff line number Diff line change
@@ -1,70 +1,35 @@
# SPDX-License-Identifier: Apache-2.0
MPS2_DATA_IN_FLASH = 1
CROSS_PREFIX ?= arm-none-eabi
CC := $(CROSS_PREFIX)-gcc
CPP := $(CROSS_PREFIX)-cpp
AR := $(CROSS_PREFIX)-ar
LD := $(CC)
OBJCOPY := $(CROSS_PREFIX)-objcopy
SIZE := $(CROSS_PREFIX)-size

LDSCRIPT = obj/ldscript.ld
LDSCRIPT = obj/generated.$(PLATFORM).ld

ARCH_FLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16

CFLAGS += \
$(ARCH_FLAGS) \
--specs=nosys.specs
CFLAGS += $(ARCH_FLAGS)

CPPFLAGS += \
-DMPS2_AN386

LDFLAGS += \
--specs=nosys.specs \
-Wl,--wrap=_sbrk \
-Wl,--wrap=_open \
-Wl,--wrap=_close \
-Wl,--wrap=_isatty \
-Wl,--wrap=_kill \
-Wl,--wrap=_lseek \
-Wl,--wrap=_read \
-Wl,--wrap=_write \
-Wl,--wrap=_fstat \
-Wl,--wrap=_getpid \
-Wl,--no-warn-rwx-segments \
-ffreestanding \
-T$(LDSCRIPT) \
$(ARCH_FLAGS)

LIBHAL_SRC := \
LIBHAL_SRC += \
hal/mps2/startup_MPS2.S \
hal/hal-mps2.c

ifndef KATRNG
LIBHAL_SRC += hal/notrandombytes.c
else
ifeq ($(KATRNG),NIST)
LIBHAL_SRC += \
test/common/nistkatrng.c \
test/common/aes.c
CPPFLAGS += -Itest/common
endif
endif

obj/libpqm4hal.a: $(call objs,$(LIBHAL_SRC))
obj/libpqm4hal.a: CPPFLAGS += -Ihal/mps2 $(if $(KATRNG)==NIST,-Itest/common)
obj/libhal.a: $(call objs,$(LIBHAL_SRC))
obj/libhal.a: CPPFLAGS += -Ihal/mps2 $(if $(RNG)==NISTKAT,-Itest/common)

$(LDSCRIPT): CPPFLAGS += $(if $(MPS2_DATA_IN_FLASH),-DDATA_IN_FLASH)
obj/hal/mps2/startup_MPS2.S.o: CPPFLAGS += $(if $(MPS2_DATA_IN_FLASH),-DDATA_IN_FLASH)

LDLIBS += -lpqm4hal
LIBDEPS += obj/libpqm4hal.a

$(LDSCRIPT): hal/mps2/MPS2.ld
@printf " GENLNK $@\n"; \
[ -d $(@D) ] || $(Q)mkdir -p $(@D); \
$(CC) -x assembler-with-cpp -E -Wp,-P $(CPPFLAGS) $< -o $@

$(LDSCRIPT): CPPFLAGS += -Ihal/mps2 $(if $(KATRNG)==NIST,-Itest/common)
$(LDSCRIPT): CPPFLAGS += -Ihal/mps2 $(if $(RNG)==NISTKAT,-Itest/common)

LINKDEPS += $(LDSCRIPT) $(LIBDEPS)
91 changes: 8 additions & 83 deletions mk/opencm3.mk
Original file line number Diff line number Diff line change
@@ -1,94 +1,19 @@
# SPDX-License-Identifier: Apache-2.0
LIBHAL_SRC := hal/hal-opencm3.c
OPENCM3_DIR ?=
override LDSCRIPT := obj/generated.$(DEVICE).ld

ifndef KATRNG
LIBHAL_SRC += hal/randombytes.c
else
ifeq ($(KATRNG),NIST)
LIBHAL_SRC += \
test/common/nistkatrng.c \
test/common/aes.c
endif
endif
include $(OPENCM3_DIR)/mk/genlink-config.mk
include $(OPENCM3_DIR)/mk/genlink-rules.mk
include $(OPENCM3_DIR)/mk/gcc-rules.mk

obj/libpqm4hal.a: $(call objs,$(LIBHAL_SRC))
LIBHAL_SRC += hal/hal-opencm3.c

LDLIBS += -lpqm4hal
LIBDEPS += obj/libpqm4hal.a
obj/libhal.a: $(call objs,$(LIBHAL_SRC))

LDLIBS += -lc -lgcc

ifeq ($(DEVICE),)
$(warning no DEVICE specified for linker script generator)
endif

DEVICES_DATA ?= hal/devices.data

genlink_family :=$(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) FAMILY)
genlink_subfamily :=$(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) SUBFAMILY)
genlink_cpu :=$(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) CPU)
genlink_fpu :=$(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) FPU)
genlink_cppflags :=$(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) CPPFLAGS)

ifeq ($(genlink_family),)
$(warning $(DEVICE) not found in $(DEVICES_DATA))
endif

CPPFLAGS += $(genlink_cppflags)

ARCH_FLAGS :=-mcpu=$(genlink_cpu)
ifeq ($(genlink_cpu),$(filter $(genlink_cpu),cortex-m0 cortex-m0plus cortex-m3 cortex-m4 cortex-m7))
ARCH_FLAGS +=-mthumb
endif

ifeq ($(genlink_fpu),soft)
ARCH_FLAGS += -msoft-float
else ifeq ($(genlink_fpu),hard-fpv4-sp-d16)
ARCH_FLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
else ifeq ($(genlink_fpu),hard-fpv5-sp-d16)
ARCH_FLAGS += -mfloat-abi=hard -mfpu=fpv5-sp-d16
else
$(warning No match for the FPU flags)
endif

LIBNAME = opencm3_$(genlink_family)

LDLIBS += -l$(LIBNAME)

LDFLAGS += -L$(OPENCM3_DIR)/lib
CPPFLAGS += -I$(OPENCM3_DIR)/include $(if $(KATRNG)==NIST,-Itest/common)

LDSCRIPT = obj/generated.$(DEVICE).ld
$(LDSCRIPT): $(OPENCM3_DIR)/ld/linker.ld.S $(OPENCM3_DIR)/ld/devices.data $(CONFIG)
@printf " GENLNK $(DEVICE)\n"
$(Q)mkdir -p $(@D)
$(Q)$(CPP) $(ARCH_FLAGS) $(shell $(OPENCM3_DIR)/scripts/genlink.py $(DEVICES_DATA) $(DEVICE) DEFS) -P -E $< -o $@

CROSS_PREFIX ?= arm-none-eabi
CC := $(CROSS_PREFIX)-gcc
CPP := $(CROSS_PREFIX)-cpp
AR := $(CROSS_PREFIX)-gcc-ar
LD := $(CC)
OBJCOPY := $(CROSS_PREFIX)-objcopy
SIZE := $(CROSS_PREFIX)-size

CFLAGS += \
$(ARCH_FLAGS) \
CFLAGS += $(ARCH_FLAGS)

LDFLAGS += \
--specs=nosys.specs \
-Wl,--wrap=_sbrk \
-Wl,--wrap=_close \
-Wl,--wrap=_isatty \
-Wl,--wrap=_kill \
-Wl,--wrap=_lseek \
-Wl,--wrap=_read \
-Wl,--wrap=_write \
-Wl,--wrap=_fstat \
-Wl,--wrap=_getpid \
-Wl,--wrap=_open \
-nostartfiles \
-ffreestanding \
-T$(LDSCRIPT) \
$(ARCH_FLAGS)

Expand Down
Loading

0 comments on commit 48d5029

Please sign in to comment.