Skip to content

Commit f73fc4a

Browse files
authored
Mac compatibility (#128)
* Add bytes to be divisible by 8, add mac test support * Revert changes to base object file and make pad dependent on ar * Minor cleanup and make padding selection narrower * Try one more time with wasmtime 34 before removing and just adding macos wasmtime ci test to match ubuntu * Prune wasmtime dep because CI unhappy * Revert build changes * Just update assembly and use single padded object * Try using rust archiver tool for better portability * Downgrade ar_archiver to meet rust 1.63 requirements
1 parent 7fc1d95 commit f73fc4a

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

psm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ readme = "README.mkd"
1414
[dependencies]
1515

1616
[build-dependencies]
17+
ar_archive_writer = "0.2.0"
1718
cc = "1.2.33"

psm/build.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,61 @@ fn main() {
111111
// directly to `ar` to assemble an archive. Otherwise we're actually
112112
// compiling the source assembly file.
113113
if asm.ends_with(".o") {
114-
cfg.object(asm);
114+
use ar_archive_writer::{write_archive_to_stream, NewArchiveMember, ArchiveKind};
115+
use std::fs::{read, metadata};
116+
use std::path::PathBuf;
117+
use std::io::Cursor;
118+
119+
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
120+
let output_path = PathBuf::from(&out_dir).join("libpsm_s.a");
121+
122+
let object_data = read(asm).expect("Failed to read object file");
123+
let file_metadata = metadata(asm).expect("Failed to read file metadata");
124+
125+
// Extract file metadata
126+
let mtime = file_metadata.modified().ok()
127+
.and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
128+
.map(|duration| duration.as_secs())
129+
.unwrap_or(0);
130+
131+
#[cfg(unix)]
132+
let (uid, gid, perms) = {
133+
use std::os::unix::fs::MetadataExt;
134+
(file_metadata.uid(), file_metadata.gid(), file_metadata.mode())
135+
};
136+
137+
#[cfg(not(unix))]
138+
let (uid, gid, perms) = (0, 0, 0o644);
139+
140+
let filename = asm.rsplit('/').next().unwrap_or(asm);
141+
let member = NewArchiveMember {
142+
buf: Box::new(object_data),
143+
get_symbols: |_data: &[u8], _callback: &mut dyn FnMut(&[u8]) -> Result<(), std::io::Error>| Ok(true),
144+
member_name: filename.to_string(),
145+
mtime,
146+
uid,
147+
gid,
148+
perms,
149+
};
150+
151+
let mut output_bytes = Cursor::new(Vec::new());
152+
write_archive_to_stream(
153+
&mut output_bytes,
154+
&[member],
155+
// Unfortunately, getDefaultKind() is not available in ar_archive_writer
156+
// however looking at the llvm-ar source it looks like for wasm32-any-any
157+
// it falls through to Gnu https://llvm.org/doxygen/Object_2Archive_8cpp_source.html
158+
ArchiveKind::Gnu,
159+
false,
160+
).expect("Failed to write archive");
161+
162+
std::fs::write(&output_path, output_bytes.into_inner()).expect("Failed to write archive file");
163+
164+
println!("cargo:rustc-link-search=native={}", out_dir);
165+
println!("cargo:rustc-link-lib=static=psm_s");
115166
} else {
116167
cfg.file(asm);
168+
cfg.compile("libpsm_s.a");
117169
}
118170

119-
cfg.compile("libpsm_s.a");
120-
}
171+
}

0 commit comments

Comments
 (0)