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

[Resolves #71] Add trimpath arg to gobuild #102

Merged
merged 7 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 68 additions & 0 deletions pkg/build/gbbuild_113.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2018 Google LLC 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.
//
// house gb.build function for go1.13 and later version with -trimpath available
// +build go1.13

package build

import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"

v1 "github.com/google/go-containerregistry/pkg/v1"
)

func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "ko")
if err != nil {
return "", err
}
file := filepath.Join(tmpDir, "out")

args := make([]string, 0, 7)
args = append(args, "build")
if disableOptimizations {
// Disable optimizations (-N) and inlining (-l).
args = append(args, "-gcflags", "all=-N -l")
}
args = append(args, "-o", file)
args = append(args, "-trimpath")
stanleynguyen marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, ip)
cmd := exec.Command("go", args...)

// Last one wins
defaultEnv := []string{
"CGO_ENABLED=0",
"GOOS=" + platform.OS,
"GOARCH=" + platform.Architecture,
}
cmd.Env = append(defaultEnv, os.Environ()...)

var output bytes.Buffer
cmd.Stderr = &output
cmd.Stdout = &output

log.Printf("Building %s", ip)
if err := cmd.Run(); err != nil {
os.RemoveAll(tmpDir)
log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String())
return "", err
}
return file, nil
}
67 changes: 67 additions & 0 deletions pkg/build/gbbuild_pre13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2018 Google LLC 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.
//
// house gb.build function for go1.12 and earlier version without -trimpath
// +build !go1.13

package build

import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"

v1 "github.com/google/go-containerregistry/pkg/v1"
)

func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "ko")
if err != nil {
return "", err
}
file := filepath.Join(tmpDir, "out")

args := make([]string, 0, 6)
args = append(args, "build")
if disableOptimizations {
// Disable optimizations (-N) and inlining (-l).
args = append(args, "-gcflags", "all=-N -l")
}
args = append(args, "-o", file)
args = append(args, ip)
cmd := exec.Command("go", args...)

// Last one wins
defaultEnv := []string{
"CGO_ENABLED=0",
"GOOS=" + platform.OS,
"GOARCH=" + platform.Architecture,
}
cmd.Env = append(defaultEnv, os.Environ()...)

var output bytes.Buffer
cmd.Stderr = &output
cmd.Stdout = &output

log.Printf("Building %s", ip)
if err := cmd.Run(); err != nil {
os.RemoveAll(tmpDir)
log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String())
return "", err
}
return file, nil
}
39 changes: 0 additions & 39 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
gb "go/build"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -147,44 +146,6 @@ func (g *gobuild) importPackage(s string) (*gb.Package, error) {
return nil, moduleErr
}

func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "ko")
if err != nil {
return "", err
}
file := filepath.Join(tmpDir, "out")

args := make([]string, 0, 6)
args = append(args, "build")
if disableOptimizations {
// Disable optimizations (-N) and inlining (-l).
args = append(args, "-gcflags", "all=-N -l")
}
args = append(args, "-o", file)
args = append(args, ip)
cmd := exec.Command("go", args...)

// Last one wins
defaultEnv := []string{
"CGO_ENABLED=0",
"GOOS=" + platform.OS,
"GOARCH=" + platform.Architecture,
}
cmd.Env = append(defaultEnv, os.Environ()...)

var output bytes.Buffer
cmd.Stderr = &output
cmd.Stdout = &output

log.Printf("Building %s", ip)
if err := cmd.Run(); err != nil {
os.RemoveAll(tmpDir)
log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String())
return "", err
}
return file, nil
}

func appFilename(importpath string) string {
base := filepath.Base(importpath)

Expand Down