packaging stuff

This commit is contained in:
Sebastian Moser
2024-11-27 21:16:19 +01:00
parent 58e5fc9daf
commit 23721f306f
5 changed files with 37 additions and 26 deletions

View File

@@ -93,3 +93,4 @@ wasm-bindgen-test = { version = "0.3.34" }
[profile.release] [profile.release]
# Tell `rustc` to optimize for small code size. # Tell `rustc` to optimize for small code size.
opt-level = "s" opt-level = "s"

View File

@@ -55,6 +55,9 @@ let
src = self; src = self;
doCheck = false; # tests does not work in wasm doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
cargoExtraArgs = "--features wasm-target --no-default-features";
RUSTFLAGS="-C linker=wasm-ld";
buildInputs = with pkgs; [ cargo-binutils lld ];
}); });
defaultMizeConfig = { defaultMizeConfig = {
@@ -124,6 +127,7 @@ in {
cargoExtraArgs = "--bin mize --features wasm-target"; cargoExtraArgs = "--bin mize --features wasm-target";
doCheck = false; doCheck = false;
}; };
inherit wasmArtifacts;
# Thanks to: # Thanks to:
# - https://github.com/dl-solarity/hardhat-diamond-tools/blob/e5b8bc9624fbf89eda83a8ce4f6b6672ea83f550/flake.nix#L103 # - https://github.com/dl-solarity/hardhat-diamond-tools/blob/e5b8bc9624fbf89eda83a8ce4f6b6672ea83f550/flake.nix#L103
@@ -144,7 +148,8 @@ in {
echo build hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii echo build hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
mkdir -p $out/pkg mkdir -p $out/pkg
HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --out-dir $out/pkg --scope=c2vi -- --no-default-features --features wasm-target export HOME=$(mktemp -d fake-homeXXXX)
wasm-pack build --out-dir $out/pkg --scope=c2vi -- --no-default-features --features wasm-target
''; '';
buildInputs = with pkgs; [ wasm-bindgen-cli binaryen wasm-pack wasmToolchain ]; buildInputs = with pkgs; [ wasm-bindgen-cli binaryen wasm-pack wasmToolchain ];

47
lib.nix
View File

@@ -129,19 +129,20 @@ rec {
toolchain_version = pkgs.lib.strings.removeSuffix "\n" (builtins.readFile "${toolchain_version_drv}/rustc-version"); toolchain_version = pkgs.lib.strings.removeSuffix "\n" (builtins.readFile "${toolchain_version_drv}/rustc-version");
mkMizeModule = attrs: pkgsCross.stdenv.mkDerivation ({ mkMizeModule = attrs: pkgsCross.stdenv.mkDerivation ( rec {
name = attrs.modName; name = attrs.modName;
inherit (attrs) modName; inherit (attrs) modName;
selector_string = mkSelString (attrs.select or {} // { selector_string = mkSelString (attrs.select or {} // {
inherit (attrs) modName; inherit (attrs) modName;
}); });
hash = builtins.substring 0 32 (builtins.hashString "sha256" selector_string);
} // attrs); } // attrs);
########## build Rust Module ########## build Rust Module
mkMizeRustModule = attrs: craneLib.buildPackage (attrs // { mkMizeRustModule = attrs: craneLib.buildPackage (attrs // {
MIZE_BUILD_CONFIG = mizeBildConfig; MIZE_BUILD_CONFIG = mizeBuildConfigStr;
selector_string = mkSelString (attrs.select or {} // { selector_string = mkSelString (attrs.select or {} // {
inherit (attrs) modName; inherit (attrs) modName;
}); });
@@ -202,6 +203,7 @@ rec {
buildModule = path: extraArgs: pkgs.callPackage ((import path).module or (attrs: null)) ({ buildModule = path: extraArgs: pkgs.callPackage ((import path).module or (attrs: null)) ({
inherit buildMizeForSystem mizeBuildConfig mizeBuildConfigStr;
inherit mkSelString craneLib toolchain_version; inherit mkSelString craneLib toolchain_version;
inherit mkMizeModule mkMizeRustModule buildModule findModules crossSystem pkgsCross pkgsNative mkMizeRustShell; inherit mkMizeModule mkMizeRustModule buildModule findModules crossSystem pkgsCross pkgsNative mkMizeRustShell;
mize_version = main-default.version; mize_version = main-default.version;
@@ -214,14 +216,16 @@ rec {
}; };
mizeBildConfig = let mizeBuildConfig = {
settingsFormat = pkgs.formats.toml { };
in settingsFormat.generate "mize-build-config.toml" {
config = {
namespace = "mize.buildtime.ns"; namespace = "mize.buildtime.ns";
module_url = "c2vi.dev"; module_url = "c2vi.dev";
selector = builtins.fromJSON (mkSelString {}); selector = builtins.fromJSON (mkSelString {});
}; };
mizeBuildConfigStr = let
settingsFormat = pkgs.formats.toml { };
in settingsFormat.generate "mize-build-config.toml" {
config = mizeBuildConfig;
}; };
####### build mize ####### build mize
@@ -240,7 +244,7 @@ rec {
(if crossSystem.nameFull == "x86_64-unknown-linux-gnu" then pkgs.openssl else pkgsCross.openssl) (if crossSystem.nameFull == "x86_64-unknown-linux-gnu" then pkgs.openssl else pkgsCross.openssl)
]; ];
MIZE_BUILD_CONFIG = mizeBildConfig; MIZE_BUILD_CONFIG = mizeBuildConfigStr;
# patch the interpreter to run on most linux-gnu distros # patch the interpreter to run on most linux-gnu distros
postBuild = postBuild =
@@ -272,7 +276,7 @@ rec {
CARGO_BUILD_TARGET = "${crossSystem.cpu.name}-pc-windows-gnu"; CARGO_BUILD_TARGET = "${crossSystem.cpu.name}-pc-windows-gnu";
MIZE_BUILD_CONFIG = mizeBildConfig; MIZE_BUILD_CONFIG = mizeBuildConfigStr;
# fixes issues related to libring # fixes issues related to libring
TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc"; TARGET_CC = "${pkgs.pkgsCross.mingwW64.stdenv.cc}/bin/${pkgs.pkgsCross.mingwW64.stdenv.cc.targetPrefix}cc";
@@ -295,6 +299,8 @@ rec {
doCheck = false; # tests does not work in wasm doCheck = false; # tests does not work in wasm
CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
cargoExtraArgs = "--features wasm-target --no-default-features"; cargoExtraArgs = "--features wasm-target --no-default-features";
RUSTFLAGS="-C linker=wasm-ld";
buildInputs = with pkgs; [ cargo-binutils lld ];
}); });
main-wasm = craneLib.mkCargoDerivation { main-wasm = craneLib.mkCargoDerivation {
name = "mize-npm-package"; name = "mize-npm-package";
@@ -302,21 +308,26 @@ rec {
# i can't use CARGO_BUILD_TARGET to set the target # i can't use CARGO_BUILD_TARGET to set the target
# because then the cargo install run by wasm-pack tries to build the wasm-bindgen-cli for a wasm target... # because then the cargo install run by wasm-pack tries to build the wasm-bindgen-cli for a wasm target...
src = self; src = self;
doInstallCargoArtifacts = false;
cargoArtifacts = wasmArtifacts; cargoArtifacts = wasmArtifacts;
doCheck = false; doCheck = false;
buildPhaseCargoCommand = '' buildPhaseCargoCommand = ''
mkdir -p $out/pkg mkdir -p $out/pkg
HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --out-dir $out/pkg --scope=c2vi -- --features wasm-target --no-default-features HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --target no-modules --out-dir $out/pkg --scope=c2vi -- --features wasm-target --no-default-features
'';
postInstall = ''
rm -rf $out/target.tar.zst
''; '';
buildInputs = with pkgsNative; [ wasm-bindgen-cli binaryen wasm-pack ]; buildInputs = with pkgsNative; [ wasm-bindgen-cli binaryen wasm-pack ];
MIZE_BUILD_CONFIG = mizeBildConfig; MIZE_BUILD_CONFIG = mizeBuildConfigStr;
}; };
@@ -351,7 +362,7 @@ rec {
}); });
mkMizeModuleShell = attrs: pkgs.mkShell (attrs // { mkMizeModuleShell = attrs: pkgs.mkShell (attrs // {
MIZE_BUILD_CONFIG = mizeBildConfig; MIZE_BUILD_CONFIG = mizeBuildConfigStr;
}); });
@@ -372,7 +383,7 @@ rec {
pkg-config pkg-config
]; ];
MIZE_BUILD_CONFIG = mizeBildConfig; MIZE_BUILD_CONFIG = mizeBuildConfigStr;
shellHook = '' shellHook = ''
export MIZE_CONFIG_FILE=${self}/test-config.toml export MIZE_CONFIG_FILE=${self}/test-config.toml
@@ -412,13 +423,7 @@ rec {
# install mize # install mize
mkdir -p $out/mize/${mize.system.name} mkdir -p $out/mize/${mize.system.name}
${ cp -r ${mize.main}/* $out/mize/${mize.system.name}/
if mize.system.kernel.name == "windows"
then "cp ${mize.main}/bin/mize.exe $out/mize/${mize.system.name}/"
else if mize.system.cpu.name == "wasm32"
then "cp ${mize.main}/lib/mize.wasm $out/mize/${mize.system.name}/"
else "cp ${mize.main}/bin/mize $out/mize/${mize.system.name}/"
}
# intall the modules # intall the modules
${mkModulesInstallPhase mize.modulesList} ${mkModulesInstallPhase mize.modulesList}
@@ -427,7 +432,7 @@ rec {
mkModulesInstallPhase = modules: pkgs.lib.concatStringsSep "\n" (map mkModuleInstallPhase modules); mkModulesInstallPhase = modules: pkgs.lib.concatStringsSep "\n" (map mkModuleInstallPhase modules);
mkModuleInstallPhase = module: let mkModuleInstallPhase = module: let
hash = builtins.substring 0 32 (builtins.hashString "sha256" module.selector_string); hash = module.hach;
in '' in ''
echo got module: ${module.name} echo got module: ${module.name}
echo out: $out echo out: $out

View File

@@ -16,11 +16,11 @@ pub struct EmptyModule {
} }
impl Module for EmptyModule { impl Module for EmptyModule {
fn init(&mut self, instance: Instance) -> MizeResult<()> { fn init(&mut self, instance: &Instance) -> MizeResult<()> {
println!("empty module fn init"); println!("empty module fn init");
Ok(()) Ok(())
} }
fn exit(&mut self, instance: Instance) -> MizeResult<()> { fn exit(&mut self, instance: &Instance) -> MizeResult<()> {
println!("empty module fn exit"); println!("empty module fn exit");
Ok(()) Ok(())

View File

@@ -98,6 +98,6 @@ pub fn instance_init(instance: &mut core::instance::Instance) {}
pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<()> { Ok(()) } pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<()> { Ok(()) }
pub fn fetch_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) } pub fn fetch_module(instance: &mut core::instance::Instance, name: &str) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) }