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

lib: Supporting building for wasm32-wasip1 #4519

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ jobs:
- name: Build
run: cargo build -p jj-lib --no-default-features --verbose

build-wasi:
name: Build jj-lib for wasi
# This requires the nightly compiler as many wasi pieces (like symlinks) are behind a flag.
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332

- name: Install Rust
uses: dtolnay/rust-toolchain@1482605bfc5719782e1267fd0c0cc350fe7646b8
with:
toolchain: nightly
targets: wasm32-wasip1
- name: Build
run: cargo +nightly build -p jj-lib --no-default-features --target wasm32-wasip1 --verbose

check-protos:
name: Check protos
runs-on: ubuntu-latest
Expand Down
16 changes: 16 additions & 0 deletions lib/src/file_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ mod platform {
}
}

#[cfg(target_os = "wasi")]
mod platform {
use std::io;
use std::os::wasi::fs::symlink_path;
use std::path::Path;

/// Symlinks are available on wasi.
pub fn check_symlink_support() -> io::Result<bool> {
Ok(true)
}

pub fn try_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
symlink_path(original, link)
}
}

#[cfg(windows)]
mod platform {
use std::io;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#![warn(missing_docs)]
#![deny(unused_must_use)]
#![forbid(unsafe_code)]
// Required for wasi symlink support.
#![cfg_attr(target_os = "wasi", feature(wasi_ext))]

// Needed so that proc macros can be used inside jj_lib and by external crates
// that depend on it.
Expand Down
21 changes: 11 additions & 10 deletions lib/src/local_working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ use crate::working_copy::WorkingCopyStateError;

#[cfg(unix)]
type FileExecutableFlag = bool;
#[cfg(windows)]
// Wasi doesn't have a concept of an executable file.
#[cfg(any(windows, target_os = "wasi"))]
Earthmark marked this conversation as resolved.
Show resolved Hide resolved
type FileExecutableFlag = ();

#[derive(Debug, PartialEq, Eq, Clone)]
Expand All @@ -132,7 +133,7 @@ impl FileState {
fn placeholder() -> Self {
#[cfg(unix)]
let executable = false;
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
let executable = ();
FileState {
file_type: FileType::Normal { executable },
Expand All @@ -142,7 +143,7 @@ impl FileState {
}

fn for_file(executable: bool, size: u64, metadata: &Metadata) -> Self {
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
let executable = {
// Windows doesn't support executable bit.
let _ = executable;
Expand Down Expand Up @@ -349,7 +350,7 @@ fn file_state_from_proto(proto: &crate::protos::working_copy::FileState) -> File
#[cfg(unix)]
crate::protos::working_copy::FileType::Executable => FileType::Normal { executable: true },
// can exist in files written by older versions of jj
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
crate::protos::working_copy::FileType::Executable => FileType::Normal { executable: () },
crate::protos::working_copy::FileType::Symlink => FileType::Symlink,
crate::protos::working_copy::FileType::Conflict => FileType::Normal {
Expand All @@ -371,7 +372,7 @@ fn file_state_to_proto(file_state: &FileState) -> crate::protos::working_copy::F
FileType::Normal { executable: false } => crate::protos::working_copy::FileType::Normal,
#[cfg(unix)]
FileType::Normal { executable: true } => crate::protos::working_copy::FileType::Executable,
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
FileType::Normal { executable: () } => crate::protos::working_copy::FileType::Normal,
FileType::Symlink => crate::protos::working_copy::FileType::Symlink,
FileType::GitSubmodule => crate::protos::working_copy::FileType::GitSubmodule,
Expand Down Expand Up @@ -492,7 +493,7 @@ fn file_state(metadata: &Metadata) -> Option<FileState> {
} else {
Some(FileType::Normal { executable: false })
}
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
Some(FileType::Normal { executable: () })
} else {
None
Expand Down Expand Up @@ -1199,7 +1200,7 @@ impl TreeState {
let _ = current_tree_value; // use the variable
let id = self.write_file_to_store(repo_path, disk_path).await?;
// On Windows, we preserve the executable bit from the current tree.
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
let executable = {
let () = executable; // use the variable
if let Some(TreeValue::File { id: _, executable }) = current_tree_value {
Expand All @@ -1224,7 +1225,7 @@ impl TreeState {
match new_file_ids.into_resolved() {
Ok(file_id) => {
// On Windows, we preserve the executable bit from the merged trees.
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
let executable = {
let () = executable; // use the variable
if let Some(merge) = current_tree_values.to_executable_merge() {
Expand Down Expand Up @@ -1323,7 +1324,7 @@ impl TreeState {
Ok(FileState::for_file(executable, size, &metadata))
}

#[cfg_attr(windows, allow(unused_variables))]
#[cfg_attr(any(windows, target_os = "wasi"), allow(unused_variables))]
fn set_executable(&self, disk_path: &Path, executable: bool) -> Result<(), CheckoutError> {
#[cfg(unix)]
{
Expand Down Expand Up @@ -1515,7 +1516,7 @@ impl TreeState {
Ok(value) => match value.unwrap() {
#[cfg(unix)]
TreeValue::File { id: _, executable } => FileType::Normal { executable },
#[cfg(windows)]
#[cfg(any(windows, target_os = "wasi"))]
TreeValue::File { .. } => FileType::Normal { executable: () },
TreeValue::Symlink(_id) => FileType::Symlink,
TreeValue::Conflict(_id) => {
Expand Down