Skip to content

Commit af46999

Browse files
Hitbearemilio
Hitbear
authored andcommitted
Added support for integrating the package_version information in a comment of the header file.
The new command line argument for this is --package-version. The TOML config file flag is package_version=true||false. Closes mozilla#926 Co-Authored-By: Emilio Cobos Álvarez <[email protected]>
1 parent 400f437 commit af46999

22 files changed

+236
-7
lines changed

rust-toolchain.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[toolchain]
2-
channel = "nightly"
2+
channel = "nightly"

src/bindgen/bindings.rs

+34-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Bindings {
3333
/// Bindings are generated by a recursive call to cbindgen
3434
/// and shouldn't do anything when written anywhere.
3535
noop: bool,
36+
package_version: String,
3637
}
3738

3839
#[derive(PartialEq, Eq)]
@@ -53,6 +54,7 @@ impl Bindings {
5354
functions: Vec<Function>,
5455
source_files: Vec<path::PathBuf>,
5556
noop: bool,
57+
package_version: String,
5658
) -> Bindings {
5759
Bindings {
5860
config,
@@ -65,6 +67,7 @@ impl Bindings {
6567
functions,
6668
source_files,
6769
noop,
70+
package_version,
6871
}
6972
}
7073

@@ -209,6 +212,20 @@ impl Bindings {
209212
return;
210213
}
211214

215+
if self.config.package_version {
216+
out.new_line_if_not_start();
217+
match self.config.language {
218+
Language::C | Language::Cxx => {
219+
write!(out, "/* Package version: {} */", self.package_version);
220+
}
221+
Language::Cython => {
222+
write!(out, "''' Package version: {} '''", self.package_version);
223+
}
224+
}
225+
226+
out.new_line();
227+
}
228+
212229
if let Some(ref f) = self.config.header {
213230
out.new_line_if_not_start();
214231
write!(out, "{}", f);
@@ -228,11 +245,23 @@ impl Bindings {
228245
}
229246
if self.config.include_version {
230247
out.new_line_if_not_start();
231-
write!(
232-
out,
233-
"/* Generated with cbindgen:{} */",
234-
crate::bindgen::config::VERSION
235-
);
248+
match self.config.language {
249+
Language::C | Language::Cxx => {
250+
write!(
251+
out,
252+
"/* Generated with cbindgen:{} */",
253+
crate::bindgen::config::VERSION
254+
);
255+
}
256+
Language::Cython => {
257+
write!(
258+
out,
259+
"''' Generated with cbindgen:{} '''",
260+
crate::bindgen::config::VERSION
261+
);
262+
}
263+
}
264+
236265
out.new_line();
237266
}
238267
if let Some(ref f) = self.config.autogen_warning {

src/bindgen/builder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl Builder {
361361
Default::default(),
362362
Default::default(),
363363
true,
364+
String::new(),
364365
));
365366
}
366367

@@ -405,6 +406,7 @@ impl Builder {
405406
result.typedefs,
406407
result.functions,
407408
result.source_files,
409+
result.package_version,
408410
)
409411
.generate()
410412
}

