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

add ability to generate toolchain config w/o docker #962

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
21 changes: 15 additions & 6 deletions cmd/rbe_configs_gen/rbe_configs_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"github.com/bazelbuild/bazel-toolchains/pkg/options"
"log"
"os"

Expand All @@ -29,11 +30,13 @@ import (

var (
// Mandatory input arguments.
toolchainContainer = flag.String("toolchain_container", "", "Repository path to toolchain image to generate configs for. E.g., l.gcr.io/google/rbe-ubuntu16-04:latest")
execOS = flag.String("exec_os", "", "The OS (linux|windows) of the toolchain container image a.k.a, the execution platform in Bazel.")
targetOS = flag.String("target_os", "", "The OS (linux|windows) artifacts built will target a.k.a, the target platform in Bazel.")
execOS = flag.String("exec_os", "", "The OS (linux|windows|osx) of the toolchain container image a.k.a, the execution platform in Bazel.")
targetOS = flag.String("target_os", "", "The OS (linux|windows|osx) artifacts built will target a.k.a, the target platform in Bazel.")

// Optional input arguments.
runner = flag.String("runner", "", "Runner (host|docker) to use to generate configs. Defaults to docker for linux|windows, host for osx.")
// toolchainContainer is required option for runner=docker
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.")
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.")

// Arguments affecting output generation not specific to either C++ or Java Configs.
Expand Down Expand Up @@ -62,7 +65,12 @@ var (
// binary. Printing defaults are skipped as much as possible to avoid cluttering the output.
func printFlags() {
log.Println("rbe_configs_gen.go \\")
log.Printf("--toolchain_container=%q \\", *toolchainContainer)
if len(*runner) != 0 {
log.Printf("--runner=%q \\", *runner)
}
if len(*toolchainContainer) != 0 {
log.Printf("--toolchain_container=%q \\", *toolchainContainer)
}
log.Printf("--exec_os=%q \\", *execOS)
log.Printf("--target_os=%q \\", *targetOS)
log.Printf("--bazel_version=%q \\", *bazelVersion)
Expand Down Expand Up @@ -123,7 +131,7 @@ func initMonitoringClient(ctx context.Context) (*monitoring.Client, error) {

// genConfigs is just a wrapper for the config generation code so that the caller can report
// results if monitoring is enabled before exiting.
func genConfigs(o rbeconfigsgen.Options) error {
func genConfigs(o options.Options) error {
if err := o.ApplyDefaults(o.ExecOS); err != nil {
return fmt.Errorf("failed to apply default options for OS name %q specified to --exec_os: %w", *execOS, err)
}
Expand All @@ -146,8 +154,9 @@ func main() {
log.Fatalf("Failed to initialize monitoring: %v", err)
}

o := rbeconfigsgen.Options{
o := options.Options{
BazelVersion: *bazelVersion,
Runner: *runner,
ToolchainContainer: *toolchainContainer,
ExecOS: *execOS,
TargetOS: *targetOS,
Expand Down
71 changes: 65 additions & 6 deletions pkg/rbeconfigsgen/options.go → pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package rbeconfigsgen contains utilities to generate C++ & Java Toolchain configs for Bazel to be
// used to run RBE builds
package rbeconfigsgen
// Package options describes cli options for rbe_configs_gen
package options

import (
"fmt"
Expand All @@ -26,6 +25,21 @@ import (
"github.com/bazelbuild/bazelisk/repositories"
)

// PlatformToolchainsTemplateParams is used as the input to the toolchains & platform BUILD file
// template 'platformsToolchainBuildTemplate'.
type PlatformToolchainsTemplateParams struct {
ExecConstraints []string
TargetConstraints []string
CppToolchainTarget string
ToolchainContainer string
OSFamily string
}

func (p PlatformToolchainsTemplateParams) String() string {
return fmt.Sprintf("{ExecConstraints: %v, TargetConstraints: %v, CppToolchainTarget: %q, ToolchainContainer: %q, OSFamily: %q}",
p.ExecConstraints, p.TargetConstraints, p.CppToolchainTarget, p.ToolchainContainer, p.OSFamily)
}

// Options are the options to tweak Bazel C++/Java Toolchain config generation.
type Options struct {
// BazelVersion is the version of Bazel to generate configs for. If unset, the latest Bazel
Expand All @@ -39,6 +53,9 @@ type Options struct {
// TargetOS is the OS to be used as the target platform in the generated platform rule. This
// is the OS that artifacts built by Bazel will be executed on.
TargetOS string
// Runner is a name of the runner to use for generating toolchain configuration.
// Allowed values are: docker (default), host
Runner string
// OutputTarball is the path at with a tarball will be generated containing the C++/Java
// configs.
OutputTarball string
Expand Down Expand Up @@ -110,12 +127,15 @@ const (
OSLinux = "linux"
// OSWindows represents Windows when selecting platforms.
OSWindows = "windows"
// OSMacos represents MacOS when selecting platforms.
OSMacos = "osx"
)

var (
validOS = []string{
OSLinux,
OSWindows,
OSMacos,
}

// DefaultExecOptions is a map from the ExecOS to default values for certain fields in Options
Expand Down Expand Up @@ -167,6 +187,33 @@ var (
CppBazelCmd: "query",
CPPToolchainTargetName: "cc-compiler-x64_windows",
},
OSMacos: {
PlatformParams: PlatformToolchainsTemplateParams{
ExecConstraints: []string{
"@bazel_tools//platforms:osx",
"@bazel_tools//platforms:x86_64",
"@bazel_tools//tools/cpp:clang",
},
TargetConstraints: []string{
"@bazel_tools//platforms:osx",
//"@bazel_tools//platforms:x86_64",
},
OSFamily: "MacOS",
},
// excluding osx_archs.bzl because of issue
// described here https://github.com/bazelbuild/bazel/pull/13528
CPPConfigTargets: []string{"@local_config_cc//...", "--", "-@local_config_cc//:osx_archs.bzl"},
CPPConfigRepo: "local_config_cc",
CppBazelCmd: "build",
CppGenEnv: map[string]string{
"ABI_VERSION": "clang",
"BAZEL_COMPILER": "clang",
"BAZEL_TARGET_CPU": "darwin_x86_64",
"CC": "clang",
"CC_TOOLCHAIN_NAME": "darwin_x86_64",
},
CPPToolchainTargetName: "cc-compiler-darwin_x86_64",
},
}
)

Expand All @@ -182,6 +229,14 @@ func strListContains(l []string, s string) bool {
// ApplyDefaults applies platform specific default values to the given options for the given
// OS.
func (o *Options) ApplyDefaults(os string) error {
if o.Runner == "" {
switch o.ExecOS {
case OSMacos:
o.Runner = "host"
case OSLinux, OSWindows:
o.Runner = "docker"
}
}
dopts, ok := DefaultExecOptions[os]
if !ok {
return fmt.Errorf("got unknown OS %q, want one of %s", os, strings.Join(validOS, ", "))
Expand Down Expand Up @@ -222,15 +277,18 @@ func (o *Options) Validate() error {
}
o.BazelVersion = v
}
if o.ToolchainContainer == "" {
return fmt.Errorf("ToolchainContainer was not specified")
}
if o.ExecOS == "" {
return fmt.Errorf("ExecOS was not specified")
}
if !strListContains(validOS, o.ExecOS) {
return fmt.Errorf("invalid exec_os, got %q, want one of %s", o.ExecOS, strings.Join(validOS, ", "))
}
if o.Runner == "" {
return fmt.Errorf("Runner wasn't specified")
}
if o.ToolchainContainer == "" && (o.Runner == "docker"){
return fmt.Errorf("ToolchainContainer was not specified")
}
if o.TargetOS == "" {
return fmt.Errorf("TargetOS was not specified")
}
Expand Down Expand Up @@ -263,6 +321,7 @@ func (o *Options) Validate() error {
}
log.Printf("rbeconfigsgen.Options:")
log.Printf("BazelVersion=%q", o.BazelVersion)
log.Printf("Runner=%q", o.Runner)
log.Printf("ToolchainContainer=%q", o.ToolchainContainer)
log.Printf("ExecOS=%q", o.ExecOS)
log.Printf("TargetOS=%q", o.TargetOS)
Expand Down
Loading