Skip to content

Commit 5c67d05

Browse files
committed
Fix build, start using libc instead of core
1 parent 11ca70e commit 5c67d05

File tree

6 files changed

+32
-47
lines changed

6 files changed

+32
-47
lines changed

rts/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TOMMATHFILES = \
2121
MUSLFILES = \
2222
pow pow_data sin cos tan asin acos atan atan2 exp exp_data log log_data fmod \
2323
towctrans iswspace iswupper iswlower iswalpha wcschr wcslen \
24-
floor scalbn frexp strnlen memchr memset memcpy snprintf vsnprintf vfprintf \
24+
floor scalbn frexp strnlen memchr printf snprintf vsnprintf vfprintf \
2525
__math_oflow __math_uflow __math_xflow __math_divzero __math_invalid \
2626
__rem_pio2 __rem_pio2_large __sin __cos __tan \
2727
stubs
@@ -158,7 +158,11 @@ _build/wasm/musl_%.o: %.c | _build/wasm
158158

159159
.PHONY: _build/wasm/libmotoko_rts.a
160160
_build/wasm/libmotoko_rts.a: | _build/wasm
161-
cd motoko-rts && cargo build --target=wasm32-unknown-emscripten --release
161+
# NB. codegen-units=1 is to make debugging easier, not strictly
162+
# necessary
163+
cd motoko-rts && \
164+
xargo rustc --target=wasm32-unknown-emscripten --release -- \
165+
-Crelocation-model=pic -Ccodegen-units=1
162166
cp motoko-rts/target/wasm32-unknown-emscripten/release/libmotoko_rts.a $@
163167

164168
.PHONY: _build/native/libmotoko_rts.a
@@ -179,6 +183,7 @@ mo-rts.wasm: $(RTS_WASM_O) $(RTS_RUST_WASM_O) $(TOMMATH_WASM_O) $(MUSL_WASM_O)
179183
$(WASM_LD) -o $@ \
180184
--import-memory --shared --no-entry --gc-sections \
181185
--export=__wasm_call_ctors \
186+
--whole-archive \
182187
$+
183188

184189
#

rts/motoko-rts/.cargo/config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# NB. codegen-units=1 is not necessary, but it generates less .o files for core,
2+
# std, and compiler_builtins and makes it easier to find symbols.
3+
[build]
4+
rustflags = ["-Crelocation-model=pic", "-Ccodegen-units=1"]

rts/motoko-rts/Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rts/motoko-rts/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ version = "0.1.0"
44
authors = ["Ömer Sinan Ağacan <[email protected]>"]
55
edition = "2018"
66

7+
[dependencies]
8+
libc = "0.2.73"
9+
710
[lib]
811
crate-type = ["staticlib"]
912

rts/motoko-rts/src/common.rs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern "C" {
22
pub fn rts_trap_with(msg: *const u8) -> !;
33
}
44

5+
/*
56
#[cfg(target_arch = "wasm32")]
67
mod ic0 {
78
// NB: The #[link(...)] line below really needs to be before `extern "C"` part, we can't move
@@ -12,46 +13,10 @@ mod ic0 {
1213
pub fn debug_print(msg: *const u8, len: u32);
1314
}
1415
}
16+
*/
1517

1618
#[cfg(target_arch = "wasm32")]
1719
pub unsafe fn debug_print(s: &str) {
18-
ic0::debug_print(s.as_ptr(), s.len() as u32)
19-
}
20-
21-
pub struct Wrapper<'a> {
22-
buf: &'a mut [u8],
23-
offset: usize,
24-
}
25-
26-
impl<'a> Wrapper<'a> {
27-
pub fn new(buf: &'a mut [u8]) -> Self {
28-
Wrapper {
29-
buf: buf,
30-
offset: 0,
31-
}
32-
}
33-
}
34-
35-
use core::fmt;
36-
37-
impl<'a> fmt::Write for Wrapper<'a> {
38-
fn write_str(&mut self, s: &str) -> fmt::Result {
39-
let bytes = s.as_bytes();
40-
41-
// Skip over already-copied data
42-
let remainder = &mut self.buf[self.offset..];
43-
// Check if there is space remaining (return error instead of panicking)
44-
if remainder.len() < bytes.len() {
45-
return Err(core::fmt::Error);
46-
}
47-
// Make the two slices the same length
48-
let remainder = &mut remainder[..bytes.len()];
49-
// Copy
50-
remainder.copy_from_slice(bytes);
51-
52-
// Update offset to avoid overwriting
53-
self.offset += bytes.len();
54-
55-
Ok(())
56-
}
20+
// ic0::debug_print(s.as_ptr(), s.len() as u32)
21+
libc::printf("%s\n".as_ptr() as *const _, s);
5722
}

rts/motoko-rts/src/gc.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
// TODO: inconsistent use of size vs. len
77

88
use core::arch::wasm32;
9-
use core::fmt::Write;
109

1110
use crate::array::array_idx_unchecked;
12-
use crate::common::{debug_print, rts_trap_with, Wrapper};
11+
use crate::common::{debug_print, rts_trap_with};
1312
use crate::types::*;
1413

1514
extern "C" {
@@ -427,12 +426,12 @@ pub unsafe extern "C" fn rust_collect_garbage() {
427426
debug_print("### Evacuating roots");
428427

429428
let mut buf = [0 as u8; 100];
430-
let _ = write!(
431-
Wrapper::new(&mut buf),
432-
"### begin_from_space={}",
429+
libc::snprintf(
430+
buf.as_mut_ptr() as *mut _,
431+
100,
432+
"### begin_from_space=%d".as_ptr() as *const _,
433433
begin_from_space.unskew()
434434
);
435-
436435

437436
// Evacuate roots
438437
end_to_space = evac_static_roots(begin_from_space, begin_to_space, end_to_space, static_roots);

0 commit comments

Comments
 (0)