Skip to content

Commit

Permalink
java: Adjust how we handle manual cross-compilation
Browse files Browse the repository at this point in the history
Follow-up to 0453438. Previously, setting CARGO_BUILD_TARGET before
manually calling build_jni.sh merely did a Cargo build with the same
settings as the host build. However, adding support for cross-compiles
for the server build broke this "ad hoc" cross-compilation.
Fortunately, it was used only in one place: providing both flavors of
Mac library in a built libsignal-client and libsignal-server. We can
use the *new* cross-compilation support for this instead, reducing
special cases.

This also limits the conditions where build_jni.sh will automatically
set CC and other environment variables, and allows that behavior to be
overridden by manually setting them.
  • Loading branch information
jrose-signal committed Jul 31, 2024
1 parent 3e06408 commit d3adfba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
49 changes: 33 additions & 16 deletions .github/workflows/jni_artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ jobs:
os: [windows-latest, macos-12]
include:
- os: windows-latest
library: signal_jni.dll
- os: macos-12
library: libsignal_jni.dylib
additional-rust-target: aarch64-apple-darwin
# Ubuntu binaries are built using Docker, below

Expand Down Expand Up @@ -68,27 +66,41 @@ jobs:
- run: brew install protobuf
if: matrix.os == 'macos-12'

- name: Build for host (should be x86_64)
- name: Build client for host
run: java/build_jni.sh desktop
shell: bash

- name: Build for alternate target (arm64)
- name: Build server for host
run: java/build_jni.sh server
shell: bash

- name: Build client for alternate target
run: java/build_jni.sh desktop
if: matrix.os == 'macos-12'
env:
CARGO_BUILD_TARGET: ${{ matrix.additional-rust-target }}

- name: Merge library slices (for macOS)
# Using target/release/ for both the input and output wouldn't normally be ideal
# from a build system perspective, but we're going to immediately upload the merged library.
run: lipo -create target/release/${{ matrix.library }} target/${{ matrix.additional-rust-target }}/release/${{ matrix.library }} -output target/release/${{ matrix.library }}
- name: Build server for alternate target
run: java/build_jni.sh server
if: matrix.os == 'macos-12'
env:
CARGO_BUILD_TARGET: ${{ matrix.additional-rust-target }}

- name: Upload library
- name: Upload client libraries
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: libsignal_jni (${{matrix.os}})
path: target/release/${{ matrix.library }}
name: libsignal-client libraries (${{matrix.os}})
path: |
java/client/src/main/resources/*.dll
java/client/src/main/resources/*.dylib
- name: Upload server libraries
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: libsignal-server libraries (${{matrix.os}})
path: |
java/server/src/main/resources/*.dll
java/server/src/main/resources/*.dylib
verify-rust:
name: Verify JNI bindings
Expand Down Expand Up @@ -122,14 +134,19 @@ jobs:
with:
submodules: recursive

- name: Download built libraries
id: download
- name: Download built client libraries
uses: actions/download-artifact@c850b930e6ba138125429b7e5c93fc707a7f8427 # v4.1.4
with:
path: artifacts
path: java/client/src/main/resources
pattern: libsignal-client*
merge-multiple: true

- name: Copy libraries
run: mv ${{ steps.download.outputs.download-path }}/*/* java/shared/resources && find java/shared/resources
- name: Download built server libraries
uses: actions/download-artifact@c850b930e6ba138125429b7e5c93fc707a7f8427 # v4.1.4
with:
path: java/server/src/main/resources
pattern: libsignal-server*
merge-multiple: true

- run: make
if: ${{ inputs.dry_run }}
Expand Down
33 changes: 15 additions & 18 deletions java/build_jni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ fi

android_abis=()

# usage: build_desktop_for_arch target_triple host_triple
# usage: build_desktop_for_arch target_triple host_triple output_dir
build_desktop_for_arch () {
local RUSTFLAGS
local CC
local CXX
local CPATH
Expand All @@ -68,23 +67,19 @@ build_desktop_for_arch () {
esac
if [[ "$1" != "$2" ]]; then
# Set up cross-compiling flags
if [[ "$1" != *-linux-* ]]; then
echo "cross-compiling only supported for Linux targets; update build_jni.sh"
exit 2
fi
if [[ "$cpuarch" == aarch64 ]]; then
RUSTFLAGS="-C target-feature=+v8.2a ${RUSTFLAGS:-}"
if [[ "$1" == *-linux-* && "$2" == *-linux-* && -z "${CC:-}" ]]; then
# When cross-compiling *from* Linux *to* Linux,
# set up standard cross-compiling environment if not already set
echo 'setting Linux cross-compilation options...'
export "CARGO_TARGET_$(echo "$cpuarch" | tr "[:lower:]" "[:upper:]")_UNKNOWN_LINUX_GNU_LINKER"="${cpuarch}-linux-gnu-gcc"
export CC="${cpuarch}-linux-gnu-gcc"
export CXX="${cpuarch}-linux-gnu-g++"
export CPATH="/usr/${cpuarch}-linux-gnu/include"
fi
export "CARGO_TARGET_$(echo "$cpuarch" | tr "[:lower:]" "[:upper:]")_UNKNOWN_LINUX_GNU_LINKER"="${cpuarch}-linux-gnu-gcc"
export CC="${cpuarch}-linux-gnu-gcc"
export CXX="${cpuarch}-linux-gnu-g++"
export CPATH="/usr/${cpuarch}-linux-gnu/include"
fi
echo_then_run cargo build -p libsignal-jni -p libsignal-jni-testing --release ${FEATURES:+--features "${FEATURES[*]}"} --target "$1"
if [[ -z "${CARGO_BUILD_TARGET:-}" ]]; then
copy_built_library "target/${1}/release" signal_jni "$lib_dir" "signal_jni_${suffix}"
copy_built_library "target/${1}/release" signal_jni_testing "$lib_dir" "signal_jni_testing_${suffix}"
fi
copy_built_library "target/${1}/release" signal_jni "$lib_dir" "signal_jni_${suffix}"
copy_built_library "target/${1}/release" signal_jni_testing "$lib_dir" "signal_jni_testing_${suffix}"
}

while [ "${1:-}" != "" ]; do
Expand All @@ -104,9 +99,11 @@ while [ "${1:-}" != "" ]; do
host_triple=$(rustc -vV | sed -n 's|host: ||p')
if [[ "$1" == "server-all" ]]; then
build_desktop_for_arch x86_64-unknown-linux-gnu "$host_triple" $lib_dir
build_desktop_for_arch aarch64-unknown-linux-gnu "$host_triple" $lib_dir
# Enable ARMv8.2 extensions for a production aarch64 server build
RUSTFLAGS="-C target-feature=+v8.2a ${RUSTFLAGS:-}" \
build_desktop_for_arch aarch64-unknown-linux-gnu "$host_triple" $lib_dir
else
build_desktop_for_arch "$host_triple" "$host_triple" $lib_dir
build_desktop_for_arch "${CARGO_BUILD_TARGET:-$host_triple}" "$host_triple" $lib_dir
fi
exit
;;
Expand Down

0 comments on commit d3adfba

Please sign in to comment.