@@ -185,15 +185,17 @@ def get_cc_user_link_flags(ctx):
185
185
"""
186
186
return ctx .fragments .cpp .linkopts
187
187
188
- def get_linker_and_args (ctx , cc_toolchain , feature_configuration , rpaths ):
188
+ def get_linker_and_args (ctx , attr , cc_toolchain , feature_configuration , rpaths ):
189
189
"""Gathers cc_common linker information
190
190
191
191
Args:
192
192
ctx (ctx): The current target's context object
193
+ attr (struct): Attributes to use in gathering linker args
193
194
cc_toolchain (CcToolchain): cc_toolchain for which we are creating build variables.
194
195
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
195
196
rpaths (depset): Depset of directories where loader will look for libraries at runtime.
196
197
198
+
197
199
Returns:
198
200
tuple: A tuple of the following items:
199
201
- (str): The tool path for given action.
@@ -204,7 +206,7 @@ def get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths):
204
206
205
207
# Add linkopt's from dependencies. This includes linkopts from transitive
206
208
# dependencies since they get merged up.
207
- for dep in ctx . attr . deps :
209
+ for dep in getattr ( attr , " deps" , []) :
208
210
if CcInfo in dep and dep [CcInfo ].linking_context :
209
211
for linker_input in dep [CcInfo ].linking_context .linker_inputs .to_list ():
210
212
for flag in linker_input .user_link_flags :
@@ -482,7 +484,7 @@ def construct_arguments(
482
484
# linker since it won't understand.
483
485
if toolchain .target_arch != "wasm32" :
484
486
rpaths = _compute_rpaths (toolchain , output_dir , dep_info )
485
- ld , link_args , link_env = get_linker_and_args (ctx , cc_toolchain , feature_configuration , rpaths )
487
+ ld , link_args , link_env = get_linker_and_args (ctx , attr , cc_toolchain , feature_configuration , rpaths )
486
488
env .update (link_env )
487
489
rustc_flags .add ("--codegen=linker=" + ld )
488
490
rustc_flags .add_joined ("--codegen" , link_args , join_with = " " , format_joined = "link-args=%s" )
@@ -528,6 +530,7 @@ def construct_arguments(
528
530
529
531
def rustc_compile_action (
530
532
ctx ,
533
+ attr ,
531
534
toolchain ,
532
535
crate_info ,
533
536
output_hash = None ,
@@ -537,6 +540,7 @@ def rustc_compile_action(
537
540
538
541
Args:
539
542
ctx (ctx): The rule's context object
543
+ attr (struct): Attributes to use for the rust compile action
540
544
toolchain (rust_toolchain): The current `rust_toolchain`
541
545
crate_info (CrateInfo): The CrateInfo provider for the current target.
542
546
output_hash (str, optional): The hashed path of the crate root. Defaults to None.
@@ -559,35 +563,35 @@ def rustc_compile_action(
559
563
)
560
564
561
565
compile_inputs , out_dir , build_env_files , build_flags_files = collect_inputs (
562
- ctx ,
563
- ctx .file ,
564
- ctx .files ,
565
- toolchain ,
566
- cc_toolchain ,
567
- crate_info ,
568
- dep_info ,
569
- build_info ,
566
+ ctx = ctx ,
567
+ file = ctx .file ,
568
+ files = ctx .files ,
569
+ toolchain = toolchain ,
570
+ cc_toolchain = cc_toolchain ,
571
+ crate_info = crate_info ,
572
+ dep_info = dep_info ,
573
+ build_info = build_info ,
570
574
)
571
575
572
576
args , env = construct_arguments (
573
- ctx ,
574
- ctx . attr ,
575
- ctx .file ,
576
- toolchain ,
577
- toolchain .rustc .path ,
578
- cc_toolchain ,
579
- feature_configuration ,
580
- crate_info ,
581
- dep_info ,
582
- output_hash ,
583
- rust_flags ,
584
- out_dir ,
585
- build_env_files ,
586
- build_flags_files ,
577
+ ctx = ctx ,
578
+ attr = attr ,
579
+ file = ctx .file ,
580
+ toolchain = toolchain ,
581
+ tool_path = toolchain .rustc .path ,
582
+ cc_toolchain = cc_toolchain ,
583
+ feature_configuration = feature_configuration ,
584
+ crate_info = crate_info ,
585
+ dep_info = dep_info ,
586
+ output_hash = output_hash ,
587
+ rust_flags = rust_flags ,
588
+ out_dir = out_dir ,
589
+ build_env_files = build_env_files ,
590
+ build_flags_files = build_flags_files ,
587
591
)
588
592
589
- if hasattr (ctx . attr , "version" ) and ctx . attr .version != "0.0.0" :
590
- formatted_version = " v{}" .format (ctx . attr .version )
593
+ if hasattr (attr , "version" ) and attr .version != "0.0.0" :
594
+ formatted_version = " v{}" .format (attr .version )
591
595
else :
592
596
formatted_version = ""
593
597
@@ -613,9 +617,9 @@ def rustc_compile_action(
613
617
collect_data = True ,
614
618
)
615
619
616
- out_binary = False
617
- if hasattr ( ctx . attr , "out_binary" ):
618
- out_binary = getattr (ctx . attr , "out_binary" )
620
+ # TODO: Remove after some resolution to
621
+ # https://github.com/bazelbuild/rules_rust/issues/771
622
+ out_binary = getattr (attr , "out_binary" , False )
619
623
620
624
providers = [
621
625
crate_info ,
@@ -628,18 +632,19 @@ def rustc_compile_action(
628
632
),
629
633
]
630
634
if toolchain .target_arch != "wasm32" :
631
- providers += establish_cc_info (ctx , crate_info , toolchain , cc_toolchain , feature_configuration )
635
+ providers += establish_cc_info (ctx , attr , crate_info , toolchain , cc_toolchain , feature_configuration )
632
636
633
637
return providers
634
638
635
639
def _is_dylib (dep ):
636
640
return not bool (dep .static_library or dep .pic_static_library )
637
641
638
- def establish_cc_info (ctx , crate_info , toolchain , cc_toolchain , feature_configuration ):
642
+ def establish_cc_info (ctx , attr , crate_info , toolchain , cc_toolchain , feature_configuration ):
639
643
"""If the produced crate is suitable yield a CcInfo to allow for interop with cc rules
640
644
641
645
Args:
642
646
ctx (ctx): The rule's context object
647
+ attr (struct): Attributes to use in gathering CcInfo
643
648
crate_info (CrateInfo): The CrateInfo provider of the target crate
644
649
toolchain (rust_toolchain): The current `rust_toolchain`
645
650
cc_toolchain (CcToolchainInfo): The current `CcToolchainInfo`
@@ -649,7 +654,17 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
649
654
list: A list containing the CcInfo provider
650
655
"""
651
656
652
- if crate_info .is_test or crate_info .type not in ("staticlib" , "cdylib" , "rlib" , "lib" ) or getattr (ctx .attr , "out_binary" , False ):
657
+ # A test will not need to produce CcInfo as nothing can depend on test targets
658
+ if crate_info .is_test :
659
+ return []
660
+
661
+ # Only generate CcInfo for particular crate types
662
+ if crate_info .type not in ("staticlib" , "cdylib" , "rlib" , "lib" ):
663
+ return []
664
+
665
+ # TODO: Remove after some resolution to
666
+ # https://github.com/bazelbuild/rules_rust/issues/771
667
+ if getattr (attr , "out_binary" , False ):
653
668
return []
654
669
655
670
if crate_info .type == "staticlib" :
@@ -698,7 +713,7 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
698
713
)
699
714
700
715
cc_infos = [CcInfo (linking_context = linking_context )]
701
- for dep in ctx . attr . deps :
716
+ for dep in getattr ( attr , " deps" , []) :
702
717
if CcInfo in dep :
703
718
cc_infos .append (dep [CcInfo ])
704
719
0 commit comments