Skip to content

Commit

Permalink
Merge pull request #900 from ulrikstrid/ulrikstrid--add-rust-targets
Browse files Browse the repository at this point in the history
Add rust targets
  • Loading branch information
domenkozar committed Apr 15, 2024
2 parents 8d77a55 + 40f434d commit 02196df
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/rust-wasm-cross/.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import("./app/pkg/app.js").then((app) => app.main());
17 changes: 17 additions & 0 deletions examples/rust-wasm-cross/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -ex
cargo --version
rustc --version

if [[ "$(uname)" == "Darwin" ]]; then
echo "$RUSTFLAGS" | grep -- "-L framework=$DEVENV_PROFILE/Library/Frameworks"
echo "$RUSTDOCFLAGS" | grep -- "-L framework=$DEVENV_PROFILE/Library/Frameworks"
echo "$CFLAGS" | grep -- "-iframework $DEVENV_PROFILE/Library/Frameworks"
fi

[[ "$CARGO_INSTALL_ROOT" == "$DEVENV_STATE/cargo-install" ]]
echo "$PATH" | grep -- "$CARGO_INSTALL_ROOT/bin"

wasm-pack build ./app --target nodejs

node .test.js
123 changes: 123 additions & 0 deletions examples/rust-wasm-cross/app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/rust-wasm-cross/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[lib]
crate-type = ["cdylib", "rlib"]

[package]
name = "app"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasm-bindgen = "0.2.84"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

[workspace]
36 changes: 36 additions & 0 deletions examples/rust-wasm-cross/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use wasm_bindgen::prelude::*;

// First up let's take a look of binding `console.log` manually, without the
// help of `web_sys`. Here we're writing the `#[wasm_bindgen]` annotations
// manually ourselves, and the correctness of our program relies on the
// correctness of these annotations!

#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);

// The `console.log` is quite polymorphic, so we can bind it with multiple
// signatures. Note that we need to use `js_name` to ensure we always call
// `log` in JS.
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);

// Multiple arguments too!
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_many(a: &str, b: &str);
}

macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}

// Called by our JS entry point to run the example.
#[wasm_bindgen]
pub fn main() {
console_log!("Hello, from devenv!");
}
26 changes: 26 additions & 0 deletions examples/rust-wasm-cross/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{ pkgs, lib, ... }:

{
languages.rust = {
enable = true;
# https://devenv.sh/reference/options/#languagesrustchannel
channel = "nightly";

targets = [ "wasm32-unknown-unknown" ];

components = [ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" "rust-std" ];
};

# These break us
# pre-commit.hooks = {
# rustfmt.enable = true;
# clippy.enable = true;
# };

packages = [
pkgs.wasm-pack
pkgs.nodejs
] ++ lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin.apple_sdk; [
frameworks.Security
]);
}
6 changes: 6 additions & 0 deletions examples/rust-wasm-cross/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
inputs:
fenix:
url: github:nix-community/fenix
inputs:
nixpkgs:
follows: nixpkgs
42 changes: 39 additions & 3 deletions src/modules/languages/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ in
'';
};

targets = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
defaultText = lib.literalExpression ''[ ]'';
description = ''
List of extra [targets](https://github.com/nix-community/fenix#supported-platforms-and-targets)
to install. Defaults to only the native target.
'';
};

channel = lib.mkOption {
type = lib.types.enum [ "nixpkgs" "stable" "beta" "nightly" ];
default = "nixpkgs";
Expand Down Expand Up @@ -76,7 +86,10 @@ in
export PATH="$PATH:$CARGO_INSTALL_ROOT/bin"
'';

packages = (builtins.map (c: cfg.toolchain.${c} or (throw "toolchain.${c}")) cfg.components)
packages =
# If there are targets we want to add the whole toolchain instead
# TODO: It might always be fine to add the whole toolchain when not using `nixpkgs`
lib.optionals (cfg.targets == [ ]) (builtins.map (c: cfg.toolchain.${c} or (throw "toolchain.${c}")) cfg.components)
++ lib.optional pkgs.stdenv.isDarwin pkgs.libiconv;

# enable compiler tooling by default to expose things like cc
Expand Down Expand Up @@ -108,10 +121,33 @@ in
let
toolchain =
if cfg.channel == "nightly"
then rustPackages.latest
else rustPackages.${cfg.channel};
then
rustPackages.latest
else
rustPackages.${cfg.channel}
;
in
(builtins.mapAttrs (_: pkgs.lib.mkDefault) toolchain);

packages = [
(rustPackages.combine
(
(map (c: config.languages.rust.toolchain.${c}) cfg.components) ++
(map
(t:
let
target_toolchain =
if cfg.channel == "nightly"
then
rustPackages.targets.${t}.latest
else
rustPackages.targets.${t}.${cfg.channel}
;
in
target_toolchain.rust-std)
cfg.targets)
))
];
}
))
];
Expand Down

0 comments on commit 02196df

Please sign in to comment.