xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/builders/filter_buildid.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	"os"
19	"os/exec"
20	"runtime"
21	"syscall"
22)
23
24// filterBuildID executes the tool on the command line, filtering out any
25// -buildid arguments. It is intended to be used with -toolexec.
26func filterBuildID(args []string) error {
27	newArgs := make([]string, 0, len(args))
28	for i := 0; i < len(args); i++ {
29		arg := args[i]
30		if arg == "-buildid" {
31			i++
32			continue
33		}
34		newArgs = append(newArgs, arg)
35	}
36	if runtime.GOOS == "windows" {
37		cmd := exec.Command(newArgs[0], newArgs[1:]...)
38		cmd.Stdout = os.Stdout
39		cmd.Stderr = os.Stderr
40		return cmd.Run()
41	} else {
42		return syscall.Exec(newArgs[0], newArgs, os.Environ())
43	}
44}
45