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

Use uniffi.toml config file if it exist #1278

Merged
merged 3 commits into from
Nov 19, 2022
Merged
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
4 changes: 2 additions & 2 deletions .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[profile.default]
# Terminate slow tests after 5 minutes
slow-timeout = { period = "60s", terminate-after = 5 }
# Terminate slow tests after 10 minutes
slow-timeout = { period = "60s", terminate-after = 10 }

[[profile.default.overrides]]
# See https://nexte.st/book/threads-required.html
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ jobs:
runs-on: ubuntu-latest
env:
RUST_BACKTRACE: '1'
CARGO_INCREMENTAL: '0'
CARGO_TERM_COLOR: always
container: alpine:edge
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -342,6 +344,9 @@ jobs:
name: Test Docker
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
env:
CARGO_INCREMENTAL: '0'
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
Expand Down
86 changes: 72 additions & 14 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,31 @@ pub fn write_cffi_module(
Ok(())
}

fn generate_uniffi_bindings(crate_dir: &Path, target_dir: &Path) -> Result<PathBuf> {
/// uniffi.toml
#[derive(Debug, serde::Deserialize)]
struct UniFfiToml {
#[serde(default)]
bindings: HashMap<String, UniFfiBindingsConfig>,
}

/// `bindings` section of uniffi.toml
#[derive(Debug, serde::Deserialize)]
struct UniFfiBindingsConfig {
cdylib_name: Option<String>,
}

#[derive(Debug, Clone)]
struct UniFfiBindings {
name: String,
cdylib: String,
path: PathBuf,
}

fn generate_uniffi_bindings(
crate_dir: &Path,
target_dir: &Path,
target_os: Os,
) -> Result<UniFfiBindings> {
let binding_dir = target_dir.join("maturin").join("uniffi");
fs::create_dir_all(&binding_dir)?;

Expand All @@ -831,9 +855,29 @@ fn generate_uniffi_bindings(crate_dir: &Path, target_dir: &Path) -> Result<PathB
}

let udl = &udls[0];
let config_file = crate_dir.join("uniffi.toml");
let mut cdylib_name = None;
let config_file = {
if config_file.is_file() {
let uniffi_toml: UniFfiToml =
toml_edit::easy::from_str(&fs::read_to_string(&config_file)?)?;
cdylib_name = uniffi_toml
.bindings
.get("python")
.and_then(|py| py.cdylib_name.clone());
Some(
config_file
.as_path()
.try_into()
.expect("path contains non-utf8"),
)
} else {
None
}
};
uniffi_bindgen::generate_bindings(
udl.as_path().try_into().expect("path contains non-utf8"),
None,
config_file,
vec!["python"],
Some(
binding_dir
Expand All @@ -846,7 +890,24 @@ fn generate_uniffi_bindings(crate_dir: &Path, target_dir: &Path) -> Result<PathB
)?;
let py_binding_name = udl.file_stem().unwrap();
let py_binding = binding_dir.join(py_binding_name).with_extension("py");
Ok(py_binding)
let name = py_binding_name.to_str().unwrap().to_string();

// uniffi bindings hardcoded the extension filenames
let cdylib_name = match cdylib_name {
Some(name) => name,
None => format!("uniffi_{}", name),
};
let cdylib = match target_os {
Os::Macos => format!("lib{}.dylib", cdylib_name),
Os::Windows => format!("{}.dll", cdylib_name),
_ => format!("lib{}.so", cdylib_name),
};

Ok(UniFfiBindings {
name,
cdylib,
path: py_binding,
})
}

/// Creates the uniffi module with the shared library
Expand All @@ -862,15 +923,12 @@ pub fn write_uniffi_module(
editable: bool,
pyproject_toml: Option<&PyProjectToml>,
) -> Result<()> {
let uniffi_binding = generate_uniffi_bindings(crate_dir, target_dir)?;
let binding_name = uniffi_binding.file_stem().unwrap().to_str().unwrap();
let UniFfiBindings {
name: binding_name,
cdylib,
path: uniffi_binding,
} = generate_uniffi_bindings(crate_dir, target_dir, target_os)?;
let py_init = format!("from .{} import * # NOQA\n", binding_name);
// uniffi bindings hardcoded the extension filenames
let dylib_name = match target_os {
Os::Macos => format!("libuniffi_{}.dylib", binding_name),
Os::Windows => format!("uniffi_{}.dll", binding_name),
_ => format!("libuniffi_{}.so", binding_name),
};

let module;

Expand All @@ -883,15 +941,15 @@ pub fn write_uniffi_module(
if editable {
let base_path = python_module.join(module_name);
fs::create_dir_all(&base_path)?;
let target = base_path.join(&dylib_name);
let target = base_path.join(&cdylib);
fs::copy(artifact, &target).context(format!(
"Failed to copy {} to {}",
artifact.display(),
target.display()
))?;

File::create(base_path.join("__init__.py"))?.write_all(py_init.as_bytes())?;
let target = base_path.join(binding_name).with_extension("py");
let target = base_path.join(&binding_name).with_extension("py");
fs::copy(&uniffi_binding, &target).context(format!(
"Failed to copy {} to {}",
uniffi_binding.display(),
Expand Down Expand Up @@ -926,7 +984,7 @@ pub fn write_uniffi_module(
module.join(binding_name).with_extension("py"),
uniffi_binding,
)?;
writer.add_file_with_permissions(&module.join(dylib_name), artifact, 0o755)?;
writer.add_file_with_permissions(&module.join(cdylib), artifact, 0o755)?;
}

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions test-crates/uniffi-pure/uniffi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bindings.python]
cdylib_name = "example"