build: replace precompiled *.lla with local compilation

Switching from using precompiled *.lla files to compiling locally during
the build process for better flexibility and maintainability.

Fixes #411
This commit is contained in:
Aofei Sheng
2024-07-08 07:39:40 +08:00
parent 4ca50f8973
commit 5bec1729f1
25 changed files with 4 additions and 47 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -58,10 +58,6 @@ func TestFromTestdata(t *testing.T) {
cltest.FromDir(t, "", "./_testdata", false) cltest.FromDir(t, "", "./_testdata", false)
} }
func TestSqlite(t *testing.T) {
cltest.Pkg(t, "github.com/goplus/llgo/c/sqlite", "../c/sqlite/llgo_autogen.ll")
}
func TestFromTestpymath(t *testing.T) { func TestFromTestpymath(t *testing.T) {
cltest.Pkg(t, ssa.PkgPython+"/math", "../py/math/llgo_autogen.ll") cltest.Pkg(t, ssa.PkgPython+"/math", "../py/math/llgo_autogen.ll")
} }

View File

@@ -17,12 +17,10 @@
package build package build
import ( import (
"archive/zip"
"fmt" "fmt"
"go/constant" "go/constant"
"go/token" "go/token"
"go/types" "go/types"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@@ -253,7 +251,8 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
pkg.ExportFile = "" pkg.ExportFile = ""
case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule: case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule:
if isPkgInLLGo(pkg.PkgPath) { if isPkgInLLGo(pkg.PkgPath) {
pkg.ExportFile = concatPkgLinkFiles(pkg, verbose) buildPkg(ctx, aPkg, verbose)
pkg.ExportFile = " " + concatPkgLinkFiles(pkg, verbose) + " " + pkg.ExportFile
} else { } else {
// panic("todo") // panic("todo")
// TODO(xsw): support packages out of llgo // TODO(xsw): support packages out of llgo
@@ -611,7 +610,7 @@ func concatPkgLinkFiles(pkg *packages.Package, verbose bool) string {
var b strings.Builder var b strings.Builder
var ret string var ret string
var n int var n int
llgoPkgLinkFiles(pkg, "", func(linkFile string) { llgoPkgLinkFiles(pkg, func(linkFile string) {
if n == 0 { if n == 0 {
ret = linkFile ret = linkFile
} else { } else {
@@ -629,14 +628,13 @@ func concatPkgLinkFiles(pkg *packages.Package, verbose bool) string {
} }
// const LLGoFiles = "file1; file2; ..." // const LLGoFiles = "file1; file2; ..."
func llgoPkgLinkFiles(pkg *packages.Package, llFile string, procFile func(linkFile string), verbose bool) { func llgoPkgLinkFiles(pkg *packages.Package, procFile func(linkFile string), verbose bool) {
if o := pkg.Types.Scope().Lookup("LLGoFiles"); o != nil { if o := pkg.Types.Scope().Lookup("LLGoFiles"); o != nil {
val := o.(*types.Const).Val() val := o.(*types.Const).Val()
if val.Kind() == constant.String { if val.Kind() == constant.String {
clFiles(constant.StringVal(val), pkg, procFile, verbose) clFiles(constant.StringVal(val), pkg, procFile, verbose)
} }
} }
unzipPkgLinkFiles(pkg.PkgPath, llFile, procFile)
} }
// files = "file1; file2; ..." // files = "file1; file2; ..."
@@ -660,30 +658,6 @@ func clFile(cFile, expFile string, procFile func(linkFile string), verbose bool)
procFile(llFile) procFile(llFile)
} }
func unzipPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) {
dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/"
if llFile == "" {
llFile = "llgo_autogen.ll"
}
llPath := dir + llFile
llaPath := llPath + "a"
zipf, err := zip.OpenReader(llaPath)
if err != nil {
procFile(llPath)
return
}
defer zipf.Close()
for _, f := range zipf.File {
procFile(dir + f.Name)
}
if _, err := os.Stat(llPath); os.IsNotExist(err) {
for _, f := range zipf.File {
decodeFile(dir+f.Name, f)
}
}
}
const ( const (
llgoModPath = "github.com/goplus/llgo" llgoModPath = "github.com/goplus/llgo"
) )
@@ -700,19 +674,6 @@ func isPkgInMod(pkgPath, modPath string) bool {
return false return false
} }
func decodeFile(outFile string, zipf *zip.File) (err error) {
f, err := zipf.Open()
if err != nil {
return
}
defer f.Close()
data, err := io.ReadAll(f)
if err == nil {
err = os.WriteFile(outFile, data, 0644)
}
return
}
func pkgExists(initial []*packages.Package, pkg *packages.Package) bool { func pkgExists(initial []*packages.Package, pkg *packages.Package) bool {
for _, v := range initial { for _, v := range initial {
if v == pkg { if v == pkg {

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.