Skip to content

Commit 76a36d1

Browse files
committed
feat: it works
1 parent e8ed5a0 commit 76a36d1

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

crates/pixi-build-mojo/src/build_script.j2

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
{% endmacro -%}
55

66
{# - Set up common variables -#}
7-
{%- set build_dir = "build" -%}
8-
{%- set source_dir = env("SRC_DIR") -%}
97
{%- set library_prefix = "%LIBRARY_PREFIX%" if build_platform == "windows" else "$PREFIX" -%}
108

119
mojo --version
10+
mkdir -p {{ source_dir }}/{{ dist }}
1211

13-
mojo {{ extra_args | join(" ") }} -o {{ library_prefix }}/main
12+
{#- Build any binaries -#}
13+
{% if bins %}
14+
{% for bin in bins %}
15+
mojo build {{ bin.extra_args | join(" ") }} {{ bin.path }} -o {{ library_prefix }}/{{ bin.name }}
16+
cp {{ library_prefix }}/{{ bin.name }} {{ source_dir }}/{{ dist }}
17+
{% endfor %}
18+
{% endif %}
19+
20+
{#- Build pkg -#}
21+
{% if pkg %}
22+
mojo package {{ pkg.extra_args | join(" ") }} {{ pkg.path }} -o {{ library_prefix }}/{{ pkg.name}}.mojopkg
23+
cp {{ library_prefix }}/{{ pkg.name }}.mojopkg {{ source_dir }}/{{ dist }}
24+
{% endif %}

crates/pixi-build-mojo/src/build_script.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1+
use super::config::{MojoBinConfig, MojoPkgConfig};
12
use minijinja::Environment;
23
use serde::Serialize;
34

4-
#[derive(Serialize)]
5+
#[derive(Debug, Serialize)]
56
pub struct BuildScriptContext {
7+
/// The platform that the build is taking place on.
68
pub build_platform: BuildPlatform,
9+
/// The directory where the source code is located, the manifest root.
710
pub source_dir: String,
8-
pub extra_args: Vec<String>,
11+
/// The directory name to place output artifacts, will be created in `source_dir`.
12+
pub dist: String,
13+
/// Any executable artifacts to create.
14+
pub bins: Option<Vec<MojoBinConfig>>,
15+
/// Any packages to create.
16+
pub pkg: Option<MojoPkgConfig>,
17+
18+
/// Not currenlty used
919
/// The package has a host dependency on Python.
1020
/// This is used to determine if the build script
1121
/// should include Python-related logic.
1222
pub has_host_python: bool,
1323
}
1424

15-
#[derive(Serialize)]
25+
#[derive(Debug, Serialize)]
1626
#[serde(rename_all = "kebab-case")]
1727
pub enum BuildPlatform {
1828
Unix,

crates/pixi-build-mojo/src/config.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@ use std::path::{Path, PathBuf};
22

33
use indexmap::IndexMap;
44
use pixi_build_backend::generated_recipe::BackendConfig;
5-
use serde::Deserialize;
5+
use serde::{Deserialize, Serialize};
66

7-
#[derive(Debug, Default, Deserialize)]
7+
#[derive(Debug, Deserialize)]
88
#[serde(rename_all = "kebab-case")]
99
pub struct MojoBackendConfig {
10-
/// Extra args for mojo invocation
11-
#[serde(default)]
12-
pub extra_args: Vec<String>,
1310
/// Environment Variables
1411
#[serde(default)]
1512
pub env: IndexMap<String, String>,
1613

14+
/// Directory that will be created to place output artifacts.
15+
///
16+
/// This is releative to the manifest dir.
17+
#[serde(default = "default_dist_dir")]
18+
pub dist_dir: PathBuf,
19+
20+
/// Dir that can be specified for outputting pixi debug state.
1721
pub debug_dir: Option<PathBuf>,
22+
23+
/// Binary executables to produce.
24+
pub bins: Option<Vec<MojoBinConfig>>,
25+
26+
/// Packages to produce.
27+
pub pkg: Option<MojoPkgConfig>,
1828
}
1929

2030
impl BackendConfig for MojoBackendConfig {
@@ -23,6 +33,44 @@ impl BackendConfig for MojoBackendConfig {
2333
}
2434
}
2535

36+
impl Default for MojoBackendConfig {
37+
fn default() -> Self {
38+
Self {
39+
env: Default::default(),
40+
dist_dir: default_dist_dir(),
41+
debug_dir: Default::default(),
42+
bins: Default::default(),
43+
pkg: Default::default(),
44+
}
45+
}
46+
}
47+
48+
fn default_dist_dir() -> PathBuf {
49+
PathBuf::from("target")
50+
}
51+
52+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
53+
#[serde(rename_all = "kebab-case")]
54+
pub struct MojoBinConfig {
55+
pub name: String,
56+
pub path: String,
57+
#[serde(default, rename(serialize = "extra_args"))]
58+
pub extra_args: Option<Vec<String>>,
59+
#[serde(default)]
60+
pub env: IndexMap<String, String>,
61+
}
62+
63+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
64+
#[serde(rename_all = "kebab-case")]
65+
pub struct MojoPkgConfig {
66+
pub name: String,
67+
pub path: String,
68+
#[serde(default, rename(serialize = "extra_args"))]
69+
pub extra_args: Option<Vec<String>>,
70+
#[serde(default)]
71+
pub env: IndexMap<String, String>,
72+
}
73+
2674
#[cfg(test)]
2775
mod tests {
2876
use serde_json::json;

crates/pixi-build-mojo/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ impl GenerateRecipe for MojoGenerator {
4949
.build
5050
.push(mojo_compiler_pkg.parse().into_diagnostic()?);
5151
}
52-
eprintln!("resolved compiler");
5352

5453
// Check if the host platform has a host python dependency
5554
// TODO: surely this will be needed for compiling bindings or something? or maybe those
5655
// will be handled by uv?
5756
let has_host_python = resolved_requirements.contains(&PackageName::new_unchecked("python"));
5857

58+
// TODO: have different build scripts based on configuration?
59+
5960
let build_script = BuildScriptContext {
6061
build_platform: if build_platform.is_windows() {
6162
return Err(Error::msg(
@@ -65,18 +66,18 @@ impl GenerateRecipe for MojoGenerator {
6566
BuildPlatform::Unix
6667
},
6768
source_dir: manifest_root.display().to_string(),
68-
extra_args: config.extra_args.clone(),
69+
dist: config.dist_dir.display().to_string(),
70+
bins: config.bins.clone(),
71+
pkg: config.pkg.clone(),
6972
has_host_python,
7073
}
7174
.render();
72-
eprintln!("rendered build script");
7375

7476
generated_recipe.recipe.build.script = Script {
7577
content: build_script,
7678
env: config.env.clone(),
7779
..Default::default()
7880
};
79-
eprintln!("Recipe script created");
8081

8182
Ok(generated_recipe)
8283
}

0 commit comments

Comments
 (0)