@@ -8,6 +8,7 @@ PywrapInfo = provider(
8
8
"py_stub" : "Pybind Python stub used to resolve cross-package references" ,
9
9
"cc_only" : "True if this PywrapInfo represents cc-only library (no PyIni_)" ,
10
10
"starlark_only" : "" ,
11
+ "default_runfiles" : "" ,
11
12
},
12
13
)
13
14
@@ -21,6 +22,7 @@ PywrapFilters = provider(
21
22
fields = {
22
23
"pywrap_lib_filter" : "" ,
23
24
"common_lib_filters" : "" ,
25
+ "dynamic_lib_filter" : "" ,
24
26
},
25
27
)
26
28
@@ -68,12 +70,16 @@ def pywrap_library(
68
70
if starlark_only_pywrap_count > 0 :
69
71
starlark_only_filter_full_name = "%s%s__starlark_only_common" % (cur_pkg , name )
70
72
73
+ inverse_common_lib_filters = _construct_inverse_common_lib_filters (
74
+ common_lib_filters ,
75
+ )
76
+
71
77
_linker_input_filters (
72
78
name = linker_input_filters_name ,
73
79
dep = ":%s" % info_collector_name ,
74
80
pywrap_lib_filter = pywrap_lib_filter ,
75
81
pywrap_lib_exclusion_filter = pywrap_lib_exclusion_filter ,
76
- common_lib_filters = { v : k for k , v in common_lib_filters . items ()} ,
82
+ common_lib_filters = inverse_common_lib_filters ,
77
83
starlark_only_filter_name = starlark_only_filter_full_name ,
78
84
)
79
85
@@ -108,7 +114,7 @@ def pywrap_library(
108
114
common_cc_binary_name = "%s" % common_lib_name
109
115
common_import_name = _construct_common_binary (
110
116
common_cc_binary_name ,
111
- [":%s" % common_split_name ] + common_deps ,
117
+ common_deps + [":%s" % common_split_name ],
112
118
linkopts ,
113
119
testonly ,
114
120
compatible_with ,
@@ -117,6 +123,7 @@ def pywrap_library(
117
123
binaries_data .values (),
118
124
common_lib_pkg ,
119
125
ver_script ,
126
+ data = [":%s" % common_split_name ],
120
127
)
121
128
actual_binaries_data = binaries_data
122
129
actual_common_deps = common_deps
@@ -223,7 +230,8 @@ def _construct_common_binary(
223
230
local_defines ,
224
231
dependency_common_lib_packages ,
225
232
dependent_common_lib_package ,
226
- version_script ):
233
+ version_script ,
234
+ data ):
227
235
actual_linkopts = _construct_linkopt_soname (name ) + _construct_linkopt_rpaths (
228
236
dependency_common_lib_packages ,
229
237
dependent_common_lib_package ,
@@ -242,6 +250,7 @@ def _construct_common_binary(
242
250
compatible_with = compatible_with ,
243
251
win_def_file = win_def_file ,
244
252
local_defines = local_defines ,
253
+ # data = data,
245
254
)
246
255
247
256
if_lib_name = "%s_if_lib" % name
@@ -263,6 +272,14 @@ def _construct_common_binary(
263
272
compatible_with = compatible_with ,
264
273
)
265
274
275
+ cc_lib_name = "%s_cc_library" % name
276
+ native .cc_library (
277
+ name = cc_lib_name ,
278
+ deps = [":%s" % import_name ],
279
+ testonly = testonly ,
280
+ data = data ,
281
+ )
282
+
266
283
return import_name
267
284
268
285
def _pywrap_split_library_impl (ctx ):
@@ -275,6 +292,7 @@ def _pywrap_split_library_impl(ctx):
275
292
276
293
split_linker_inputs = []
277
294
private_linker_inputs = []
295
+ default_runfiles = None
278
296
if not pw .cc_only :
279
297
split_linker_inputs .append (li )
280
298
pywrap_lib_filter = ctx .attr .linker_input_filters [PywrapFilters ].pywrap_lib_filter
@@ -286,11 +304,14 @@ def _pywrap_split_library_impl(ctx):
286
304
depset (direct = private_lis ),
287
305
]
288
306
307
+ # default_runfiles = pw.default_runfiles
308
+
289
309
return _construct_split_library_cc_info (
290
310
ctx ,
291
311
split_linker_inputs ,
292
312
user_link_flags ,
293
313
private_linker_inputs ,
314
+ default_runfiles ,
294
315
)
295
316
296
317
_pywrap_split_library = rule (
@@ -332,15 +353,30 @@ def _pywrap_common_split_library_impl(ctx):
332
353
else :
333
354
libs_to_include = filters .common_lib_filters [ctx .attr .common_lib_full_name ]
334
355
356
+ user_link_flags = {}
357
+ dynamic_lib_filter = filters .dynamic_lib_filter
358
+ default_runfiles = ctx .runfiles ()
335
359
for pw in pywrap_infos :
336
360
pw_lis = pw .cc_info .linking_context .linker_inputs .to_list ()[1 :]
361
+ pw_runfiles_merged = False
337
362
for li in pw_lis :
338
363
if li in libs_to_exclude :
339
364
continue
340
- if include_all_not_excluded or (li in libs_to_include ):
365
+ if include_all_not_excluded or (li in libs_to_include ) or li in dynamic_lib_filter :
341
366
split_linker_inputs .append (li )
367
+ for user_link_flag in li .user_link_flags :
368
+ user_link_flags [user_link_flag ] = True
369
+ if not pw_runfiles_merged :
370
+ default_runfiles = default_runfiles .merge (pw .default_runfiles )
371
+ pw_runfiles_merged = True
342
372
343
- return _construct_split_library_cc_info (ctx , split_linker_inputs , [], [])
373
+ return _construct_split_library_cc_info (
374
+ ctx ,
375
+ split_linker_inputs ,
376
+ list (user_link_flags .keys ()),
377
+ [],
378
+ default_runfiles ,
379
+ )
344
380
345
381
_pywrap_common_split_library = rule (
346
382
attrs = {
@@ -367,7 +403,8 @@ def _construct_split_library_cc_info(
367
403
ctx ,
368
404
split_linker_inputs ,
369
405
user_link_flags ,
370
- private_linker_inputs ):
406
+ private_linker_inputs ,
407
+ default_runfiles ):
371
408
dependency_libraries = _construct_dependency_libraries (
372
409
ctx ,
373
410
split_linker_inputs ,
@@ -386,7 +423,11 @@ def _construct_split_library_cc_info(
386
423
),
387
424
)
388
425
389
- return [CcInfo (linking_context = linking_context )]
426
+ return [
427
+ CcInfo (linking_context = linking_context ),
428
+ # DefaultInfo(files = default_runfiles.files)
429
+ DefaultInfo (runfiles = default_runfiles ),
430
+ ]
390
431
391
432
def _construct_dependency_libraries (ctx , split_linker_inputs ):
392
433
cc_toolchain = find_cpp_toolchain (ctx )
@@ -400,7 +441,7 @@ def _construct_dependency_libraries(ctx, split_linker_inputs):
400
441
for split_linker_input in split_linker_inputs :
401
442
for lib in split_linker_input .libraries :
402
443
lib_copy = lib
403
- if not lib .alwayslink :
444
+ if not lib .alwayslink and ( lib . static_library or lib . pic_static_library ) :
404
445
lib_copy = cc_common .create_library_to_link (
405
446
actions = ctx .actions ,
406
447
cc_toolchain = cc_toolchain ,
@@ -418,6 +459,10 @@ def _linker_input_filters_impl(ctx):
418
459
pywrap_lib_exclusion_filter = {}
419
460
pywrap_lib_filter = {}
420
461
visited_filters = {}
462
+
463
+ #
464
+ # pywrap private filter
465
+ #
421
466
if ctx .attr .pywrap_lib_exclusion_filter :
422
467
for li in ctx .attr .pywrap_lib_exclusion_filter [CcInfo ].linking_context .linker_inputs .to_list ():
423
468
pywrap_lib_exclusion_filter [li ] = li .owner
@@ -429,13 +474,19 @@ def _linker_input_filters_impl(ctx):
429
474
430
475
common_lib_filters = {k : {} for k in ctx .attr .common_lib_filters .values ()}
431
476
477
+ #
478
+ # common lib filters
479
+ #
432
480
for filter , name in ctx .attr .common_lib_filters .items ():
433
481
filter_li = filter [CcInfo ].linking_context .linker_inputs .to_list ()
434
482
for li in filter_li :
435
483
if li not in visited_filters :
436
484
common_lib_filters [name ][li ] = li .owner
437
485
visited_filters [li ] = li .owner
438
486
487
+ #
488
+ # starlark -only filter
489
+ #
439
490
pywrap_infos = ctx .attr .dep [CollectedPywrapInfo ].pywrap_infos .to_list ()
440
491
starlark_only_filter = {}
441
492
@@ -451,10 +502,29 @@ def _linker_input_filters_impl(ctx):
451
502
starlark_only_filter .pop (li , None )
452
503
453
504
common_lib_filters [ctx .attr .starlark_only_filter_name ] = starlark_only_filter
505
+
506
+ #
507
+ # dynamic libs filter
508
+ #
509
+ dynamic_lib_filter = {}
510
+ empty_lib_filter = {}
511
+ for pw in pywrap_infos :
512
+ for li in pw .cc_info .linking_context .linker_inputs .to_list ()[1 :]:
513
+ all_dynamic = None
514
+ for lib in li .libraries :
515
+ if lib .static_library or lib .pic_static_library or not lib .dynamic_library :
516
+ all_dynamic = False
517
+ break
518
+ elif all_dynamic == None :
519
+ all_dynamic = True
520
+ if all_dynamic :
521
+ dynamic_lib_filter [li ] = li .owner
522
+
454
523
return [
455
524
PywrapFilters (
456
525
pywrap_lib_filter = pywrap_lib_filter ,
457
526
common_lib_filters = common_lib_filters ,
527
+ dynamic_lib_filter = dynamic_lib_filter ,
458
528
),
459
529
]
460
530
@@ -488,7 +558,7 @@ _linker_input_filters = rule(
488
558
def pywrap_common_library (name , dep , filter_name = None ):
489
559
native .alias (
490
560
name = name ,
491
- actual = "%s_import " % (filter_name if filter_name else dep + "_common" ),
561
+ actual = "%s_cc_library " % (filter_name if filter_name else dep + "_common" ),
492
562
)
493
563
494
564
def pywrap_binaries (name , dep , ** kwargs ):
@@ -621,10 +691,15 @@ def _pywrap_info_wrapper_impl(ctx):
621
691
substitutions = substitutions ,
622
692
)
623
693
694
+ default_runfiles = ctx .runfiles ().merge (
695
+ ctx .attr .deps [0 ][DefaultInfo ].default_runfiles ,
696
+ )
697
+
624
698
return [
625
699
PyInfo (transitive_sources = depset ()),
626
700
PywrapInfo (
627
701
cc_info = ctx .attr .deps [0 ][CcInfo ],
702
+ default_runfiles = default_runfiles ,
628
703
owner = ctx .label ,
629
704
common_lib_packages = ctx .attr .common_lib_packages ,
630
705
py_stub = py_stub ,
@@ -652,11 +727,16 @@ _pywrap_info_wrapper = rule(
652
727
653
728
def _cc_only_pywrap_info_wrapper_impl (ctx ):
654
729
wrapped_dep = ctx .attr .deps [0 ]
730
+ default_runfiles = ctx .runfiles ().merge (
731
+ ctx .attr .deps [0 ][DefaultInfo ].default_runfiles ,
732
+ )
733
+
655
734
return [
656
735
PyInfo (transitive_sources = depset ()),
657
736
PywrapInfo (
658
737
cc_info = wrapped_dep [CcInfo ],
659
738
owner = ctx .label ,
739
+ default_runfiles = default_runfiles ,
660
740
common_lib_packages = ctx .attr .common_lib_packages ,
661
741
py_stub = None ,
662
742
cc_only = True ,
@@ -923,6 +1003,20 @@ def _get_common_lib_package_and_name(common_lib_full_name):
923
1003
return common_lib_full_name .rsplit ("/" , 1 )
924
1004
return "" , common_lib_full_name
925
1005
1006
+ def _construct_inverse_common_lib_filters (common_lib_filters ):
1007
+ inverse_common_lib_filters = {}
1008
+ for common_lib_k , common_lib_v in common_lib_filters .items ():
1009
+ new_common_lib_k = common_lib_v
1010
+ if type (common_lib_v ) == type ([]):
1011
+ new_common_lib_k = "_%s_common_lib_filter" % common_lib_k .rsplit ("/" , 1 )[- 1 ]
1012
+ native .cc_library (
1013
+ name = new_common_lib_k ,
1014
+ deps = common_lib_v ,
1015
+ )
1016
+
1017
+ inverse_common_lib_filters [new_common_lib_k ] = common_lib_k
1018
+ return inverse_common_lib_filters
1019
+
926
1020
def _construct_linkopt_soname (name ):
927
1021
soname = name .rsplit ("/" , 1 )[1 ] if "/" in name else name
928
1022
soname = soname if name .startswith ("lib" ) else ("lib%s" % soname )
0 commit comments