Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native compilation #1700

Merged
merged 15 commits into from
Mar 18, 2024
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,28 @@ jobs:
path: /home/runner/.config/azle
# path: $HOME/.config/azle
key: config-azle-${{ hashFiles('src/compiler/Dockerfile', 'src/compiler/rust/**') }}
# This is for the --native-compilation tests
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
name: Install Rust
shell: bash -l {0}
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain=1.73.0 --profile=minimal
rustup target add wasm32-wasi
# This is for the --native-compilation tests
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
name: Install wasi2ic
shell: bash -l {0}
run: cargo install --git https://github.com/wasm-forge/wasi2ic --rev 806c3558aad24224852a9582f018178402cb3679
# This is for the --native-compilation tests
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
name: Install wasmedge-quickjs
shell: bash -l {0}
run: |
mkdir -p /home/runner/.config/azle
cd /home/runner/.config/azle
git clone https://github.com/demergent-labs/wasmedge-quickjs
cd wasmedge-quickjs
git checkout 6c81d7e6fe4b22a468beceed0ee697f4163e7ca8
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.os == 'ubuntu-latest' }}
shell: bash -l {0}
run: sudo apt-get install -y podman
Expand Down Expand Up @@ -271,6 +293,11 @@ jobs:
shell: bash -l {0}
working-directory: ${{ matrix.example_directories }}
run: npm link azle
# This is to put wasmedge-quickjs in the correct location for the --native-compilation tests
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }}
shell: bash -l {0}
working-directory: ${{ matrix.example_directories }}
run: mv /home/runner/.config/azle/wasmedge-quickjs "/home/runner/.config/azle/wasmedge-quickjs_$(npx azle dockerfile-hash)"
- if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && !contains(github.head_ref, 'release--') && !(github.ref == 'refs/heads/main' && contains(github.event.head_commit.message, 'Merge pull request') && contains(github.event.head_commit.message, 'demergent-labs/release--')) }}
shell: bash -l {0}
working-directory: ${{ matrix.example_directories }}
Expand Down
1 change: 1 addition & 0 deletions examples/key_value_store/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.dfx
dfx_generated
node_modules
target
2 changes: 1 addition & 1 deletion examples/key_value_store/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle key_value_store",
"build": "npx azle key_value_store --native-compilation",
"wasm": ".azle/key_value_store/key_value_store.wasm",
"gzip": true,
"declarations": {
Expand Down
1 change: 1 addition & 0 deletions examples/primitive_types/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.dfx
dfx_generated
node_modules
target
2 changes: 1 addition & 1 deletion examples/primitive_types/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "custom",
"main": "src/index.ts",
"candid": "src/index.did",
"build": "npx azle primitive_types",
"build": "npx azle primitive_types --native-compilation",
"wasm": ".azle/primitive_types/primitive_types.wasm",
"gzip": true,
"declarations": {
Expand Down
1 change: 1 addition & 0 deletions examples/stable_structures/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.dfx
dfx_generated
node_modules
target
6 changes: 3 additions & 3 deletions examples/stable_structures/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "custom",
"main": "src/canister1/index.ts",
"candid": "src/canister1/index.did",
"build": "npx azle canister1",
"build": "npx azle canister1 --native-compilation",
"wasm": ".azle/canister1/canister1.wasm",
"gzip": true,
"declarations": {
Expand All @@ -26,7 +26,7 @@
"type": "custom",
"main": "src/canister2/index.ts",
"candid": "src/canister2/index.did",
"build": "npx azle canister2",
"build": "npx azle canister2 --native-compilation",
"wasm": ".azle/canister2/canister2.wasm",
"gzip": true,
"declarations": {
Expand All @@ -48,7 +48,7 @@
"type": "custom",
"main": "src/canister3/index.ts",
"candid": "src/canister3/index.did",
"build": "npx azle canister3",
"build": "npx azle canister3 --native-compilation",
"wasm": ".azle/canister3/canister3.wasm",
"gzip": true,
"declarations": {
Expand Down
25 changes: 25 additions & 0 deletions src/compiler/compile_rust_code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { execSync, IOType } from 'child_process';

export function compileRustCode(
dockerContainerName: string,
bdemann marked this conversation as resolved.
Show resolved Hide resolved
canisterName: string,
stdio: IOType,
nativeCompilation: boolean
) {
if (nativeCompilation === true) {
compileRustCodeNatively(canisterName, stdio);
} else {
compileRustCodeWithPodman(dockerContainerName, canisterName, stdio);
}
}

function compileRustCodeWithPodman(
dockerContainerName: string,
canisterName: string,
stdio: IOType
Expand Down Expand Up @@ -37,3 +50,15 @@ export function compileRustCode(
{ stdio }
);
}

function compileRustCodeNatively(canisterName: string, stdio: IOType) {
execSync(
`CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path .azle/${canisterName}/canister/Cargo.toml --release`,
bdemann marked this conversation as resolved.
Show resolved Hide resolved
{ stdio }
);

execSync(
`wasi2ic target/wasm32-wasi/release/canister.wasm .azle/${canisterName}/${canisterName}.wasm`,
{ stdio }
);
}
12 changes: 10 additions & 2 deletions src/compiler/compile_rust_code_with_candid_and_compiler_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ export function compileRustCodeWithCandidAndCompilerInfo(
compilerInfo: CompilerInfo,
dockerContainerName: string,
canisterName: string,
stdioType: IOType
stdioType: IOType,
nativeCompilation: boolean
) {
// This is for the Rust canister to have access to the candid file
writeFileSync(rustStagingCandidPath, candid);

// TODO why not just write the dfx.json file here as well?
writeFileSync(compilerInfoPath, JSON.stringify(compilerInfo));

compileRustCode(dockerContainerName, canisterName, stdioType);
console.log('before compileRustCode nativeCompilation', nativeCompilation);
bdemann marked this conversation as resolved.
Show resolved Hide resolved

compileRustCode(
dockerContainerName,
canisterName,
stdioType,
nativeCompilation
);
}
6 changes: 4 additions & 2 deletions src/compiler/get_candid_and_canister_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function getCandidAndCanisterMethods(
stdioType: IOType,
envVars: [string, string][],
rustStagingCandidPath: string,
rustStagingWasmPath: string
rustStagingWasmPath: string,
nativeCompilation: boolean
): {
candid: string;
canisterMethods: CanisterMethods;
Expand All @@ -41,7 +42,8 @@ export function getCandidAndCanisterMethods(
compilerInfo,
dockerContainerName,
canisterName,
stdioType
stdioType,
nativeCompilation
);

const { candid, canisterMethods } =
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/get_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export async function getNamesBeforeCli() {
.toString()
.trim();

const nativeCompilation = process.argv.includes('--native-compilation');
bdemann marked this conversation as resolved.
Show resolved Hide resolved

return {
stdioType,
dockerfileHash,
Expand All @@ -52,7 +54,8 @@ export async function getNamesBeforeCli() {
dockerImagePathTar,
dockerImagePathTarGz,
wasmedgeQuickJsPath,
replicaWebServerPort
replicaWebServerPort,
nativeCompilation
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/compiler/handle_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export function handleCli(
return true;
}

if (commandName === '--version') {
handleVersionCommand();

return true;
}

return false;
}

Expand Down Expand Up @@ -90,3 +96,7 @@ function handleCommandClean(

console.info(`azle images removed`);
}

function handleVersionCommand() {
console.info(azleVersion);
}
27 changes: 16 additions & 11 deletions src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ async function azle() {
dockerImagePathTarGz,
dockerContainerName,
wasmedgeQuickJsPath,
replicaWebServerPort
replicaWebServerPort,
nativeCompilation
} = await getNamesBeforeCli();

const commandExecuted = handleCli(
Expand Down Expand Up @@ -68,14 +69,16 @@ async function azle() {
async () => {
createAzleDirectories();

prepareDockerImage(
stdioType,
dockerImageName,
dockerImagePathTar,
dockerImagePathTarGz,
dockerContainerName,
wasmedgeQuickJsPath
);
if (nativeCompilation === false) {
bdemann marked this conversation as resolved.
Show resolved Hide resolved
prepareDockerImage(
stdioType,
dockerImageName,
dockerImagePathTar,
dockerImagePathTarGz,
dockerContainerName,
wasmedgeQuickJsPath
);
}

const canisterJavaScript = unwrap(
getCanisterJavaScript(canisterConfig.main, wasmedgeQuickJsPath)
Expand All @@ -96,7 +99,8 @@ async function azle() {
stdioType,
envVars,
rustStagingCandidPath,
rustStagingWasmPath
rustStagingWasmPath,
nativeCompilation
);

// This is for the dfx.json candid property
Expand All @@ -117,7 +121,8 @@ async function azle() {
compilerInfo,
dockerContainerName,
canisterName,
stdioType
stdioType,
nativeCompilation
);
}
);
Expand Down
Loading