Skip to content

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

License

Notifications You must be signed in to change notification settings

wasm-oj/compilet

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages