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

env: allow compiling code outside llgo directory #933

Merged
merged 1 commit into from
Jan 4, 2025
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
8 changes: 5 additions & 3 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ import (
"golang.org/x/tools/go/ssa"

"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/env"
"github.com/goplus/llgo/internal/packages"
"github.com/goplus/llgo/internal/typepatch"
"github.com/goplus/llgo/ssa/abi"
"github.com/goplus/llgo/xtool/env"
xenv "github.com/goplus/llgo/xtool/env"
"github.com/goplus/llgo/xtool/env/llvm"

llssa "github.com/goplus/llgo/ssa"
Expand Down Expand Up @@ -175,6 +176,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
}

altPkgPaths := altPkgs(initial, llssa.PkgRuntime)
cfg.Dir = env.LLGoROOT()
altPkgs, err := packages.LoadEx(dedup, sizes, cfg, altPkgPaths...)
check(err)

Expand Down Expand Up @@ -316,7 +318,7 @@ func buildAllPkgs(ctx *context, initial []*packages.Package, verbose bool) (pkgs
for _, param := range altParts {
param = strings.TrimSpace(param)
if strings.ContainsRune(param, '$') {
expdArgs = append(expdArgs, env.ExpandEnvToArgs(param)...)
expdArgs = append(expdArgs, xenv.ExpandEnvToArgs(param)...)
ctx.nLibdir++
} else {
fields := strings.Fields(param)
Expand Down Expand Up @@ -748,7 +750,7 @@ func clFiles(ctx *context, files string, pkg *packages.Package, procFile func(li
args := make([]string, 0, 16)
if strings.HasPrefix(files, "$") { // has cflags
if pos := strings.IndexByte(files, ':'); pos > 0 {
cflags := env.ExpandEnvToArgs(files[:pos])
cflags := xenv.ExpandEnvToArgs(files[:pos])
files = files[pos+1:]
args = append(args, cflags...)
}
Expand Down
58 changes: 58 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package env

import (
"os"
"path/filepath"
"strings"
)

const (
LLGoCompilerPkg = "github.com/goplus/llgo"
)

func LLGoROOT() string {
if root, ok := isLLGoRoot(os.Getenv("LLGO_ROOT")); ok {
return root
}
// Get executable path
exe, err := os.Executable()
if err != nil {
return ""
}
// Resolve any symlinks
exe, err = filepath.EvalSymlinks(exe)
if err != nil {
return ""
}
// Check if parent directory is bin
dir := filepath.Dir(exe)
if filepath.Base(dir) != "bin" {
return ""
}
// Get parent directory of bin
root := filepath.Dir(dir)
if root, ok := isLLGoRoot(root); ok {
return root
}
return ""
}

func isLLGoRoot(root string) (string, bool) {
if root == "" {
return "", false
}
root, err := filepath.Abs(root)
if err != nil {
return "", false
}
// Check for go.mod
data, err := os.ReadFile(filepath.Join(root, "go.mod"))
if err != nil {
return "", false
}
// Check module name
if !strings.Contains(string(data), "module "+LLGoCompilerPkg+"\n") {
return "", false
}
return root, true
}
Loading