@@ -32,6 +32,54 @@ def _get_bin_dir_from_action(action):
3232 bin_dir = bin_dir .split ("/bin/" )[0 ] + "/bin"
3333 return bin_dir
3434
35+ def _get_darwin_component (arg ):
36+ """Extract darwin component from a path.
37+
38+ Args:
39+ arg: Path like "path/to/darwin_x86_64-fastbuild-ST-abc123/package"
40+
41+ Returns:
42+ Darwin component like "darwin" (ignoring arch and compilation mode)
43+ """
44+
45+ # path/to/darwin_x86_64-fastbuild-ST-abc123/package -> darwin_x86_64-fastbuild-ST-abc123
46+ darwin_component = [x for x in arg .split ("/" ) if x .startswith ("darwin" )][0 ]
47+
48+ # darwin_x86_64-fastbuild-ST-abc123 -> darwin
49+ return darwin_component .split ("-" )[0 ]
50+
51+ def _assert_bin_dir_structure (env , ctx , bin_dir , toolchain ):
52+ """Validate bin_dir structure, ignoring ST-{hash} suffix from config transitions.
53+
54+ Args:
55+ env: The analysis test environment
56+ ctx: The test context
57+ bin_dir: The bin directory path to validate
58+ toolchain: The toolchain info
59+ """
60+ compilation_mode = ctx .var ["COMPILATION_MODE" ]
61+
62+ # bin_dir should be like: bazel-out/{platform}-{mode}[-ST-{hash}]/bin
63+ asserts .true (env , bin_dir .startswith ("bazel-out/" ), "bin_dir should start with bazel-out/" )
64+ asserts .true (env , bin_dir .endswith ("/bin" ), "bin_dir should end with /bin" )
65+
66+ # Validate it contains compilation mode (ignoring potential ST-{hash})
67+ bin_dir_components = bin_dir .split ("/" )[1 ] # Get the platform-mode component
68+ asserts .true (
69+ env ,
70+ compilation_mode in bin_dir_components ,
71+ "bin_dir should contain compilation mode: expected '{}' in '{}'" .format (compilation_mode , bin_dir_components ),
72+ )
73+
74+ # For Darwin platforms, validate darwin component
75+ if toolchain .target_os in ["macos" , "darwin" ] and "darwin" in bin_dir :
76+ darwin_component = _get_darwin_component (bin_dir )
77+ asserts .true (
78+ env ,
79+ darwin_component .startswith ("darwin" ),
80+ "darwin component should start with 'darwin', got '{}'" .format (darwin_component ),
81+ )
82+
3583def _rlib_has_no_native_libs_test_impl (ctx ):
3684 env = analysistest .begin (ctx )
3785 tut = analysistest .target_under_test (env )
@@ -138,7 +186,7 @@ def _extract_linker_args(argv):
138186 )
139187 ]
140188
141- def _bin_has_native_dep_and_alwayslink_test_impl (ctx ):
189+ def _bin_has_native_dep_and_alwayslink_test_impl (ctx , use_cc_linker ):
142190 env = analysistest .begin (ctx )
143191 tut = analysistest .target_under_test (env )
144192 action = tut .actions [0 ]
@@ -147,8 +195,9 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
147195 link_args = _extract_linker_args (action .argv )
148196 bin_dir = _get_bin_dir_from_action (action )
149197
150- # Use the explicit test attribute to determine expected behavior
151- use_cc_linker = ctx .attr ._use_cc_linker
198+ # Validate bin_dir structure (ignoring ST-{hash} suffix from config transitions)
199+ _assert_bin_dir_structure (env , ctx , bin_dir , toolchain )
200+
152201 if toolchain .target_os in ["macos" , "darwin" ]:
153202 if use_cc_linker :
154203 # When using CC linker, args are passed with -Wl, prefix as separate arguments
@@ -216,7 +265,7 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
216265 assert_list_contains_adjacent_elements (env , link_args , want )
217266 return analysistest .end (env )
218267
219- def _cdylib_has_native_dep_and_alwayslink_test_impl (ctx ):
268+ def _cdylib_has_native_dep_and_alwayslink_test_impl (ctx , use_cc_linker ):
220269 toolchain = _get_toolchain (ctx )
221270
222271 env = analysistest .begin (ctx )
@@ -226,11 +275,12 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx):
226275 linker_args = _extract_linker_args (action .argv )
227276 bin_dir = _get_bin_dir_from_action (action )
228277
278+ # Validate bin_dir structure (ignoring ST-{hash} suffix from config transitions)
279+ _assert_bin_dir_structure (env , ctx , bin_dir , toolchain )
280+
229281 compilation_mode = ctx .var ["COMPILATION_MODE" ]
230282 pic_suffix = _get_pic_suffix (ctx , compilation_mode )
231283
232- # Use the explicit test attribute to determine expected behavior
233- use_cc_linker = ctx .attr ._use_cc_linker
234284 if toolchain .target_os in ["macos" , "darwin" ]:
235285 if use_cc_linker :
236286 # When using CC linker, args are passed with -Wl, prefix as separate arguments
@@ -319,48 +369,57 @@ bin_has_native_libs_test = analysistest.make(_bin_has_native_libs_test_impl, att
319369 "_linker_preference" : attr .label (default = Label ("//rust/settings:toolchain_linker_preference" )),
320370 "_toolchain" : attr .label (default = Label ("//rust/toolchain:current_rust_toolchain" )),
321371})
372+
373+ def _bin_has_native_dep_and_alwayslink_rust_linker_test_impl (ctx ):
374+ return _bin_has_native_dep_and_alwayslink_test_impl (ctx , False )
375+
322376bin_has_native_dep_and_alwayslink_rust_linker_test = analysistest .make (
323- _bin_has_native_dep_and_alwayslink_test_impl ,
377+ _bin_has_native_dep_and_alwayslink_rust_linker_test_impl ,
324378 attrs = {
325379 "_linker_preference" : attr .label (default = Label ("//rust/settings:toolchain_linker_preference" )),
326380 "_toolchain" : attr .label (default = Label ("//rust/toolchain:current_rust_toolchain" )),
327- "_use_cc_linker" : attr .bool (default = False , doc = "Whether to expect CC linker flags (vs rust-lld)" ),
328381 },
329382 config_settings = {
330383 str (Label ("//rust/settings:toolchain_linker_preference" )): "rust" ,
331384 },
332385)
333386
387+ def _bin_has_native_dep_and_alwayslink_cc_linker_test_impl (ctx ):
388+ return _bin_has_native_dep_and_alwayslink_test_impl (ctx , True )
389+
334390bin_has_native_dep_and_alwayslink_cc_linker_test = analysistest .make (
335- _bin_has_native_dep_and_alwayslink_test_impl ,
391+ _bin_has_native_dep_and_alwayslink_cc_linker_test_impl ,
336392 attrs = {
337393 "_linker_preference" : attr .label (default = Label ("//rust/settings:toolchain_linker_preference" )),
338394 "_toolchain" : attr .label (default = Label ("//rust/toolchain:current_rust_toolchain" )),
339- "_use_cc_linker" : attr .bool (default = True , doc = "Whether to expect CC linker flags (vs rust-lld)" ),
340395 },
341396 config_settings = {
342397 str (Label ("//rust/settings:toolchain_linker_preference" )): "cc" ,
343398 },
344399)
345400
401+ def _cdylib_has_native_dep_and_alwayslink_rust_linker_test_impl (ctx ):
402+ return _cdylib_has_native_dep_and_alwayslink_test_impl (ctx , False )
403+
346404cdylib_has_native_dep_and_alwayslink_rust_linker_test = analysistest .make (
347- _cdylib_has_native_dep_and_alwayslink_test_impl ,
405+ _cdylib_has_native_dep_and_alwayslink_rust_linker_test_impl ,
348406 attrs = {
349407 "_linker_preference" : attr .label (default = Label ("//rust/settings:toolchain_linker_preference" )),
350408 "_toolchain" : attr .label (default = Label ("//rust/toolchain:current_rust_toolchain" )),
351- "_use_cc_linker" : attr .bool (default = False , doc = "Whether to expect CC linker flags (vs rust-lld)" ),
352409 },
353410 config_settings = {
354411 str (Label ("//rust/settings:toolchain_linker_preference" )): "rust" ,
355412 },
356413)
357414
415+ def _cdylib_has_native_dep_and_alwayslink_cc_linker_test_impl (ctx ):
416+ return _cdylib_has_native_dep_and_alwayslink_test_impl (ctx , True )
417+
358418cdylib_has_native_dep_and_alwayslink_cc_linker_test = analysistest .make (
359- _cdylib_has_native_dep_and_alwayslink_test_impl ,
419+ _cdylib_has_native_dep_and_alwayslink_cc_linker_test_impl ,
360420 attrs = {
361421 "_linker_preference" : attr .label (default = Label ("//rust/settings:toolchain_linker_preference" )),
362422 "_toolchain" : attr .label (default = Label ("//rust/toolchain:current_rust_toolchain" )),
363- "_use_cc_linker" : attr .bool (default = True , doc = "Whether to expect CC linker flags (vs rust-lld)" ),
364423 },
365424 config_settings = {
366425 str (Label ("//rust/settings:toolchain_linker_preference" )): "cc" ,
0 commit comments