build: download and compile with wasi-sdk
This commit is contained in:
62
internal/crosscompile/cosscompile.go
Normal file
62
internal/crosscompile/cosscompile.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package crosscompile
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/goplus/llgo/internal/env"
|
||||
"github.com/goplus/llgo/internal/xtool/llvm"
|
||||
)
|
||||
|
||||
type Export struct {
|
||||
CCFLAGS []string
|
||||
CFLAGS []string
|
||||
LDFLAGS []string
|
||||
}
|
||||
|
||||
const wasiSdkUrl = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-macos.tar.gz"
|
||||
|
||||
func cacheDir() string {
|
||||
return filepath.Join(env.LLGoCacheDir(), "crosscompile")
|
||||
}
|
||||
|
||||
func UseCrossCompileSDK(goos, goarch string) (export Export, err error) {
|
||||
if runtime.GOOS == goos && runtime.GOARCH == goarch {
|
||||
// not cross compile
|
||||
return
|
||||
}
|
||||
if goarch == "wasm" {
|
||||
sdkDir := filepath.Join(cacheDir(), llvm.GetTargetTriple(goos, goarch))
|
||||
if _, err = os.Stat(sdkDir); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return
|
||||
}
|
||||
if err = downloadAndExtract(wasiSdkUrl, sdkDir); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
// Set up flags for the SDK
|
||||
wasiSdkRoot := filepath.Join(sdkDir, "wasi-sdk-25.0-x86_64-macos")
|
||||
sysrootDir := filepath.Join(wasiSdkRoot, "share", "wasi-sysroot")
|
||||
libclangDir := filepath.Join(wasiSdkRoot, "lib", "clang", "19")
|
||||
includeDir := filepath.Join(sysrootDir, "include", "wasm32-wasip1")
|
||||
libDir := filepath.Join(sysrootDir, "lib", "wasm32-wasip1")
|
||||
|
||||
export.CCFLAGS = []string{
|
||||
"--sysroot=" + sysrootDir,
|
||||
"-resource-dir=" + libclangDir,
|
||||
}
|
||||
export.CFLAGS = []string{
|
||||
"-I" + includeDir,
|
||||
}
|
||||
export.LDFLAGS = []string{
|
||||
"-L" + libDir,
|
||||
}
|
||||
return
|
||||
}
|
||||
// TODO(lijie): supports other platforms
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user