Skip to content

Commit

Permalink
temp: try building macos native bins with zig
Browse files Browse the repository at this point in the history
  • Loading branch information
SKalt committed Sep 28, 2023
1 parent b2af9aa commit 3c2fd0e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 25 deletions.
45 changes: 32 additions & 13 deletions .github/workflows/zigbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ jobs:
# vscode-target: win32-x64
# x86 linux
- os: ubuntu-latest
native:
native: # works great
target: x86_64-unknown-linux-gnu
vscode-target: linux-x64 # todo: fully-static MUSL?
zig: false
# cross:
# target: aarch64-unknown-linux-gnu
# vscode-target: linux-arm64
# zig: true
# TODO: arm64 linux
# x86 macos
- os: macos-latest
native:
target: x86_64-apple-darwin
vscode-target: darwin-x64x
zig: true
# arm64 macos
# FIXME: the host is still x86_64; need to install aarch64-apple-darwin binutils
# - os: macos-latest
Expand All @@ -45,35 +48,51 @@ jobs:
fetch-depth: 0 # shallow clone for speed
- name: set MacOS SDKROOT and MACOSX_DEPLOYMENT_TARGET
# see https://stackoverflow.com/questions/66849112/how-do-i-cross-compile-a-rust-application-from-macos-x86-to-macos-silicon
if: matrix.os == 'macos-latest'
run: |
{
echo "SDKROOT=$(xcrun -sdk macosx --show-sdk-path)"
echo "MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx --show-sdk-platform-version)"
} | tee -a $GITHUB_ENV
- name: "Install rust toolchain"
if: steps.cache-bin.outputs.cache-hit != 'true'
shell: bash
run: |
rustup toolchain install stable --profile minimal
rustup target add ${{ matrix.native.target }}
rustup component add rust-src
rustup component add llvm-tools-preview # 32mb
bins_dir="$(rustup run stable rustc --print sysroot)/lib/rustlib/${{ matrix.native.target }}/bin"
export PATH="$bins_dir:$PATH"
echo "PATH=$PATH" >> $GITHUB_ENV
# pulling 3x32MB of llvm tools is still better than pushing+pulling
# 6x60MB of the built binary
# pulling 3x32MB of llvm tools is still better than pushing+pulling
# 6x60MB of the built binary
- uses: Swatinem/rust-cache@v2 # see https://github.com/Swatinem/rust-cache
- name: "native build: ${{matrix.native.target}}}"
- name: setup zig
shell: bash
run: python3 -m pip install ziglang cargo-zigbuild
- name: modify $PATH
shell: bash
run: |
{
site_packages="$(
python3 -c 'import sys, pathlib as p; [
print(i) for i in sys.path
if p.Path(i).name == "site-packages"
]'
)"
_sysroot="$(rustup run stable rustc --print sysroot)"
bins_dir="$_sysroot/lib/rustlib/${{ matrix.native.target }}/bin"
export PATH="$bins_dir:$site_packages/ziglang:$PATH"
echo "PATH=$PATH"
} | tee -a $GITHUB_ENV | tr ':' '\n' | sed 's/^/ - /g'
- name: "native build: ${{ matrix.native.target }}"
shell: bash
run: |
target=${{ matrix.native.target }}
export RUSTFLAGS="-Clink-args=-Wl,--build-id=sha1"
rust-lld --version && rust-lld --help || echo "rust-lld missing"
./scripts/build_bin.sh \
--version=base \
--profile=release \
--target ${{matrix.native.target}}
./scripts/build_bin.sh \
--version=base \
--profile=release \
--zig=${{ matrix.native.zig || 'false' }} \
--target ${{ matrix.native.target }}
# TODO: cross build
- name: upload ${{ matrix.native.target }} bin
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion pkg/base/src/document/linting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub trait LintConfig {
})
.map(|f| f(doc))
.map(|mut v| {
for mut diagnostic in v.iter_mut() {
for diagnostic in v.iter_mut() {
if diagnostic.severity.is_none() {
match &diagnostic.code {
Some(lsp_types::NumberOrString::String(code)) => {
Expand Down
37 changes: 26 additions & 11 deletions scripts/build_bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
### --version: base or pro (default: base)
### --profile: debug or release (default: debug)
### --target: one of the target identifiers listed by `rustup target list`
### (default: x86_64-unknown-linux-gnu)
### --zig: whether to use `cargo zigbuild` to build the binary (default: false)


if [[ "${BASH_SOURCE[0]}" = */* ]]; then this_dir="${BASH_SOURCE[0]%/*}"; else this_dir=.; fi
this_dir="$(cd "${this_dir}" && pwd)"
Expand Down Expand Up @@ -38,16 +41,22 @@ derive_cargo_cmd() {
local profile=$1
local variant=$2
local target=$3
local use_zig=$4

local cmd="cargo"
case "$profile" in
true) cmd="$cmd --release" ;;
*) cmd="$cmd build";;
esac
case "$profile" in
debug) ;;
release) cmd="$cmd --release" ;;
*) log_fail "invalid profile: $profile" ;;
esac

local cmd="cargo build"
cmd="$cmd --bin ${variant}"
cmd="$cmd --target ${target}"
cmd="$cmd --all-features --timings"
case "$profile" in
debug) ;;
release) cmd="$cmd --release" ;;
*) log_fail "invalid profile: $profile" ;;
esac
printf "%s" "$cmd"
}

Expand All @@ -66,13 +75,17 @@ build_bin() {
local target=$1
local version=$2
local profile=$3
local use_zig=$4
cd "$repo_root" || exit 1
local variant="${version}_language_server"
local build_cmd=""
build_cmd="$(derive_cargo_cmd "$profile" "$variant" "$target")"

require_cli "cargo"

if [ "$use_zig" = true ]; then
require_cli "zig"
require_cli "cargo-zigbuild"
fi
local objcopy
objcopy="$(find_objcopy)"
log_dbug "using objcopy: $objcopy"
Expand Down Expand Up @@ -153,11 +166,13 @@ main() {
case "$1" in
-h | --help) usage && exit 0 ;;
--version=*) version="${1#*=}" && shift ;;
--version) version="$2" && shift 2 ;;
--profile=*) profile="${1#*=}" && shift ;;
--target=*) target="${1#*=}" && shift ;;
--zig=*) use_zig="${1#*=}" && shift ;;
--version) version="$2" && shift 2 ;;
--profile) profile="$2" && shift 2 ;;
--target=*) target="${1#*=}" && shift ;;
--target) target="$2" && shift 2 ;;
--target) target="$2" && shift 2 ;;
--zig) use_zig=true && shift ;;
*) echo "unexpected argument: $1" >&2 && usage >&2 && exit 1 ;;
esac
done
Expand All @@ -166,7 +181,7 @@ main() {
profile="$(parse_profile "$profile")"

log_dbug "log file: ${log_file}"
build_bin "$target" "$version" "$profile"
build_bin "$target" "$version" "$profile" "$use_zig"
}

if [ "${BASH_SOURCE[0]}" = "$0" ]; then main "$@"; fi

0 comments on commit 3c2fd0e

Please sign in to comment.