Skip to content

Commit bb64087

Browse files
committed
fix doc comments and error messages
1 parent dfb6189 commit bb64087

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

cmd/rbe_configs_gen/rbe_configs_gen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
// Optional input arguments.
3737
runner = flag.String("runner", "", "Runner (host|docker) to use to generate configs. Defaults to docker for linux|windows, host for osx.")
3838
// toolchainContainer is required option for runner=docker
39-
toolchainContainer = flag.String("toolchain_container", "", "Repository path to toolchain image to generate configs for. E.g., l.gcr.io/google/rbe-ubuntu16-04:latest. Required if runner=docker, ignored if runner=host.")
39+
toolchainContainer = flag.String("toolchain_container", "", "Repository path to toolchain image to generate configs for. E.g., l.gcr.io/google/rbe-ubuntu16-04:latest. Required if runner=docker.")
4040
bazelVersion = flag.String("bazel_version", "", "(Optional) Bazel release version to generate configs for. E.g., 4.0.0. If unspecified, the latest available Bazel release is picked.")
4141

4242
// Arguments affecting output generation not specific to either C++ or Java Configs.

pkg/options/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
//
15-
// Package rbeconfigsgen contains utilities to generate C++ & Java Toolchain configs for Bazel to be
16-
// used to run RBE builds
15+
// Package options describes cli options for rbe_configs_gen
1716
package options
1817

1918
import (
@@ -211,6 +210,7 @@ var (
211210
"BAZEL_COMPILER": "clang",
212211
"BAZEL_TARGET_CPU": "darwin_x86_64",
213212
"CC": "clang",
213+
"CC_TOOLCHAIN_NAME": "darwin_x86_64",
214214
},
215215
CPPToolchainTargetName: "cc-compiler-darwin_x86_64",
216216
},

