env: find llgo root for development

This commit is contained in:
Li Jie
2025-01-18 12:33:54 +08:00
parent 521b70c715
commit cabc7ffca8
5 changed files with 48 additions and 9 deletions

38
compiler/internal/env/build.go vendored Normal file
View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package env
import "runtime/debug"
// buildTime is the LLGo binary's build time. It should be set by the linker.
var buildTime string
// BuildTime returns the build time of the running LLGo binary.
func BuildTime() string {
if buildTime != "" {
return buildTime
}
info, ok := debug.ReadBuildInfo()
if ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.time" {
return setting.Value
}
}
}
return ""
}

View File

@@ -2,9 +2,11 @@ package env
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
@@ -12,6 +14,7 @@ const (
LLGoCompilerPkg = "github.com/goplus/llgo"
LLGoRuntimePkgName = "runtime"
LLGoRuntimePkg = LLGoCompilerPkg + "/" + LLGoRuntimePkgName
envFileName = "/compiler/internal/env/env.go"
)
func GOROOT() string {
@@ -53,13 +56,22 @@ func LLGoROOT() string {
}
// Check if parent directory is bin
dir := filepath.Dir(exe)
if filepath.Base(dir) != "bin" {
return ""
if filepath.Base(dir) == "bin" {
// Get parent directory of bin
root := filepath.Dir(dir)
if root, ok := isLLGoRoot(root); ok {
return root
}
}
// Get parent directory of bin
root := filepath.Dir(dir)
if root, ok := isLLGoRoot(root); ok {
return root
if Devel() {
root, err := getRuntimePkgDirByCaller()
if err != nil {
return ""
}
if root, ok := isLLGoRoot(root); ok {
fmt.Fprintln(os.Stderr, "WARNING: Using LLGO root for devel: "+root)
return root
}
}
return ""
}
@@ -83,3 +95,22 @@ func isLLGoRoot(root string) (string, bool) {
}
return root, true
}
func getRuntimePkgDirByCaller() (string, error) {
_, file, _, ok := runtime.Caller(0)
if !ok {
return "", fmt.Errorf("cannot get caller")
}
if !strings.HasSuffix(file, envFileName) {
return "", fmt.Errorf("wrong caller")
}
// check file exists
if _, err := os.Stat(file); os.IsNotExist(err) {
return "", fmt.Errorf("file %s not exists", file)
}
modPath := strings.TrimSuffix(file, envFileName)
if st, err := os.Stat(modPath); os.IsNotExist(err) || !st.IsDir() {
return "", fmt.Errorf("not llgo compiler root: %s", modPath)
}
return modPath, nil
}

View File

@@ -58,8 +58,10 @@ func TestLLGoRuntimeDir(t *testing.T) {
defer os.Setenv("LLGO_ROOT", origLLGoRoot)
os.Setenv("LLGO_ROOT", "/nonexistent/path")
if got := LLGoRuntimeDir(); got != "" {
t.Errorf("LLGoRuntimeDir() = %v, want empty string", got)
wd, _ := os.Getwd()
expected, _ := filepath.Abs(filepath.Join(wd, "../../../runtime"))
if got := LLGoRuntimeDir(); got != expected {
t.Errorf("LLGoRuntimeDir() = %v, want %v", got, expected)
}
})
}

41
compiler/internal/env/version.go vendored Normal file
View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package env
import "runtime/debug"
// buildVersion is the LLGo tree's version string at build time. It should be
// set by the linker.
var buildVersion string
// Version returns the version of the running LLGo binary.
//
//export LLGoVersion
func Version() string {
if buildVersion != "" {
return buildVersion
}
info, ok := debug.ReadBuildInfo()
if ok {
return info.Main.Version
}
return "(devel)"
}
func Devel() bool {
return Version() == "(devel)"
}