@@ -28,7 +28,7 @@ use std::sync::Arc;
28
28
29
29
use crate :: core:: compiler:: unit_dependencies:: { build_unit_dependencies, IsArtifact } ;
30
30
use crate :: core:: compiler:: unit_graph:: { self , UnitDep , UnitGraph } ;
31
- use crate :: core:: compiler:: { standard_lib, TargetInfo } ;
31
+ use crate :: core:: compiler:: { standard_lib, CrateType , TargetInfo } ;
32
32
use crate :: core:: compiler:: { BuildConfig , BuildContext , Compilation , Context } ;
33
33
use crate :: core:: compiler:: { CompileKind , CompileMode , CompileTarget , RustcTargetData , Unit } ;
34
34
use crate :: core:: compiler:: { DefaultExecutor , Executor , UnitInterner } ;
@@ -505,6 +505,10 @@ pub fn create_bcx<'a, 'cfg>(
505
505
interner,
506
506
) ?;
507
507
508
+ if let Some ( args) = target_rustc_crate_types {
509
+ override_rustc_crate_types ( & mut units, args, interner) ?;
510
+ }
511
+
508
512
let mut scrape_units = match rustdoc_scrape_examples {
509
513
Some ( arg) => {
510
514
let filter = match arg. as_str ( ) {
@@ -648,28 +652,6 @@ pub fn create_bcx<'a, 'cfg>(
648
652
}
649
653
}
650
654
651
- let mut crate_types = HashMap :: new ( ) ;
652
- if let Some ( args) = target_rustc_crate_types {
653
- if units. len ( ) != 1 {
654
- anyhow:: bail!(
655
- "crate types to rustc can only be passed to one \
656
- target, consider filtering\n the package by passing, \
657
- e.g., `--lib` or `--example` to specify a single target"
658
- ) ;
659
- }
660
- match units[ 0 ] . target . kind ( ) {
661
- TargetKind :: Lib ( _) | TargetKind :: ExampleLib ( _) => {
662
- crate_types. insert ( units[ 0 ] . clone ( ) , args. clone ( ) ) ;
663
- }
664
- _ => {
665
- anyhow:: bail!(
666
- "crate types can only be specified for libraries and example libraries.\n \
667
- Binaries, tests, and benchmarks are always the `bin` crate type"
668
- ) ;
669
- }
670
- }
671
- }
672
-
673
655
if honor_rust_version {
674
656
// Remove any pre-release identifiers for easier comparison
675
657
let current_version = & target_data. rustc . version ;
@@ -706,7 +688,6 @@ pub fn create_bcx<'a, 'cfg>(
706
688
build_config,
707
689
profiles,
708
690
extra_compiler_args,
709
- crate_types,
710
691
target_data,
711
692
units,
712
693
unit_graph,
@@ -1871,3 +1852,50 @@ fn remove_duplicate_doc(
1871
1852
}
1872
1853
unit_graph. retain ( |unit, _| visited. contains ( unit) ) ;
1873
1854
}
1855
+
1856
+ /// Override crate types for given units.
1857
+ ///
1858
+ /// This is primarily used by `cargo rustc --crate-type`.
1859
+ fn override_rustc_crate_types (
1860
+ units : & mut [ Unit ] ,
1861
+ args : & [ String ] ,
1862
+ interner : & UnitInterner ,
1863
+ ) -> CargoResult < ( ) > {
1864
+ if units. len ( ) != 1 {
1865
+ anyhow:: bail!(
1866
+ "crate types to rustc can only be passed to one \
1867
+ target, consider filtering\n the package by passing, \
1868
+ e.g., `--lib` or `--example` to specify a single target"
1869
+ ) ;
1870
+ }
1871
+
1872
+ let unit = & units[ 0 ] ;
1873
+ let override_unit = |f : fn ( Vec < CrateType > ) -> TargetKind | {
1874
+ let crate_types = args. iter ( ) . map ( |s| s. into ( ) ) . collect ( ) ;
1875
+ let mut target = unit. target . clone ( ) ;
1876
+ target. set_kind ( f ( crate_types) ) ;
1877
+ interner. intern (
1878
+ & unit. pkg ,
1879
+ & target,
1880
+ unit. profile . clone ( ) ,
1881
+ unit. kind ,
1882
+ unit. mode ,
1883
+ unit. features . clone ( ) ,
1884
+ unit. is_std ,
1885
+ unit. dep_hash ,
1886
+ unit. artifact ,
1887
+ )
1888
+ } ;
1889
+ units[ 0 ] = match unit. target . kind ( ) {
1890
+ TargetKind :: Lib ( _) => override_unit ( TargetKind :: Lib ) ,
1891
+ TargetKind :: ExampleLib ( _) => override_unit ( TargetKind :: ExampleLib ) ,
1892
+ _ => {
1893
+ anyhow:: bail!(
1894
+ "crate types can only be specified for libraries and example libraries.\n \
1895
+ Binaries, tests, and benchmarks are always the `bin` crate type"
1896
+ ) ;
1897
+ }
1898
+ } ;
1899
+
1900
+ Ok ( ( ) )
1901
+ }
0 commit comments