11"""Implementation of the `custom_toolchain` rule."""
22
3+ load ("//xcodeproj/internal:providers.bzl" , "ToolchainInfo" )
4+
35def _get_xcode_product_version (* , xcode_config ):
46 raw_version = str (xcode_config .xcode_version ())
57 if not raw_version :
@@ -21,31 +23,38 @@ def _custom_toolchain_impl(ctx):
2123 )
2224
2325 toolchain_name_base = ctx .attr .toolchain_name
24- toolchain_dir = ctx . actions . declare_directory (
25- toolchain_name_base + "{}" .format (xcode_version ) + ".xctoolchain" ,
26- )
26+ toolchain_id = "com.rules_xcodeproj.{}.{}" . format ( toolchain_name_base , xcode_version )
27+ full_toolchain_name = "{}{} " .format (toolchain_name_base , xcode_version )
28+ toolchain_dir = ctx . actions . declare_directory ( full_toolchain_name + ".xctoolchain" )
2729
2830 resolved_overrides = {}
2931 override_files = []
3032
31- for tool_target , tool_name in ctx .attr .overrides .items ():
32- files = tool_target .files .to_list ()
33+ # Process tools from comma-separated list
34+ for stub_target , tools_str in ctx .attr .overrides .items ():
35+ files = stub_target .files .to_list ()
3336 if not files :
34- fail ("ERROR: Override for '{}' does not produce any files!" . format ( tool_name ) )
37+ fail ("ERROR: Override stub does not produce any files!" )
3538
3639 if len (files ) > 1 :
37- fail ("ERROR: Override for '{}' produces multiple files ({}). Each override must have exactly one file." .format (
38- tool_name ,
39- len (files ),
40- ))
40+ fail ("ERROR: Override stub produces multiple files ({}). Each stub must have exactly one file." .format (
41+ len (files )))
42+
43+ stub_file = files [0 ]
44+ if stub_file not in override_files :
45+ override_files .append (stub_file )
4146
42- override_file = files [0 ]
43- override_files .append (override_file )
44- resolved_overrides [tool_name ] = override_file .path
47+ # Split comma-separated list of tool names
48+ tool_names = [name .strip () for name in tools_str .split ("," )]
49+
50+ # Add an entry for each tool name
51+ for tool_name in tool_names :
52+ if tool_name : # Skip empty names
53+ resolved_overrides [tool_name ] = stub_file .path
4554
4655 overrides_list = " " .join (["{}={}" .format (k , v ) for k , v in resolved_overrides .items ()])
4756
48- script_file = ctx .actions .declare_file (toolchain_name_base + "_setup.sh" )
57+ script_file = ctx .actions .declare_file (full_toolchain_name + "_setup.sh" )
4958
5059 ctx .actions .expand_template (
5160 template = ctx .file ._symlink_template ,
@@ -54,8 +63,9 @@ def _custom_toolchain_impl(ctx):
5463 substitutions = {
5564 "%overrides_list%" : overrides_list ,
5665 "%toolchain_dir%" : toolchain_dir .path ,
57- "%toolchain_name_base%" : toolchain_name_base ,
66+ "%toolchain_name_base%" : full_toolchain_name ,
5867 "%xcode_version%" : xcode_version ,
68+ "%toolchain_id%" : toolchain_id ,
5969 },
6070 )
6171
@@ -74,23 +84,30 @@ def _custom_toolchain_impl(ctx):
7484 use_default_shell_env = True ,
7585 )
7686
77- # Create runfiles with the override files and script file
7887 runfiles = ctx .runfiles (files = override_files + [script_file ])
7988
80- return [DefaultInfo (
81- files = depset ([toolchain_dir ]),
82- runfiles = runfiles ,
83- )]
89+ toolchain_provider = ToolchainInfo (
90+ name = full_toolchain_name ,
91+ identifier = toolchain_id ,
92+ )
93+
94+ return [
95+ DefaultInfo (
96+ files = depset ([toolchain_dir ]),
97+ runfiles = runfiles ,
98+ ),
99+ toolchain_provider ,
100+ ]
84101
85102custom_toolchain = rule (
86103 implementation = _custom_toolchain_impl ,
87104 attrs = {
105+ "toolchain_name" : attr .string (mandatory = True ),
88106 "overrides" : attr .label_keyed_string_dict (
89107 allow_files = True ,
90- mandatory = False ,
91- default = {} ,
108+ mandatory = True ,
109+ doc = "Map from stub target to comma-separated list of tool names that should use that stub" ,
92110 ),
93- "toolchain_name" : attr .string (mandatory = True ),
94111 "_symlink_template" : attr .label (
95112 allow_single_file = True ,
96113 default = Label ("//xcodeproj/internal/templates:custom_toolchain_symlink.sh" ),
@@ -102,4 +119,4 @@ custom_toolchain = rule(
102119 ),
103120 ),
104121 },
105- )
122+ )
0 commit comments