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

[DO NOT MERGE] Compile using Xahau Repo #32

Open
wants to merge 5 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,4 +9,4 @@
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ unzip bin.zip
```

- CD to docker folder `cd docker`
- Run `make c2wasm-api && make clangd && make wasi-sdk && make hook-cleaner`
- Run `make c2wasm-api && make clangd && make wasi-sdk && make hook-cleaner && make c2wasm-api/clang/includes/src/ripple/app/hook`
- Run `docker-compose build`
- Run `docker-compose up` or `docker-compose up -d`
- This should start server at port `:9000`, the actual compiling endpoint is this: [http://localhost:9000/api/build](localhost:9000/api/build). Note that it takes a while to start.
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/include/hook/*.h /app/clang/includes
76 changes: 69 additions & 7 deletions c2wasm-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,51 @@ function shell_exec(cmd: string, cwd: string) {
return result;
}

const optimization_options = [
/* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Oz'
];

function get_optimization_options(options: string) {
const optimization_options = [
/* default '-O0' not included */ '-O1', '-O2', '-O3', '-O4', '-Os'
];

let safe_options = '';
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 @@ -173,7 +206,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;
console.log(cmd);

const out = shell_exec(cmd, cwd);
result_obj.console = sanitize_shell_output(out);
if (!existsSync(output)) {
Expand All @@ -186,7 +227,7 @@ function link_c_files(source_files: string[], compile_options: string, link_opti

function optimize_wasm(cwd: string, inplace: string, opt_options: string, result_obj: Task) {
const unopt = cwd + '/unopt.wasm';
const cmd = 'wasm-opt ' + opt_options + ' -o ' + inplace + ' ' + unopt;
const cmd = 'wasm-opt ' + opt_options + ' -o ' + inplace + ' ' + unopt;
const out = openSync(cwd + '/opt.log', 'w');
let error = '';
let success = true;
Expand Down Expand Up @@ -309,6 +350,27 @@ function build_project(project: RequestBody, base: string) {
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');
}
}

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 = {
Expand Down
2 changes: 1 addition & 1 deletion 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 Down