fix: macos fail to build

This commit is contained in:
lyyyuna 2021-06-21 15:59:59 +08:00
parent a45b1077b9
commit 2774123526
5 changed files with 46 additions and 51 deletions

View File

@ -41,7 +41,8 @@ func NewBuild(cmd *cobra.Command, args []string) *Build {
func (b *Build) Build() { func (b *Build) Build() {
// 1. 拷贝至临时目录 // 1. 拷贝至临时目录
b.copyProjectToTmp() b.copyProjectToTmp()
// defer b.clean() defer b.clean()
log.Donef("project copied to temporary directory") log.Donef("project copied to temporary directory")
// 2. inject cover vars // 2. inject cover vars
cover.Inject() cover.Inject()
@ -50,6 +51,8 @@ func (b *Build) Build() {
} }
func (b *Build) doBuildInTemp() { func (b *Build) doBuildInTemp() {
log.StartWait("building the injected project")
goflags := config.GocConfig.Goflags goflags := config.GocConfig.Goflags
// 检查用户是否自定义了 -o // 检查用户是否自定义了 -o
oSet := false oSet := false
@ -64,22 +67,19 @@ func (b *Build) doBuildInTemp() {
goflags = append(goflags, "-o", config.GocConfig.CurWd) goflags = append(goflags, "-o", config.GocConfig.CurWd)
} }
pacakges := config.GocConfig.TmpPkgDir pacakges := config.GocConfig.Packages
if config.GocConfig.ContainSpecialPattern {
pacakges = pacakges + "/..."
}
goflags = append(goflags, pacakges) goflags = append(goflags, pacakges...)
args := []string{"build"} args := []string{"build"}
args = append(args, goflags...) args = append(args, goflags...)
// go 命令行由 go build [-o output] [build flags] [packages] 组成 // go 命令行由 go build [-o output] [build flags] [packages] 组成
cmd := exec.Command("go", args...) cmd := exec.Command("go", args...)
cmd.Dir = config.GocConfig.TmpModProjectDir cmd.Dir = config.GocConfig.TmpWd
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
log.Infof("go build cmd is: %v", cmd.Args) log.Infof("go build cmd is: %v, in path [%v]", cmd.Args, cmd.Dir)
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Fatalf("fail to execute go build: %v", err) log.Fatalf("fail to execute go build: %v", err)
} }
@ -88,5 +88,6 @@ func (b *Build) doBuildInTemp() {
} }
// done // done
log.StopWait()
log.Donef("go build done") log.Donef("go build done")
} }

View File

