@@ -41,7 +41,7 @@ def rules_d_dependencies():
4141########
4242_DOC = "Fetch external tools needed for d toolchain"
4343_ATTRS = {
44- "d_version" : attr .string (mandatory = True , values = [ "auto" , "dmd" , "ldc" ] + SDK_VERSIONS .keys ()),
44+ "d_version" : attr .string (mandatory = True , values = SDK_VERSIONS .keys ()),
4545 "platform" : attr .string (mandatory = True , values = PLATFORMS .keys ()),
4646}
4747
@@ -61,20 +61,6 @@ def _d_repo_impl(repository_ctx):
6161 d_version = repository_ctx .attr .d_version
6262 platform = repository_ctx .attr .platform
6363
64- if d_version == "auto" :
65- for compiler in ["dmd" , "ldc" ]:
66- for version , platforms in SDK_VERSIONS .items ():
67- if version .startswith (compiler ) and platform in platforms :
68- d_version = compiler
69- break
70- if d_version != "auto" :
71- break
72-
73- if d_version in ["dmd" , "ldc" ]:
74- sdks = [version for version , platforms in SDK_VERSIONS .items () if version .startswith (d_version ) and platform in platforms ]
75- sdks = sorted (sdks , key = lambda x : [int (v ) if v .isdigit () else v for v in x [4 :].split ("." )], reverse = True )
76- d_version = sdks [0 ] if sdks else d_version
77-
7864 if d_version not in SDK_VERSIONS :
7965 fail ("Unknown d_version: %s" % d_version )
8066 if platform not in SDK_VERSIONS [d_version ]:
@@ -107,12 +93,15 @@ def d_register_toolchains(name, register = True, **kwargs):
10793 Users can avoid this macro and do these steps themselves, if they want more control.
10894
10995 Args:
110- name: base name for all created repos, like "d1_14"
96+ name: base name for all created repositories
11197 register: whether to call through to native.register_toolchains.
11298 Should be True for WORKSPACE users, but false when used under bzlmod extension
11399 **kwargs: passed to each d_repositories call
114100 """
115101 for platform in PLATFORMS .keys ():
102+ d_version = kwargs .get ("d_version" )
103+ if not d_version or d_version not in SDK_VERSIONS or platform not in SDK_VERSIONS [d_version ]:
104+ continue
116105 d_repositories (name = name + "_" + platform , platform = platform , ** kwargs )
117106 if register :
118107 native .register_toolchains ("@%s_toolchains//:%s_toolchain" % (name , platform ))
@@ -121,3 +110,36 @@ def d_register_toolchains(name, register = True, **kwargs):
121110 name = name + "_toolchains" ,
122111 user_repository_name = name ,
123112 )
113+
114+ def _get_platform_id (os ):
115+ """Get the platform id for the given os repository_os information."""
116+ if os .name == "linux" :
117+ if os .arch in ["amd64" , "x86_64" ]:
118+ return "x86_64-unknown-linux-gnu"
119+ elif os .arch in ["aarch64" , "arm64" ]:
120+ return "aarch64-unknown-linux-gnu"
121+ elif os .name in ["darwin" , "mac os x" , "macos" , "osx" ]:
122+ if os .arch in ["amd64" , "x86_64" ]:
123+ return "x86_64-apple-darwin"
124+ elif os .arch in ["aarch64" , "arm64" ]:
125+ return "aarch64-apple-darwin"
126+ elif "windows" in os .name :
127+ if os .arch in ["amd64" , "x86_64" ]:
128+ return "x86_64-pc-windows-msvc"
129+ fail ("Unsupported OS/arch combination: %s/%s" % (os .name , os .arch ))
130+
131+ def select_compiler_by_os (versions , os ):
132+ """Select the most appropriate compiler version for the given OS.
133+
134+ Args:
135+ versions: list of compiler versions
136+ os: repository_os information
137+
138+ Returns:
139+ selected compiler version
140+ """
141+ platform_id = _get_platform_id (os )
142+ for version in versions :
143+ if platform_id in SDK_VERSIONS .get (version , {}):
144+ return version
145+ fail ("No suitable d compiler found for OS %s among versions: %s." % (os , ", " .join (versions )))
0 commit comments