cl: supports decl: <param>

This commit is contained in:
Li Jie
2024-06-06 10:59:35 +08:00
parent 3ecb43072d
commit 15fad2e841
4 changed files with 25 additions and 13 deletions

2
.gitignore vendored
View File

@@ -9,7 +9,7 @@
*.dylib *.dylib
test.db test.db
llgo_autogen.ll llgo_autogen*.ll
stories*.bin stories*.bin
.DS_Store .DS_Store
err.log err.log

View File

@@ -26,6 +26,7 @@ import (
"log" "log"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"testing" "testing"
@@ -74,7 +75,7 @@ func decodeLinkFile(llFile string) (data []byte, err error) {
return return
} }
defer zipf.Close() defer zipf.Close()
f, err := zipf.Open("llgo_autogen.ll") f, err := zipf.Open(filepath.Base(llFile))
if err != nil { if err != nil {
return return
} }

View File

@@ -105,6 +105,8 @@ func pkgKind(v string) (int, string) {
return PkgLinkExtern, v[5:] return PkgLinkExtern, v[5:]
} else if strings.HasPrefix(v, "py.") { // "py.<module>" } else if strings.HasPrefix(v, "py.") { // "py.<module>"
return PkgPyModule, v[3:] return PkgPyModule, v[3:]
} else if strings.HasPrefix(v, "decl:") { // "decl: <param>"
return PkgDeclOnly, v[5:]
} }
} }
return PkgLLGo, "" return PkgLLGo, ""

View File

@@ -449,8 +449,13 @@ func allLinkFiles(rt []*packages.Package) (outFiles []string) {
outFiles = make([]string, 0, len(rt)) outFiles = make([]string, 0, len(rt))
packages.Visit(rt, nil, func(p *packages.Package) { packages.Visit(rt, nil, func(p *packages.Package) {
pkgPath := p.PkgPath pkgPath := p.PkgPath
kind, param := cl.PkgKindOf(p.Types)
if isRuntimePkg(pkgPath) { if isRuntimePkg(pkgPath) {
llgoPkgLinkFiles(pkgPath, func(linkFile string) { exptFile := ""
if kind == cl.PkgLinkIR || kind == cl.PkgDeclOnly {
exptFile = strings.TrimSpace(param)
}
llgoPkgLinkFiles(pkgPath, exptFile, func(linkFile string) {
outFiles = append(outFiles, linkFile) outFiles = append(outFiles, linkFile)
}) })
} }
@@ -459,13 +464,14 @@ func allLinkFiles(rt []*packages.Package) (outFiles []string) {
} }
const ( const (
pkgAbi = llgoModPath + "/internal/abi" pkgAbi = llgoModPath + "/internal/abi"
pkgRuntime = llgoModPath + "/internal/runtime" pkgRuntime = llgoModPath + "/internal/runtime"
pkgRuntimeC = llgoModPath + "/internal/runtime/c"
) )
func isRuntimePkg(pkgPath string) bool { func isRuntimePkg(pkgPath string) bool {
switch pkgPath { switch pkgPath {
case pkgRuntime, pkgAbi: case pkgRuntime, pkgAbi, pkgRuntimeC:
return true return true
} }
return false return false
@@ -501,7 +507,7 @@ func concatPkgLinkFiles(pkgPath string) string {
var b strings.Builder var b strings.Builder
var ret string var ret string
var n int var n int
llgoPkgLinkFiles(pkgPath, func(linkFile string) { llgoPkgLinkFiles(pkgPath, "", func(linkFile string) {
if n == 0 { if n == 0 {
ret = linkFile ret = linkFile
} else { } else {
@@ -518,13 +524,16 @@ func concatPkgLinkFiles(pkgPath string) string {
return ret return ret
} }
func llgoPkgLinkFiles(pkgPath string, procFile func(linkFile string)) { func llgoPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) {
dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/" dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/"
llFile := dir + "llgo_autogen.ll" if llFile == "" {
llaFile := llFile + "a" llFile = "llgo_autogen.ll"
zipf, err := zip.OpenReader(llaFile) }
llPath := dir + llFile
llaPath := llPath + "a"
zipf, err := zip.OpenReader(llaPath)
if err != nil { if err != nil {
procFile(llFile) procFile(llPath)
return return
} }
defer zipf.Close() defer zipf.Close()
@@ -532,7 +541,7 @@ func llgoPkgLinkFiles(pkgPath string, procFile func(linkFile string)) {
for _, f := range zipf.File { for _, f := range zipf.File {
procFile(dir + f.Name) procFile(dir + f.Name)
} }
if _, err := os.Stat(llFile); os.IsNotExist(err) { if _, err := os.Stat(llPath); os.IsNotExist(err) {
for _, f := range zipf.File { for _, f := range zipf.File {
decodeFile(dir+f.Name, f) decodeFile(dir+f.Name, f)
} }