1
1
use crate :: args:: Args ;
2
2
use crate :: artifact:: { Artifact , CrateType } ;
3
3
use crate :: error:: Error ;
4
+ use crate :: manifest:: Manifest ;
4
5
use crate :: profile:: Profile ;
5
6
use crate :: { utils, LocalizedConfig } ;
6
7
use std:: io:: BufRead ;
@@ -35,6 +36,7 @@ impl Subcommand {
35
36
"`--exclude` is not supported yet by `cargo-subcommand`"
36
37
) ;
37
38
let ( manifest_path, package) = utils:: find_package (
39
+ // TODO: This must be the path to the package already!
38
40
& args
39
41
. manifest_path
40
42
. clone ( )
@@ -50,6 +52,8 @@ impl Subcommand {
50
52
config. set_env_vars ( ) . unwrap ( ) ;
51
53
}
52
54
55
+ let parsed_manifest = Manifest :: parse_from_toml ( & manifest_path) ?;
56
+
53
57
let target_dir = args
54
58
. target_dir
55
59
. clone ( )
@@ -75,6 +79,8 @@ impl Subcommand {
75
79
. join ( utils:: get_target_dir_name ( config. as_deref ( ) ) . unwrap ( ) )
76
80
} ) ;
77
81
82
+ // https://doc.rust-lang.org/cargo/reference/cargo-targets.html#target-auto-discovery
83
+
78
84
let mut artifacts = vec ! [ ] ;
79
85
if args. examples {
80
86
for file in utils:: list_rust_files ( & root_dir. join ( "examples" ) ) ? {
@@ -85,17 +91,41 @@ impl Subcommand {
85
91
artifacts. push ( Artifact :: Example ( example. into ( ) ) ) ;
86
92
}
87
93
}
88
- if args. bins {
89
- for file in utils:: list_rust_files ( & root_dir. join ( "src" ) . join ( "bin" ) ) ? {
90
- artifacts. push ( Artifact :: Root ( file) ) ;
94
+
95
+ let specific_target_selected = args. specific_target_selected ( ) ;
96
+
97
+ // https://doc.rust-lang.org/cargo/reference/cargo-targets.html#binaries
98
+ if args. bins || !specific_target_selected {
99
+ // Parse all autobins
100
+ if parsed_manifest. package . map_or ( true , |p| p. autobins ) {
101
+ // TODO: Autobins can still be "configured" through [[bin]] in the manifest
102
+ // and should then NOT end up in the artifacts list twice!
103
+ // We should effectively merge those lists here.
104
+ for file in utils:: list_rust_files ( & root_dir. join ( "src" ) . join ( "bin" ) ) ? {
105
+ artifacts. push ( Artifact :: Root ( file) ) ;
106
+ }
107
+ if root_dir. join ( "src/main.rs" ) . is_file ( ) {
108
+ artifacts. push ( Artifact :: Root ( package. clone ( ) ) ) ;
109
+ }
110
+ }
111
+
112
+ // Add all explicitly configured binaries
113
+ for bin in parsed_manifest. bins {
114
+ artifacts. push ( Artifact :: Root ( bin. name . clone ( ) ) ) ;
91
115
}
92
116
} else {
117
+ // TODO: Use info from the above if body to validate that these bins exist?
93
118
for bin in & args. bin {
94
- artifacts. push ( Artifact :: Root ( bin. into ( ) ) ) ;
119
+ artifacts. push ( Artifact :: Root ( bin. clone ( ) ) ) ;
95
120
}
96
121
}
97
- if artifacts. is_empty ( ) {
98
- artifacts. push ( Artifact :: Root ( package. clone ( ) ) ) ;
122
+ if args. lib || !specific_target_selected {
123
+ // Add the one and only library target, if it exists
124
+ let name = parsed_manifest
125
+ . lib
126
+ . and_then ( |lib| lib. name )
127
+ . unwrap_or_else ( || package. clone ( ) ) ;
128
+ artifacts. push ( Artifact :: Root ( name) ) ;
99
129
}
100
130
let host_triple = Command :: new ( "rustc" )
101
131
. arg ( "-vV" )
@@ -178,6 +208,8 @@ impl Subcommand {
178
208
) -> PathBuf {
179
209
let triple = target. unwrap_or_else ( || self . host_triple ( ) ) ;
180
210
let file_name = artifact. file_name ( crate_type, triple) ;
181
- self . build_dir ( target) . join ( artifact) . join ( file_name)
211
+ self . build_dir ( target)
212
+ . join ( artifact. build_dir ( ) )
213
+ . join ( file_name)
182
214
}
183
215
}
0 commit comments