Commit e427499e authored by nagayama15's avatar nagayama15

🆕 Add build scripts

parent 38e5ab62
node_modules/ node_modules/
/build/
const cli = require('webassembly/cli/util');
const mkdirp = require('mkdirp');
const srcDir = `${__dirname}/src`;
const buildDir = `${__dirname}/build`;
// CFLAGS
const cflags = [
'-O3',
'--target=wasm32-unknown-unknown',
'-D', 'WEBASSEMBLY',
'-emit-llvm',
'-nostdinc',
'-nostdlib',
'-fno-builtin',
'-isystem', `${cli.basedir}/include`,
'-isystem', `${cli.basedir}/lib/musl-wasm32/include`,
'-isystem', `${srcDir}/include`,
'-I', `${srcDir}/zlib`,
];
// clang
async function clang(input, output, cflags, cwd) {
await cli.run(`${cli.bindir}/clang`, [
input,
'-o', output,
'-c',
...cflags,
], { cwd });
}
// llvm-link
async function link(input, output, linkflags, cwd) {
await cli.run(`${cli.bindir}/llvm-link`, [
input,
'-o', output,
...linkflags,
], { cwd });
}
// llc
async function llc(input, output, llcflags, cwd) {
await cli.run(`${cli.bindir}/llc`, [
input,
'-o', output,
...llcflags,
], { cwd });
}
// s2wasm
async function s2wasm(input, output, s2wasmflags, cwd) {
await cli.run(`${cli.bindir}/s2wasm`, [
input,
'-o', output,
...s2wasmflags,
], { cwd });
}
// wasm-opt
async function opt(input, output, optflags, cwd) {
await cli.run(`${cli.bindir}/wasm-opt`, [
input,
'-o', output,
...optflags,
], { cwd });
}
const srcs = [
'zlib/adler32',
'zlib/compress',
'zlib/crc32',
'zlib/deflate',
'zlib/gzclose',
'zlib/gzlib',
'zlib/gzread',
'zlib/gzwrite',
'zlib/inflate',
'zlib/infback',
'zlib/inftrees',
'zlib/inffast',
'zlib/trees',
'zlib/uncompr',
'zlib/zutil',
'zlib-exports',
];
(async () => {
mkdirp.sync(`${buildDir}/zlib`);
// *.c -> *.bc
await Promise.all(
srcs.map(src => clang(`../src/${src}.c`, `./${src}.bc`, cflags, buildDir)),
);
// *.bc -> zlib.bc
await link([
...srcs.map(src => `./${src}.bc`),
`${cli.basedir}/lib/webassembly.bc`,
], 'zlib.bc', [
'-only-needed',
], buildDir);
// zlib.bc -> zlib.s
await llc('zlib.bc', 'zlib.s', [
'-march=wasm32',
'-filetype=asm',
'-asm-verbose=false',
'-thread-model=single',
'-data-sections',
'-function-sections',
], buildDir);
// zlib.s -> zlib.wat
await s2wasm('zlib.s', 'zlib.wat', [
'--import-memory',
'--allocate-stack', '10000',
], buildDir);
// zlib.wat -> zlib.wasm
await opt('zlib.wat', 'zlib.wasm', [
'-O3',
'--coalesce-locals-learning',
'--dce',
'--duplicate-function-elimination',
'--inlining',
'--local-cse',
'--merge-blocks',
'--optimize-instructions',
'--reorder-functions',
'--reorder-locals',
'--vacuum',
], buildDir);
})();
#pragma once
#pragma once
#include <sys/types.h>
#define O_RDONLY 0
#define O_WRONLY 1
#define O_CREAT 64
#define O_TRUNC 512
#define O_APPEND 1024
int open(const char *, int, ...);
int close(int);
ssize_t write(int, const void *, size_t);
ssize_t read(int, void *, size_t);
#pragma once
#include <stddef.h>
int snprintf(char *, size_t, const char *, ...);
int vsnprintf(char *, size_t, const char *, ...);
int vsnprintf(char *, size_t, const char *, ...);
#pragma once
typedef long ssize_t;
typedef long long off_t;
off_t lseek(int, off_t, int);
#include <stdlib.h>
#include "import.h"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment