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

Optimize docker image #39

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion c2wasm-api/clang/includes
4 changes: 2 additions & 2 deletions c2wasm-api/copyheaders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ 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
rm /app/clang/includes/*.h
else
mkdir -p /app/clang/includes
fi
cp clang/includes/*.h /app/clang/includes
cp clang/includes/hook/*.h /app/clang/includes
76 changes: 68 additions & 8 deletions c2wasm-api/src/chooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
// END Compile code
5 changes: 4 additions & 1 deletion docker/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.git
.git

wasi-sdk/bin/*
!wasi-sdk/bin/clang
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MAINTAINER Vaclav Barta "[email protected]"
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 .
Expand All @@ -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"]
2 changes: 2 additions & 0 deletions docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions wasi-sdk
Submodule wasi-sdk added at 628938
Loading