@@ -6,23 +6,66 @@ def _custom_toolchain_impl(ctx):
6
6
user_toolchain_path = "$(eval echo ~)/Library/Developer/Toolchains/{}.xctoolchain" .format (ctx .attr .toolchain_name )
7
7
built_toolchain_path = "$(eval pwd)/" + toolchain_dir .path
8
8
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
+
9
30
# Generate symlink creation commands dynamically, excluding plist files
31
+ overrides_list = " " .join (["{}={}" .format (k , v ) for k , v in resolved_overrides .items ()])
32
+
10
33
symlink_script = """#!/bin/bash
11
34
set -e
12
35
13
36
mkdir -p "{toolchain_dir}"
14
37
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
+
15
45
find "{default_toolchain}" -type f -o -type l | while read file; do
16
46
base_name="$(basename "$file")"
17
47
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
21
63
mkdir -p "{toolchain_dir}/$(dirname "$rel_path")"
22
64
cp "$override_path" "{toolchain_dir}/$rel_path"
23
65
continue
24
66
fi
25
-
67
+
68
+ # Symlink everything else
26
69
if [[ "$rel_path" != "ToolchainInfo.plist" ]]; then
27
70
mkdir -p "{toolchain_dir}/$(dirname "$rel_path")"
28
71
ln -s "$file" "{toolchain_dir}/$rel_path"
@@ -37,13 +80,13 @@ if [ -e "{user_toolchain_path}" ]; then
37
80
fi
38
81
ln -s "{built_toolchain_path}" "{user_toolchain_path}"
39
82
""" .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
+ )
47
90
48
91
script_file = ctx .actions .declare_file (ctx .attr .toolchain_name + "_setup.sh" )
49
92
ctx .actions .write (output = script_file , content = symlink_script , is_executable = True )
@@ -91,7 +134,8 @@ custom_toolchain = rule(
91
134
implementation = _custom_toolchain_impl ,
92
135
attrs = {
93
136
"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
+ ),
95
140
},
96
141
)
97
-
0 commit comments