2024-07-18 13:15:33 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const LUAU_VERSION = "0.634"
|
|
|
|
const ARTIFACT_NAME = "libLuau.VM.a"
|
|
|
|
|
|
|
|
func bail(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 13:24:38 +01:00
|
|
|
func buildVm(artifactPath string, cmakeFlags ...string) {
|
2024-07-18 13:17:42 +01:00
|
|
|
dir, homeDirErr := os.MkdirTemp("", "lei-build")
|
2024-07-18 13:15:33 +01:00
|
|
|
bail(homeDirErr)
|
|
|
|
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
// Clone down the Luau repo and checkout the required tag
|
|
|
|
Exec("git", "", "clone", "https://github.com/luau-lang/luau.git", dir)
|
|
|
|
Exec("git", dir, "checkout", LUAU_VERSION)
|
|
|
|
|
|
|
|
// Build the Luau VM using CMake
|
|
|
|
buildDir := path.Join(dir, "cmake")
|
|
|
|
bail(os.Mkdir(buildDir, os.ModePerm))
|
|
|
|
|
|
|
|
defaultCmakeFlags := []string{"..", "-DCMAKE_BUILD_TYPE=RelWithDebInfo", "-DLUAU_EXTERN_C=ON"}
|
|
|
|
Exec("cmake", buildDir, append(defaultCmakeFlags, cmakeFlags...)...)
|
|
|
|
Exec("cmake", buildDir, "--build", ".", "--target Luau.VM", "--config", "RelWithDebInfo")
|
|
|
|
|
|
|
|
// Copy the artifact to the artifact directory
|
2024-07-18 13:24:38 +01:00
|
|
|
artifactFile, artifactErr := os.ReadFile(path.Join(buildDir, ARTIFACT_NAME))
|
2024-07-18 13:15:33 +01:00
|
|
|
bail(artifactErr)
|
2024-07-18 13:24:38 +01:00
|
|
|
bail(os.WriteFile(artifactPath, artifactFile, os.ModePerm))
|
2024-07-18 13:15:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
homeDir, homeDirErr := os.UserHomeDir()
|
|
|
|
bail(homeDirErr)
|
|
|
|
|
2024-07-18 13:17:42 +01:00
|
|
|
artifactDir := path.Join(homeDir, ".lei")
|
2024-07-18 13:15:33 +01:00
|
|
|
artifactPath := path.Join(artifactDir, ARTIFACT_NAME)
|
|
|
|
|
|
|
|
bail(os.MkdirAll(artifactDir, os.ModePerm))
|
|
|
|
|
|
|
|
// TODO: Args for clean build
|
|
|
|
args := os.Args[1:]
|
|
|
|
|
|
|
|
goArgs := []string{}
|
|
|
|
cmakeFlags := []string{}
|
|
|
|
features := []string{}
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
if arg == "--enable-vector4" {
|
|
|
|
features = append(features, "LUAU_VECTOR4")
|
|
|
|
cmakeFlags = append(cmakeFlags, "-DLUAU_VECTOR_SIZE=4")
|
|
|
|
} else {
|
|
|
|
goArgs = append(goArgs, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(artifactPath); err == nil {
|
|
|
|
fmt.Printf("[build] Using existing artifact at %s\n", artifactPath)
|
|
|
|
} else {
|
2024-07-18 13:24:38 +01:00
|
|
|
buildVm(artifactPath, cmakeFlags...)
|
2024-07-18 13:15:33 +01:00
|
|
|
}
|
|
|
|
|
2024-07-18 13:27:44 +01:00
|
|
|
buildTags := []string{}
|
2024-07-18 13:15:33 +01:00
|
|
|
if len(features) > 0 {
|
2024-07-18 13:27:44 +01:00
|
|
|
buildTags = append(buildTags, []string{"-tags", strings.Join(features, ",")}...)
|
2024-07-18 13:15:33 +01:00
|
|
|
}
|
|
|
|
|
2024-07-19 18:14:49 +01:00
|
|
|
subcommand := goArgs[0]
|
|
|
|
goArgs = goArgs[1:]
|
|
|
|
combinedArgs := append(buildTags, goArgs...)
|
2024-07-18 13:15:33 +01:00
|
|
|
cmd, _, _, _ := Command("go").
|
2024-07-19 18:14:49 +01:00
|
|
|
WithArgs(append([]string{subcommand}, combinedArgs...)...).
|
2024-07-18 13:15:33 +01:00
|
|
|
WithVar(
|
|
|
|
"CGO_LDFLAGS",
|
|
|
|
fmt.Sprintf("-L %s -lLuau.VM -lm -lstdc++", artifactDir),
|
|
|
|
).
|
|
|
|
WithVar("CGO_ENABLED", "1").
|
|
|
|
PipeAll(Forward).
|
|
|
|
ToCommand()
|
|
|
|
|
|
|
|
bail(cmd.Start())
|
|
|
|
bail(cmd.Wait())
|
|
|
|
}
|