[go: up one dir, main page]

Skip to content

Commit

Permalink
clean up the crazy arg list for crosscompile
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jun 18, 2015
1 parent de8271a commit b0521bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
39 changes: 24 additions & 15 deletions go.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,35 @@ type OutputTemplateData struct {
Arch string
}

type CompileOpts struct {
PackagePath string
Platform Platform
OutputTpl string
Ldflags string
Tags string
}

// GoCrossCompile
func GoCrossCompile(packagePath string, platform Platform, outputTpl string, ldflags string, tags string) error {
func GoCrossCompile(opts *CompileOpts) error {
env := append(os.Environ(),
"GOOS="+platform.OS,
"GOARCH="+platform.Arch)
"GOOS="+opts.Platform.OS,
"GOARCH="+opts.Platform.Arch)

var outputPath bytes.Buffer
tpl, err := template.New("output").Parse(outputTpl)
tpl, err := template.New("output").Parse(opts.OutputTpl)
if err != nil {
return err
}
tplData := OutputTemplateData{
Dir: filepath.Base(packagePath),
OS: platform.OS,
Arch: platform.Arch,
Dir: filepath.Base(opts.PackagePath),
OS: opts.Platform.OS,
Arch: opts.Platform.Arch,
}
if err := tpl.Execute(&outputPath, &tplData); err != nil {
return err
}

if platform.OS == "windows" {
if opts.Platform.OS == "windows" {
outputPath.WriteString(".exe")
}

Expand All @@ -56,7 +64,7 @@ func GoCrossCompile(packagePath string, platform Platform, outputTpl string, ldf
// the GOPATH.For this, we just drop it since we move to that
// directory to build.
chdir := ""
if packagePath[0] == '_' {
if opts.PackagePath[0] == '_' {
if runtime.GOOS == "windows" {
// We have to replace weird paths like this:
//
Expand All @@ -67,19 +75,20 @@ func GoCrossCompile(packagePath string, platform Platform, outputTpl string, ldf
// c:\Users
//
re := regexp.MustCompile("^/([a-zA-Z])_/")
chdir = re.ReplaceAllString(packagePath[1:], "$1:\\")
chdir = re.ReplaceAllString(opts.PackagePath[1:], "$1:\\")
chdir = strings.Replace(chdir, "/", "\\", -1)
} else {
chdir = packagePath[1:]
chdir = opts.PackagePath[1:]
}
packagePath = ""

opts.PackagePath = ""
}

_, err = execGo(env, chdir, "build",
"-ldflags", ldflags,
"-tags", tags,
"-ldflags", opts.Ldflags,
"-tags", opts.Tags,
"-o", outputPathReal,
packagePath)
opts.PackagePath)
return err
}

Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ func realMain() int {
defer wg.Done()
semaphore <- 1
fmt.Printf("--> %15s: %s\n", platform.String(), path)
if err := GoCrossCompile(path, platform, outputTpl, ldflags, tags); err != nil {

opts := &CompileOpts{
PackagePath: path,
Platform: platform,
OutputTpl: outputTpl,
Ldflags: ldflags,
Tags: tags,
}
if err := GoCrossCompile(opts); err != nil {
errorLock.Lock()
defer errorLock.Unlock()
errors = append(errors,
Expand Down

0 comments on commit b0521bd

Please sign in to comment.