xtool: utilize llvm-config to find LLVM executables

This commit is contained in:
Aofei Sheng
2024-06-25 18:16:46 +08:00
parent 57fa592a13
commit 43a6837e81
19 changed files with 381 additions and 34 deletions

View File

@@ -30,7 +30,7 @@ func main() {
return return
} }
nm := llvm.New().Nm() nm := llvm.New("").Nm()
items, err := nm.List(os.Args[1]) items, err := nm.List(os.Args[1])
for _, item := range items { for _, item := range items {
if item.File != "" { if item.File != "" {

View File

@@ -54,7 +54,7 @@ The commands are:
} }
func makeIndex() { func makeIndex() {
env := llvm.New() env := llvm.New("")
idxDir := indexDir() idxDir := indexDir()
os.MkdirAll(idxDir, 0755) os.MkdirAll(idxDir, 0755)

View File

@@ -37,8 +37,8 @@ import (
"github.com/goplus/llgo/cl" "github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/packages" "github.com/goplus/llgo/internal/packages"
"github.com/goplus/llgo/ssa/abi" "github.com/goplus/llgo/ssa/abi"
"github.com/goplus/llgo/xtool/clang"
"github.com/goplus/llgo/xtool/env" "github.com/goplus/llgo/xtool/env"
"github.com/goplus/llgo/xtool/env/llvm"
llssa "github.com/goplus/llgo/ssa" llssa "github.com/goplus/llgo/ssa"
clangCheck "github.com/goplus/llgo/xtool/clang/check" clangCheck "github.com/goplus/llgo/xtool/clang/check"
@@ -308,21 +308,29 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
if app == "" { if app == "" {
app = filepath.Join(conf.BinPath, name+conf.AppExt) app = filepath.Join(conf.BinPath, name+conf.AppExt)
} }
const N = 6 args := make([]string, 0, len(pkg.Imports)+len(llFiles)+10)
args := make([]string, N, len(pkg.Imports)+len(llFiles)+(N+1)) args = append(
args[0] = "-o" args,
args[1] = app "-o", app,
args[2] = "-Wno-override-module" "-fuse-ld=lld",
args[3] = "-Xlinker" "-Wno-override-module",
if runtime.GOOS == "darwin" { // ld64.lld (macOS) // "-O2", // FIXME: This will cause TestFinalizer in _test/bdwgc.go to fail on macOS.
args[4] = "-dead_strip" )
args[5] = "" // It's ok to leave it empty, as we can assume libpthread is built-in on macOS. switch runtime.GOOS {
} else { // ld.lld (Unix), lld-link (Windows), wasm-ld (WebAssembly) case "darwin": // ld64.lld (macOS)
args[4] = "--gc-sections" args = append(
args[5] = "-lpthread" // libpthread is built-in since glibc 2.34 (2021-08-01); we need to support earlier versions. args,
"-Xlinker", "-dead_strip",
)
case "windows": // lld-link (Windows)
// TODO: Add options for Windows.
default: // ld.lld (Unix), wasm-ld (WebAssembly)
args = append(
args,
"-Xlinker", "--gc-sections",
"-lpthread", // libpthread is built-in since glibc 2.34 (2021-08-01); we need to support earlier versions.
)
} }
//args[6] = "-fuse-ld=lld" // TODO(xsw): to check lld exists or not
//args[7] = "-O2"
needRuntime := false needRuntime := false
needPyInit := false needPyInit := false
packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) { packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) {
@@ -378,7 +386,7 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
if verbose { if verbose {
fmt.Fprintln(os.Stderr, "clang", args) fmt.Fprintln(os.Stderr, "clang", args)
} }
err := clang.New("").Exec(args...) err := llvm.New("").Clang().Exec(args...)
check(err) check(err)
switch mode { switch mode {
@@ -646,7 +654,7 @@ func clFile(cFile, expFile string, procFile func(linkFile string), verbose bool)
if verbose { if verbose {
fmt.Fprintln(os.Stderr, "clang", args) fmt.Fprintln(os.Stderr, "clang", args)
} }
err := clang.New("").Exec(args...) err := llvm.New("").Clang().Exec(args...)
check(err) check(err)
procFile(llFile) procFile(llFile)
} }

16
xtool/env/env.go vendored
View File

@@ -1,3 +1,19 @@
/*
* 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 package env
import ( import (

View File

@@ -18,28 +18,73 @@ package llvm
import ( import (
"os" "os"
"os/exec"
"path/filepath"
"strings"
"github.com/goplus/llgo/xtool/clang"
"github.com/goplus/llgo/xtool/llvm/llvmlink"
"github.com/goplus/llgo/xtool/nm" "github.com/goplus/llgo/xtool/nm"
) )
type Env struct { // -----------------------------------------------------------------------------
root string
nmprefix string
}
func New() *Env { // defaultLLVMConfigBin returns the default path to the llvm-config binary. It
var nmprefix string // checks the LLVM_CONFIG environment variable first, then searches in PATH. If
var root = os.Getenv("LLGO_LLVM_ROOT") // not found, it returns [ldLLVMConfigBin] as a last resort.
if root != "" { func defaultLLVMConfigBin() string {
nmprefix = root + "/bin/llvm-" bin := os.Getenv("LLVM_CONFIG")
if bin != "" {
return bin
} }
return &Env{root, nmprefix} bin, _ = exec.LookPath("llvm-config")
if bin != "" {
return bin
}
return ldLLVMConfigBin
} }
func (p *Env) Root() string { // -----------------------------------------------------------------------------
return p.root
// Env represents an LLVM installation.
type Env struct {
binDir string
} }
func (p *Env) Nm() *nm.Cmd { // New creates a new [Env] instance.
return nm.New(p.nmprefix + "nm") func New(llvmConfigBin string) *Env {
if llvmConfigBin == "" {
llvmConfigBin = defaultLLVMConfigBin()
}
// Note that an empty binDir is acceptable. In this case, LLVM
// executables are assumed to be in PATH.
binDir, _ := exec.Command(llvmConfigBin, "--bindir").Output()
e := &Env{binDir: strings.TrimSpace(string(binDir))}
return e
} }
// BinDir returns the directory containing LLVM executables. An empty string
// means LLVM executables are assumed to be in PATH.
func (e *Env) BinDir() string { return e.binDir }
// Clang returns a new [clang.Cmd] instance.
func (e *Env) Clang() *clang.Cmd {
bin := filepath.Join(e.BinDir(), "clang")
return clang.New(bin)
}
// Link returns a new [llvmlink.Cmd] instance.
func (e *Env) Link() *llvmlink.Cmd {
bin := filepath.Join(e.BinDir(), "llvm-link")
return llvmlink.New(bin)
}
// Nm returns a new [nm.Cmd] instance.
func (e *Env) Nm() *nm.Cmd {
bin := filepath.Join(e.BinDir(), "llvm-nm")
return nm.New(bin)
}
// -----------------------------------------------------------------------------

26
xtool/env/llvm/llvm_config_byollvm.go vendored Normal file
View File

@@ -0,0 +1,26 @@
//go:build byollvm
/*
* 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.
*/
// The word "byollvm" stands for "Bring Your Own LLVM" and is a build tag used
// to indicate that the package is being built with a custom LLVM installation.
package llvm
// ldLLVMConfigBin is the path to the llvm-config binary. It shoud be set via
// -ldflags when building the package.
var ldLLVMConfigBin string

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && amd64 && llvm14
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/local/opt/llvm@14/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && amd64 && llvm15
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/local/opt/llvm@15/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && amd64 && llvm16
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/local/opt/llvm@16/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && amd64 && !llvm14 && !llvm15 && !llvm16
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/local/opt/llvm@17/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && !amd64 && llvm14
/*
* 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 llvm
const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@14/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && !amd64 && llvm15
/*
* 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 llvm
const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@15/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && !amd64 && llvm16
/*
* 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 llvm
const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@16/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && darwin && !amd64 && !llvm14 && !llvm15 && !llvm16
/*
* 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 llvm
const ldLLVMConfigBin = "/opt/homebrew/opt/llvm@17/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && linux && llvm14
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/lib/llvm-14/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && linux && llvm15
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/lib/llvm-15/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && linux && llvm16
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/lib/llvm-16/bin/llvm-config"

View File

@@ -0,0 +1,21 @@
//go:build !byollvm && linux && !llvm14 && !llvm15 && !llvm16
/*
* 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 llvm
const ldLLVMConfigBin = "/usr/lib/llvm-17/bin/llvm-config"

View File

@@ -35,7 +35,7 @@ type Cmd struct {
// New creates a new llvm-link command. // New creates a new llvm-link command.
func New(app string) *Cmd { func New(app string) *Cmd {
if app == "" { if app == "" {
app = os.Getenv("LLGO_LLVM_ROOT") + "/bin/llvm-link" app = "llvm-link"
} }
return &Cmd{app, os.Stdout, os.Stderr} return &Cmd{app, os.Stdout, os.Stderr}
} }