@@ -120,6 +120,80 @@ obj session(targ_cfg: @config,
120
120
fn filesearch ( ) -> filesearch:: filesearch { filesearch }
121
121
fn building_library ( ) -> bool { opts. crate_type == lib_crate }
122
122
}
123
+
124
+ fn building_library ( req_crate_type : crate_type , crate : @ast:: crate ) -> bool {
125
+ alt req_crate_type {
126
+ bin_crate. { false }
127
+ lib_crate. { true }
128
+ unknown_crate. {
129
+ alt front :: attr:: get_meta_item_value_str_by_name (
130
+ crate . node. attrs ,
131
+ "crate_type" ) {
132
+ option:: some ( "lib" ) { true }
133
+ _ { false }
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ #[ cfg( test) ]
140
+ mod test {
141
+ import syntax:: ast_util;
142
+
143
+ fn make_crate_type_attr ( t : str ) -> ast:: attribute {
144
+ ast_util:: respan ( ast_util:: dummy_sp ( ) , {
145
+ style: ast:: attr_outer,
146
+ value: ast_util:: respan ( ast_util:: dummy_sp ( ) ,
147
+ ast:: meta_name_value (
148
+ "crate_type" ,
149
+ ast_util:: respan ( ast_util:: dummy_sp ( ) ,
150
+ ast:: lit_str ( t) ) ) )
151
+ } )
152
+ }
153
+
154
+ fn make_crate ( with_bin : bool , with_lib : bool ) -> @ast:: crate {
155
+ let attrs = [ ] ;
156
+ if with_bin { attrs += [ make_crate_type_attr ( "bin" ) ] ; }
157
+ if with_lib { attrs += [ make_crate_type_attr ( "lib" ) ] ; }
158
+ @ast_util:: respan ( ast_util:: dummy_sp ( ) , {
159
+ directives: [ ] ,
160
+ module : { view_items : [ ] , items : [ ] } ,
161
+ attrs: attrs,
162
+ config: [ ]
163
+ } )
164
+ }
165
+
166
+ #[ test]
167
+ fn bin_crate_type_attr_results_in_bin_output ( ) {
168
+ let crate = make_crate ( true , false ) ;
169
+ assert ! building_library( unknown_crate, crate ) ;
170
+ }
171
+
172
+ #[ test]
173
+ fn lib_crate_type_attr_results_in_lib_output ( ) {
174
+ let crate = make_crate ( false , true ) ;
175
+ assert building_library ( unknown_crate, crate ) ;
176
+ }
177
+
178
+ #[ test]
179
+ fn bin_option_overrides_lib_crate_type ( ) {
180
+ let crate = make_crate ( false , true ) ;
181
+ assert ! building_library( bin_crate, crate ) ;
182
+ }
183
+
184
+ #[ test]
185
+ fn lib_option_overrides_bin_crate_type ( ) {
186
+ let crate = make_crate ( true , false ) ;
187
+ assert building_library ( lib_crate, crate ) ;
188
+ }
189
+
190
+ #[ test]
191
+ fn bin_crate_type_is_default ( ) {
192
+ let crate = make_crate ( false , false ) ;
193
+ assert ! building_library( unknown_crate, crate ) ;
194
+ }
195
+ }
196
+
123
197
// Local Variables:
124
198
// fill-column: 78;
125
199
// indent-tabs-mode: nil
0 commit comments