Skip to content

Commit afe5867

Browse files
kixelatedemilsas
andauthored
Add universal libmoq build for macos (#861)
Co-authored-by: Emil Santurio <emilsas@gmail.com>
1 parent ad7086c commit afe5867

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

.github/workflows/libmoq.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
os: macos-15-intel
2626
- target: aarch64-apple-darwin
2727
os: macos-latest
28+
- target: universal-apple-darwin
29+
os: macos-latest
2830
- target: x86_64-pc-windows-msvc
2931
os: windows-latest
3032

@@ -34,7 +36,7 @@ jobs:
3436
- name: Install Rust
3537
uses: dtolnay/rust-toolchain@stable
3638
with:
37-
targets: ${{ matrix.target }}
39+
targets: ${{ matrix.target == 'universal-apple-darwin' && 'x86_64-apple-darwin,aarch64-apple-darwin' || matrix.target }}
3840

3941
- name: Install cross-compilation tools (Linux ARM64)
4042
if: matrix.target == 'aarch64-unknown-linux-gnu'

flake.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
"rust-src"
4141
"rust-analyzer"
4242
];
43+
targets = pkgs.lib.optionals pkgs.stdenv.isDarwin [
44+
"x86_64-apple-darwin"
45+
"aarch64-apple-darwin"
46+
];
4347
};
4448

4549
# Rust dependencies