@ -20,7 +20,7 @@ func (b *Build) readProjectMetaInfo() {
config.GocConfig.GOPATH = b.readGOPATH() config.GocConfig.GOPATH = b.readGOPATH()
config.GocConfig.GOBIN = b.readGOBIN() config.GocConfig.GOBIN = b.readGOBIN()
// 获取 [packages] 及其依赖的 package list // 获取 [packages] 及其依赖的 package list
pkgs := b.listPackages(config.GocConfig.CurPkgDir) pkgs := b.listPackages(config.GocConfig.CurWd)
// get mod info // get mod info
for _, pkg := range pkgs { for _, pkg := range pkgs {
@ -35,8 +35,8 @@ func (b *Build) readProjectMetaInfo() {
break break
} }
// 如果目录不是工程根目录,那再次 go list 一次,获取整个工程的包信息 // 如果当前目录不是工程根目录,那再次 go list 一次,获取整个工程的包信息
if config.GocConfig.CurPkgDir != config.GocConfig.CurModProjectDir { if config.GocConfig.CurWd != config.GocConfig.CurModProjectDir {
config.GocConfig.Pkgs = b.listPackages(config.GocConfig.CurModProjectDir) config.GocConfig.Pkgs = b.listPackages(config.GocConfig.CurModProjectDir)
} else { } else {
config.GocConfig.Pkgs = pkgs config.GocConfig.Pkgs = pkgs
@ -44,8 +44,8 @@ func (b *Build) readProjectMetaInfo() {
// get tmp folder name // get tmp folder name
config.GocConfig.TmpModProjectDir = filepath.Join(os.TempDir(), tmpFolderName(config.GocConfig.CurModProjectDir)) config.GocConfig.TmpModProjectDir = filepath.Join(os.TempDir(), tmpFolderName(config.GocConfig.CurModProjectDir))
// get cur pkg dir in the corresponding tmp dir // get working dir in the corresponding tmp dir
config.GocConfig.TmpPkgDir = filepath.Join(config.GocConfig.TmpModProjectDir, config.GocConfig.CurPkgDir[len(config.GocConfig.CurModProjectDir):]) config.GocConfig.TmpWd = filepath.Join(config.GocConfig.TmpModProjectDir, config.GocConfig.CurWd[len(config.GocConfig.CurModProjectDir):])
// get GlobalCoverVarImportPath // get GlobalCoverVarImportPath
config.GocConfig.GlobalCoverVarImportPath = path.Join(config.GocConfig.ImportPath, tmpFolderName(config.GocConfig.CurModProjectDir)) config.GocConfig.GlobalCoverVarImportPath = path.Join(config.GocConfig.ImportPath, tmpFolderName(config.GocConfig.CurModProjectDir))
log.Donef("project meta information parsed") log.Donef("project meta information parsed")

View File

@ -4,24 +4,23 @@ import "time"
type gocConfig struct { type gocConfig struct {
Debug bool Debug bool
CurWd string
Goflags []string
ImportPath string // import path of the project
CurPkgDir string
CurModProjectDir string
TmpModProjectDir string
TmpPkgDir string
ContainSpecialPattern bool // 参数中包含 ...
Pkgs map[string]*Package
GOPATH string
GOBIN string
IsMod bool // deprecated
GlobalCoverVarImportPath string
GlobalCoverVarImportPathDir string
Host string Host string
Mode string // cover mode Mode string // cover mode
GOPATH string
GOBIN string
CurWd string
TmpWd string
CurModProjectDir string
TmpModProjectDir string
Goflags []string // command line flags
Packages []string // command line [packages]
ImportPath string // the whole import path of the project
Pkgs map[string]*Package
GlobalCoverVarImportPath string
GlobalCoverVarImportPathDir string
} }
// GocConfig 全局变量,存放 goc 的各种元属性 // GocConfig 全局变量,存放 goc 的各种元属性

View File

@ -2,12 +2,12 @@ package flag
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/qiniu/goc/v2/pkg/config" "github.com/qiniu/goc/v2/pkg/config"
"github.com/qiniu/goc/v2/pkg/log"
) )
// GetPackagesDir parse [pacakges] part of args, it will fatal if error encountered // GetPackagesDir parse [pacakges] part of args, it will fatal if error encountered
@ -19,6 +19,7 @@ import (
// 如果 [packages] 非法(即不符合 go 原生的定义),则返回对应错误 // 如果 [packages] 非法(即不符合 go 原生的定义),则返回对应错误
// 这里只考虑 go mod 的方式 // 这里只考虑 go mod 的方式
func GetPackagesDir(patterns []string) { func GetPackagesDir(patterns []string) {
packages := make([]string, 0)
for _, p := range patterns { for _, p := range patterns {
// patterns 只支持两种格式 // patterns 只支持两种格式
// 1. 要么是直接指向某些 .go 文件的相对/绝对路径 // 1. 要么是直接指向某些 .go 文件的相对/绝对路径
@ -29,33 +30,27 @@ func GetPackagesDir(patterns []string) {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
// 获取绝对路径 // 获取相对于 current working directory 对路径
absp, err := filepath.Abs(p) for _, p := range patterns {
if filepath.IsAbs(p) {
relPath, err := filepath.Rel(config.GocConfig.CurWd, p)
if err != nil { if err != nil {
log.Fatalf("%v", err) log.Fatalf("fail to get [packages] relative path from current working directory: %v", err)
} }
packages = append(packages, relPath)
} else {
packages = append(packages, p)
}
}
config.GocConfig.Packages = packages
// 获取当前 [packages] 所在的目录位置,供后续插桩使用。
config.GocConfig.CurPkgDir = filepath.Dir(absp)
// 获取二进制名字
// config.GocConfig.BinaryName = filepath.Base(absp)
return return
} }
} }
} }
// 2. 要么是 import path // 2. 要么是 import path
coverWd, err := getDirFromImportPaths(patterns) config.GocConfig.Packages = patterns
if err != nil {
log.Fatalf("%v", err)
}
config.GocConfig.CurPkgDir = coverWd
// 是否包含 ...
if strings.Contains(patterns[0], "...") {
config.GocConfig.ContainSpecialPattern = true
}
} }
// goFilesPackage 对一组 go 文件解析,判断是否合法 // goFilesPackage 对一组 go 文件解析,判断是否合法

View File

@ -8,8 +8,8 @@ import (
) )
const ( const (
PongWait = 20 * time.Second PongWait = 10 * time.Second
PingWait = 10 * time.Second PingWait = 5 * time.Second
) )
type ProfileReq string type ProfileReq string