aboutsummaryrefslogtreecommitdiffhomepage
path: root/loader/list.go
blob: 16b674c74a2eaf2f3a1569530de4bce5f8075cc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package loader

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

	"github.com/tinygo-org/tinygo/compileopts"
	"github.com/tinygo-org/tinygo/goenv"
)

// List returns a ready-to-run *exec.Cmd for running the `go list` command with
// the configuration used for TinyGo.
func List(config *compileopts.Config, extraArgs, pkgs []string) (*exec.Cmd, error) {
	goroot, err := GetCachedGoroot(config)
	if err != nil {
		return nil, err
	}
	args := append([]string{"list"}, extraArgs...)
	if len(config.BuildTags()) != 0 {
		args = append(args, "-tags", strings.Join(config.BuildTags(), " "))
	}
	args = append(args, pkgs...)
	cgoEnabled := "0"
	if config.CgoEnabled() {
		cgoEnabled = "1"
	}
	cmd := exec.Command(filepath.Join(goenv.Get("GOROOT"), "bin", "go"), args...)
	cmd.Env = append(os.Environ(), "GOROOT="+goroot, "GOOS="+config.GOOS(), "GOARCH="+config.GOARCH(), "CGO_ENABLED="+cgoEnabled)
	if config.Options.Directory != "" {
		cmd.Dir = config.Options.Directory
	}
	return cmd, nil
}