src/bindgen/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,8 @@ pub struct Config {
919919
/// This option is useful when using cbindgen with tools such as python's cffi which
920920
/// doesn't understand include directives
921921
pub no_includes: bool,
922+
// Package version: True if the package version should appear as a comment in the .h file
923+
pub package_version: bool,
922924
/// Optional text to output at major sections to deter manual editing
923925
pub autogen_warning: Option<String>,
924926
/// Include a comment with the version of cbindgen used to generate the file
@@ -1040,6 +1042,7 @@ impl Default for Config {
10401042
autogen_warning: None,
10411043
include_version: false,
10421044
no_includes: false,
1045+
package_version: false,
10431046
namespace: None,
10441047
namespaces: None,
10451048
using_namespaces: None,

src/bindgen/ir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::collections::HashMap;
77
use std::io::Write;
88

99
use syn::ext::IdentExt;
10-
use syn::{self, UnOp};
10+
use syn::UnOp;
1111

1212
use crate::bindgen::config::{Config, Language};
1313
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;

src/bindgen/library.rs

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct Library {
2727
typedefs: ItemMap<Typedef>,
2828
functions: Vec<Function>,
2929
source_files: Vec<PathBuf>,
30+
package_version: String,
3031
}
3132

3233
impl Library {
@@ -42,6 +43,7 @@ impl Library {
4243
typedefs: ItemMap<Typedef>,
4344
functions: Vec<Function>,
4445
source_files: Vec<PathBuf>,
46+
package_version: String,
4547
) -> Library {
4648
Library {
4749
config,
@@ -54,6 +56,7 @@ impl Library {
5456
typedefs,
5557
functions,
5658
source_files,
59+
package_version,
5760
}
5861
}
5962

@@ -141,6 +144,7 @@ impl Library {
141144
functions,
142145
self.source_files,
143146
false,
147+
self.package_version,
144148
))
145149
}
146150

src/bindgen/parser.rs

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ pub(crate) fn parse_lib(lib: Cargo, config: &Config) -> ParseResult {
8181
let binding_crate = context.lib.as_ref().unwrap().binding_crate_ref();
8282
context.parse_crate(&binding_crate)?;
8383
context.out.source_files = context.cache_src.keys().map(|k| k.to_owned()).collect();
84+
context.out.package_version = context
85+
.lib
86+
.as_ref()
87+
.unwrap()
88+
.binding_crate_ref()
89+
.version
90+
.unwrap();
8491
Ok(context.out)
8592
}
8693

@@ -409,6 +416,7 @@ pub struct Parse {
409416
pub typedefs: ItemMap<Typedef>,
410417
pub functions: Vec<Function>,
411418
pub source_files: Vec<FilePathBuf>,
419+
pub package_version: String,
412420
}
413421

414422
impl Parse {
@@ -423,6 +431,7 @@ impl Parse {
423431
typedefs: ItemMap::default(),
424432
functions: Vec::new(),
425433
source_files: Vec::new(),
434+
package_version: String::new(),
426435
}
427436
}
428437

@@ -471,6 +480,7 @@ impl Parse {
471480
self.typedefs.extend_with(&other.typedefs);
472481
self.functions.extend_from_slice(&other.functions);
473482
self.source_files.extend_from_slice(&other.source_files);
483+
self.package_version = other.package_version.clone();
474484
}
475485

476486
fn load_syn_crate_mod<'a>(

src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ fn apply_config_overrides(config: &mut Config, matches: &ArgMatches) {
4949
config.only_target_dependencies = true;
5050
}
5151

52+
if matches.get_flag("package-version") {
53+
config.package_version = true;
54+
}
55+
5256
match matches.try_get_one::<String>("style") {
5357
Ok(Some(style)) => {
5458
config.style = bindgen::Style::from_str(style).unwrap();
@@ -163,6 +167,12 @@ fn main() {
163167
.help("Specify the language to output bindings in")
164168
.value_parser(["c++", "C++", "c", "C", "cython", "Cython"]),
165169
)
170+
.arg(
171+
Arg::new("package-version")
172+
.long("package-version")
173+
.action(ArgAction::SetTrue)
174+
.help("Include the package version in the header comment")
175+
)
166176
.arg(
167177
Arg::new("cpp-compat")
168178
.long("cpp-compat")

tests/expectations/package_version.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
typedef struct {
9+
uint64_t bar;
10+
} Foo;
11+
12+
void doit(const Foo*);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
typedef struct {
9+
uint64_t bar;
10+
} Foo;
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif // __cplusplus
15+
16+
void doit(const Foo*);
17+
18+
#ifdef __cplusplus
19+
} // extern "C"
20+
#endif // __cplusplus
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <cstdarg>
4+
#include <cstdint>
5+
#include <cstdlib>
6+
#include <ostream>
7+
#include <new>
8+
9+
struct Foo {
10+
uint64_t bar;
11+
};
12+
13+
extern "C" {
14+
15+
void doit(const Foo*);
16+
17+
} // extern "C"
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
''' Package version: 0.1.0 '''
2+
3+
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
4+
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
5+
cdef extern from *:
6+
ctypedef bint bool
7+
ctypedef struct va_list
8+
9+
cdef extern from *:
10+
11+
ctypedef struct Foo:
12+
uint64_t bar;
13+
14+
void doit(const Foo*);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
typedef struct Foo {
9+
uint64_t bar;
10+
} Foo;
11+
12+
void doit(const struct Foo*);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
typedef struct Foo {
9+
uint64_t bar;
10+
} Foo;
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif // __cplusplus
15+
16+
void doit(const struct Foo*);
17+
18+
#ifdef __cplusplus
19+
} // extern "C"
20+
#endif // __cplusplus
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
struct Foo {
9+
uint64_t bar;
10+
};
11+
12+
void doit(const struct Foo*);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Package version: 0.1.0 */
2+
3+
#include <stdarg.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
struct Foo {
9+
uint64_t bar;
10+
};
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif // __cplusplus
15+
16+
void doit(const struct Foo*);
17+
18+
#ifdef __cplusplus
19+
} // extern "C"
20+
#endif // __cplusplus
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
''' Package version: 0.1.0 '''
2+
3+
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
4+
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
5+
cdef extern from *:
6+
ctypedef bint bool
7+
ctypedef struct va_list
8+
9+
cdef extern from *:
10+
11+
cdef struct Foo:
12+
uint64_t bar;
13+
14+
void doit(const Foo*);

tests/rust/package_version/Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rust/package_version/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "package_version"
3+
version = "0.1.0"
4+
authors = ["hitbear"]
5+
6+
[features]
7+
cbindgen = []
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package_version = true

tests/rust/package_version/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[repr(C)]
2+
pub struct Foo {
3+
bar: u64,
4+
}
5+
6+
#[no_mangle]
7+
pub extern "C" fn doit(_: &Foo) {}

0 commit comments

Comments
 (0)