Skip to content

Commit

Permalink
Merge pull request #24 from leighleighleigh/cleanup
Browse files Browse the repository at this point in the history
Futher migration of bash scripts to Nix shells, tidied up cross-compilation of non-nix python wheels
  • Loading branch information
leighleighleigh authored Nov 11, 2023
2 parents 9ebf763 + 3a87a26 commit 783ba30
Show file tree
Hide file tree
Showing 30 changed files with 164 additions and 437 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
# Ignore pycache
**/__pycache__/**


# Ignore nix
result
20 changes: 5 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"jcan",
"jcan-python",
"scripts-postbuild",
"jcan_python",
"scripts-postbuild"
]
51 changes: 0 additions & 51 deletions bump_version.sh

This file was deleted.

16 changes: 0 additions & 16 deletions clean.sh

This file was deleted.

103 changes: 103 additions & 0 deletions cross-build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# cross-build.nix
# This shell uses the 'cross-rs' tool to cross-compile JCAN.
# Whilst this is done easily on Nix, through it's own cross-compilation support,
# it doesn't produce a python wheel which is useful on non-Nix systems.
#
# Hence, this shell is primarily designed to produce a Python wheel for release to PyPi.
#
{ pkgs ? import <nixpkgs> {} }:
let
clean-script = pkgs.writeScript "clean.sh" ''
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "''${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Remove jcan-python/dist, build, **.egg-info folders
rm -rf "''${SCRIPT_DIR}/out"
rm -rf "''${SCRIPT_DIR}/jcan_python/dist"
rm -rf "''${SCRIPT_DIR}/jcan_python/build"
rm -rf "''${SCRIPT_DIR}/jcan_python/jcan.egg-info"
# Run cargo clean
cargo clean
'';

build-script = pkgs.writeScript "crossbuild.sh" ''
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "''${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# This function takes a TARGET as an argument, and builds the library for that target
# It then moves the build artifacts to out/<profile>/<target>/jcan/
function build_target {
CROSSTARGET="''${1}"
export CROSS_CONTAINER_ENGINE=docker
# Very important to clean, incase old crates for x86 are present
cargo clean
cross build --package jcan --target $CROSSTARGET --release
cross build --package scripts_postbuild --target $CROSSTARGET --release
# python build uses a special pyo3 image
export CARGO=cross
export CARGO_BUILD_TARGET=''${CROSSTARGET}
export CROSS_CONFIG=''${SCRIPT_DIR}/jcan_python/Cross.toml
cross build --package jcan_python --target $CROSSTARGET --release
# Run setuptools-rust on jcan_python
cd jcan_python
rm -rf ./dist
rm -rf ./build
# Change plat-name depending on CROSSTARGET
if [[ "''${CROSSTARGET}" == "aarch64-unknown-linux-gnu" ]];
then
PLATNAME="manylinux2014_aarch64"
elif [[ "''${CROSSTARGET}" == "x86_64-unknown-linux-gnu" ]];
then
PLATNAME="manylinux2014_x86_64"
else
echo "Unknown CROSSTARGET: ''${CROSSTARGET}"
exit 1
fi
python setup.py bdist_wheel --plat-name $PLATNAME --py-limited-api=cp38 || exit 1
cd ..
# Copy the resulting wheels to out folder
mkdir -p out/python/
cp -r jcan_python/dist/*.whl out/python/
}
# Build for aarch64
build_target "aarch64-unknown-linux-gnu"
# Build for x86_64
build_target "x86_64-unknown-linux-gnu"
'';
in
(pkgs.buildFHSEnv {
name = "jcan-cross-env";

targetPkgs = pkgs: [
pkgs.rustup
pkgs.cargo
pkgs.python3
pkgs.python310Packages.pip
pkgs.python310Packages.wheel
pkgs.python310Packages.setuptools-rust
pkgs.python3Packages.toml
pkgs.docker
#pkgs.podman
pkgs.hostname
pkgs.direnv
];

runScript = pkgs.writeScript "init.sh" ''
export PYTHONPATH="/lib/python3.10/site-packages/"
bash ${clean-script}
bash ${build-script}
rm -rf ./target
'';
}).env
93 changes: 0 additions & 93 deletions crossbuild.sh

This file was deleted.

7 changes: 0 additions & 7 deletions default.nix

This file was deleted.

Loading

0 comments on commit 783ba30

Please sign in to comment.