Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to generate a list of symbols for dynamic linkage #916

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ impl Bindings {
fields
}

/// Lists the exported symbols that can be dynamically linked, i.e. globals and functions.
pub fn dynamic_symbols_names(&self) -> Vec<String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably:

fn dynamic_symbols_names(&self) -> impl Iterator<Item = &str>

And just chain the two things:

self.functions.iter().map(|f| f.path().name())
    .chain(self.globals.iter().map(|g| g.export_name()))

use crate::bindgen::ir::Item;

let function_names = self.functions.iter().map(|f| f.path().name().to_string());
let global_names = self.globals.iter().map(|g| g.export_name().to_string());

let mut res = Vec::new();
res.extend(function_names);
res.extend(global_names);
res
}

pub fn generate_symfile<P: AsRef<path::Path>>(&self, symfile_path: P) {
let symbols = self.dynamic_symbols_names();
if let Some(dir) = symfile_path.as_ref().parent() {
std::fs::create_dir_all(dir).unwrap();
}
let mut symfile = File::create(symfile_path).unwrap();
write!(&mut symfile, "{{\n{};\n}};\n", symbols.join(";\n")).expect("Writing symbol file failed");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably write this iteratively using the above function rather than storing all the file contents in memory, but it's probably not a huge deal.

}

pub fn generate_depfile<P: AsRef<path::Path>>(&self, header_path: P, depfile_path: P) {
if let Some(dir) = depfile_path.as_ref().parent() {
if !dir.exists() {
Expand Down
16 changes: 15 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ fn main() {
This option is ignored if `--out` is missing."
)
)
.arg(
Arg::new("symfile")
.value_name("PATH")
.long("symfile")
.num_args(1)
.required(false)
.help("Generate a list of symbols at the given Path. This list can be \
given to a linker in order to compile an application that exposes \
dynamic symbols. Useful when creating a plugin system with a C interface."
)
)
.get_matches();

if matches.get_flag("verify") && !matches.contains_id("out") {
Expand Down Expand Up @@ -343,7 +354,10 @@ fn main() {
std::process::exit(2);
}
if let Some(depfile) = matches.get_one("depfile") {
bindings.generate_depfile(file, depfile)
bindings.generate_depfile(file, depfile);
}
if let Some(symfile) = matches.get_one::<String>("symfile") {
bindings.generate_symfile(symfile);
}
}
_ => {
Expand Down
4 changes: 4 additions & 0 deletions tests/expectations-symbols/abi_string.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
c;
c_unwind;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/alias.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/annotation.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/array.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/asserted_cast.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
foo;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/assoc_constant.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/associated_in_body.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/bitfield.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/bitflags.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/body.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
5 changes: 5 additions & 0 deletions tests/expectations-symbols/box.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
root;
drop_box;
drop_box_opt;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/cdecl.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
O;
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/cell.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
5 changes: 5 additions & 0 deletions tests/expectations-symbols/cfg.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
root;
root;
cond;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/cfg_2.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/char.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/const_generics.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/const_generics_arrayvec.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
push;
};
6 changes: 6 additions & 0 deletions tests/expectations-symbols/const_generics_bool.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
new_set;
set_for_each;
new_map;
map_for_each;
};
5 changes: 5 additions & 0 deletions tests/expectations-symbols/const_generics_byte.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
init_parens_parser;
destroy_parens_parser;
init_braces_parser;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/const_generics_char.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
until_nul;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/const_generics_constant.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/const_generics_thru.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
one;
two;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/constant.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/constant_sort_name.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
C;
D;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/constant_sort_none.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
D;
C;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/custom_header.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/decl_name_conflicting.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/dep_v2.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
get_x;
};
8 changes: 8 additions & 0 deletions tests/expectations-symbols/deprecated.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
deprecated_without_note;
deprecated_without_bracket;
deprecated_with_note;
deprecated_with_note_and_since;
deprecated_with_note_which_requires_to_be_escaped;
dummy;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/derive_eq.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/derive_ostream.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/destructor_and_copy_ctor.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/display_list.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
push_item;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/doclength_short.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
root;
trunk;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/docstyle_auto.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/docstyle_c99.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/docstyle_doxy.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/documentation.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/documentation_attr.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/enum.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/enum_discriminant.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/enum_self.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/euclid.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/exclude_generic_monomorph.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/expand.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/expand_default_features.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
extra_debug_fn;
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/expand_dep.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
get_x;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/expand_dep_v2.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
get_x;
};
5 changes: 5 additions & 0 deletions tests/expectations-symbols/expand_features.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
extra_debug_fn;
cbindgen;
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/expand_no_default_features.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/export_name.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
do_the_thing_with_export_name;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/extern.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
foo;
bar;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/extern_2.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
first;
second;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/external_workspace_child.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
consume_ext;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/fns.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
root;
no_return;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/forward_declaration.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
5 changes: 5 additions & 0 deletions tests/expectations-symbols/function_args.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
unnamed;
pointer_test;
print_from_rust;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/function_noreturn.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
loop_forever;
normal_return;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/function_ptr.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
my_function;
};
6 changes: 6 additions & 0 deletions tests/expectations-symbols/function_sort_name.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
A;
B;
C;
D;
};
6 changes: 6 additions & 0 deletions tests/expectations-symbols/function_sort_none.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
C;
B;
D;
A;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/generic_pointer.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/global_variable.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
MUT_GLOBAL_ARRAY;
CONST_GLOBAL_ARRAY;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/ignore.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
no_ignore_root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/include_guard.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/inner_mod.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/item_types.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/item_types_renamed.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/lifetime_arg.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/linestyle_cr.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/linestyle_crlf.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/linestyle_lf.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/mangle.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
4 changes: 4 additions & 0 deletions tests/expectations-symbols/manuallydrop.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
root;
take;
};
3 changes: 3 additions & 0 deletions tests/expectations-symbols/maybeuninit.c.sym
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};