Skip to content

Commit

Permalink
sdk: Prepare for split LTS & latest release manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
adisbladis committed Nov 4, 2024
1 parent fceaebe commit 6668d70
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 142 deletions.
144 changes: 6 additions & 138 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,162 +1,30 @@
{ zephyr-src
, pyproject-nix
, lib
, fetchurl
, python310
, newScope
, openocd
, gcc_multi
, autoreconfHook
, fetchFromGitHub
, pkgs
}:

let
sdk' = lib.importJSON ./sdk.json;
inherit (sdk') version;

getPlatform = stdenv:
if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "macos"
else throw "Unsupported platform";

getArch = stdenv:
if stdenv.isAarch64 then "aarch64"
else if stdenv.isx86_64 then "x86_64"
else throw "Unsupported arch";

baseURL = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}";

fetchSDKFile = file: fetchurl {
url = "${baseURL}/${file}";
sha256 = sdk'.files.${file};
};
lib.makeScope newScope (self: let
inherit (self) callPackage;

sdkArgs = {
python3 = python310;
sdk = callPackage (import ./sdk.nix (lib.importJSON ./sdk.json)) {
python3 = pkgs.python310;
};

in
lib.makeScope newScope (self: let
inherit (self) callPackage;
in {
inherit (sdk) sdk sdkFull hosttools;

# Zephyr/west Python environment.
pythonEnv = callPackage ./python.nix {
inherit zephyr-src;
inherit pyproject-nix;
};

# Pre-package Zephyr SDK.
sdk = callPackage
({ stdenv
, which
, cmake
, autoPatchelfHook
, libxcrypt-legacy
, ncurses
, python3
, targets ? [ ]
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
in
stdenv.mkDerivation {
pname = "zephyr-sdk";
inherit version;

srcs = [
(fetchSDKFile "zephyr-sdk-${version}_${platform}-${arch}_minimal.tar.xz")
] ++ map fetchSDKFile (map (target: "toolchain_${platform}-${arch}_${target}.tar.xz") targets);

passthru = {
inherit platform arch targets;
};

nativeBuildInputs =
[ which cmake ]
++ lib.optional (!stdenv.isDarwin) autoPatchelfHook
;

buildInputs = [ stdenv.cc.cc ncurses libxcrypt-legacy python3 ];

dontBuild = true;
dontUseCmakeConfigure = true;

sourceRoot = ".";

installPhase = ''
runHook preInstall
rm -f zephyr-sdk-$version/zephyr-sdk-${arch}-hosttools-standalone-*.sh
rm -f env-vars
mv zephyr-sdk-$version $out
if [ -n "$(ls -A .)" ]; then
mv * $out
fi
mkdir -p $out/nix-support
cat <<EOF >> $out/nix-support/setup-hook
export ZEPHYR_SDK_INSTALL_DIR=$out
EOF
runHook postInstall
'';
})
sdkArgs;

# # SDK with all targets selected
sdkFull =
let
inherit (self.sdk.passthru) platform arch;
mToolchain = builtins.match "toolchain_${platform}-${arch}_(.+)\.tar\.xz";
allTargets = map (x: builtins.head (mToolchain x)) (builtins.filter (f: mToolchain f != null) (lib.attrNames sdk'.files));
in
self.sdk.override {
targets = allTargets;
};

# Binary host tools provided by the Zephyr project.
hosttools = callPackage
({ stdenv
, which
, autoPatchelfHook
, python3
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
in
stdenv.mkDerivation {
pname = "zephyr-sdk-hosttools";
inherit version;

src = fetchSDKFile "hosttools_${platform}-${arch}.tar.xz";

nativeBuildInputs =
[ which ]
++ lib.optional (!stdenv.isDarwin) autoPatchelfHook
;

buildInputs = [ python3 ];

dontBuild = true;
dontFixup = true;

sourceRoot = ".";

installPhase = ''
runHook preInstall
mkdir -p $out/usr/share/zephyr/hosttools
./zephyr-sdk-${arch}-hosttools-standalone-*.sh -d $out/usr/share/zephyr/hosttools
ln -s $out/usr/share/zephyr/hosttools/sysroots/${arch}-pokysdk-${platform}/usr/bin $out/bin
runHook postInstall
'';
})
sdkArgs;

openocd-zephyr = openocd.overrideAttrs(old: let
pname = "openocd-zephyr";
version = "20220611";
Expand Down
166 changes: 166 additions & 0 deletions sdk.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
sdk':
let
inherit (sdk') version;

baseURL = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}";

getPlatform =
stdenv:
if stdenv.isLinux then
"linux"
else if stdenv.isDarwin then
"macos"
else
throw "Unsupported platform";

getArch =
stdenv:
if stdenv.isAarch64 then
"aarch64"
else if stdenv.isx86_64 then
"x86_64"
else
throw "Unsupported arch";

fetchSDKFile' =
fetchurl: file:
fetchurl {
url = "${baseURL}/${file}";
sha256 = sdk'.files.${file};
};

in
{
lib,
python3,
newScope,
ncurses,
libxcrypt-legacy,
}:
lib.makeScope newScope (self: let
inherit (self) callPackage;
in {

# Pre-packaged Zephyr SDK.
sdk =
callPackage ({
stdenv,
which,
cmake,
autoPatchelfHook,
python3,
fetchurl,
lib,
targets ? [ ],
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;

fetchSDKFile = fetchSDKFile' fetchurl;

in
stdenv.mkDerivation {
pname = "zephyr-sdk";
inherit version;

srcs = [
(fetchSDKFile "zephyr-sdk-${version}_${platform}-${arch}_minimal.tar.xz")
] ++ map fetchSDKFile (map (target: "toolchain_${platform}-${arch}_${target}.tar.xz") targets);

passthru = {
inherit platform arch targets;
};

nativeBuildInputs = [
which
cmake
] ++ lib.optional (!stdenv.isDarwin) autoPatchelfHook;

buildInputs = [
stdenv.cc.cc
python3
ncurses
libxcrypt-legacy
];

dontBuild = true;
dontUseCmakeConfigure = true;

sourceRoot = ".";

installPhase = ''
runHook preInstall
rm -f zephyr-sdk-$version/zephyr-sdk-${arch}-hosttools-standalone-*.sh
rm -f env-vars
mv zephyr-sdk-$version $out
if [ -n "$(ls -A .)" ]; then
mv * $out
fi
mkdir -p $out/nix-support
cat <<EOF >> $out/nix-support/setup-hook
export ZEPHYR_SDK_INSTALL_DIR=$out
EOF
runHook postInstall
'';
}) {
inherit python3;
};

# SDK with all targets selected
sdkFull =
let
inherit (self.sdk.passthru) platform arch;
mToolchain = builtins.match "toolchain_${platform}-${arch}_(.+)\.tar\.xz";
allTargets = map (x: builtins.head (mToolchain x)) (builtins.filter (f: mToolchain f != null) (lib.attrNames sdk'.files));
in
self.sdk.override {
targets = allTargets;
};

# Binary host tools provided by the Zephyr project.
hosttools =
callPackage ({
stdenv,
which,
autoPatchelfHook,
python3,
lib,
fetchurl,
}:
let
platform = getPlatform stdenv;
arch = getArch stdenv;
fetchSDKFile = fetchSDKFile' fetchurl;
in
stdenv.mkDerivation {
pname = "zephyr-sdk-hosttools";
inherit version;

src = fetchSDKFile "hosttools_${platform}-${arch}.tar.xz";

nativeBuildInputs = [ which ] ++ lib.optional (!stdenv.isDarwin) autoPatchelfHook;

buildInputs = [ python3 ];

dontBuild = true;
dontFixup = true;

sourceRoot = ".";

installPhase = ''
runHook preInstall
mkdir -p $out/usr/share/zephyr/hosttools
./zephyr-sdk-${arch}-hosttools-standalone-*.sh -d $out/usr/share/zephyr/hosttools
ln -s $out/usr/share/zephyr/hosttools/sysroots/${arch}-pokysdk-${platform}/usr/bin $out/bin
runHook postInstall
'';
}) {
inherit python3;
};
})
10 changes: 6 additions & 4 deletions update-sdk
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

set -euo pipefail

version=$1
# Output file (different output files for LTS & latest SDK)
out=$1
version=$2

if [ -z "$version" ]; then
echo "ERROR: missing version argument. For example: ./update-sdk 0.16.6"
if [ -z "$out" ] || [ -z "$version" ]; then
echo "Usage: ./update-sdk sdk.json 0.16.6"
exit 1
fi

curl -L "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v$version/sha256.sum" \
| jq -R -n "[ inputs | split(\" \") | {(.[2]): .[0]} ] | add | { \"files\": ., \"version\": \"$version\"}" > sdk.json
| jq -R -n "[ inputs | split(\" \") | {(.[2]): .[0]} ] | add | { \"files\": ., \"version\": \"$version\"}" > "$out"

0 comments on commit 6668d70

Please sign in to comment.