Skip to content

Commit e274c2a

Browse files
authored
Add ic-repl and test fatfs example (#12)
* Add ic-repl and test fatfs example * Restore crate-type for Wasm build products * Revert back to using custom canister type * Remove src directory from examples * Provide interface in .did file * Change fatfs ls return type * Update how Rust packages are built * Update DFINITY SDK for patchelfing fix * Update DFINITY SDK for nativeBuildInputs fix * Attempt to fix build inputs * Try patchelfing binaries on linux * Try using --emulator * Don't use emulator, try different patchelfing * Update dfinity-sdk for unconditional patchelfing * Update dfinity-sdk for icx-proxy patchelfing * Try 0.9.2 * Update dfinity-sdk to remove patchelfing * Update dfinity-sdk to restore patchelfing * Revert back to 0.8.4 * Update dfinity-sdk to remove patchelfing * Remove Linux from the build matrix for now * Revert "Remove Linux from the build matrix for now" This reverts commit 4fe71be. * Only run example tests on darwin for now
1 parent a5b1a93 commit e274c2a

File tree

13 files changed

+143
-51
lines changed

13 files changed

+143
-51
lines changed

Diff for: Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
members = [
33
"crates/icfs",
44
"crates/icfs-fatfs",
5-
"examples/src/fatfs",
5+
"examples/fatfs",
66
]

Diff for: crates/icfs-fatfs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["Paul Young <[email protected]>"]
88

99
[lib]
1010
path = "lib.rs"
11+
crate-type = ["cdylib", "lib"]
1112

1213
[dependencies]
1314
fatfs = { git = "https://github.com/rafalh/rust-fatfs", rev = "87fc1ed5074a32b4e0344fcdde77359ef9e75432" }

Diff for: crates/icfs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["Paul Young <[email protected]>"]
88

99
[lib]
1010
path = "lib.rs"
11+
crate-type = ["cdylib", "lib"]
1112

1213
[dependencies]
1314
ic-cdk = { git = "https://github.com/dfinity/cdk-rs.git", rev = "a253119adb08929b6304d007ee0a6a37960656ed" }

Diff for: dfx.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"dfx": "0.8.4",
44
"canisters": {
55
"fatfs": {
6-
"type": "rust",
7-
"package": "fatfs_example",
8-
"candid": "examples/src/fatfs/fatfs.did"
6+
"type": "custom",
7+
"build": "nix build '.#fatfs-example'",
8+
"candid": "examples/fatfs/fatfs.did",
9+
"wasm": "result/lib/fatfs_example.wasm"
910
}
1011
},
1112
"networks": {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "fatfs_example"
2+
name = "fatfs-example"
33
version = "0.1.0"
44
edition = "2018"
55
authors = ["Paul Young <[email protected]>"]
@@ -8,12 +8,12 @@ authors = ["Paul Young <[email protected]>"]
88

99
[lib]
1010
path = "lib.rs"
11-
crate-type = ["cdylib"]
11+
crate-type = ["cdylib", "lib"]
1212

1313
[dependencies]
1414
fatfs = { git = "https://github.com/rafalh/rust-fatfs", rev = "87fc1ed5074a32b4e0344fcdde77359ef9e75432" }
1515
fscommon = "0.1"
1616
ic-cdk = { git = "https://github.com/dfinity/cdk-rs.git", rev = "a253119adb08929b6304d007ee0a6a37960656ed" }
1717
ic-cdk-macros = "0.3"
18-
icfs = { path = "../../../crates/icfs" }
19-
icfs-fatfs = { path = "../../../crates/icfs-fatfs" }
18+
icfs = { path = "../../crates/icfs" }
19+
icfs-fatfs = { path = "../../crates/icfs-fatfs" }

Diff for: examples/fatfs/fatfs.did

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
service : {
2+
ls : () -> (vec text) query;
3+
write_file : (filename : text, contents : text) -> ();
4+
read_file : (filename : text) -> (text) query;
5+
}

Diff for: examples/src/fatfs/lib.rs renamed to examples/fatfs/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ thread_local! {
5757
}
5858

5959
#[query]
60-
fn ls() -> String {
60+
fn ls() -> Vec<String> {
6161
_ls().unwrap()
6262
}
6363

64-
fn _ls() -> std::io::Result<String> {
64+
fn _ls() -> std::io::Result<Vec<String>> {
6565
FS.with(|fs| {
6666
let fs = fs.borrow();
6767
let root_dir = fs.root_dir();
@@ -73,7 +73,7 @@ fn _ls() -> std::io::Result<String> {
7373
.map_err(|error| std::io::Error::new(std::io::ErrorKind::Other, error))
7474
})
7575
.collect();
76-
entries.map(|entries| entries.join("\n"))
76+
entries
7777
})
7878
}
7979

Diff for: examples/fatfs/test.ic-repl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import fatfs = "rrkah-fqaaa-aaaaa-aaaaq-cai" as "fatfs.did";
2+
// import fatfs = "ai7t5-aibaq-aaaaa-aaaaa-c" as "fatfs.did";
3+
4+
let result = call fatfs.ls();
5+
assert result == vec {};
6+
7+
let result = call fatfs.write_file("hello.txt", "Hello, World!");
8+
assert result == null;
9+
10+
let result = call fatfs.ls();
11+
assert result == vec { "hello.txt"; };
12+
13+
let result = call fatfs.read_file("hello.txt");
14+
assert result == "Hello, World!";
15+
16+
let result = call fatfs.write_file("hello.txt", "Hello!");
17+
assert result == null;
18+
19+
let result = call fatfs.read_file("hello.txt");
20+
assert result == "Hello!";
21+
22+
let result = call fatfs.write_file("goodbye.txt", "Goodbye!");
23+
assert result == null;
24+
25+
let result = call fatfs.ls();
26+
assert result == vec { "hello.txt"; "goodbye.txt"; };
27+
28+
let result = call fatfs.read_file("goodbye.txt");
29+
assert result == "Goodbye!";

Diff for: examples/src/fatfs/README.md

-21
This file was deleted.

Diff for: examples/src/fatfs/fatfs.did

Whitespace-only changes.

Diff for: flake.lock

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: flake.nix

+75-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
url = "github:paulyoung/nixpkgs-dfinity-sdk";
55
flake = false;
66
};
7+
ic-repl-src = {
8+
url = "github:chenyan2002/ic-repl";
9+
flake = false;
10+
};
711
flake-utils.url = "github:numtide/flake-utils";
812

913
# https://github.com/nix-community/naersk/pull/211
@@ -19,6 +23,7 @@
1923
nixpkgs,
2024
dfinity-sdk,
2125
flake-utils,
26+
ic-repl-src,
2227
naersk,
2328
nixpkgs-mozilla,
2429
}:
@@ -34,8 +39,8 @@
3439
# Get a specific rust version
3540
mozilla = pkgs.callPackage (nixpkgs-mozilla + "/package-set.nix") {};
3641
rust = (mozilla.rustChannelOf {
37-
channel = "1.55.0";
38-
sha256 = "HNIlEerJvk6sBfd8zugzwSgSiHcQH8ZbqWQn9BGfmpo=";
42+
channel = "1.56.0";
43+
sha256 = "L1e0o7azRjOHd0zBa+xkFnxdFulPofTedSTEYZSjj2s=";
3944
# sha256 = pkgs.lib.fakeSha256;
4045
}).rust.override {
4146
extensions = [
@@ -67,40 +72,94 @@
6772
sdkSystem = system;
6873
})."0.8.4";
6974

70-
buildRustPackage = name:
75+
buildRustPackage = name: root: attrs:
7176
let
72-
defaultArgs = [
73-
"--target" "wasm32-unknown-unknown"
74-
];
7577
packageArgs = [
7678
"--package" name
7779
];
7880
in
79-
naersk-lib.buildPackage {
80-
root = ./.;
81-
cargoBuildOptions = x: x ++ defaultArgs ++ packageArgs;
82-
cargoTestOptions = x: x ++ defaultArgs ++ packageArgs;
81+
naersk-lib.buildPackage ({
82+
inherit root;
8383
compressTarget = true;
84-
copyBins = false;
84+
copyBins = true;
85+
copyLibs = true;
8586
copyTarget = true;
86-
};
87+
} // attrs);
88+
89+
buildLocalRustPackage = name:
90+
let
91+
options = [
92+
"--package" name
93+
"--target" "wasm32-unknown-unknown"
94+
];
95+
in
96+
buildRustPackage name ./. {
97+
cargoBuildOptions = x: x ++ options;
98+
cargoTestOptions = x: x ++ options;
99+
copyBins = false;
100+
}
101+
;
102+
103+
ic-repl =
104+
buildRustPackage "ic-repl" ic-repl-src {
105+
buildInputs = [
106+
pkgs.libiconv
107+
108+
# https://nixos.wiki/wiki/Rust#Building_the_openssl-sys_crate
109+
pkgs.openssl_1_1
110+
pkgs.pkgconfig
111+
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
112+
pkgs.darwin.apple_sdk.frameworks.Security
113+
];
114+
};
115+
116+
buildExampleTest = name: package: pkgs.runCommand "${name}-example-test" {
117+
buildInputs = [
118+
dfinitySdk
119+
ic-repl
120+
pkgs.jq
121+
];
122+
} ''
123+
HOME=$TMP
124+
cp -R ${package}/. result
125+
mkdir -p examples/${name}
126+
cp -R ${./examples}/${name}/. examples/${name}
127+
128+
cp ${./dfx.json} dfx.json
129+
jq '.canisters = (.canisters | map_values(.build = "echo"))' dfx.json > new.dfx.json
130+
mv new.dfx.json dfx.json
131+
132+
dfx start --background
133+
dfx deploy
134+
ic-repl --replica local examples/${name}/test.ic-repl
135+
dfx stop
136+
137+
touch $out
138+
'';
139+
140+
packages = {
141+
icfs = buildLocalRustPackage "icfs";
142+
icfs-fatfs = buildLocalRustPackage "icfs-fatfs";
143+
fatfs-example = buildLocalRustPackage "fatfs-example";
144+
};
87145
in
88-
rec {
146+
{
89147
# `nix build`
90148
defaultPackage = pkgs.runCommand "all" {
91149
buildInputs = pkgs.lib.attrValues packages;
92150
} ''
93151
touch $out
94152
'';
95153

96-
packages.icfs = buildRustPackage "icfs";
97-
packages.icfs-fatfs = buildRustPackage "icfs-fatfs";
98-
packages.fatfs-example = buildRustPackage "fatfs_example";
154+
packages = packages // pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin {
155+
fatfs-example-test = buildExampleTest "fatfs" packages.fatfs-example;
156+
};
99157

100158
# `nix develop`
101159
devShell = pkgs.mkShell {
102160
buildInputs = [
103161
dfinitySdk
162+
ic-repl
104163
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
105164
pkgs.libiconv
106165
];

0 commit comments

Comments
 (0)