Skip to content

Commit 37565bc

Browse files
committed
1 parent d628c86 commit 37565bc

File tree

11 files changed

+249
-1
lines changed

11 files changed

+249
-1
lines changed

.github/workflows/ci.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule: [cron: "40 1 * * *"]
7+
8+
permissions:
9+
contents: read
10+
11+
env:
12+
CARGO_TERM_COLOR: always
13+
OPENBSD_COMMIT: 6354f7248140d3c98676a00bc94948c0a0e89826
14+
15+
jobs:
16+
test:
17+
name: test ${{ matrix.os }} rust ${{ matrix.rust }}
18+
runs-on: ${{ matrix.os }}
19+
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
os: [ubuntu-latest, macos-latest]
24+
rust: [stable, beta, nightly]
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install Homebrew llvm on macOS
30+
if: matrix.os == 'macos-latest'
31+
run: brew install llvm
32+
33+
- run: rustup default ${{ matrix.rust }}
34+
- run: rustup update ${{ matrix.rust }}
35+
- run: rustup target add wasm32-unknown-unknown
36+
- run: curl -L -o code.tar.gz https://github.com/openbsd/src/archive/${{env.OPENBSD_COMMIT}}.tar.gz
37+
- run: tar xzf code.tar.gz src-${{env.OPENBSD_COMMIT}}/include/ src-${{env.OPENBSD_COMMIT}}/sys/sys/ src-${{env.OPENBSD_COMMIT}}/lib/libc/string/ src-${{env.OPENBSD_COMMIT}}/lib/libc/stdlib/
38+
- run: mv src-${{env.OPENBSD_COMMIT}} openbsd-src
39+
40+
- run: find openbsd-src -type f -name "*.3" -exec rm -f {} +
41+
- run: rm openbsd-src/sys/sys/videoio.h
42+
- run: rm openbsd-src/sys/sys/sysctl.h
43+
- run: rm openbsd-src/lib/libc/stdlib/malloc.c
44+
45+
- run: rm code.tar.gz
46+
- run: cargo build --target wasm32-unknown-unknown
47+
48+
- if: matrix.os == 'macos-latest'
49+
run: cargo publish --allow-dirty --no-verify --dry-run
50+
51+
- if: matrix.os != 'macos-latest'
52+
run: cargo publish --allow-dirty --dry-run
53+
54+
publish:
55+
needs: [test]
56+
runs-on: ubuntu-latest
57+
if: ${{ contains(github.event.head_commit.message, 'wasm32-unknown-unknown-openbsd-libc@') && github.event_name == 'push' && github.ref == 'refs/heads/main' }}
58+
59+
steps:
60+
- uses: actions/checkout@v4
61+
62+
- run: rustup default stable
63+
- run: rustup update stable
64+
- run: curl -L -o code.tar.gz https://github.com/openbsd/src/archive/${{env.OPENBSD_COMMIT}}.tar.gz
65+
- run: tar xzf code.tar.gz src-${{env.OPENBSD_COMMIT}}/include/ src-${{env.OPENBSD_COMMIT}}/sys/sys/ src-${{env.OPENBSD_COMMIT}}/lib/libc/string/ src-${{env.OPENBSD_COMMIT}}/lib/libc/stdlib/
66+
- run: mv src-${{env.OPENBSD_COMMIT}} openbsd-src
67+
68+
- run: find openbsd-src -type f -name "*.3" -exec rm -f {} +
69+
- run: rm openbsd-src/sys/sys/videoio.h
70+
- run: rm openbsd-src/sys/sys/sysctl.h
71+
- run: rm openbsd-src/lib/libc/stdlib/malloc.c
72+
73+
- run: rm code.tar.gz
74+
75+
- run: cargo publish --allow-dirty --token ${{ secrets.CARGO_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
Cargo.lock

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
description = "The parts of OpenBSD libc that make sense on wasm32-unknown-unknown."
3+
edition = "2018"
4+
license = "BSD-3-Clause AND ISC AND MIT"
5+
name = "wasm32-unknown-unknown-openbsd-libc"
6+
repository = "https://github.com/trevyn/wasm32-unknown-unknown-openbsd-libc"
7+
version = "0.1.0"
8+
9+
[build-dependencies]
10+
cc = "1"

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
# wasm32-unknown-unknown-openbsd-libc
1+
# wasm32-unknown-unknown-openbsd-libc
2+
3+
A small chunk of OpenBSD's libc, conveniently packaged as a Rust crate.
4+
5+
Created for compiling C code that relies on a reasonable subset of libc to `wasm32-unknown-unknown`.
6+
7+
```toml
8+
[dependencies]
9+
wasm32-unknown-unknown-openbsd-libc = "0.1"
10+
11+
[build-dependencies]
12+
wasm32-unknown-unknown-openbsd-libc = "0.1"
13+
```
14+
15+
In your `build.rs` using `cc`:
16+
17+
```rust
18+
cfg.includes(wasm32_unknown_unknown_openbsd_libc::includes());
19+
20+
println!("cargo:rustc-link-lib=wasm32-unknown-unknown-openbsd-libc");
21+
```

build.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use std::env;
2+
3+
fn main() {
4+
if !env::var("TARGET").map_or(false, |t| t.starts_with("wasm")) {
5+
return;
6+
}
7+
8+
// Apple clang doesn't support wasm32, so use Homebrew clang by default.
9+
if env::var("HOST") == Ok("x86_64-apple-darwin".to_string()) {
10+
if env::var("CC").is_err() {
11+
std::env::set_var("CC", "/usr/local/opt/llvm/bin/clang");
12+
}
13+
if env::var("AR").is_err() {
14+
std::env::set_var("AR", "/usr/local/opt/llvm/bin/llvm-ar");
15+
}
16+
} else if env::var("HOST") == Ok("aarch64-apple-darwin".to_string()) {
17+
if env::var("CC").is_err() {
18+
std::env::set_var("CC", "/opt/homebrew/opt/llvm/bin/clang");
19+
}
20+
if env::var("AR").is_err() {
21+
std::env::set_var("AR", "/opt/homebrew/opt/llvm/bin/llvm-ar");
22+
}
23+
}
24+
25+
let sources = [
26+
"stdlib/heapsort.c",
27+
"stdlib/qsort.c",
28+
"string/bcmp.c",
29+
"string/bcmp.c",
30+
"string/bcmp.c",
31+
"string/bcopy.c",
32+
"string/bzero.c",
33+
"string/explicit_bzero.c",
34+
"string/ffs.c",
35+
"string/memccpy.c",
36+
"string/memchr.c",
37+
"string/memcmp.c",
38+
// "string/memcpy.c",
39+
"string/memmem.c",
40+
// "string/memmove.c",
41+
"string/memrchr.c",
42+
// "string/memset.c",
43+
"string/stpcpy.c",
44+
"string/stpncpy.c",
45+
"string/strcasecmp.c",
46+
"string/strcasecmp_l.c",
47+
"string/strcasestr.c",
48+
"string/strcat.c",
49+
"string/strchr.c",
50+
"string/strcmp.c",
51+
"string/strcoll.c",
52+
"string/strcpy.c",
53+
"string/strcspn.c",
54+
"string/strdup.c",
55+
"string/strerror.c",
56+
// "string/strerror_l.c",
57+
// "string/strerror_r.c",
58+
"string/strlcat.c",
59+
"string/strlcpy.c",
60+
"string/strlen.c",
61+
"string/strmode.c",
62+
"string/strncat.c",
63+
"string/strncmp.c",
64+
"string/strncpy.c",
65+
"string/strndup.c",
66+
"string/strnlen.c",
67+
"string/strpbrk.c",
68+
"string/strrchr.c",
69+
"string/strsep.c",
70+
// "string/strsignal.c",
71+
"string/strspn.c",
72+
"string/strstr.c",
73+
"string/strtok.c",
74+
"string/strxfrm.c",
75+
"string/strxfrm_l.c",
76+
"string/swab.c",
77+
"string/timingsafe_bcmp.c",
78+
"string/timingsafe_memcmp.c",
79+
];
80+
81+
let sources = sources
82+
.iter()
83+
.map(|f| format!("openbsd-src/lib/libc/{}", f))
84+
.collect::<Vec<_>>();
85+
86+
cc::Build::new()
87+
.include("openbsd-src/sys")
88+
.include("openbsd-src/include")
89+
.include("include")
90+
.files(sources)
91+
.file("src/errno.c")
92+
.flag("-w")
93+
.compile("wasm32-unknown-unknown-openbsd-libc");
94+
}

include/machine/_types.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* clang -target wasm32-unknown-unknown -x c /dev/null -dM -E */
2+
3+
typedef __INTPTR_TYPE__ __intptr_t;
4+
typedef __PTRDIFF_TYPE__ __ptrdiff_t;
5+
typedef __WCHAR_TYPE__ __wchar_t;
6+
typedef __WINT_TYPE__ __wint_t;
7+
typedef __INT8_TYPE__ __int8_t;
8+
typedef __UINT8_TYPE__ __uint8_t;
9+
typedef __INT16_TYPE__ __int16_t;
10+
typedef __UINT16_TYPE__ __uint16_t;
11+
typedef __INT32_TYPE__ __int32_t;
12+
typedef __UINT32_TYPE__ __uint32_t;
13+
typedef __INT64_TYPE__ __int64_t;
14+
typedef __UINT64_TYPE__ __uint64_t;
15+
16+
/* Register size */
17+
typedef long __register_t;
18+
19+
/* VM system types */
20+
typedef unsigned long __vaddr_t;
21+
typedef unsigned long __paddr_t;
22+
typedef unsigned long __vsize_t;
23+
typedef unsigned long __psize_t;
24+
25+
typedef unsigned long __size_t;
26+
typedef long __ssize_t;
27+
28+
typedef float __float_t;
29+
typedef double __double_t;
30+
31+
struct __va_list_tag;
32+
typedef struct __va_list_tag *__va_list;

include/machine/cdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#define DEF_WEAK(x)
2+
#define DEF_STRONG(x)
3+
#define __weak_alias(x, y)

include/machine/endian.h

Whitespace-only changes.

include/machine/limits.h

Whitespace-only changes.

src/errno.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int *__errno(void) {
2+
static int errno;
3+
return &errno;
4+
}

0 commit comments

Comments
 (0)