From 7ce8cadaa5c8b0b31c8414b0102362c3cac6b07e Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Date: Wed, 28 Sep 2022 15:45:26 +0200 Subject: [PATCH] Ci and multiplatform build (#2) * Make go discoverable in unix systems * Refactor build script * Fix which path * Fix typo in error message * Added gh actions ci * Fix lib_dir path * Use checkout v3 * Use recursive submodules * Filter branches Use submodule force update * Use git directly instead of action * Build go with relative paths * Added missing cargo config file * Use target os instead of family * Add targets to matrix * Try to use default for target * Set toolchain * Fix toolchain matrix --- .cargo/config.toml | 4 ++ .github/workflows/main.yml | 109 +++++++++++++++++++++++++++++++++++++ waku-sys/build.rs | 61 ++++++++++++++++----- 3 files changed, 160 insertions(+), 14 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 .github/workflows/main.yml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..5bc05eb --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,4 @@ +[target.'cfg(target_os = "macos")'] +# when using osx, we need to link against some golang libraries, it did just work with this missing flags +# from: https://github.com/golang/go/issues/42459 +rustflags = ["-C", "link-args=-framework CoreFoundation -framework Security"] \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..cb3f848 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,109 @@ +# copy of https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md +on: + push: + branches: [master] + pull_request: + branches: + - "*" + +name: CI + +jobs: + check: + name: Check + strategy: + matrix: + include: + - os: ubuntu-latest + toolchain: stable-x86_64-unknown-linux-gnu + - os: windows-latest + toolchain: stable-x86_64-pc-windows-gnu + - os: macos-latest + toolchain: stable-x86_64-apple-darwin + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Checkout submodules + run: git submodule update --init --recursive + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.toolchain }} + default: true + override: true + - uses: actions-rs/cargo@v1 + continue-on-error: false + with: + command: check + + test: + name: Test Suite + strategy: + matrix: + include: + - os: ubuntu-latest + toolchain: stable-x86_64-unknown-linux-gnu + - os: windows-latest + toolchain: stable-x86_64-pc-windows-gnu + - os: macos-latest + toolchain: stable-x86_64-apple-darwin + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Checkout submodules + run: git submodule update --init --recursive + - uses: actions/setup-go@v3 # we need go to build go-waku + with: + go-version: '1.19' + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.toolchain }} + default: true + override: true + - uses: actions-rs/cargo@v1 + continue-on-error: false + with: + command: build + - uses: actions-rs/cargo@v1 + continue-on-error: false + with: + command: test + + + lints: + name: Rust lints + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Checkout submodules + run: git submodule update --init --recursive + - uses: actions/setup-go@v3 # we need go to build go-waku + with: + go-version: '1.19' + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt, clippy + + - name: Run cargo fmt + uses: actions-rs/cargo@v1 + continue-on-error: false + with: + command: fmt + args: --all -- --check + + - name: Run cargo clippy + uses: actions-rs/cargo@v1 + continue-on-error: false + with: + command: clippy + args: -- --deny warnings \ No newline at end of file diff --git a/waku-sys/build.rs b/waku-sys/build.rs index b0ac775..af75627 100644 --- a/waku-sys/build.rs +++ b/waku-sys/build.rs @@ -1,29 +1,53 @@ -use bindgen; use std::env; use std::env::set_current_dir; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::Command; -fn main() { +fn get_go_bin() -> String { + if cfg!(target_family = "unix") { + let output = String::from_utf8( + Command::new("/usr/bin/which") + .arg("go") + .output() + .map_err(|e| println!("cargo:warning=Couldn't find `which` command: {}", e)) + .expect("`which` command not found") + .stdout, + ) + .expect("which output couldnt be parsed"); + + if output.is_empty() { + println!("cargo:warning=Couldn't find go binary installed, please ensure that it is installed and/or withing the system paths"); + panic!("Couldn't find `go` binary installed"); + } + output.trim().to_string() + } else if cfg!(target_family = "windows") { + "go".into() + } else { + panic!("OS not supported!"); + } +} + +fn build_go_waku_lib(go_bin: &str, project_dir: &Path) { // Build go-waku static lib // build command taken from waku make file: // https://github.com/status-im/go-waku/blob/eafbc4c01f94f3096c3201fb1e44f17f907b3068/Makefile#L115 - let output_lib = "libgowaku.a"; - set_current_dir("./vendor").unwrap(); - Command::new("go") + let vendor_path = project_dir.join("vendor"); + set_current_dir(vendor_path).expect("Moving to vendor dir"); + Command::new(go_bin) + .env("CGO_ENABLED", "1") .arg("build") .arg("-buildmode=c-archive") .arg("-o") - .arg(format!("./build/lib/{output_lib}")) - .arg("./library/") + .arg("./build/lib/libgowaku.a") + .arg("./library") .status() - .map_err(|e| println!("cargo:warning={}", e)) + .map_err(|e| println!("cargo:warning=go build failed due to: {}", e)) .unwrap(); - set_current_dir("../").unwrap(); - let mut lib_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); - lib_dir.push("vendor"); - lib_dir.push("build"); - lib_dir.push("lib"); + set_current_dir(project_dir).expect("Going back to project dir"); +} + +fn generate_bindgen_code(project_dir: &Path) { + let lib_dir = project_dir.join("vendor/build/lib"); println!("cargo:rustc-link-search={}", lib_dir.display()); println!("cargo:rustc-link-lib=static=gowaku"); @@ -48,3 +72,12 @@ fn main() { .write_to_file(out_path.join("bindings.rs")) .expect("Couldn't write bindings!"); } + +fn main() { + let go_bin = get_go_bin(); + + let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + + build_go_waku_lib(&go_bin, &project_dir); + generate_bindgen_code(&project_dir); +}