Skip to content

Commit

Permalink
Add Rust cross compilation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Orris committed Mar 8, 2024
1 parent 834563d commit 35ec22c
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/common/rust-cross/determine-cross-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set -e
set -x
set -o pipefail

VERSION=$1

if [ -z "$VERSION" ]; then
JSON=$( curl \
--request GET \
--header "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/cross-rs/cross/releases/latest )
VERSION=$( echo "$JSON" | jq -r ".tag_name")
fi

echo "cross-version=$VERSION" >> $GITHUB_OUTPUT
18 changes: 18 additions & 0 deletions .github/workflows/common/rust-cross/install-cross-nix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set -e
set -x
set -o pipefail

CROSS_DIR="$1"
VERSION="$2"

VERSION_ARGS=""
if [ -n "$VERSION" ]; then
VERSION_ARGS="--tag $VERSION"
fi

cd "$CROSS_DIR"
export TARGET=.
curl --silent --location \
https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
sh
./ubi --project cross-rs/cross --matching musl --in . $VERSION_ARGS
22 changes: 22 additions & 0 deletions .github/workflows/common/rust-cross/perltidyrc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-l=78
-i=4
-ci=4
-se
-b
-bar
-boc
-vt=0
-vtc=0
-cti=0
-pt=1
-bt=1
-sbt=1
-bbt=1
-nolq
-npro
-nsfs
--blank-lines-before-packages=0
--opening-hash-brace-right
--no-outdent-long-comments
--iterations=2
-wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
47 changes: 47 additions & 0 deletions .github/workflows/common/rust-cross/precious.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
exclude = [
"target",
]

[commands.perltidy]
type = "both"
include = [ "**/*.{pl,pm,t,psgi}" ]
exclude = "tests/lib/**"
cmd = [ "perltidy", "--profile=$PRECIOUS_ROOT/perltidyrc" ]
lint_flags = [ "--assert-tidy", "--no-standard-output", "--outfile=/dev/null" ]
tidy_flags = [ "--backup-and-modify-in-place", "--backup-file-extension=/" ]
ok_exit_codes = 0
lint_failure_exit_codes = 2
ignore_stderr = "Begin Error Output Stream"

[commands.prettier-md]
type = "both"
include = [ "**/*.md" ]
cmd = [ "./node_modules/.bin/prettier", "--no-config", "--print-width", "100", "--prose-wrap", "always" ]
lint_flags = "--check"
tidy_flags = "--write"
ok_exit_codes = 0
lint_failure_exit_codes = 1
ignore_stderr = [ "Code style issues" ]

[commands.prettier-yml]
type = "both"
include = [ "**/*.yml" ]
cmd = [ "./node_modules/.bin/prettier", "--no-config" ]
lint_flags = "--check"
tidy_flags = "--write"
ok_exit_codes = 0
lint_failure_exit_codes = 1
ignore_stderr = [ "Code style issues" ]

[commands.omegasort-gitignore]
type = "both"
include = "**/.gitignore"
cmd = [ "omegasort", "--sort", "path", "--unique" ]
lint_flags = "--check"
tidy_flags = "--in-place"
ok_exit_codes = 0
lint_failure_exit_codes = 1
ignore_stderr = [
"The .+ file is not sorted",
"The .+ file is not unique",
]
9 changes: 9 additions & 0 deletions .github/workflows/common/rust-cross/set-build-command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set -e
set -x

CROSS_DIR="$1"
if [ -f "$CROSS_DIR/cross" ]; then
echo "build-command=$CROSS_DIR/cross" >> $GITHUB_OUTPUT
else
echo "build-command=cargo" >> $GITHUB_OUTPUT
fi
22 changes: 22 additions & 0 deletions .github/workflows/common/rust-cross/set-cross-compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set -e
set -x

TARGET="$1"

# On macOS and Windows, we can cross-compile to all possible targets without
# using cross.
if uname -a | grep --quiet --extended-regexp -i "darwin|msys|windows"; then
echo "needs-cross=false" >> $GITHUB_OUTPUT
exit 0
fi

# On Linux, we should be able to cross-compile to i586 and i686, but in
# practice this fails with some crates, notably openssl with the "vendored"
# feature. This feature makes it compile openssl itself, which fails without
# cross.
if echo "$TARGET" | grep --quiet --extended-regexp -i 'x86_64.+linux-(gnu|musl)'; then
echo "needs-cross=false" >> $GITHUB_OUTPUT
exit 0
fi

echo "needs-cross=true" >> $GITHUB_OUTPUT
4 changes: 4 additions & 0 deletions .github/workflows/common/rust-cross/set-cross-dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set -e
set -x

echo "cross-dir=$RUNNER_TEMP" >> $GITHUB_OUTPUT
42 changes: 42 additions & 0 deletions .github/workflows/common/rust-cross/strip-binary.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
set -e
set -x

TARGET=$1
did_strip=""

strip_binary () {
if [[ $( uname -s ) =~ "Darwin" ]]; then
stripped=$(
find "$1" -maxdepth 1 -type f -perm +111 | while read exe; do
strip "$exe"
echo "stripped $exe"
done
)
else
stripped=$(
find "$1" -maxdepth 1 -type f -executable | while read exe; do
strip "$exe"
echo "stripped $exe"
done
)
fi

if [ -z "$stripped" ]; then
echo "Could not find any binaries to strip in $1"
else
did_strip="true"
fi
}

for type in debug release; do
if [ -d "target/$TARGET/$type" ]; then
strip_binary "target/$TARGET/$type"
elif [ -d "target/$type" ]; then
strip_binary "target/$type"
fi
done

if [ -z "$did_strip" ]; then
echo "No binaries were stripped"
exit 1
fi

0 comments on commit 35ec22c

Please sign in to comment.