pkg/rbeconfigsgen/rbeconfigsgen.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ type outputConfigs struct {
157157
// licence will contain the OSS license applicable for the generated configs.
158158
license generatedFile
159159
// cppConfigsTarball is the path to the tarball file containing the C++ configs generated by
160-
// Bazel inside the toolchain container.
160+
// Bazel inside the runner.
161161
cppConfigsTarball string
162162
// configBuild represents the BUILD file containing the C++ crosstool top toolchain target
163163
// and the default platform definition.
@@ -181,8 +181,8 @@ func BazeliskDownloadInfo(os string) (string, string, error) {
181181
}
182182

183183
// installBazelisk downloads bazelisk locally to the specified directory for the given os and copies
184-
// it into the running toolchain container.
185-
// Returns the path Bazelisk was installed to inside the running toolchain container.
184+
// it into the runner.
185+
// Returns the path Bazelisk was installed to inside the runner.
186186
func installBazelisk(r runner.Runner, downloadDir, execOS string) (string, error) {
187187
url, filename, err := BazeliskDownloadInfo(execOS)
188188
if err != nil {
@@ -210,7 +210,7 @@ func installBazelisk(r runner.Runner, downloadDir, execOS string) (string, error
210210
}
211211

212212
if _, err := r.ExecCmd("chmod", "+x", bazeliskRunnerPath); err != nil {
213-
return "", fmt.Errorf("failed to mark the Bazelisk binary as executable inside the container: %w", err)
213+
return "", fmt.Errorf("failed to mark the Bazelisk binary as executable inside the runner: %w", err)
214214
}
215215
return bazeliskRunnerPath, nil
216216
}
@@ -243,10 +243,10 @@ func appendCppEnv(env map[string]string, o *options.Options) (map[string]string,
243243
return env, nil
244244
}
245245

246-
// genCppConfigs generates C++ configs inside the running toolchain container represented by the
247-
// given docker runner according to the given options. bazeliskPath is the path to the bazelisk
248-
// binary inside the running toolchain container.
249-
// The return value is the path to the C++ configs tarball copied out of the toolchain container.
246+
// genCppConfigs generates C++ configs inside the runner represented by the
247+
// given runner according to the given options. bazeliskPath is the path to the bazelisk
248+
// binary inside the runner.
249+
// The return value is the path to the C++ configs tarball copied out of the runner.
250250
func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (string, error) {
251251
if !o.GenCPPConfigs {
252252
return "", nil
@@ -256,7 +256,7 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
256256
// command we run in this function.
257257
cppProjDir := path.Join(r.GetWorkdir(), "cpp_configs_project")
258258
if _, err := r.ExecCmd("mkdir", cppProjDir); err != nil {
259-
return "", fmt.Errorf("failed to create empty directory %q inside the toolchain container: %w", cppProjDir, err)
259+
return "", fmt.Errorf("failed to create empty directory %q inside the runner: %w", cppProjDir, err)
260260
}
261261
oldWorkDir := r.GetWorkdir()
262262
if err := r.SetWorkdir(cppProjDir); err != nil {
@@ -265,7 +265,7 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
265265
defer r.SetWorkdir(oldWorkDir)
266266

267267
if _, err := r.ExecCmd("touch", "WORKSPACE", "BUILD.bazel"); err != nil {
268-
return "", fmt.Errorf("failed to create empty build & workspace files in the container to initialize a blank Bazel repository: %w", err)
268+
return "", fmt.Errorf("failed to create empty build & workspace files in the runner to initialize a blank Bazel repository: %w", err)
269269
}
270270

271271
// Backup the current environment & restore it before returning.
@@ -280,24 +280,24 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
280280
// logs.
281281
generationEnv, err := appendCppEnv(bazeliskEnv, o)
282282
if err != nil {
283-
return "", fmt.Errorf("failed to add additional environment variables to the C++ config generation docker command: %w", err)
283+
return "", fmt.Errorf("failed to add additional environment variables to the C++ config generation command: %w", err)
284284
}
285285
r.SetAdditionalEnv(generationEnv)
286286

287287
args := []string{o.CppBazelCmd}
288288
args = append(args, o.CPPConfigTargets...)
289289
if _, err := r.ExecCmd(bazeliskPath, args...); err != nil {
290-
return "", fmt.Errorf("Bazel was unable to build the C++ config generation targets in the toolchain container: %w", err)
290+
return "", fmt.Errorf("Bazel was unable to build the C++ config generation targets in the runner: %w", err)
291291
}
292292

293293
// Restore the env needed for Bazelisk.
294294
r.SetAdditionalEnv(bazeliskEnv)
295295
bazelOutputRoot, err := r.ExecCmd(bazeliskPath, "info", "output_base")
296296
if err != nil {
297-
return "", fmt.Errorf("unable to determine the build output directory where Bazel produced C++ configs in the toolchain container: %w", err)
297+
return "", fmt.Errorf("unable to determine the build output directory where Bazel produced C++ configs in the runner: %w", err)
298298
}
299299
cppConfigDir := path.Join(bazelOutputRoot, "external", o.CPPConfigRepo)
300-
log.Printf("Extracting C++ config files generated by Bazel at %q from the toolchain container.", cppConfigDir)
300+
log.Printf("Extracting C++ config files generated by Bazel at %q from the runner.", cppConfigDir)
301301

302302
// Restore the old env now that we're done with Bazelisk commands. This is purely to reduce
303303
// noise in the logs.
@@ -306,7 +306,7 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
306306
// 1. Get a list of symlinks in the config output directory.
307307
// 2. Harden each link.
308308
// 3. Archive the contents of the config output directory into a tarball.
309-
// 4. Copy the tarball from the container to the local temp directory.
309+
// 4. Copy the tarball from the runner to the local temp directory.
310310
var out string
311311
if o.ExecOS == "windows" {
312312
out, err = r.ExecCmd("cmd", "/r", "dir", filepath.Clean(cppConfigDir), "/a:l", "/b")
@@ -332,7 +332,7 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
332332
}
333333
resolvedPath, err := r.ExecCmd("readlink", s)
334334
if err != nil {
335-
return "", fmt.Errorf("unable to determine what the symlink %q in %q in the toolchain container points to: %w", s, cppConfigDir, err)
335+
return "", fmt.Errorf("unable to determine what the symlink %q in %q in the runner points to: %w", s, cppConfigDir, err)
336336
}
337337
if _, err := r.ExecCmd("ln", "-f", resolvedPath, s); err != nil {
338338
return "", fmt.Errorf("failed to harden symlink %q in %q pointing to %q: %w", s, cppConfigDir, resolvedPath, err)
@@ -342,11 +342,11 @@ func genCppConfigs(r runner.Runner, o *options.Options, bazeliskPath string) (st
342342
outputTarball := "cpp_configs.tar"
343343
// Explicitly use absolute paths to avoid confusion on what's the working directory.
344344
outputTarballPath := path.Join(o.TempWorkDir, outputTarball)
345-
outputTarballContainerPath := path.Join(cppProjDir, outputTarball)
346-
if _, err := r.ExecCmd("tar", "-cf", outputTarballContainerPath, "-C", cppConfigDir, "."); err != nil {
345+
outputTarballRunnerPath := path.Join(cppProjDir, outputTarball)
346+
if _, err := r.ExecCmd("tar", "-cf", outputTarballRunnerPath, "-C", cppConfigDir, "."); err != nil {
347347
return "", fmt.Errorf("failed to archive the C++ configs into a tarball inside the toolchain container: %w", err)
348348
}
349-
if err := r.CopyFrom(outputTarballContainerPath, outputTarballPath); err != nil {
349+
if err := r.CopyFrom(outputTarballRunnerPath, outputTarballPath); err != nil {
350350
return "", fmt.Errorf("failed to copy the C++ config tarball out of the toolchain container: %w", err)
351351
}
352352
log.Printf("Generated C++ configs at %s.", outputTarballPath)
@@ -368,20 +368,20 @@ func UsesLocalJavaRuntime(bazelVersion string) (bool, error) {
368368

369369
// genJavaConfigs returns a BUILD file containing a Java toolchain rule definition that contains
370370
// the following attributes determined by probing details about the JDK version installed in the
371-
// running toolchain container.
372-
// 1. Value of the JAVA_HOME environment variable set in the toolchain image.
371+
// runner.
372+
// 1. Value of the JAVA_HOME environment variable set in the runner or as reported by java binary.
373373
// 2. Value of the Java version as reported by the java binary installed in JAVA_HOME inside the
374-
// running toolchain container.
374+
// runner.
375375
func genJavaConfigs(r runner.Runner, o *options.Options) (generatedFile, error) {
376376
if !o.GenJavaConfigs {
377377
return generatedFile{}, nil
378378
}
379379
javaBin := "java"
380-
imageEnv, err := r.GetEnv()
380+
runnerEnv, err := r.GetEnv()
381381
if err != nil {
382382
return generatedFile{}, fmt.Errorf("unable to get the environment of the toolchain image to determine JAVA_HOME: %w", err)
383383
}
384-
javaHome, ok := imageEnv["JAVA_HOME"]
384+
javaHome, ok := runnerEnv["JAVA_HOME"]
385385
if ok && len(javaHome) > 0 {
386386
log.Printf("JAVA_HOME was %q.", javaHome)
387387
javaBin = path.Join(javaHome, "bin/java")
@@ -392,7 +392,7 @@ func genJavaConfigs(r runner.Runner, o *options.Options) (generatedFile, error)
392392
// some non-deterministic prefix.
393393
out, err := r.ExecCmd(javaBin, "-XshowSettings:properties", "-version")
394394
if err != nil {
395-
return generatedFile{}, fmt.Errorf("unable to determine the Java version installed in the toolchain container: %w", err)
395+
return generatedFile{}, fmt.Errorf("unable to determine the Java version installed in the runner: %w", err)
396396
}
397397
javaVersion := ""
398398
for _, line := range strings.Split(out, "\n") {
@@ -412,9 +412,13 @@ func genJavaConfigs(r runner.Runner, o *options.Options) (generatedFile, error)
412412
}
413413
}
414414
if len(javaVersion) == 0 {
415-
return generatedFile{}, fmt.Errorf("unable to determine the java version installed in the container by running 'java -XshowSettings:properties' in the container because it didn't return a line that looked like java.version = <version>")
415+
return generatedFile{}, fmt.Errorf("unable to determine the java version installed in the runner by running 'java -XshowSettings:properties' because it didn't return a line that looked like java.version = <version>")
416416
}
417417
log.Printf("Java version: '%s'.", javaVersion)
418+
if len(javaHome) == 0 {
419+
return generatedFile{}, fmt.Errorf("unable to determine the JAVA_HOME installed in the runner: not in env, not returned as line `java.home = <path>` by 'java -XshowSettings:properties'")
420+
}
421+
log.Printf("Java home: '%s'.", javaHome)
418422

419423
usesNewJavaRule, err := UsesLocalJavaRuntime(o.BazelVersion)
420424
if err != nil {
@@ -770,10 +774,11 @@ func createManifest(o *options.Options) error {
770774
}
771775
// Extract the sha256 digest from the image name to be included in the manifest.
772776
s := imageDigestRegexp.FindStringSubmatch(o.PlatformParams.ToolchainContainer)
773-
if len(s) != 2 {
777+
if len(s) != 2 && o.Runner == "docker" {
774778
return fmt.Errorf("failed to extract sha256 digest using regex from image name %q, got %d substrings, want 2", o.PlatformParams.ToolchainContainer, len(s))
779+
} else if len(s) == 2 {
780+
m.ImageDigest = s[1]
775781
}
776-
m.ImageDigest = s[1]
777782
// Include the sha256 digest of the configs tarball if output tarball generation was enabled by
778783
// actually hashing the contents of the output tarball.
779784
if len(o.OutputTarball) != 0 {

pkg/runner/host_runner.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type hostRunner struct {
3939
additionalEnv map[string]string
4040
}
4141

42-
// NewHostRunner creates a new Runner which executes commands directly in host environment. deleteWorkdir
43-
// determines if the Cleanup function on the hostRunner will remove temporary directory
42+
// NewHostRunner creates a new Runner which executes commands directly on the host where rbe_configs_gen runs.
43+
// deleteWorkdir determines if the Cleanup function of the hostRunner will remove temporary directory
4444
func NewHostRunner(deleteWorkdir bool) (*hostRunner, error) {
4545

4646
workdir, err := ioutil.TempDir("", "host_runner_")
@@ -55,7 +55,7 @@ func NewHostRunner(deleteWorkdir bool) (*hostRunner, error) {
5555
}, nil
5656
}
5757

58-
// execCmd runs the given command and returns the output with whitespace
58+
// ExecCmd runs the given command and returns the output with whitespace
5959
// trimmed from the edges.
6060
func (r *hostRunner) ExecCmd(cmd string, args ...string) (string, error) {
6161
log.Printf("Running: %s", strings.Join(append([]string{cmd}, args...), " "))
@@ -71,7 +71,7 @@ func (r *hostRunner) ExecCmd(cmd string, args ...string) (string, error) {
7171
return strings.TrimSpace(string(o)), err
7272
}
7373

74-
// cleanup stops the running container if stopContainer was true when the hostRunner was created.
74+
// Cleanup deletes runner temporary files if deleteWorkdir was true when the hostRunner was created.
7575
func (r *hostRunner) Cleanup() {
7676
if !r.deleteWorkdir {
7777
log.Printf("Not deleting workdir %v because the Cleanup option was set to false.", r.globalWorkdir)
@@ -83,29 +83,28 @@ func (r *hostRunner) Cleanup() {
8383
}
8484
}
8585

86-
// copyTo copies the local file at 'src' to the container where 'dst' is the path inside
87-
// the container. d.workdir has no impact on this function.
86+
// CopyTo copies the local file at 'src' to 'dst' on the host
87+
// CopyTo works the same way as CopyFrom for the host runner
8888
func (r *hostRunner) CopyTo(src, dst string) error {
8989
if _, err := runCmd("cp", src, dst); err != nil {
9090
return err
9191
}
9292
return nil
9393
}
9494

95-
// copyFrom extracts the file at 'src' from inside the container and copies it to the path
96-
// 'dst' locally. d.workdir has no impact on this function.
95+
// CopyFrom copies the local file at 'src' to 'dst' on the host
96+
// CopyFrom works the same way as CopyTo for the host runner
9797
func (r *hostRunner) CopyFrom(src, dst string) error {
9898
if _, err := runCmd("cp", src, dst); err != nil {
9999
return err
100100
}
101101
return nil
102102
}
103103

104-
// getEnv gets the shell environment values from the toolchain container as determined by the
105-
// image config. Env value set or changed by running commands after starting the container aren't
104+
// GetEnv gets the shell environment values from the host.
105+
// Env values set or changed by running commands inside the runner aren't
106106
// captured by the return value of this function.
107-
// The return value of this function is a map from env keys to their values. If the image config,
108-
// specifies the same env key multiple times, later values supercede earlier ones.
107+
// The return value of this function is a map from env keys to their values.
109108
func (r *hostRunner) GetEnv() (map[string]string, error) {
110109
result := make(map[string]string)
111110
for _, s := range os.Environ() {

0 commit comments

Comments
 (0)