Skip to content

Commit

Permalink
Generate bindings once for all platforms and architectures
Browse files Browse the repository at this point in the history
Uses CI based on Smithay/drm-rs#176.

Includes #29.
  • Loading branch information
ids1024 authored and Drakulix committed Nov 14, 2023
1 parent 349d1d8 commit 6bdb1bc
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 2,925 deletions.
111 changes: 63 additions & 48 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- master
- develop # TODO: remove before merging to master
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
pull_request:
Expand All @@ -12,7 +11,7 @@ jobs:
format:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
Expand All @@ -22,7 +21,7 @@ jobs:
default: true
override: true
- name: Cargo cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
Expand All @@ -37,7 +36,7 @@ jobs:
doc:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- run: sudo apt-get install -y libwayland-dev
- name: Setup Rust
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -149,6 +148,10 @@ jobs:
os: ubuntu-22.04
rust: stable
target: aarch64-unknown-linux-gnu
- task: bindings
os: ubuntu-22.04
rust: stable
target: riscv64gc-unknown-linux-gnu
# Test channels
- task: channels
os: ubuntu-22.04
Expand All @@ -164,45 +167,28 @@ jobs:
target: x86_64-unknown-linux-gnu
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Setup linux toolchain
- uses: actions/checkout@v3
- name: Install dependencies
if: contains(matrix.target, '-linux-') && startsWith(matrix.target, 'x86_64-')
run: |
sudo apt-get update -y
sudo apt-get install -y libdrm-dev libgbm-dev libwayland-dev
- name: Setup cross linux toolchain
if: contains(matrix.target, '-linux-') && !startsWith(matrix.target, 'x86_64-')
sudo apt-get install -y libwayland-dev pkg-config
- name: Install i686 dependencies
if: contains(matrix.target, '-linux-') && startsWith(matrix.target, 'i686-')
run: |
case "${{ matrix.target }}" in
i686-*) SYSTEM_ARCH=i386 ;;
arm*) SYSTEM_ARCH=armhf ;;
aarch64*) SYSTEM_ARCH=arm64 ;;
esac
GCC_TARGET=$(printf "${{ matrix.target }}" | sed 's/-unknown-/-/' | sed 's/arm[^-]*/arm/g')
ENV_TARGET=$(printf "${{ matrix.target }}" | tr '-' '_')
ENV_TARGET_UC=$(printf "${ENV_TARGET}" | tr '[[:lower:]]' '[[:upper:]]')
sudo rm -f /etc/apt/sources.list.d/*.list
case "${{ matrix.target }}" in
arm* | aarch64*)
sudo tee /etc/apt/sources.list << EOF
deb [arch=i386,amd64] http://archive.ubuntu.com/ubuntu/ jammy main universe
deb [arch=i386,amd64] http://archive.ubuntu.com/ubuntu/ jammy-updates main universe
deb [arch=i386,amd64] http://security.ubuntu.com/ubuntu/ jammy-security main universe
deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy main universe
deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy-updates main universe
deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports/ jammy-security main universe
EOF
;;
esac
sudo dpkg --add-architecture ${SYSTEM_ARCH}
dpkg --print-foreign-architectures
sudo dpkg --add-architecture i386
sudo apt-get update -y
sudo apt-get upgrade -y --fix-broken
sudo apt-get install -y libdrm-dev:${SYSTEM_ARCH} libgbm-dev:${SYSTEM_ARCH} libwayland-dev:${SYSTEM_ARCH} gcc-${GCC_TARGET} pkg-config
echo "CARGO_TARGET_${ENV_TARGET_UC}_LINKER=${GCC_TARGET}-gcc" >> $GITHUB_ENV
sudo apt-get install -y libgbm-dev:i386 libwayland-dev:i386
echo "PKG_CONFIG_ALLOW_CROSS=1" >> $GITHUB_ENV
echo "PKG_CONFIG_${ENV_TARGET}=${GCC_TARGET}-pkg-config" >> $GITHUB_ENV
echo "PKG_CONFIG=${GCC_TARGET}-pkg-config" >> $GITHUB_ENV
echo "PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig" >> $GITHUB_ENV
- name: Setup linux cross toolchain
if: contains(matrix.target, '-linux-') && !startsWith(matrix.target, 'x86_64-')
run: |
GCC_TARGET=$(printf "${{ matrix.target }}" | sed 's/-unknown-/-/;s/arm[^-]*/arm/;s/riscv[^-]*/riscv64/')
ENV_TARGET=$(printf "${{ matrix.target }}" | tr '-' '_' | tr '[[:lower:]]' '[[:upper:]]')
sudo apt-get update -y
sudo apt-get install -y gcc-${GCC_TARGET}
echo "CARGO_TARGET_${ENV_TARGET}_LINKER=${GCC_TARGET}-gcc" >> $GITHUB_ENV
echo "BINDGEN_EXTRA_CLANG_ARGS=--sysroot=/usr/${GCC_TARGET}" >> $GITHUB_ENV
- name: Setup Rust
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -238,20 +224,20 @@ jobs:
with:
command: build
args: --manifest-path gbm-sys/Cargo.toml --target ${{ matrix.target }} --features update_bindings
- name: Copy bindings
run: cp gbm-sys/src/bindings.rs bindings-${{ matrix.target }}.rs
- name: Upload bindings
if: matrix.task == 'bindings'
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: bindings
path: |
gbm-sys/${{ env.GBM_SYS_BINDINGS_FILE }}
LICENSE
README.md
path: bindings-*.rs
- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --target ${{ matrix.target }}
# Build without wayland feature so libwayland isn't needed for cross test
args: --target ${{ matrix.target }} --no-default-features
- name: Test
if: contains(matrix.target, '-linux-') && (startsWith(matrix.target, 'x86_64-') || startsWith(matrix.target, 'i686-'))
uses: actions-rs/cargo@v1
Expand All @@ -262,17 +248,46 @@ jobs:
command: test
args: --all --target ${{ matrix.target }}

