Skip to content

Commit

Permalink
Use nix for building libopencm3 libraries instead of adding it as git…
Browse files Browse the repository at this point in the history
… submodule (#33)

* libopencm3 nix specification

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

* build using libopencm3 nix

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

* remove libopencm3 submodule

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

* fix cannot execute script on linux

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

* parametrize libopencm3 nix with targets

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

* use stdenvNoCC instead

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

---------

Signed-off-by: Thing-han, Lim <[email protected]>
  • Loading branch information
potsrevennil authored May 6, 2024
1 parent 4e03a70 commit 18597f6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 28 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,5 +39,3 @@ clean:
$(Q)rm -rf elf/
$(Q)rm -rf bin/
$(Q)rm -rf obj/

distclean: libclean clean
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<ELF_FILE_NAME>` 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`.
Expand Down
12 changes: 10 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -45,6 +51,7 @@
];

shellHook = ''
export OPENCM3_DIR=${libopencm3}
export PATH=$PWD/scripts:$PWD/scripts/ci:$PATH
'';
};
Expand All @@ -53,6 +60,7 @@
packages = core;

shellHook = ''
export OPENCM3_DIR=${libopencm3}
export PATH=$PWD/scripts:$PWD/scripts/ci:$PATH
'';
};
Expand Down
1 change: 0 additions & 1 deletion libopencm3
Submodule libopencm3 deleted from ec5aeb
50 changes: 50 additions & 0 deletions libopencm3.nix
Original file line number Diff line number Diff line change
@@ -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/";
};
}
15 changes: 0 additions & 15 deletions mk/opencm3.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -102,9 +92,4 @@ LDFLAGS += \
-T$(LDSCRIPT) \
$(ARCH_FLAGS)

.PHONY: libclean

libclean:
$(MAKE) -C $(OPENCM3_DIR) clean

LINKDEPS += $(LDSCRIPT) $(LIBDEPS)
4 changes: 2 additions & 2 deletions scripts/ci/lint
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/format
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 18597f6

Please sign in to comment.