Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change go build cache to /tmp #279

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,22 @@ Debug with IDEs:
Following these instructions, using your favorite IDE like VSCode,GoLand or just terminal to debug:
<img width="1792" alt="image" src="https://github.com/xhd2015/xgo/assets/14964938/673df393-6632-4eed-a004-400e0c70d0d1">

# Release xgo
Each new release will have two tags, for example: `v1.0.49` and `runtime/v1.0.49`.

The general rule is to make these two tags remain the same with each other, and with the one in [cmd/xgo/version.go](cmd/xgo/version.go).

Here is a guide on how to make a new release:
- update `VERSION` in [cmd/xgo/version.go](cmd/xgo/version.go).
- update `CORE_VERSION` to match `VERSION` if there is a change in this version that makes `cmd/xgo` depends on the newest runtime, otherwise, keep in untouched.
- run `go generate`.
- check whether `CORE_REVISION`,`CORE_NUMBER` matches `REVISION`,`NUMBER` if `CORE_VERSION` is updated to the same with `VERSION`, if not, run `go generate` again and check, and manually update if necessary.
- run `git add -A`.
- run `git commit -m "release v1.0.49"`, this will run git hooks that copies `CORE_VERSION`,`CORE_REVISION`,`CORE_NUMBER` to [runtime/core/version.go](runtime/core/version.go) so that if a runtime is running with an older xgo, it will print warnings.
- run `git tag v1.0.49 && git tag runtime/v1.0.49`.
- run `git push --tags`.
- go to github release page to draft a new release
- run `go run ./script/build-release`, run this in a standalone worktree if necessary.
- upload built binaries.
- update release notes.
- release.
63 changes: 49 additions & 14 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,31 @@ func handleBuild(cmd string, args []string) error {
logDebug("effective GOROOT: %s", goroot)

// create a tmp dir for communication with exec_tool
tmpDir, err := os.MkdirTemp("", "xgo-"+cmd)
tmpRoot, err := getTmpDir()
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
// the xgoTmpDir can be thought as a shortly-stable cache dir
// - it won't gets deleted in a short period
// - the system will reclaim it if out of storage
// it will hold:
// - instrumentation
// - build cache
xgoTmpDir := filepath.Join(tmpRoot, "xgo")
logDebug("xgoTmpDir: %s", xgoTmpDir)
err = os.MkdirAll(xgoTmpDir, 0755)
if err != nil {
return err
}
// sessionTmpDir is specifically for this run session
sessionTmpDir, err := os.MkdirTemp(xgoTmpDir, "session-"+cmd+"-")
logDebug("sessionTmpDir: %s", sessionTmpDir)
if err != nil {
return err
}
defer os.RemoveAll(sessionTmpDir)

optionsFromFile, optionsFromFileContent, err := mergeOptionFiles(tmpDir, opts.optionsFromFile, opts.mockRules)
optionsFromFile, optionsFromFileContent, err := mergeOptionFiles(sessionTmpDir, opts.optionsFromFile, opts.mockRules)
if err != nil {
return err
}
Expand All @@ -209,7 +227,7 @@ func handleBuild(cmd string, args []string) error {
var vscodeDebugFileSuffix string
if !noInstrument && debugTarget != "" {
var err error
vscodeDebugFile, vscodeDebugFileSuffix, err = getVscodeDebugFile(tmpDir, vscode)
vscodeDebugFile, vscodeDebugFileSuffix, err = getVscodeDebugFile(sessionTmpDir, vscode)
if err != nil {
return err
}
Expand All @@ -219,10 +237,10 @@ func handleBuild(cmd string, args []string) error {
var tmpASTFile string
if !noInstrument {
if dumpIR != "" {
tmpIRFile = filepath.Join(tmpDir, "dump-ir")
tmpIRFile = filepath.Join(sessionTmpDir, "dump-ir")
}
if dumpAST != "" {
tmpASTFile = filepath.Join(tmpDir, "dump-ast")
tmpASTFile = filepath.Join(sessionTmpDir, "dump-ast")
}
}

Expand All @@ -240,17 +258,17 @@ func handleBuild(cmd string, args []string) error {
}

// abs xgo dir
xgoDir, err := getXgoHome(xgoHome)
xgoDir, err := getOrMakeAbsXgoHome(xgoHome)
if err != nil {
return err
}
binDir := filepath.Join(xgoDir, "bin")
logDir := filepath.Join(xgoDir, "log")
logDir := filepath.Join(xgoTmpDir, "log")
instrumentSuffix := ""
if isDevelopment {
instrumentSuffix = "-dev"
}
instrumentDir := filepath.Join(xgoDir, "go-instrument"+instrumentSuffix, mappedGorootName)
instrumentDir := filepath.Join(xgoTmpDir, "go-instrument"+instrumentSuffix, mappedGorootName)
logDebug("instrument dir: %s", instrumentDir)

exeSuffix := osinfo.EXE_SUFFIX
Expand Down Expand Up @@ -466,7 +484,7 @@ func handleBuild(cmd string, args []string) error {
}
debugCompilePkg = pkgs[0]
}
debugCompileLogFile = filepath.Join(tmpDir, "debug-compile.log")
debugCompileLogFile = filepath.Join(sessionTmpDir, "debug-compile.log")
go tailLog(debugCompileLogFile)
logDebug("debug compile package: %s", debugCompilePkg)
}
Expand Down Expand Up @@ -551,7 +569,7 @@ func handleBuild(cmd string, args []string) error {
if cmdBuild || (cmdTest && flagC) || debugMode {
// output
if !debugMode && noBuildOutput {
discardOut := filepath.Join(tmpDir, "discard.out")
discardOut := filepath.Join(sessionTmpDir, "discard.out")
buildCmdArgs = append(buildCmdArgs, "-o", discardOut)
} else if output != "" {
finalBuildOutput = output
Expand All @@ -564,7 +582,7 @@ func handleBuild(cmd string, args []string) error {
finalBuildOutput = absOutput
}
} else if debugMode {
finalBuildOutput = filepath.Join(tmpDir, "debug.bin")
finalBuildOutput = filepath.Join(sessionTmpDir, "debug.bin")
}
if finalBuildOutput != "" {
buildCmdArgs = append(buildCmdArgs, "-o", finalBuildOutput)
Expand Down Expand Up @@ -749,8 +767,8 @@ func checkGoVersion(goroot string, noInstrument bool) (*goinfo.GoVersion, error)
return goVersion, nil
}

// getXgoHome returns absolute path to xgo homoe
func getXgoHome(xgoHome string) (string, error) {
// getOrMakeAbsXgoHome returns absolute path to xgo homoe
func getOrMakeAbsXgoHome(xgoHome string) (string, error) {
if xgoHome == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
Expand All @@ -774,6 +792,23 @@ func getXgoHome(xgoHome string) (string, error) {
return absHome, nil
}

// getTmpDir for build caches
func getTmpDir() (string, error) {
if runtime.GOOS != "windows" {
// the publicly known /tmp dir
const KNOWN_TMP = "/tmp"
info, err := os.Stat(KNOWN_TMP)
if err == nil && info.IsDir() {
return KNOWN_TMP, nil
}
}
tmpDir := os.TempDir()
if tmpDir == "" {
return "", fmt.Errorf("cannot get tmp dir")
}
return tmpDir, nil
}

func getNakedGo() string {
if nakedGoReplacement != "" {
return nakedGoReplacement
Expand Down
2 changes: 1 addition & 1 deletion cmd/xgo/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var shadowFS embed.FS

func handleShadow() error {
xgoHome, err := getXgoHome("")
xgoHome, err := getOrMakeAbsXgoHome("")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import "fmt"
// VERSION is manually updated when needed a new tag
// see also runtime/core/version.go
const VERSION = "1.0.49"
const REVISION = "0851e4edf6d78c3da276a587656c19e489590d3f+1"
const NUMBER = 312
const REVISION = "847f7f1a887cb48c1bfd94612ac3c648080d63cb+1"
const NUMBER = 313

// the matching runtime/core's version
// manually updated
Expand Down
2 changes: 2 additions & 0 deletions script/git-hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func preCommitCheck(noCommit bool, amend bool, noUpdateVersion bool) error {
// suffix "+1" to indicate this
rev := commitHash + "+1"

// cmd/xgo/version.go
xgoVersionRelFile := revision.GetXgoVersionFile("")
// runtime/core/version.go
runtimeVersionRelFile := revision.GetRuntimeVersionFile("")

xgoVersionFile := filepath.Join(rootDir, xgoVersionRelFile)
Expand Down
Loading