From bdd2cb7b9a1d9d1e34e1c838311a57a9ea552516 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 10 Oct 2021 21:48:10 +0200 Subject: Extract basic Teensy image builder --- Makefile.default | 17 ++++++++++++++ build.nix | 30 ++++++++++++++++++++++++ flake.nix | 13 +++++++++-- test.nix | 2 +- test/Makefile | 17 -------------- ulisp.nix | 71 ++++++++++++++++---------------------------------------- 6 files changed, 79 insertions(+), 71 deletions(-) create mode 100644 Makefile.default create mode 100644 build.nix delete mode 100644 test/Makefile diff --git a/Makefile.default b/Makefile.default new file mode 100644 index 0000000..3bf5a70 --- /dev/null +++ b/Makefile.default @@ -0,0 +1,17 @@ +include $(TEENSY_PATH)/include/flags.mk + +CXXFLAGS += -std=gnu++20 + +LIBS = -lm -lstdc++ -lteensy-core + +CPP_FILES := $(wildcard *.cpp) +TARGETS := $(CPP_FILES:.cpp=.hex) + +%.elf: %.o + $(CC) $(LDFLAGS) -o $@ $< $(LIBS) + +%.hex: %.elf + $(SIZE) $< + $(OBJCOPY) -O ihex -R .eeprom $< $@ + +all: $(TARGETS) diff --git a/build.nix b/build.nix new file mode 100644 index 0000000..3770d57 --- /dev/null +++ b/build.nix @@ -0,0 +1,30 @@ +{ pkgs, teensy-core, ... }: + +{ + build = name: source: pkgs.stdenv.mkDerivation rec { + inherit name; + + src = source; + + buildInputs = with pkgs; [ + gcc-arm-embedded + teensy-core + ]; + + buildPhase = '' + export CC=arm-none-eabi-gcc + export CXX=arm-none-eabi-g++ + export OBJCOPY=arm-none-eabi-objcopy + export SIZE=arm-none-eabi-size + + cp ${./Makefile.default} Makefile + export TEENSY_PATH=${teensy-core} + make + ''; + + installPhase = '' + mkdir $out + cp *.hex $out/ + ''; + }; +} diff --git a/flake.nix b/flake.nix index 560b5fa..43e8824 100644 --- a/flake.nix +++ b/flake.nix @@ -10,8 +10,17 @@ pkgs = import nixpkgs { inherit system; }; teensy-core = import ./core.nix { inherit pkgs; }; - teensy-test = import ./test.nix { inherit pkgs teensy-core; }; - teensy-ulisp = import ./ulisp.nix { inherit pkgs teensy-core; }; + + image = import ./build.nix { inherit pkgs teensy-core; }; + + teensy-test = image.build "teensy-test" ./test; + + teensy-ulisp = let + ulisp-source = import ./ulisp.nix { inherit pkgs; }; + in image.build + "teensy-ulisp" + (pkgs.linkFarmFromDrvs "ulisp" [ ulisp-source ]); + in { packages.${system} = { inherit teensy-core teensy-test teensy-ulisp; diff --git a/test.nix b/test.nix index 8945759..1e26a70 100644 --- a/test.nix +++ b/test.nix @@ -16,8 +16,8 @@ pkgs.stdenv.mkDerivation rec { export OBJCOPY=arm-none-eabi-objcopy export SIZE=arm-none-eabi-size + cp ${teensy-core}/include/Makefile.default Makefile export TEENSY_PATH=${teensy-core} - make ''; diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index 3bf5a70..0000000 --- a/test/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -include $(TEENSY_PATH)/include/flags.mk - -CXXFLAGS += -std=gnu++20 - -LIBS = -lm -lstdc++ -lteensy-core - -CPP_FILES := $(wildcard *.cpp) -TARGETS := $(CPP_FILES:.cpp=.hex) - -%.elf: %.o - $(CC) $(LDFLAGS) -o $@ $< $(LIBS) - -%.hex: %.elf - $(SIZE) $< - $(OBJCOPY) -O ihex -R .eeprom $< $@ - -all: $(TARGETS) diff --git a/ulisp.nix b/ulisp.nix index 6fc6e33..b422c58 100644 --- a/ulisp.nix +++ b/ulisp.nix @@ -1,65 +1,34 @@ -{ pkgs, teensy-core, ... }: +{ pkgs, ... }: -let - # hacky usage of arduino-cli to preprocess ulisp "ino" source into compilable C++ code - preprocessed-ulisp-arm = pkgs.stdenv.mkDerivation rec { - name = "preprocessed-ulisp-arm.cpp"; +# hacky usage of arduino-cli to preprocess ulisp "ino" source into compilable C++ code +pkgs.stdenv.mkDerivation rec { + name = "ulisp-arm.cpp"; - src = pkgs.fetchFromGitHub { - owner = "technoblogy"; - repo = "ulisp-arm"; - rev = "a25fff3ef1072bdc2733d8064fcf0738579ab7ed"; - sha256 = "p6H9To9vccHrP46Cv5m+cAFDVPXDCORUWcqOz18c3kg="; - }; - - outputHashMode = "flat"; - outputHashAlgo = "sha256"; - outputHash = "mutVLBFSpTXgUzu594zZ3akR/Z7e9n5SytU6WoQ6rKA="; - - buildInputs = with pkgs; [ - cacert - arduino-cli - ]; - - buildPhase = '' - export HOME=/tmp/arduino - arduino-cli core install arduino:samd - mv ulisp-arm.ino source.ino - arduino-cli compile --fqbn arduino:samd:arduino_zero_native --preprocess > ulisp-arm.cpp - echo 'extern "C" int main(void) { setup(); while(true) { loop(); } }' >> ulisp-arm.cpp - ''; - - installPhase = '' - cp ulisp-arm.cpp $out - ''; + src = pkgs.fetchFromGitHub { + owner = "technoblogy"; + repo = "ulisp-arm"; + rev = "a25fff3ef1072bdc2733d8064fcf0738579ab7ed"; + sha256 = "p6H9To9vccHrP46Cv5m+cAFDVPXDCORUWcqOz18c3kg="; }; -in pkgs.stdenv.mkDerivation rec { - name = "teensy-ulisp"; + outputHashMode = "flat"; + outputHashAlgo = "sha256"; + outputHash = "mutVLBFSpTXgUzu594zZ3akR/Z7e9n5SytU6WoQ6rKA="; buildInputs = with pkgs; [ - gcc-arm-embedded - teensy-core + cacert + arduino-cli ]; - phases = [ "buildPhase" "installPhase" ]; - buildPhase = '' - cp ${./test/Makefile} Makefile - cp ${preprocessed-ulisp-arm} ulisp-arm.cpp - - export CC=arm-none-eabi-gcc - export CXX=arm-none-eabi-g++ - export OBJCOPY=arm-none-eabi-objcopy - export SIZE=arm-none-eabi-size - - export TEENSY_PATH=${teensy-core} - - make + export HOME=/tmp/arduino + arduino-cli core install arduino:samd + mv ulisp-arm.ino source.ino + arduino-cli compile --fqbn arduino:samd:arduino_zero_native --preprocess > ulisp-arm.cpp + echo 'extern "C" int main(void) { setup(); while(true) { loop(); } }' >> ulisp-arm.cpp ''; installPhase = '' - mkdir $out - cp *.hex $out/ + cp ulisp-arm.cpp $out ''; } -- cgit v1.2.3