Skip to content

Commit

Permalink
first init
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Feb 5, 2024
0 parents commit 323b717
Show file tree
Hide file tree
Showing 19 changed files with 1,562 additions and 0 deletions.
126 changes: 126 additions & 0 deletions .github/workflows/cross-compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
name: cross-compile

on:
workflow_dispatch:
schedule:
- cron: '10 20 * * *'
pull_request:
paths:
- 'cross-compile/**'
- '**.yml'
push:
branches:
- main
- master
paths:
- 'cross-compile/**'
- '**.yml'

# Allow one concurrent deployment
concurrency:
group: "cross-compile"
cancel-in-progress: true

env:
OUTPUT: /tmp/cc.md

jobs:
go:
timeout-minutes: 600
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: cross-compile
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
steps:
- uses: actions/checkout@v2
- name: Install Zig
uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 'stable'
- name: Prepare
run: |
echo "| GOOS | GOARCH | Status |" >> $OUTPUT
echo "|:-:|:-:|:-:|" >> $OUTPUT
- run: |
GOOS=linux GOARCH=386 ./run-go.sh
- run: |
GOOS=linux GOARCH=amd64 ./run-go.sh
- run: |
GOOS=linux GOARCH=arm ./run-go.sh
- run: |
GOOS=linux GOARCH=arm64 ./run-go.sh
- run: |
GOOS=windows GOARCH=386 ./run-go.sh
- run: |
GOOS=windows GOARCH=amd64 ./run-go.sh
- run: |
GOOS=windows GOARCH=arm ./run-go.sh
- run: |
GOOS=windows GOARCH=arm64 ./run-go.sh
- run: |
GOOS=darwin GOARCH=amd64 ./run-go.sh
- run: |
GOOS=darwin GOARCH=arm64 ./run-go.sh
- run: |
cat $OUTPUT >> $GITHUB_STEP_SUMMARY
rust:
timeout-minutes: 600
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: cross-compile
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
# zig-targets: [x86_64-windows, x86_64-linux, x86_64-macos, aarch64-macos]
steps:
- uses: actions/checkout@v2
- name: Cache Rust Dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo
key: rust-${{ runner.os }}
- name: Install Zig
uses: goto-bus-stop/setup-zig@v1
with:
version: master
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Prepare
run: |
cargo install cargo-zigbuild
echo "| Target | Project | Build | Status |" >> $OUTPUT
echo "|:-:|:-:|:-:|:-:|" >> $OUTPUT
- run: |
./run-rust.sh aarch64-unknown-linux-gnu
- run: |
./run-rust.sh i686-pc-windows-gnu
- run: |
./run-rust.sh i686-pc-windows-msvc
- run: |
./run-rust.sh i686-unknown-linux-gnu
- run: |
./run-rust.sh x86_64-apple-darwin
- run: |
./run-rust.sh x86_64-pc-windows-gnu
- run: |
./run-rust.sh x86_64-pc-windows-msvc
- run: |
./run-rust.sh x86_64-unknown-linux-gnu
- run: |
cat $OUTPUT >> $GITHUB_STEP_SUMMARY
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ROOT_DIR = $(shell pwd)
CC = $(ROOT_DIR)/zigcc
CXX = $(ROOT_DIR)/zigcxx

clean:
@for dir in $(shell ls -d */ | grep -v go-mode); do pushd $${dir} && cargo clean && popd ; done

run-rust:
@for dir in $(shell ls -d */ | grep -v go-demo); do \
cd $(ROOT_DIR)/$${dir} ; \
CARGO_BUILD_TARGET=$(TARGET) CC=$(CC) CXX=$(CXX) cargo build; \
done

run-go:
GOOS=linux GOARCH=386 ./run-go.sh
GOOS=linux GOARCH=amd64 ./run-go.sh
GOOS=linux GOARCH=arm ./run-go.sh
GOOS=linux GOARCH=arm64 ./run-go.sh
GOOS=windows GOARCH=386 ./run-go.sh
GOOS=windows GOARCH=amd64 ./run-go.sh
GOOS=windows GOARCH=arm ./run-go.sh
GOOS=windows GOARCH=arm64 ./run-go.sh
GOOS=darwin GOARCH=amd64 ./run-go.sh
GOOS=darwin GOARCH=arm64 ./run-go.sh
23 changes: 23 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
* Zig-cc
Use Zig to cross compile Go/Rust projects.
#+begin_src bash :results verbatim :exports both
uname -a
zig version
cargo version
#+end_src

#+RESULTS:
: Darwin mario 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020 arm64
: 0.12.0-dev.2307+1b8f7e46f
: cargo 1.70.0 (ec8a8a0ca 2023-04-25)

** Config
#+begin_src conf
# cat ~/.cargo/config

[target.x86_64-unknown-linux-musl]
linker = "zigcc"

[target.x86_64-unknown-linux-gnu]
linker = "zigcc"
#+end_src
3 changes: 3 additions & 0 deletions go-demo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module cgo-demo

go 1.20
24 changes: 24 additions & 0 deletions go-demo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

/*
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import "unsafe"

// import "C" 与上面的注释不能有空行,否则会报下面的错误:
// cmd/cgo: could not determine kind of name for C.FILE
// https://github.com/golang/go/issues/9733

func main() {
cs := C.CString("hello world")
defer C.free(unsafe.Pointer(cs))

C.fputs(cs, (*C.FILE)(C.stdout))

// cgo 不能调用 C 里面的 printf ,因为它是变参函数(variadic functions),报错信息如下:
// cgo: ./main.go:23:2: unexpected type: ...
// https://stackoverflow.com/a/26853031/2163429
// C.printf(cs)
}
7 changes: 7 additions & 0 deletions hello-world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions hello-world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "demo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
Loading

0 comments on commit 323b717

Please sign in to comment.