compare-bindings:
needs:
- test
runs-on: ubuntu-22.04
steps:
- name: download bindings
uses: actions/download-artifact@v3
with:
name: bindings
- name: compare
run: |
code=0
for i in bindings-*.rs
do
if cmp -s bindings-x86_64-unknown-linux-gnu.rs ${i}
then
echo ${i} matches x86_64 bindings
else
echo ${i} does not match x86_64 bindings
diff bindings-x86_64-unknown-linux-gnu.rs ${i}
code=1
fi
done
exit ${code}
update-bindings:
if: ${{ github.event_name != 'pull_request' && !startsWith(github.ref, 'refs/tags/') }}
needs:
- test
- compare-bindings
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: Download bindings
uses: actions/download-artifact@v2
- uses: actions/checkout@v3
- name: download bindings
uses: actions/download-artifact@v3
with:
name: bindings
- name: Copy x86_64 bindings
run: |
cp bindings-x86_64-unknown-linux-gnu.rs gbm-sys/src/bindings.rs
rm bindings-*.rs
- name: Create pull request
uses: peter-evans/create-pull-request@v3
with:
Expand All @@ -294,7 +309,7 @@ jobs:
- test
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
Expand Down
9 changes: 7 additions & 2 deletions gbm-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ license = "MIT"
path = "src/lib.rs"

[build-dependencies.bindgen]
version = "0.58"
version = "0.69"
optional = true

# Specify version of bindgen deps to fix `check-minimal` test
[build-dependencies]
proc-macro2 = { version = "1.0.69", optional = true }
regex = { version = "1.10", optional = true }

[dependencies]
libc = "0.2"

[features]
gen = ["bindgen"]
gen = ["bindgen", "dep:proc-macro2", "dep:regex"]
update_bindings = ["gen"]
63 changes: 15 additions & 48 deletions gbm-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
#[cfg(feature = "gen")]
extern crate bindgen;

use std::{env, path::Path};

#[cfg(not(feature = "gen"))]
fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();

let bindings_file = Path::new("src")
.join("platforms")
.join(&target_os)
.join(&target_arch)
.join("gen.rs");

if bindings_file.is_file() {
println!(
"cargo:rustc-env=GBM_SYS_BINDINGS_PATH={}/{}",
target_os, target_arch
);
} else {
panic!(
"No prebuilt bindings for target OS `{}` and/or architecture `{}`. Try `gen` feature.",
target_os, target_arch
);
}
}
fn main() {}

#[cfg(feature = "gen")]
fn main() {
use std::{env, path::Path};

const TMP_BIND_PREFIX: &str = "__BINDGEN_TMP_";
const TMP_BIND_PREFIX_REG: &str = "_BINDGEN_TMP_.*";

Expand Down Expand Up @@ -101,42 +80,30 @@ fn main() {
.header_contents("bindings.h", &create_header())
.blocklist_type(TMP_BIND_PREFIX_REG)
.ctypes_prefix("libc")
.allowlist_type(r"^gbm_.*$")
.allowlist_function(r"^gbm_.*$")
.allowlist_type("^gbm_.*$")
.allowlist_function("^gbm_.*$")
.allowlist_var("GBM_.*|gbm_.*")
.constified_enum_module(r"^gbm_.*$")
.constified_enum_module("^gbm_.*$")
// Layout tests are incorrect across architectures
.layout_tests(false)
.generate()
.unwrap();

println!("cargo:rerun-if-changed=include/gbm.h");

// Generate the bindings
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("gen.rs");
let dest_path = Path::new(&out_dir).join("bindings.rs");

generated.write_to_file(dest_path).unwrap();

#[cfg(feature = "update_bindings")]
{
use std::{fs, io::Write};

let bind_file = Path::new(&out_dir).join("gen.rs");
let dest_dir = Path::new("src")
.join("platforms")
.join(env::var("CARGO_CFG_TARGET_OS").unwrap())
.join(env::var("CARGO_CFG_TARGET_ARCH").unwrap());
let dest_file = dest_dir.join("gen.rs");

fs::create_dir_all(&dest_dir).unwrap();
fs::copy(&bind_file, &dest_file).unwrap();

if let Ok(github_env) = env::var("GITHUB_ENV") {
let mut env_file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(github_env)
.unwrap();
writeln!(env_file, "GBM_SYS_BINDINGS_FILE={}", dest_file.display()).unwrap();
}
use std::fs;

let bind_file = Path::new(&out_dir).join("bindings.rs");
let dest_file = "src/bindings.rs";

fs::copy(bind_file, dest_file).unwrap();
}
}
Loading

0 comments on commit 6bdb1bc

Please sign in to comment.