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
This commit is contained in:
Daniel Sanchez 2022-09-28 15:45:26 +02:00 committed by GitHub
parent 5c35417f49
commit 7ce8cadaa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 160 additions and 14 deletions

4
.cargo/config.toml Normal file
View File

@ -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"]

109
.github/workflows/main.yml vendored Normal file
View File

@ -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

View File

@ -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);
}