Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross build golang in CI #433

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading