3
3
load ("@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" )
4
4
load ("@rules_cc//cc:defs.bzl" , "cc_library" )
5
5
load ("//rust:defs.bzl" , "rust_binary" , "rust_library" , "rust_proc_macro" , "rust_shared_library" , "rust_static_library" )
6
- load ("//test/unit:common.bzl" , "assert_argv_contains" , "assert_argv_contains_not" , "assert_argv_contains_prefix_suffix" )
6
+ load ("//test/unit:common.bzl" , "assert_argv_contains" , "assert_argv_contains_not" , "assert_argv_contains_prefix_suffix" , "assert_list_contains_adjacent_elements" )
7
7
8
8
def _native_dep_lib_name (ctx ):
9
9
if ctx .target_platform_has_constraint (ctx .attr ._windows_constraint [platform_common .ConstraintValueInfo ]):
@@ -78,7 +78,13 @@ def _bin_has_native_libs_test_impl(ctx):
78
78
return analysistest .end (env )
79
79
80
80
def _extract_linker_args (argv ):
81
- return [a for a in argv if a .startswith ("link-arg=" ) or a .startswith ("link-arg=-l" ) or a .startswith ("-l" ) or a .endswith (".lo" ) or a .endswith (".o" )]
81
+ return [a for a in argv if (
82
+ a .startswith ("link-arg=" ) or
83
+ a .startswith ("link-args=" ) or
84
+ a .startswith ("-l" ) or
85
+ a .endswith (".lo" ) or
86
+ a .endswith (".o" )
87
+ )]
82
88
83
89
def _bin_has_native_dep_and_alwayslink_test_impl (ctx ):
84
90
env = analysistest .begin (ctx )
@@ -103,7 +109,12 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx):
103
109
"link-arg=bazel-out/k8-fastbuild/bin/test/unit/native_deps/libalwayslink.lo" ,
104
110
"link-arg=-Wl,--no-whole-archive" ,
105
111
]
106
- asserts .equals (env , want , _extract_linker_args (action .argv ))
112
+ individual_link_args = [
113
+ arg
114
+ for arg in _extract_linker_args (action .argv )
115
+ if arg .startswith ("link-arg=" )
116
+ ]
117
+ asserts .equals (env , want , individual_link_args )
107
118
return analysistest .end (env )
108
119
109
120
def _cdylib_has_native_dep_and_alwayslink_test_impl (ctx ):
@@ -243,13 +254,62 @@ def _native_dep_test():
243
254
target_under_test = ":cdylib_has_native_dep_and_alwayslink" ,
244
255
)
245
256
257
+ def _linkopts_propagate_test_impl (ctx ):
258
+ env = analysistest .begin (ctx )
259
+ tut = analysistest .target_under_test (env )
260
+ action = tut .actions [0 ]
261
+
262
+ # Ensure linkopts from direct (-Llinkoptdep1) and transitive
263
+ # (-Llinkoptdep2) dependencies are propagated.
264
+ # Consistently with cc rules, dependency linkopts take precedence over
265
+ # dependent linkopts (i.e. dependency linkopts appear later in the command
266
+ # line).
267
+ linkopt_args = [
268
+ arg
269
+ for arg in _extract_linker_args (action .argv )
270
+ if arg .startswith ("link-args" )
271
+ ][0 ].split (" " )
272
+ assert_list_contains_adjacent_elements (
273
+ env ,
274
+ linkopt_args ,
275
+ ["-Llinkoptdep1" , "-Llinkoptdep2" ],
276
+ )
277
+ return analysistest .end (env )
278
+
279
+ linkopts_propagate_test = analysistest .make (_linkopts_propagate_test_impl )
280
+
281
+ def _linkopts_test ():
282
+ rust_binary (
283
+ name = "linkopts_rust_bin" ,
284
+ srcs = ["bin_using_native_dep.rs" ],
285
+ deps = [":linkopts_native_dep_a" ],
286
+ )
287
+
288
+ cc_library (
289
+ name = "linkopts_native_dep_a" ,
290
+ srcs = ["native_dep.cc" ],
291
+ linkopts = ["-Llinkoptdep1" ],
292
+ deps = [":linkopts_native_dep_b" ],
293
+ )
294
+
295
+ cc_library (
296
+ name = "linkopts_native_dep_b" ,
297
+ linkopts = ["-Llinkoptdep2" ],
298
+ )
299
+
300
+ linkopts_propagate_test (
301
+ name = "native_linkopts_propagate_test" ,
302
+ target_under_test = ":linkopts_rust_bin" ,
303
+ )
304
+
246
305
def native_deps_test_suite (name ):
247
306
"""Entry-point macro called from the BUILD file.
248
307
249
308
Args:
250
309
name: Name of the macro.
251
310
"""
252
311
_native_dep_test ()
312
+ _linkopts_test ()
253
313
254
314
native .test_suite (
255
315
name = name ,
@@ -261,5 +321,6 @@ def native_deps_test_suite(name):
261
321
":bin_has_native_libs_test" ,
262
322
":bin_has_native_dep_and_alwayslink_test" ,
263
323
":cdylib_has_native_dep_and_alwayslink_test" ,
324
+ ":native_linkopts_propagate_test" ,
264
325
],
265
326
)
0 commit comments