#compiler #wasm-build #wasm #docker-build #docker-image #wasm-oj

app compilet

Server that compiles Rust, C, and C++ into WebAssembly

7 releases

0.1.6 Sep 10, 2023
0.1.5 Sep 9, 2023
0.1.4 Jun 4, 2023
0.1.3 May 17, 2023

#888 in Command line utilities

41 downloads per month

MIT license

36KB
817 lines

Compilet

Server that compiles Rust, C, and C++ into WebAssembly.

Usage

Docker

Build

We have a docker image available on Docker Hub (jacoblincool/compilet), the latest tag supports to compile Rust, C, and C++ out of the box.

You can also use the rs tag (~500MB compressed) to compile Rust only, or the c tag (~150MB compressed) to compile C and C++ only.

Also, you can build your own image with the following command:

docker build -t compilet .

Run

You can run the image with the following command:

docker run -p 8000:8000 jacoblincool/compilet

Or use the docker compose file to run the image:

docker compose up

You may need a .env file to set the environment variables. Check the .env.example file for more information.

Both of the commands above will run the server on port 8000, so you can access the server at http://localhost:8000. You can also change the port by setting the PORT environment variable.

Cargo

You can also install the Compilet through Cargo:

cargo install compilet

It is more convenient to run is as a cli tool:

compilet compile <file>
# compilet compile -h for more information

Endpoints

Validation

Compilet uses JWT to validate the request. You can set the APP_SECRET environment variable to set the secret key for the JWT token, default is APP_SECRET.

You should pass the JWT token in the Authorization header with the Bearer scheme.

  • GET /validate endpoint to validate if the JWT token is valid. Status code 200 means the token is valid, otherwise the token is invalid.

Compile

Compilet should be able to queue the compile request in the future. But currently, it just compiles the source code directly.

  • POST /compile endpoint to compile the source code into WebAssembly

POST body:

{
    "lang": "rs",
    "code": "fn main() { println!(\"Hello, world!\"); }"
}

Response:

{
    "success": true,
    "message": "Compiled successfully",
    "hash": "bb343b0950832ccd077f1515e842196f2ae4bb9e9261b0935ac57916c3cf305d",
    "wasm": "<base64 encoded wasm binary>"
}
  • POST /submission endpoint to compile the source code into WebAssembly, but return immediately and compile the source code in the background.

POST body:

{
    "lang": "rs",
    "code": "fn main() { println!(\"Hello, world!\"); }"
}

Response:

{
    "message": "Submitted",
    "hash": "bb343b0950832ccd077f1515e842196f2ae4bb9e9261b0935ac57916c3cf305d"
}
  • GET /submission/{hash} endpoint to get the status of the submission, and the compiled WebAssembly binary if the compilation is finished.

Response:

{
    "status": "pending",
    "message": "Waiting for compilation",
    "wasm": null
}
{
    "status": "success",
    "message": "Compiled successfully",
    "wasm": "<base64 encoded wasm binary>"
}
{
    "status": "failed",
    "message": "Compilation failed (error message)",
    "wasm": null
}

System

  • GET /system endpoint to get the system information (currently only the capabilities is implemented)

Response:

{
    "capabilities": {
        "rs": "rust 2021 edition + rand 0.8.5, release build",
        "c": "clang 16, level 3 optimizations",
        "cpp": "clang++ 16, level 3 optimizations"
    },
    "status": {
        "compiling": 0,
        "pending": 0
    }
}

Development

After cloning the repository, you need to:

  • Run ./scripts/stdlib.sh to download the WASI standard library for C, and C++.
  • Copy libclang_rt.builtins-wasm32.a to somewhere that Clang can find it. (e.g. /usr/lib/llvm16/lib/clang/16/lib/wasi) (You can do it later, the error message will tell you where to put it.)

You can run the server in development mode with the following command:

cargo run

Build the server with the following command:

cargo build --release

Dependencies

~25–59MB
~1M SLoC