10
10
//!
11
11
//! The purpose of this crate is to provide the utility functions necessary to
12
12
//! compile C code into a static archive which is then linked into a Rust crate.
13
- //! The top-level `compile_library` function serves as a convenience and more
14
- //! advanced configuration is available through the `Config` builder.
13
+ //! Configuration is available through the `Config` builder.
15
14
//!
16
15
//! This crate will automatically detect situations such as cross compilation or
17
16
//! other environment variables set by Cargo and will build code appropriately.
18
17
//!
19
- //! # Examples
20
- //!
21
- //! Use the default configuration:
18
+ //! [`Config`]: struct.Config.html
22
19
//!
23
- //! ```no_run
24
- //! extern crate gcc;
25
- //!
26
- //! fn main() {
27
- //! gcc::compile_library("libfoo.a", &["src/foo.c"]);
28
- //! }
29
- //! ```
20
+ //! # Examples
30
21
//!
31
- //! Use more advanced configuration :
22
+ //! Use the `Config` builder to compile `src/foo.c` :
32
23
//!
33
24
//! ```no_run
34
25
//! extern crate gcc;
38
29
//! .file("src/foo.c")
39
30
//! .define("FOO", Some("bar"))
40
31
//! .include("src")
41
- //! .compile("libfoo.a ");
32
+ //! .compile("foo ");
42
33
//! }
43
34
//! ```
44
35
@@ -168,8 +159,10 @@ impl ToolFamily {
168
159
/// # Example
169
160
///
170
161
/// ```no_run
171
- /// gcc::compile_library("libfoo.a ", &["foo.c", "bar.c"]);
162
+ /// gcc::compile_library("foo ", &["foo.c", "bar.c"]);
172
163
/// ```
164
+ #[ deprecated]
165
+ #[ doc( hidden) ]
173
166
pub fn compile_library ( output : & str , files : & [ & str ] ) {
174
167
let mut c = Config :: new ( ) ;
175
168
for f in files. iter ( ) {
@@ -181,7 +174,9 @@ pub fn compile_library(output: &str, files: &[&str]) {
181
174
impl Config {
182
175
/// Construct a new instance of a blank set of configuration.
183
176
///
184
- /// This builder is finished with the `compile` function.
177
+ /// This builder is finished with the [`compile`] function.
178
+ ///
179
+ /// [`compile`]: struct.Config.html#method.compile
185
180
pub fn new ( ) -> Config {
186
181
Config {
187
182
include_directories : Vec :: new ( ) ,
@@ -221,7 +216,7 @@ impl Config {
221
216
/// .file("src/foo.c")
222
217
/// .include(library_path)
223
218
/// .include("src")
224
- /// .compile("libfoo.a ");
219
+ /// .compile("foo ");
225
220
/// ```
226
221
pub fn include < P : AsRef < Path > > ( & mut self , dir : P ) -> & mut Config {
227
222
self . include_directories . push ( dir. as_ref ( ) . to_path_buf ( ) ) ;
@@ -237,7 +232,7 @@ impl Config {
237
232
/// .file("src/foo.c")
238
233
/// .define("FOO", Some("BAR"))
239
234
/// .define("BAZ", None)
240
- /// .compile("libfoo.a ");
235
+ /// .compile("foo ");
241
236
/// ```
242
237
pub fn define ( & mut self , var : & str , val : Option < & str > ) -> & mut Config {
243
238
self . definitions . push ( ( var. to_string ( ) , val. map ( |s| s. to_string ( ) ) ) ) ;
@@ -258,7 +253,7 @@ impl Config {
258
253
/// gcc::Config::new()
259
254
/// .file("src/foo.c")
260
255
/// .flag("-ffunction-sections")
261
- /// .compile("libfoo.a ");
256
+ /// .compile("foo ");
262
257
/// ```
263
258
pub fn flag ( & mut self , flag : & str ) -> & mut Config {
264
259
self . flags . push ( flag. to_string ( ) ) ;
@@ -296,7 +291,7 @@ impl Config {
296
291
/// .file("src/foo.c")
297
292
/// .shared_flag(true)
298
293
/// .static_flag(true)
299
- /// .compile("libfoo.so ");
294
+ /// .compile("foo ");
300
295
/// ```
301
296
pub fn static_flag ( & mut self , static_flag : bool ) -> & mut Config {
302
297
self . static_flag = Some ( static_flag) ;
@@ -381,7 +376,7 @@ impl Config {
381
376
/// gcc::Config::new()
382
377
/// .file("src/foo.c")
383
378
/// .target("aarch64-linux-android")
384
- /// .compile("libfoo.so ");
379
+ /// .compile("foo ");
385
380
/// ```
386
381
pub fn target ( & mut self , target : & str ) -> & mut Config {
387
382
self . target = Some ( target. to_string ( ) ) ;
@@ -392,14 +387,14 @@ impl Config {
392
387
///
393
388
/// This option is automatically scraped from the `HOST` environment
394
389
/// variable by build scripts, so it's not required to call this function.
395
- ///
390
+ ///
396
391
/// # Example
397
392
///
398
393
/// ```no_run
399
394
/// gcc::Config::new()
400
395
/// .file("src/foo.c")
401
396
/// .host("arm-linux-gnueabihf")
402
- /// .compile("libfoo.so ");
397
+ /// .compile("foo ");
403
398
/// ```
404
399
pub fn host ( & mut self , host : & str ) -> & mut Config {
405
400
self . host = Some ( host. to_string ( ) ) ;
@@ -500,17 +495,20 @@ impl Config {
500
495
501
496
/// Run the compiler, generating the file `output`
502
497
///
503
- /// The name `output` must begin with `lib` and end with `.a`
498
+ /// The name `output` should be the name of the library. For backwards compatibility,
499
+ /// the `output` may start with `lib` and end with `.a`. The Rust compilier will create
500
+ /// the assembly with the lib prefix and .a extension. MSVC will create a file without prefix,
501
+ /// ending with `.lib`.
504
502
///
505
503
/// # Panics
506
504
///
507
505
/// Panics if `output` is not formatted correctly or if one of the underlying
508
506
/// compiler commands fails. It can also panic if it fails reading file names
509
507
/// or creating directories.
510
508
pub fn compile ( & self , output : & str ) {
511
- assert ! ( output. starts_with( "lib" ) ) ;
512
- assert ! ( output. ends_with( ".a" ) ) ;
513
- let lib_name = & output[ 3 ..output . len ( ) - 2 ] ;
509
+ let name_start = if output. starts_with ( "lib" ) { 3 } else { 0 } ;
510
+ let name_end = if output. ends_with ( ".a" ) { output . len ( ) - 2 } else { output . len ( ) } ;
511
+ let lib_name = & output[ name_start..name_end ] ;
514
512
let dst = self . get_out_dir ( ) ;
515
513
516
514
let mut objects = Vec :: new ( ) ;
0 commit comments