Repository navigation
Allow changing the CPP Toolchain. - #955
Conversation
| genJavaConfigs = flag.Bool("generate_java_configs", true, "(Optional) Generate Java configs. Defaults to true.") | ||
| genCppConfigs = flag.Bool("generate_cpp_configs", true, "(Optional) Generate C++ configs. Defaults to true.") | ||
| cppEnvJSON = flag.String("cpp_env_json", "", "(Optional) JSON file containing a str -> str dict of environment variables to be set when generating C++ configs inside the toolchain container. This replaces any exec OS specific defaults that would usually be applied.") | ||
| cppToolchainTarget = flag.String("cpp_target_toolchain", "", "(Optional) Set the target CPP toolchain. On linux, the default is cc-compiler-k8, on windows is cc-compiler-x64_windows.") |
There was a problem hiding this comment.
nit: Change to cpp_target_toolchain -> cpp_toolchain_target to be consistent with variable name.
Also for help Set the CPP toolchain target
Also When exec_os is linux instead of On linux, ..., ditto for windows
There was a problem hiding this comment.
My bad on target_toolchain, forgot to clean that up :)
Done.
| // command to generate C++ configs inside the toolchain container. Only one of CppGenEnv or | ||
| // CppGenEnvJSON can be specified. | ||
| CppGenEnvJSON string | ||
| // CPPToolchainTarget is the toolchain to be used by the cpp configs. It overrides the default |
There was a problem hiding this comment.
Remove It overrides the default equally named field in PlatformParams. because it's application to every other field in this struct
| // command to generate C++ configs inside the toolchain container. Only one of CppGenEnv or | ||
| // CppGenEnvJSON can be specified. | ||
| CppGenEnvJSON string | ||
| // CPPToolchainTarget is the toolchain to be used by the cpp configs. It overrides the default |
There was a problem hiding this comment.
Also, I suggest creating a new variable called CPPToolchainTarget at the top level in the Options struct to align with the fact that all command line variables translate to Option fields.
This way, CppToolchainTarget inside PlatformParams is only populated once we know the full target path based on the config dir (if specified)
| if o.OutputConfigPath != "" { | ||
| o.PlatformParams.CppToolchainTarget = fmt.Sprintf("//%s/cc:cc-compiler-k8", path.Clean(o.OutputConfigPath)) | ||
| // The Toolchain target should always start with `//`. | ||
| o.PlatformParams.CppToolchainTarget = strings.TrimPrefix(o.PlatformParams.CppToolchainTarget, "/") |
There was a problem hiding this comment.
This trimming should be unnecessary.
| if o.CPPToolchainTarget != "" { | ||
| o.PlatformParams.CppToolchainTarget = fmt.Sprintf("//cc:%s", o.CPPToolchainTarget) | ||
| } | ||
| if o.OutputConfigPath != "" { | ||
| o.PlatformParams.CppToolchainTarget = fmt.Sprintf("//%s/cc:cc-compiler-k8", path.Clean(o.OutputConfigPath)) | ||
| // The Toolchain target should always start with `//`. | ||
| o.PlatformParams.CppToolchainTarget = strings.TrimPrefix(o.PlatformParams.CppToolchainTarget, "/") | ||
| o.PlatformParams.CppToolchainTarget = fmt.Sprintf("//%s%s", path.Clean(o.OutputConfigPath), o.PlatformParams.CppToolchainTarget) | ||
| } | ||
| } else { | ||
| o.PlatformParams.CppToolchainTarget = "" |
There was a problem hiding this comment.
I think the cpp toolchain target path construction logic has become complicated enough that we should refactor it into its own function and add unit tests. Specifically I'm interested in making sure the target path is constructed correctly for various values of o.OutputConfigPath
| if o.PlatformParams.CppToolchainTarget != "" { | ||
| return generatedFile{}, fmt.Errorf("<internal error> C++ toolchain target was already set") | ||
| } |
There was a problem hiding this comment.
You should be able to add this error back now
| // CppGenEnvJSON can be specified. | ||
| CppGenEnvJSON string | ||
| // CPPToolchainTarget is the toolchain to be used by the cpp configs. | ||
| CPPToolchainTargetBasename string |
There was a problem hiding this comment.
I suggest calling this CPPToolchainTarget for simplicity. It's not immediately apparent what "basename" exactly means because it's not a standard term used to refer to Bazel labels/targets.
There was a problem hiding this comment.
I'm afraid that it's confusing with the CppToolchainTarget inside of PlatformParams. They technically represent the entire thing, while this only has the "name".
Should I call it CppToolchainName? ...TargetName? Wdyt?
|
|
||
| func genCppToolchainTarget(o *Options) string { | ||
| if o.OutputConfigPath != "" { | ||
| return fmt.Sprintf("//%s/cc:%s", path.Clean(o.OutputConfigPath), o.CPPToolchainTargetBasename) |
There was a problem hiding this comment.
Add a string replace for "\" -> "/" in o.OutputConfigPath and unit test for this case. I don't think Bazel allows "\" in labels. Also, ensure you don't mutate o.OutputConfigPath, so something like:
configPath := string.Replace(o.OutputConfigPath, "\\", "/")
if configPath != "" {
...
There was a problem hiding this comment.
Done. Also added some checks taht OutputConfigPath isn't an absolute path on Validation.
The cpp default toolchain on windows was hardset to cc-compiler-k8, which is linux exclusive. Additionally it's quite common to need to change the compiler being used. The drawback of this approach is that it potentially creates many repetitive equal cc_toolchains if you use the same container with multiple toolings installed. Long term we could add multiple cpp toolchains to configs/, but so far the limited amount of repetition isn't harmful.
The cpp default toolchain on windows was hardset to cc-compiler-k8, which
is linux exclusive. Additionally it's quite common to need to change the
compiler being used.
The drawback of this approach is that it potentially creates many repetitive
equal cc_toolchains if you use the same container with multiple toolings installed.
Long term we could add multiple cpp toolchains to configs/, but so far the limited
amount of repetition isn't harmful.