Documentation
¶
Overview ¶
Package builders provides sandboxed compilation for multiple programming languages.
Each language is implemented as a separate subpackage that registers itself via Register during init(). To use a builder, import its package for side effects:
import _ "github.com/Highload-fun/builders/golang"
Then call GetVersions and Build with the builder's ID.
Index ¶
- Constants
- func Build(ctx context.Context, builderId, version string, flags []string, ...) error
- func Check(t *testing.T, builderId string, sources fs.FS)
- func CheckBuilding(t *testing.T, description, builderId, version string, flags []string, ...)
- func CompilersDir() string
- func DiscardToolchain(dir string) error
- func DownloadAndExtractArchive(ctx context.Context, archiveUrl, dstDir string) error
- func GetBuildersIds() []string
- func HttpGet(ctx context.Context, url string) (*http.Response, error)
- func Register(id string, builder Builder)
- func SetCompilersDir(dir string)
- func StampToolchain(dir string) error
- func ToolchainComplete(dir string) bool
- type Builder
- type FakeCloser
- type Version
Constants ¶
const ( // CompilersHostDir is the host directory where downloaded compiler toolchains are cached. CompilersHostDir = "/tmp" // SourceDir is the path where user source code is mounted inside the sandbox. SourceDir = "/src" // OutDir is the path where the compiled binary is written inside the sandbox. OutDir = "/out" )
const CacheDirEnv = "HL_BUILDERS_CACHE_DIR"
CacheDirEnv names the environment variable that overrides where downloaded toolchains are cached.
Variables ¶
This section is empty.
Functions ¶
func Build ¶
func Build(ctx context.Context, builderId, version string, flags []string, srcDir, outDir string) error
Build compiles the source code in srcDir using the specified builder and version, writing the resulting binary to outDir. It creates an isolated sandbox with a 5 GB memory limit and network disabled. If version is empty, the builder's default is used.
func Check ¶
Check is a test helper that validates a builder's version listing and default-version build. It first asserts that GetVersions returns a non-empty list of valid versions, then delegates to CheckBuilding with an empty version to exercise the default.
func CheckBuilding ¶
func CheckBuilding(t *testing.T, description, builderId, version string, flags []string, sources fs.FS)
CheckBuilding is a test helper that compiles sources with the given builder and version, then verifies the resulting binary can execute inside a sandbox.
func CompilersDir ¶ added in v0.1.6
func CompilersDir() string
CompilersDir returns the directory where downloaded toolchains are cached.
The default is CompilersHostDir, i.e. /tmp, which distributions prune by age - Debian ships "D /tmp 1777 root root 30d" and sweeps daily. A toolchain there survives only as long as builds keep reading every file in it, so a long-lived service should point this at durable storage.
func DiscardToolchain ¶ added in v0.1.6
DiscardToolchain removes an incomplete toolchain so the next build downloads a whole one.
func DownloadAndExtractArchive ¶
DownloadAndExtractArchive fetches a tar archive from archiveUrl and extracts it into dstDir. Supported formats are .tar.gz, .tar.bz2, and .tar.xz.
func GetBuildersIds ¶
func GetBuildersIds() []string
GetBuildersIds returns the sorted IDs of all registered builders.
func Register ¶
Register adds a builder under the given id. It panics if the id is already taken. Language subpackages call this from their init() functions.
func SetCompilersDir ¶ added in v0.1.6
func SetCompilersDir(dir string)
SetCompilersDir overrides the directory where downloaded toolchains are cached. An empty dir restores the default: CacheDirEnv if set, else CompilersHostDir. Call it before the first Build.
func StampToolchain ¶ added in v0.1.6
StampToolchain records how many files dir holds, so ToolchainComplete can later tell a whole toolchain from one the host has deleted files out of. Call it on the staging directory, before publishing it under its final name.
func ToolchainComplete ¶ added in v0.1.6
ToolchainComplete reports whether dir still holds every file it held when it was stamped. An unstamped directory is never complete.
The directory existing is not enough: age-based /tmp cleanup deletes untouched files one by one and leaves the directory itself in place.
Types ¶
type Builder ¶
type Builder interface {
// GetVersions returns the available compiler versions for this language.
GetVersions(ctx context.Context) ([]Version, error)
// Build compiles source code inside the given sandbox, producing a "main" binary in [OutDir].
Build(ctx context.Context, sb *sandbox.Sandbox, version string, flags []string) error
}
Builder is the interface that each language compiler must implement.
type FakeCloser ¶
FakeCloser wraps an io.Reader with a no-op Close method to satisfy io.ReadCloser.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cpp provides a C++ builder that uses system-installed g++ and clang++ compilers.
|
Package cpp provides a C++ builder that uses system-installed g++ and clang++ compilers. |
|
Package csharp provides a C# builder that downloads .NET SDK toolchains from Microsoft.
|
Package csharp provides a C# builder that downloads .NET SDK toolchains from Microsoft. |
|
Package golang provides a Go builder that downloads toolchains from go.dev.
|
Package golang provides a Go builder that downloads toolchains from go.dev. |
|
test/md5
command
|
|
|
test/src
command
|
|
|
Package rust provides a Rust builder that downloads rustc toolchains from static.rust-lang.org.
|
Package rust provides a Rust builder that downloads rustc toolchains from static.rust-lang.org. |
|
Package zig provides a Zig builder that downloads toolchains from ziglang.org.
|
Package zig provides a Zig builder that downloads toolchains from ziglang.org. |