Skip to content

Commit

Permalink
Iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Feb 8, 2024
1 parent 7faaf28 commit 6d9fb04
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 78 deletions.
4 changes: 0 additions & 4 deletions src/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use clap::ArgAction;

use crate::build_kind::BuildKind;

#[derive(Debug)]
pub struct Args {
pub script: Option<String>,
Expand All @@ -13,7 +11,6 @@ pub struct Args {
pub clear_cache: bool,
pub debug: bool,
pub force: bool,
pub build_kind: BuildKind,
pub toolchain_version: Option<String>,
#[cfg(windows)]
pub install_file_association: bool,
Expand Down Expand Up @@ -163,7 +160,6 @@ impl Args {
clear_cache: m.get_flag("clear-cache"),
debug: m.get_flag("debug"),
force: m.get_flag("force"),
build_kind: BuildKind::from_flags(m.get_flag("test"), m.get_flag("bench")),
toolchain_version: m.get_one::<String>("toolchain").map(Into::into),
#[cfg(windows)]
install_file_association: m.get_flag("install-file-association"),
Expand Down
25 changes: 0 additions & 25 deletions src/build_kind.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ How old can stuff in the cache be before we automatically clear it out?
Measured in milliseconds.
*/
pub const MAX_CACHE_AGE_MS: u128 = 7 * 24 * 60 * 60 * 1000;

pub const TOOLCHAIN_VERSION: &str = "2023-09-30";
70 changes: 29 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![forbid(unsafe_code)]

mod arguments;
mod build_kind;
mod consts;
mod defer;
mod error;
Expand All @@ -26,7 +25,6 @@ use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::build_kind::BuildKind;
use crate::defer::Defer;
use crate::error::{MainError, MainResult};

Expand Down Expand Up @@ -229,22 +227,12 @@ struct InputAction {
*/
using_cache: bool,

/**
Which toolchain the script should be built with.
`None` indicates that the script should be built with a stable toolchain.
*/
toolchain_version: Option<String>,

/// If script should be built in debug mode.
debug: bool,

/// The package manifest contents.
manifest: String,

/// Did the user ask to run tests or benchmarks?
build_kind: BuildKind,

// Name of the built binary
bin_name: String,

Expand All @@ -262,7 +250,7 @@ impl InputAction {
&self,
script_args: &[String],
) -> MainResult<Command> {
let release_mode = !self.debug && !matches!(self.build_kind, BuildKind::Bench);
let release_mode = !self.debug;

let built_binary_path = platform::binary_cache_path()
.join(if release_mode { "release" } else { "debug" })
Expand All @@ -289,7 +277,7 @@ impl InputAction {
Ok(cmd)
};

if matches!(self.build_kind, BuildKind::Normal) && !self.force_compile {
if !self.force_compile {
match fs::File::open(&built_binary_path) {
Ok(built_binary_file) => {
// When possible, use creation time instead of modified time as cargo may copy
Expand Down Expand Up @@ -328,15 +316,34 @@ impl InputAction {
}
}

let maybe_toolchain_version = self.toolchain_version.as_deref();
// TODO: Relative to current binary?
let toolchain_path = "/home/fornwall/src/rust-gpu-compiler/tmp/nightly-2023-09-30-x86_64-unknown-linux-gnu";
let librustc_codegen_spirv_path = "/home/fornwall/src/rust-gpu-compiler/tmp/librustc_codegen_spirv.so";

let mut cmd = Command::new("cargo");
if let Some(toolchain_version) = maybe_toolchain_version {
cmd.arg(format!("+{}", toolchain_version));
}
cmd.arg(self.build_kind.exec_command());
let cargo_path = format!("{toolchain_path}/bin/cargo");
let mut cmd = Command::new(&cargo_path);

// cmd.arg(format!("+{}", consts::TOOLCHAIN_VERSION));

if matches!(self.build_kind, BuildKind::Normal) && !self.cargo_output {
cmd.arg("build");

// rust-gpu flags: https://embarkstudios.github.io/rust-gpu/book/writing-shader-crates.html
// TODO: Default, but take optional from cmdline arg
let target = "spirv-unknown-spv1.3";
cmd.arg("--target");
cmd.arg(target);
cmd.arg("-Z");
cmd.arg("build-std=core");
cmd.arg("-Z");
cmd.arg("build-std-features=compiler-builtins-mem");
cmd.env("RUSTFLAGS", format!("-Zcodegen-backend={librustc_codegen_spirv_path} \
-Zbinary-dep-depinfo \
-Csymbol-mangling-version=v0 \
-Zcrate-attr=feature(register_tool) \
-Zcrate-attr=register_tool(rust_gpu)"));


if !self.cargo_output {
cmd.arg("-q");
}

Expand All @@ -354,13 +361,11 @@ impl InputAction {
cmd.arg("--release");
}

if matches!(self.build_kind, BuildKind::Normal) {
if cmd.status()?.code() == Some(0) {
cmd = execute_command()?;
} else {
return Err(MainError::OtherOwned("Could not execute cargo".to_string()));
}
}

Ok(cmd)
}
Expand Down Expand Up @@ -390,14 +395,6 @@ fn decide_action_for(
info!("pkg_path: {:?}", pkg_path);
info!("using_cache: {:?}", using_cache);

let toolchain_version = args
.toolchain_version
.clone()
.or_else(|| match args.build_kind {
BuildKind::Bench => Some("nightly".into()),
_ => None,
});

let script_name = format!("{}.rs", input.safe_name());

let base_path = match &args.base_path {
Expand All @@ -411,26 +408,17 @@ fn decide_action_for(
&pkg_path,
&bin_name,
&script_name,
toolchain_version.clone(),
)?;

// Forcibly override some flags based on build kind.
let debug = match args.build_kind {
BuildKind::Normal => args.debug,
BuildKind::Test => true,
BuildKind::Bench => false,
};

Ok(InputAction {
cargo_output: args.cargo_output,
force_compile: args.force,
pkg_path,
script_path,
using_cache,
toolchain_version,
debug,
debug: args.debug,
manifest: mani_str,
build_kind: args.build_kind,
bin_name,
#[cfg(unix)]
original_script_path: args.script.clone(),
Expand Down
10 changes: 2 additions & 8 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ This module is concerned with how `rust-script` extracts the manfiest from a scr
use regex;

use self::regex::Regex;
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;

use crate::consts;
use crate::error::{MainError, MainResult};
use crate::templates;
use crate::Input;
use log::{error, info};

Expand All @@ -26,7 +24,6 @@ pub fn split_input(
package_path: impl AsRef<Path>,
bin_name: &str,
script_name: &str,
toolchain: Option<String>,
) -> MainResult<(String, PathBuf, String)> {
let (part_mani, source_path, source) = match input {
Input::File(_, path, content) => {
Expand All @@ -52,7 +49,7 @@ pub fn split_input(
.ok_or_else(|| format!("Unable to stringify {source_path:?}"))?;

// It's-a mergin' time!
let def_mani = default_manifest(bin_name, source_path_from_package, toolchain);
let def_mani = default_manifest(bin_name, source_path_from_package);

let mani = merge_manifest(def_mani, part_mani)?;

Expand Down Expand Up @@ -1085,7 +1082,6 @@ Generates a default Cargo manifest for the given input.
fn default_manifest(
bin_name: &str,
bin_source_path: &str,
toolchain: Option<String>,
) -> toml::value::Table {
let mut package_map = toml::map::Map::new();
package_map.insert(
Expand All @@ -1104,19 +1100,17 @@ fn default_manifest(
"edition".to_string(),
toml::value::Value::String("2021".to_string()),
);
if let Some(toolchain) = toolchain {
let mut metadata = toml::map::Map::new();
let mut rustscript_metadata = toml::map::Map::new();
rustscript_metadata.insert(
"toolchain".to_string(),
toml::value::Value::String(toolchain),
toml::value::Value::String(consts::TOOLCHAIN_VERSION.to_string()),
);
metadata.insert(
"rustscript".to_string(),
toml::value::Value::Table(rustscript_metadata),
);
package_map.insert("metadata".to_string(), toml::value::Value::Table(metadata));
}

let mut release_map = toml::map::Map::new();
release_map.insert("strip".to_string(), toml::value::Value::Boolean(true));
Expand Down

0 comments on commit 6d9fb04

Please sign in to comment.