Skip to content

Commit

Permalink
add pkgdata recording
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 6, 2024
1 parent c81e0ac commit 0a5cae8
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 40 deletions.
1 change: 1 addition & 0 deletions cmd/xgo/exec_tool/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func getDebugEnv(xgoCompilerEnableEnv string) map[string]string {
XGO_DEBUG_DUMP_AST_FILE: os.Getenv(XGO_DEBUG_DUMP_AST_FILE),
"GOCACHE": os.Getenv("GOCACHE"),
XGO_MAIN_MODULE: os.Getenv(XGO_MAIN_MODULE),
XGO_COMPILE_PKG_DATA_DIR: os.Getenv(XGO_COMPILE_PKG_DATA_DIR),
"GOROOT": "../..",
"PATH": "../../bin:${env:PATH}",
"XGO_COMPILER_ENABLE": xgoCompilerEnableEnv,
Expand Down
2 changes: 2 additions & 0 deletions cmd/xgo/exec_tool/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ const XGO_TOOLCHAIN_REVISION = "XGO_TOOLCHAIN_REVISION"
const XGO_TOOLCHAIN_VERSION_NUMBER = "XGO_TOOLCHAIN_VERSION_NUMBER"

const XGO_MAIN_MODULE = "XGO_MAIN_MODULE"

const XGO_COMPILE_PKG_DATA_DIR = "XGO_COMPILE_PKG_DATA_DIR"
14 changes: 10 additions & 4 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func handleBuild(cmd string, args []string) error {
compilerBin := filepath.Join(instrumentDir, "compile"+exeSuffix)
compilerBuildID := filepath.Join(instrumentDir, "compile.buildid.txt")
instrumentGoroot := filepath.Join(instrumentDir, goVersionName)
packageDataDir := filepath.Join(instrumentDir, "pkgdata")

// gcflags can cause the build cache to invalidate
// so separate them with normal one
Expand Down Expand Up @@ -272,7 +273,7 @@ func handleBuild(cmd string, args []string) error {

var revisionChanged bool
if !noInstrument && !noSetup {
err = ensureDirs(binDir, logDir, instrumentDir)
err = ensureDirs(binDir, logDir, instrumentDir, packageDataDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -363,7 +364,7 @@ func handleBuild(cmd string, args []string) error {
}
}
logDebug("resolved main module: %s", mainModule)
execCmdEnv = append(execCmdEnv, "XGO_MAIN_MODULE="+mainModule)
execCmdEnv = append(execCmdEnv, exec_tool.XGO_MAIN_MODULE+"="+mainModule)
// GOCACHE="$shdir/build-cache" PATH=$goroot/bin:$PATH GOROOT=$goroot DEBUG_PKG=$debug go build -toolexec="$shdir/exce_tool $cmd" "${build_flags[@]}" "$@"
buildCmdArgs := []string{cmd}
if toolExecFlag != "" {
Expand Down Expand Up @@ -418,6 +419,7 @@ func handleBuild(cmd string, args []string) error {
if !noInstrument {
execCmd.Env = append(execCmd.Env, "GOCACHE="+buildCacheDir)
execCmd.Env = append(execCmd.Env, "XGO_COMPILER_BIN="+compilerBin)
execCmd.Env = append(execCmd.Env, exec_tool.XGO_COMPILE_PKG_DATA_DIR+"="+packageDataDir)
// xgo versions
execCmd.Env = append(execCmd.Env, "XGO_TOOLCHAIN_VERSION="+VERSION)
execCmd.Env = append(execCmd.Env, "XGO_TOOLCHAIN_REVISION="+REVISION)
Expand Down Expand Up @@ -570,7 +572,7 @@ func checkGoroot(goroot string) (string, error) {
return "", err
}

func ensureDirs(binDir string, logDir string, instrumentDir string) error {
func ensureDirs(binDir string, logDir string, instrumentDir string, packageDataDir string) error {
err := os.MkdirAll(binDir, 0755)
if err != nil {
return fmt.Errorf("create ~/.xgo/bin: %w", err)
Expand All @@ -581,7 +583,11 @@ func ensureDirs(binDir string, logDir string, instrumentDir string) error {
}
err = os.MkdirAll(instrumentDir, 0755)
if err != nil {
return fmt.Errorf("create ~/.xgo/%s: %w", filepath.Base(instrumentDir), err)
return fmt.Errorf("create %s: %w", filepath.Base(instrumentDir), err)
}
err = os.MkdirAll(packageDataDir, 0755)
if err != nil {
return fmt.Errorf("create %s: %w", filepath.Base(packageDataDir), err)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.18"
const REVISION = "2c003e4f895ef51b9c22e7c5bb5018dd6007ff96+1"
const NUMBER = 164
const REVISION = "86b60236af7fe7147b90073421f57187fbf6990a+1"
const NUMBER = 165

func getRevision() string {
revSuffix := ""
Expand Down
1 change: 1 addition & 0 deletions patch/ctxt/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const XgoRuntimePkg = XgoModule + "/runtime"
const XgoRuntimeCorePkg = XgoModule + "/runtime/core"

var XgoMainModule = os.Getenv("XGO_MAIN_MODULE")
var XgoCompilePkgDataDir = os.Getenv("XGO_COMPILE_PKG_DATA_DIR")

func SkipPackageTrap() bool {
pkgPath := GetPkgPath()
Expand Down
172 changes: 172 additions & 0 deletions patch/pkgdata/pkgdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package pkgdata

import (
"cmd/compile/internal/base"
xgo_ctxt "cmd/compile/internal/xgo_rewrite_internal/patch/ctxt"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

type PackageData struct {
Vars map[string]bool
Consts map[string]bool
Funcs map[string]bool
}

var pkgDataMapping map[string]*PackageData

func GetPkgData(pkgPath string) *PackageData {
data, ok := pkgDataMapping[pkgPath]
if ok {
return data
}
data, err := load(pkgPath)
if err != nil {
base.Errorf("load package data: %s %v", pkgPath, err)
return nil
}
if pkgDataMapping == nil {
pkgDataMapping = make(map[string]*PackageData, 1)
}
pkgDataMapping[pkgPath] = data
return data
}
func WritePkgData(pkgPath string, pkgData *PackageData) error {
file := getPkgDataFile(pkgPath)

err := os.MkdirAll(filepath.Dir(file), 0755)
if err != nil {
return err
}
w, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer w.Close()

writeSection := func(section string, m map[string]bool) error {
if len(m) == 0 {
return nil
}
_, err := io.WriteString(w, section)
if err != nil {
return err
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
for k := range m {
_, err := io.WriteString(w, k)
if err != nil {
return err
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}
return nil
}
err = writeSection("[const]", pkgData.Consts)
if err != nil {
return err
}
writeSection("[var]", pkgData.Vars)
if err != nil {
return err
}
writeSection("[func]", pkgData.Funcs)
if err != nil {
return err
}

return nil
}

func load(pkgPath string) (*PackageData, error) {
if xgo_ctxt.XgoCompilePkgDataDir == "" {
return nil, fmt.Errorf("XGO_COMPILE_PKG_DATA_DIR not set")
}
file := getPkgDataFile(pkgPath)
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return parsePkgData(string(data))
}

type Section int

const (
Section_Func Section = 1
Section_Var Section = 2
Section_Const Section = 3
)

func getPkgDataFile(pkgPath string) string {
fsPath := pkgPath
if filepath.Separator != '/' {
split := strings.Split(pkgPath, "/")
fsPath = filepath.Join(split...)
}
return filepath.Join(xgo_ctxt.XgoCompilePkgDataDir, fsPath, "__xgo_pkgdata__.txt")
}

// [func]
func parsePkgData(content string) (*PackageData, error) {
lines := strings.Split(content, "\n")
n := len(lines)

p := &PackageData{}
var section Section
for i := 0; i < n; i++ {
line := strings.TrimSpace(lines[i])
if line == "" || strings.HasPrefix(line, "#") {
continue
}
switch line {
case "[func]":
section = Section_Func
case "[var]":
section = Section_Var
case "[const]":
section = Section_Const
default:
if section == 0 {
break
}
name := line
idx := strings.Index(line, " ")
if idx >= 0 {
name = line[:idx]
}
if name == "" {
break
}
switch section {
case Section_Func:
if p.Funcs == nil {
p.Funcs = make(map[string]bool, 1)
}
p.Funcs[name] = true
case Section_Var:
if p.Vars == nil {
p.Vars = make(map[string]bool, 1)
}
p.Vars[name] = true
case Section_Const:
if p.Consts == nil {
p.Consts = make(map[string]bool, 1)
}
p.Consts[name] = true
default:
// ignore others
}
}
}
return p, nil
}
2 changes: 1 addition & 1 deletion patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func registerFuncs(fileList []*syntax.File, addFile func(name string, r io.Reade
rewriteStdAndGenericFuncs(funcDelcs, pkgPath)

if varTrap {
trapVariables(fileList, funcDelcs)
trapVariables(pkgPath, fileList, funcDelcs)
// debug
// fmt.Fprintf(os.Stderr, "ast:")
// syntax.Fdump(os.Stderr, fileList[0])
Expand Down
Loading

0 comments on commit 0a5cae8

Please sign in to comment.