@@ -26,6 +26,8 @@ def toolchains_hub(
2626 # @unnamed-macro
2727 """Define the toolchains so that the lexicographical order registration is deterministic.
2828
29+ TODO @aignas 2025-03-09: see if this can be reused in the python toolchains.
30+
2931 Args:
3032 name: Unused.
3133 names: The names for toolchain targets.
@@ -36,11 +38,21 @@ def toolchains_hub(
3638 if len (names ) != len (implementations ):
3739 fail ("Each name must have an implementation" )
3840
39- padding = len (str (len (names ))) # get the number of digits
41+ # We are setting the order of the toolchains so that the later coming
42+ # toolchains override the previous definitions using the toolchain
43+ # resolution properties:
44+ # * the toolchains are matched by target settings and target_compatible_with
45+ # * the first toolchain satisfying the above wins
46+ #
47+ # this means we need to register the toolchains prefixed with a number of
48+ # format 00xy, where x and y are some digits and the leading zeros to
49+ # ensure lexicographical sorting.
50+ prefix_len = len (str (len (names )))
51+ prefix = "0" * (prefix_len - 1 )
52+
4053 for i , name in sorted (enumerate (names ), key = lambda x : - x [0 ]):
41- # poor mans implementation leading 0
42- number_prefix = ("0" * padding ) + "{}" .format (i )
43- number_prefix = number_prefix [- padding :]
54+ # prefix with a prefix and then truncate the string.
55+ number_prefix = "{}{}" .format (prefix , i )[- prefix_len :]
4456
4557 native .toolchain (
4658 name = "{}_{}" .format (number_prefix , name ),
0 commit comments