diff --git a/.gitmodules b/.gitmodules index cbfc72fa3ff7..50135cc77a80 100644 --- a/.gitmodules +++ b/.gitmodules @@ -9,7 +9,7 @@ url = https://github.com/XRPLF/hook-cleaner-c [submodule "c2wasm-api/clang/includes"] path = c2wasm-api/clang/includes - url = https://github.com/XRPLF/hook-macros + url = https://github.com/Xahau/xahaud [submodule "quickjslite"] path = quickjslite url = https://github.com/RichardAH/quickjslite diff --git a/c2wasm-api/clang/includes b/c2wasm-api/clang/includes index b444a04229b1..ac694c7c9020 160000 --- a/c2wasm-api/clang/includes +++ b/c2wasm-api/clang/includes @@ -1 +1 @@ -Subproject commit b444a04229b1af433a791ab9275243c416429fb4 +Subproject commit ac694c7c9020a993f27198382634ea035d0dcb47 diff --git a/c2wasm-api/copyheaders.sh b/c2wasm-api/copyheaders.sh index f296ddc80c79..cc0a03e091d0 100755 --- a/c2wasm-api/copyheaders.sh +++ b/c2wasm-api/copyheaders.sh @@ -9,7 +9,7 @@ then else mkdir -p /work/c fi -cp clang/includes/*.h /work/c +cp clang/includes/hook/*.h /work/c if [ -d /app/clang/includes ] then @@ -17,4 +17,4 @@ then else mkdir -p /app/clang/includes fi -cp clang/includes/*.h /app/clang/includes +cp clang/includes/hook/*.h /app/clang/includes diff --git a/c2wasm-api/src/chooks.ts b/c2wasm-api/src/chooks.ts index e00efbb7556e..f1d8ed69862e 100644 --- a/c2wasm-api/src/chooks.ts +++ b/c2wasm-api/src/chooks.ts @@ -97,17 +97,49 @@ function shell_exec(cmd: string, cwd: string) { return result; } -function get_optimization_options(options: string) { - const optimization_options = [ - /* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Os' - ]; +const optimization_options = [ + /* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Oz' +]; - let safe_options = ''; +function get_optimization_options(options: string) { + let optimization_level = ''; for (let o of optimization_options) { if (options.includes(o)) { - safe_options += ' ' + o; + optimization_level += ' ' + o; } } + + let safe_options = ''; + const _options = [ + '--shrink-level=100000000', + '--coalesce-locals-learning', + '--vacuum', + '--merge-blocks', + '--merge-locals', + '--flatten', + '--ignore-implicit-traps', + '-ffm', + '--const-hoisting', + '--code-folding', + '--code-pushing', + '--dae-optimizing', + '--dce', + '--simplify-globals-optimizing', + '--simplify-locals-nonesting', + '--reorder-locals', + '--rereloop', + '--precompute-propagate', + '--local-cse', + '--remove-unused-brs', + '--memory-packing', + '-c', + '--avoid-reinterprets', + optimization_level + ] + + for (let o of _options) { + safe_options += ' ' + o; + } return safe_options; } @@ -170,7 +202,15 @@ function validate_filename(name: string) { function link_c_files(source_files: string[], compile_options: string, link_options: string, cwd: string, output: string, result_obj: Task) { const files = source_files.join(' '); const clang = llvmDir + '/bin/clang'; - const cmd = clang + ' ' + get_clang_options(compile_options) + ' ' + get_lld_options(link_options) + ' ' + files + ' -o ' + output; + let optimization_level = ''; + + for (let o of optimization_options) { + if (compile_options.includes(o)) { + optimization_level == o; + } + } + const cmd = clang + ' ' + optimization_level + ' ' + get_clang_options(compile_options) + ' ' + get_lld_options(link_options) + ' ' + files + ' -o ' + output; + const out = shell_exec(cmd, cwd); result_obj.console = sanitize_shell_output(out); if (!existsSync(output)) { @@ -317,8 +357,28 @@ export function build_project(project: RequestBody, base: string) { } } + if (opt_options) { + const opt_obj = { + name: 'optimizing wasm' + }; + build_result.tasks.push(opt_obj); + if (!optimize_wasm(dir, result, opt_options, opt_obj)) { + return complete(false, 'Optimization error'); + } + } + + if (strip) { + const clean_obj = { + name: 'cleaning wasm' + }; + build_result.tasks.push(clean_obj); + if (!clean_wasm(dir, result, clean_obj)) { + return complete(false, 'Post-build error'); + } + } + build_result.output = serialize_file_data(result, compress || false); return complete(true, 'Success'); } -// END Compile code \ No newline at end of file +// END Compile code diff --git a/docker/.dockerignore b/docker/.dockerignore index 191381ee74de..476cd9ac1146 100644 --- a/docker/.dockerignore +++ b/docker/.dockerignore @@ -1 +1,4 @@ -.git \ No newline at end of file +.git + +wasi-sdk/bin/* +!wasi-sdk/bin/clang diff --git a/docker/Dockerfile b/docker/Dockerfile index ec66ad388734..0739b4faa23b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,7 +4,7 @@ MAINTAINER Vaclav Barta "vaclav@equilibrium.co" RUN apk --no-cache add su-exec COPY clangd /usr/bin WORKDIR /app -COPY c2wasm-api/clang/includes ./clang/includes +COPY c2wasm-api/clang/includes/hook ./clang/includes COPY c2wasm-api/package.json . COPY c2wasm-api/yarn.lock . COPY c2wasm-api/tsconfig.json . @@ -19,6 +19,6 @@ ADD .clang-tidy /work/.clang-tidy ADD .clangd /work/.clangd RUN cp -alf ./clang/includes /work/c && cp -alf clang/wasi-sdk/share/wasi-sysroot/include /usr && mkdir -p /usr/lib/clang/15.0.0 && cp -alf clang/wasi-sdk/lib/clang/15.0.0/include /usr/lib/clang/15.0.0 RUN addgroup -S appgroup && adduser -S appuser -G appgroup -h /app && chown appuser:appgroup /app -RUN yarn && yarn build +RUN yarn --production && yarn build && yarn cache clean EXPOSE $PORT CMD ["./run.sh", "node", "dist/index.js"] diff --git a/docker/Makefile b/docker/Makefile index 75afbc4d5972..c96e3f172d9b 100644 --- a/docker/Makefile +++ b/docker/Makefile @@ -25,12 +25,14 @@ clangd: ../bin/clangd hook-cleaner: ../bin/hook-cleaner cp $< . + chmod +x hook-cleaner c2wasm-api: ../c2wasm-api cp -LR $< . qjsc: ../bin/qjsc cp $< . + chmod +x qjsc clean: -rm -rf c2wasm-api clangd wasi-sdk hook-cleaner qjsc ../wasi-sdk.ts diff --git a/wasi-sdk b/wasi-sdk new file mode 160000 index 000000000000..628938fa7156 --- /dev/null +++ b/wasi-sdk @@ -0,0 +1 @@ +Subproject commit 628938fa7156e3c709cb7f8fce5c01976b7d3ed4