@@ -6,23 +6,66 @@ def _custom_toolchain_impl(ctx):
66 user_toolchain_path = "$(eval echo ~)/Library/Developer/Toolchains/{}.xctoolchain" .format (ctx .attr .toolchain_name )
77 built_toolchain_path = "$(eval pwd)/" + toolchain_dir .path
88
9+ resolved_overrides = {}
10+
11+ for tool_name , label_target in ctx .attr .overrides .items ():
12+ print ("DEBUG: Processing override '{}' -> '{}'" .format (tool_name , label_target ))
13+
14+ # Check if the target produces valid files
15+ if hasattr (label_target , "files" ):
16+ files = label_target .files .to_list ()
17+ else :
18+ files = []
19+
20+ if not files :
21+ fail ("ERROR: Override '{}' does not produce any files! Ensure it is wrapped in a `filegroup`." .format (tool_name ))
22+
23+ # Extract the first file from the filegroup
24+ resolved_path = files [0 ].path
25+ resolved_overrides [tool_name ] = resolved_path
26+
27+ # Debugging: Print resolved paths
28+ print ("Resolved overrides:" , resolved_overrides )
29+
930 # Generate symlink creation commands dynamically, excluding plist files
31+ overrides_list = " " .join (["{}={}" .format (k , v ) for k , v in resolved_overrides .items ()])
32+
1033 symlink_script = """#!/bin/bash
1134set -e
1235
1336mkdir -p "{toolchain_dir}"
1437
38+ # Process overrides manually (avoiding associative arrays)
39+ while IFS='=' read -r key value; do
40+ if [[ -n "$key" && -n "$value" ]]; then
41+ overrides="$overrides $key=$value"
42+ fi
43+ done <<< "{overrides_list}"
44+
1545find "{default_toolchain}" -type f -o -type l | while read file; do
1646 base_name="$(basename "$file")"
1747 rel_path="${{file#"{default_toolchain}/"}}"
18-
19- override_path="$(echo {overrides} | jq -r --arg key "$base_name" '.[$key] // empty')"
20- if [[ -f "$override_path" ]]; then
48+
49+ # Check if an override exists
50+ override_path=""
51+ for entry in $overrides; do
52+ o_key="${{entry%%=*}}"
53+ o_value="${{entry#*=}}"
54+ echo "Looking for Override: $o_key -> $o_value"
55+ if [[ "$o_key" == "$base_name" ]]; then
56+ echo "Found Override: $entry -> $o_value"
57+ override_path="$o_value"
58+ break
59+ fi
60+ done
61+
62+ if [[ -n "$override_path" ]]; then
2163 mkdir -p "{toolchain_dir}/$(dirname "$rel_path")"
2264 cp "$override_path" "{toolchain_dir}/$rel_path"
2365 continue
2466 fi
25-
67+
68+ # Symlink everything else
2669 if [[ "$rel_path" != "ToolchainInfo.plist" ]]; then
2770 mkdir -p "{toolchain_dir}/$(dirname "$rel_path")"
2871 ln -s "$file" "{toolchain_dir}/$rel_path"
@@ -37,13 +80,13 @@ if [ -e "{user_toolchain_path}" ]; then
3780fi
3881ln -s "{built_toolchain_path}" "{user_toolchain_path}"
3982""" .format (
40- toolchain_dir = toolchain_dir .path ,
41- default_toolchain = default_toolchain_path ,
42- overrides = ctx . attr . overrides ,
43- toolchain_plist = toolchain_plist_file .path ,
44- user_toolchain_path = user_toolchain_path ,
45- built_toolchain_path = built_toolchain_path
46- )
83+ toolchain_dir = toolchain_dir .path ,
84+ default_toolchain = default_toolchain_path ,
85+ overrides_list = overrides_list ,
86+ toolchain_plist = toolchain_plist_file .path ,
87+ user_toolchain_path = user_toolchain_path ,
88+ built_toolchain_path = built_toolchain_path
89+ )
4790
4891 script_file = ctx .actions .declare_file (ctx .attr .toolchain_name + "_setup.sh" )
4992 ctx .actions .write (output = script_file , content = symlink_script , is_executable = True )
@@ -91,7 +134,8 @@ custom_toolchain = rule(
91134 implementation = _custom_toolchain_impl ,
92135 attrs = {
93136 "toolchain_name" : attr .string (mandatory = True ),
94- "overrides" : attr .string_dict (default = {}),
137+ "overrides" : attr .label_keyed_string_dict (
138+ allow_files = True , mandatory = False , default = {}
139+ ),
95140 },
96141)
97-
0 commit comments