Skip to content

Commit

Permalink
Merge pull request #433 from cbgbt/cross-build-golang
Browse files Browse the repository at this point in the history
Cross build golang in CI
  • Loading branch information
cbgbt authored Jan 7, 2025
2 parents 207de61 + c9b0442 commit b0347e6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ jobs:
with:
go-version: "^1.18"
- run: make build

cross-build:
runs-on:
group: bottlerocket
labels: bottlerocket_ubuntu-latest_16-core
strategy:
matrix:
include:
- target: x86_64-unknown-linux-musl
- target: aarch64-unknown-linux-musl
env:
TARGET_TRIPLE: ${{ matrix.target }}
steps:
- uses: actions/checkout@v3
- name: Install Cross
# Pin cargo cross to a version that we know is working for us.
run: |
cargo install cross --git https://github.com/cross-rs/cross/ --rev 7b79041 --locked
- run: |
# cargo-cross warns us that multiple cross configurations are present in our
# workspace (ie dependencies have configurations), but it picks the appropriate one from
# the twoliter workspace.
#
# These warnings cause the tool to fail, so we disable them.
export CROSS_NO_WARNINGS=0
cross build --release --bin twoliter --target "${TARGET_TRIPLE}"
34 changes: 30 additions & 4 deletions tools/krane/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use std::path::PathBuf;
use std::process::Command;

const REQUIRED_TOOLS: &[&str] = &["go"];
const CFLAGS: &str = concat!(
"-O2 -g -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS ",
"-fexceptions -fstack-clash-protection -fno-omit-frame-pointer",
);
const LDFLAGS: &str = "-Wl,-z,relro -Wl,-z,now";

fn main() {
let script_dir = env::current_dir().unwrap();
Expand All @@ -14,17 +19,38 @@ fn main() {

// build krane FFI wrapper
let build_output_loc = out_dir.join("libkrane.a");
let exit_status = Command::new("go")
let mut build_command = Command::new("go");

let curr_cflags = env::var("CFLAGS").unwrap_or_default();
let desired_cflags = format!("{curr_cflags} {CFLAGS}");

let curr_ldflags = env::var("LDFLAGS").unwrap_or_default();
let desired_ldflags = format!("{curr_ldflags} {LDFLAGS}");

build_command
.env("GOOS", get_goos())
.env("GOARCH", get_goarch())
.env("CGO_ENABLED", "1")
.env("CFLAGS", &desired_cflags)
.env("CGO_CFLAGS", &desired_cflags)
.env("CXXFLAGS", &desired_cflags)
.env("CGO_CXXFLAGS", &desired_cflags)
.env("LDFLAGS", &desired_ldflags)
.env("CGO_LDFLAGS", &desired_ldflags)
.arg("build")
.arg("-buildmode=c-archive")
.arg("-o")
.arg(&build_output_loc)
.arg("main.go")
.current_dir(script_dir.join("go-src"))
.status()
.expect("Failed to build crane");
.current_dir(script_dir.join("go-src"));

// Set cross-compiler when using cargo-cross
let cross_cc_var = format!("CC_{}", env::var("TARGET").unwrap().replace("-", "_"));
if let Some(cross_cc) = env::var_os(&cross_cc_var) {
build_command.env("CC", cross_cc);
}

let exit_status = build_command.status().expect("Failed to build crane");

assert!(
exit_status.success(),
Expand Down

0 comments on commit b0347e6

Please sign in to comment.