Skip to content

Commit

Permalink
bpf2go: write dependencies to temporary file to support Windows
Browse files Browse the repository at this point in the history
The /dev/fd/ special device isn't available on Windows, so it's not possible to run bpf2go there. Use a temporary file instead.

Co-authored-by: Lorenz Bauer <lmb@users.noreply.github.com>
Signed-off-by: 网事如风 <w426405@gmail.com>
  • Loading branch information
junjiexing and lmb authored Dec 12, 2022
1 parent cc0a838 commit 5c1776b
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions cmd/bpf2go/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,36 @@ func compile(args compileArgs) error {
)
cmd.Dir = args.dir

var depRd, depWr *os.File
var depFile *os.File
if args.dep != nil {
depRd, depWr, err = os.Pipe()
depFile, err = os.CreateTemp("", "bpf2go")
if err != nil {
return err
}
defer depRd.Close()
defer depWr.Close()
defer depFile.Close()
defer os.Remove(depFile.Name())

// This becomes /dev/fd/3
cmd.ExtraFiles = append(cmd.ExtraFiles, depWr)
cmd.Args = append(cmd.Args,
// Output dependency information.
"-MD",
// Create phony targets so that deleting a dependency doesn't
// break the build.
"-MP",
// Write it to our pipe
"-MF/dev/fd/3",
// Write it to temporary file
"-MF"+depFile.Name(),
)
}

if err := cmd.Start(); err != nil {
if err := cmd.Run(); err != nil {
return fmt.Errorf("can't execute %s: %s", args.cc, err)
}

if depRd != nil {
// Close our copy of the write end so that Copy will terminate
// when cc exits.
depWr.Close()
if _, err := io.Copy(args.dep, depRd); err != nil {
if depFile != nil {
if _, err := io.Copy(args.dep, depFile); err != nil {
return fmt.Errorf("error writing depfile: %w", err)
}
}

if err := cmd.Wait(); err != nil {
return fmt.Errorf("%s: %s", args.cc, err)
}

return nil
}

Expand Down

0 comments on commit 5c1776b

Please sign in to comment.