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

Rewrite //go:cgo_ldflag comments to relative paths in stdlib action #4005

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go/tools/builders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ filegroup(
"embedcfg.go",
"env.go",
"filter.go",
"filter_buildid.go",
"flags.go",
"generate_nogo_main.go",
"generate_test_main.go",
"importcfg.go",
"link.go",
"make_hermetic.go",
"read.go",
"replicate.go",
"stdlib.go",
Expand Down
4 changes: 2 additions & 2 deletions go/tools/builders/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func main() {
switch verb {
case "compilepkg":
action = compilePkg
case "filterbuildid":
action = filterBuildID
case "make_hermetic":
action = makeHermetic
case "gentestmain":
action = genTestMain
case "link":
Expand Down
44 changes: 0 additions & 44 deletions go/tools/builders/filter_buildid.go

This file was deleted.

111 changes: 111 additions & 0 deletions go/tools/builders/make_hermetic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2018 The Bazel Authors. 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 main

import (
"bufio"
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
)

// makeHermetic executes the tool on the command line, filtering out any
// -buildid arguments and rewriting cgo files to not contain absolute paths.
// It is intended to be used with -toolexec when building the standard library.
// See stdlib.go for the usage and further comments.
func makeHermetic(args []string) error {
isCgo := filepath.Base(args[0]) == "cgo"
var objdir string
newArgs := make([]string, 0, len(args))
for i := 0; i < len(args); i++ {
arg := args[i]
if arg == "-buildid" {
i++
continue
} else if isCgo && arg == "-objdir" {
objdir = args[i+1]
}
newArgs = append(newArgs, arg)
}
if runtime.GOOS == "windows" || isCgo {
cmd := exec.Command(newArgs[0], newArgs[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
if isCgo {
// Rewrite cgo files to not contain absolute paths in ldflag
// comments.
return rewriteCgoFiles(objdir)
} else {
return nil
}
} else {
return syscall.Exec(newArgs[0], newArgs, os.Environ())
}
}

func rewriteCgoFiles(objdir string) error {
cgoGoTypes := filepath.Join(objdir, "_cgo_gotypes.go")
f, err := os.OpenFile(cgoGoTypes, os.O_RDWR, 0)
if err != nil {
// If the file doesn't exist for whatever reason, we don't need to
// rewrite it.
return nil
}
defer f.Close()
fixedData, err := rewriteCgoLdflagComments(f)
if err != nil {
return err
}
if err = f.Truncate(0); err != nil {
return err
}
if _, err = f.Seek(0, 0); err != nil {
return err
}
if _, err = f.Write(fixedData); err != nil {
return err
}
return nil
}

func rewriteCgoLdflagComments(f *os.File) ([]byte, error) {
bazelWd := []byte(os.Getenv("BAZEL_WORKING_DIRECTORY") + "/")

// Replace absolute paths in //go:cgo_ldflag comments with relative paths.
// We need to pass absolute paths to the "go install" command for building
// the standard library, but we want to strip them from the generated files
// to make the build hermetic.
s := bufio.NewScanner(f)
var buf bytes.Buffer
for s.Scan() {
line := s.Bytes()
if bytes.HasPrefix(line, []byte("//go:cgo_ldflag ")) {
line = bytes.ReplaceAll(line, bazelWd, []byte{})
}
buf.Write(line)
buf.WriteByte('\n')
}
if err := s.Err(); err != nil {
return []byte{}, err
}
return buf.Bytes(), nil
}
9 changes: 8 additions & 1 deletion go/tools/builders/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ You may need to use the flags --cpu=x64_windows --compiler=mingw-gcc.`)
// creating reproducible builds because the build ids are hashed from
// CGO_CFLAGS, which frequently contains absolute paths. As a workaround,
// we strip the build ids, since they won't be used after this.
installArgs := goenv.goCmd("install", "-toolexec", abs(os.Args[0])+" filterbuildid")
// Also rewrite absolute paths in //go:cgo_ldflag comments in files
// generated by cgo back to relative paths.
bazelWd, err := os.Getwd()
if err != nil {
return err
}
os.Setenv("BAZEL_WORKING_DIRECTORY", bazelWd)
installArgs := goenv.goCmd("install", "-toolexec", abs(os.Args[0])+" make_hermetic")
if len(build.Default.BuildTags) > 0 {
installArgs = append(installArgs, "-tags", strings.Join(build.Default.BuildTags, ","))
}
Expand Down