From 18597f6930a0c31cba0f279b9833cb1e5f4a3060 Mon Sep 17 00:00:00 2001 From: "Lim, Thing-han" <15379156+potsrevennil@users.noreply.github.com> Date: Mon, 6 May 2024 14:56:10 +0800 Subject: [PATCH] Use nix for building libopencm3 libraries instead of adding it as git submodule (#33) * libopencm3 nix specification Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> * build using libopencm3 nix Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> * remove libopencm3 submodule Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> * fix cannot execute script on linux Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> * parametrize libopencm3 nix with targets Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> * use stdenvNoCC instead Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --------- Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- .gitmodules | 3 --- Makefile | 4 +--- README.md | 1 - flake.nix | 12 ++++++++++-- libopencm3 | 1 - libopencm3.nix | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ mk/opencm3.mk | 15 --------------- scripts/ci/lint | 4 ++-- scripts/format | 2 +- 9 files changed, 64 insertions(+), 28 deletions(-) delete mode 100644 .gitmodules delete mode 160000 libopencm3 create mode 100644 libopencm3.nix diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index c4a0df1..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "libopencm3"] - path = libopencm3 - url = https://github.com/libopencm3/libopencm3.git diff --git a/Makefile b/Makefile index 4e8e813..0da1a26 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ speed: $(foreach scheme,$(KEM_SCHEMES),$(scheme)-speed) stack: $(foreach scheme,$(KEM_SCHEMES),$(scheme)-stack) nistkat: $(foreach scheme,$(KEM_SCHEMES),$(scheme)-nistkat) -.PHONY: emulate clean libclean +.PHONY: emulate clean emulate%: PLATFORM = mps2-an386 emulate%: NTESTS = 10 @@ -39,5 +39,3 @@ clean: $(Q)rm -rf elf/ $(Q)rm -rf bin/ $(Q)rm -rf obj/ - -distclean: libclean clean diff --git a/README.md b/README.md index 5f1227a..cbed9c9 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,6 @@ For example, - `make "emulate [test|speed|stack|nistkat]"` build binaries of test, speed, stack or nistkat for emulating `mps2-an386` on `QEMU` - `make "emulate run" ELF_FILE=` run emulatation for the file on `QEMU` - `make clean` cleans up intermediate artifacts -- `make distclean` additionally cleanup the `libopencm3` library ### Manual testing on board After generating the specified hex files, you can flash it to the development board using `openocd`. diff --git a/flake.nix b/flake.nix index 7ee8784..5a0ac57 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,9 @@ systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ]; perSystem = { pkgs, ... }: let + libopencm3 = pkgs.callPackage ./libopencm3.nix { + targets = [ "stm32/f4" ]; + }; core = with pkgs; [ # formatter & linters nixpkgs-fmt @@ -26,15 +29,18 @@ # build dependencies gcc-arm-embedded-13 # arm-gnu-toolchain-13.2.rel1 + python311 qemu # 8.1.5 - yq + libopencm3 - python311 + + yq python311Packages.pyserial # 3.5 python311Packages.click ]; in { + packages.default = libopencm3; devShells.default = with pkgs; mkShellNoCC { packages = core ++ [ direnv @@ -45,6 +51,7 @@ ]; shellHook = '' + export OPENCM3_DIR=${libopencm3} export PATH=$PWD/scripts:$PWD/scripts/ci:$PATH ''; }; @@ -53,6 +60,7 @@ packages = core; shellHook = '' + export OPENCM3_DIR=${libopencm3} export PATH=$PWD/scripts:$PWD/scripts/ci:$PATH ''; }; diff --git a/libopencm3 b/libopencm3 deleted file mode 160000 index ec5aeba..0000000 --- a/libopencm3 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ec5aeba354ec273782e4441440fe9000b1c965e3 diff --git a/libopencm3.nix b/libopencm3.nix new file mode 100644 index 0000000..3e0d4bc --- /dev/null +++ b/libopencm3.nix @@ -0,0 +1,50 @@ +# SPDX-License-Identifier: Apache-2.0 + +{ lib +, stdenvNoCC +, fetchFromGitHub +, python311 +, gcc-arm-embedded-13 +, targets ? [ ] +}: + +stdenvNoCC.mkDerivation rec { + pname = "libopencm3"; + version = "ec5aeba354ec273782e4441440fe9000b1c965e3"; + src = fetchFromGitHub { + owner = "libopencm3"; + repo = pname; + rev = "ec5aeba354ec273782e4441440fe9000b1c965e3"; + sha256 = "sha256-bgoMhOhBJZwPTa9gUH0vPSGZknDrb2mJZuFlCWNivYU="; + }; + buildInputs = [ + python311 + gcc-arm-embedded-13 # arm-gnu-toolchain-13.2.rel1 + ]; + postPatch = '' + patchShebangs --build scripts/irq2nvic_h + ''; + dontConfigure = true; + buildPhase = '' + make ${if targets == [] then "lib" else "TARGETS=${lib.concatStrings targets}"} + ''; + installPhase = '' + runHook preInstall + + mkdir -p $out + cp -r include/ $out/ + cp -r ld/ $out/ + cp -r lib/ $out/ + cp -r mk/ $out/ + cp -r scripts/ $out/ + + runHook postInstall + ''; + dontStrip = true; + noAuditTmpdir = true; + + meta = with lib; { + description = "Open source ARM Cortex-M microcontroller library"; + homepage = "https://libopencm3.org/"; + }; +} diff --git a/mk/opencm3.mk b/mk/opencm3.mk index 6fd9c9a..1585e3e 100644 --- a/mk/opencm3.mk +++ b/mk/opencm3.mk @@ -18,10 +18,6 @@ LIBDEPS += obj/libpqm4hal.a LDLIBS += -lc -lgcc -OPENCM3_DIR = $(CURDIR)/libopencm3 - -_git_submodule_update_opencm3 := $(shell git submodule update --init --recursive $(OPENCM3_DIR)) - ifeq ($(DEVICE),) $(warning no DEVICE specified for linker script generator) endif @@ -58,16 +54,10 @@ endif LIBNAME = opencm3_$(genlink_family) LDLIBS += -l$(LIBNAME) -LIBDEPS += $(OPENCM3_DIR)/lib/lib$(LIBNAME).a LDFLAGS += -L$(OPENCM3_DIR)/lib CPPFLAGS += -I$(OPENCM3_DIR)/include $(if $(KATRNG)==NIST,-Itest/common) -$(OPENCM3_DIR)/lib/lib$(LIBNAME).a: - $(MAKE) -C $(OPENCM3_DIR) $(OPENCM3_TARGET) - -obj/hal/hal-opencm3.c.o: $(OPENCM3_DIR)/lib/lib$(LIBNAME).a - LDSCRIPT = obj/generated.$(DEVICE).ld $(LDSCRIPT): $(OPENCM3_DIR)/ld/linker.ld.S $(OPENCM3_DIR)/ld/devices.data $(CONFIG) @printf " GENLNK $(DEVICE)\n" @@ -102,9 +92,4 @@ LDFLAGS += \ -T$(LDSCRIPT) \ $(ARCH_FLAGS) -.PHONY: libclean - -libclean: - $(MAKE) -C $(OPENCM3_DIR) clean - LINKDEPS += $(LDSCRIPT) $(LIBDEPS) diff --git a/scripts/ci/lint b/scripts/ci/lint index 904dddf..ff43932 100755 --- a/scripts/ci/lint +++ b/scripts/ci/lint @@ -41,7 +41,7 @@ echo "::endgroup::" check-eol-dry-run() { - for file in $(git ls-files -- ":/" ":/!:libopencm3"); do + for file in $(git ls-files -- ":/"); do if [[ $(tail -c1 "$file" | wc -l) == 0 ]]; then l=$(wc -l <"$file") echo "$file $l" @@ -54,7 +54,7 @@ echo "::endgroup::" check-spdx() { - for file in $(git ls-files -- ":/" ":/!:libopencm3" ":/!:*LICENSE*" ":/!:.git*" ":/!:flake.lock"); do + for file in $(git ls-files -- ":/" ":/!:*LICENSE*" ":/!:.git*" ":/!:flake.lock"); do if [[ $(grep "SPDX-License-Identifier:" $file | wc -l) == 0 ]]; then echo "$file is missing SPDX License header" SUCCESS=false diff --git a/scripts/format b/scripts/format index 1125121..1d9478c 100755 --- a/scripts/format +++ b/scripts/format @@ -30,7 +30,7 @@ astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --formatted | info "Checking for eol" check-eol() { - for file in $(git ls-files -- ":/" ":/!:libopencm3"); do + for file in $(git ls-files -- ":/"); do if [[ $(tail -c1 "$file" | wc -l) == 0 ]]; then echo "" >>"$file" echo "$file"