rs/libmoq/build.sh

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,70 @@ while [[ $# -gt 0 ]]; do
4343
esac
4444
done
4545

46-
# Detect target if not specified
47-
if [[ -z "$TARGET" ]]; then
48-
TARGET=$(rustc -vV | grep host | cut -d' ' -f2)
49-
echo "Detected target: $TARGET"
50-
fi
51-
5246
# Get version from Cargo.toml if not specified
5347
if [[ -z "$VERSION" ]]; then
5448
VERSION=$(grep '^version' "$SCRIPT_DIR/Cargo.toml" | head -1 | sed 's/.*"\(.*\)".*/\1/')
5549
echo "Detected version: $VERSION"
5650
fi
5751

58-
echo "Building libmoq for $TARGET..."
52+
if [[ "$TARGET" == "universal-apple-darwin" ]]; then
53+
if [[ "$(uname)" != "Darwin" ]]; then
54+
echo "Error: Universal builds are only supported on macOS" >&2
55+
exit 1
56+
fi
57+
58+
echo "Building libmoq for $TARGET..."
59+
60+
# Build x86_64
61+
echo "Building for x86_64-apple-darwin..."
62+
cargo build --release --package libmoq --target x86_64-apple-darwin --manifest-path "$WORKSPACE_DIR/Cargo.toml"
63+
64+
# Build arm64
65+
echo "Building for aarch64-apple-darwin..."
66+
cargo build --release --package libmoq --target aarch64-apple-darwin --manifest-path "$WORKSPACE_DIR/Cargo.toml"
67+
68+
# Define sources for packaging
69+
# Use arm64 as the reference for headers/pkgconfig (they should be identical)
70+
REF_TARGET="aarch64-apple-darwin"
71+
INCLUDE_SOURCE="$WORKSPACE_DIR/target/$REF_TARGET/include/moq.h"
72+
PKGCONFIG_SOURCE="$WORKSPACE_DIR/target/$REF_TARGET/pkgconfig/moq.pc"
73+
74+
# Libraries to combine
75+
LIB_X86="$WORKSPACE_DIR/target/x86_64-apple-darwin/release/libmoq.a"
76+
LIB_ARM64="$WORKSPACE_DIR/target/aarch64-apple-darwin/release/libmoq.a"
77+
LIB_FILE="libmoq.a"
5978

60-
# Set up cross-compilation for Linux ARM64
61-
if [[ "$TARGET" == "aarch64-unknown-linux-gnu" ]]; then
62-
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
63-
fi
79+
else
80+
# Detect target if not specified
81+
if [[ -z "$TARGET" ]]; then
82+
TARGET=$(rustc -vV | grep host | cut -d' ' -f2)
83+
echo "Detected target: $TARGET"
84+
fi
85+
86+
echo "Building libmoq for $TARGET..."
87+
88+
# Set up cross-compilation for Linux ARM64
89+
if [[ "$TARGET" == "aarch64-unknown-linux-gnu" ]]; then
90+
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
91+
fi
6492

65-
cargo build --release --package libmoq --target "$TARGET" --manifest-path "$WORKSPACE_DIR/Cargo.toml"
93+
cargo build --release --package libmoq --target "$TARGET" --manifest-path "$WORKSPACE_DIR/Cargo.toml"
94+
95+
# Define sources for packaging
96+
INCLUDE_SOURCE="$WORKSPACE_DIR/target/$TARGET/include/moq.h"
97+
PKGCONFIG_SOURCE="$WORKSPACE_DIR/target/$TARGET/pkgconfig/moq.pc"
98+
99+
TARGET_DIR="$WORKSPACE_DIR/target/$TARGET/release"
100+
if [[ "$TARGET" == *"-windows-"* ]]; then
101+
LIB_SOURCE="$TARGET_DIR/moq.lib"
102+
LIB_FILE="moq.lib"
103+
else
104+
LIB_SOURCE="$TARGET_DIR/libmoq.a"
105+
LIB_FILE="libmoq.a"
106+
fi
107+
fi
66108

67109
# Determine paths
68-
TARGET_DIR="$WORKSPACE_DIR/target/$TARGET/release"
69110
NAME="moq-${VERSION}-${TARGET}"
70111
PACKAGE_DIR="$OUTPUT_DIR/$NAME"
71112

@@ -75,36 +116,26 @@ echo "Packaging $NAME..."
75116
rm -rf "$PACKAGE_DIR"
76117
mkdir -p "$PACKAGE_DIR/include" "$PACKAGE_DIR/lib"
77118

78-
# Copy header (generated in target/$TARGET/include/ by build.rs)
79-
cp "$WORKSPACE_DIR/target/$TARGET/include/moq.h" "$PACKAGE_DIR/include/"
119+
# Copy header
120+
cp "$INCLUDE_SOURCE" "$PACKAGE_DIR/include/"
80121

81122
# Copy static library
82-
case "$TARGET" in
83-
*-windows-*)
84-
cp "$TARGET_DIR/moq.lib" "$PACKAGE_DIR/lib/"
85-
;;
86-
*)
87-
# Unix-like (macOS, Linux, etc.)
88-
cp "$TARGET_DIR/libmoq.a" "$PACKAGE_DIR/lib/"
89-
;;
90-
esac
123+
if [[ "$TARGET" == "universal-apple-darwin" ]]; then
124+
echo "Creating universal binary..."
125+
lipo -create "$LIB_X86" "$LIB_ARM64" -output "$PACKAGE_DIR/lib/$LIB_FILE"
126+
else
127+
cp "$LIB_SOURCE" "$PACKAGE_DIR/lib/"
128+
fi
91129

92130
# Copy pkg-config file (generated in target/$TARGET/pkgconfig/ by build.rs, not for Windows)
93131
if [[ "$TARGET" != *"-windows-"* ]]; then
94132
mkdir -p "$PACKAGE_DIR/lib/pkgconfig"
95-
cp "$WORKSPACE_DIR/target/$TARGET/pkgconfig/moq.pc" "$PACKAGE_DIR/lib/pkgconfig/"
133+
cp "$PKGCONFIG_SOURCE" "$PACKAGE_DIR/lib/pkgconfig/"
96134
fi
97135

98136
# Generate CMake config files from templates
99137
mkdir -p "$PACKAGE_DIR/lib/cmake/moq"
100138

101-
# Determine library filename
102-
if [[ "$TARGET" == *"-windows-"* ]]; then
103-
LIB_FILE="moq.lib"
104-
else
105-
LIB_FILE="libmoq.a"
106-
fi
107-
108139
# Extract major version
109140
MAJOR_VERSION="${VERSION%%.*}"
110141

0 commit comments

Comments
 (0)