-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Generate gir without link attribute #1508
Changes from 3 commits
e352a21
5691ef9
06abb15
18becdb
df58d90
d77be04
3abed00
5a0aa5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
+2 −2 | Cargo.lock | |
+10 −18 | generator.py | |
+49 −3 | src/codegen/sys/build.rs | |
+0 −13 | src/codegen/sys/lib_.rs | |
+0 −29 | src/nameutil.rs |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[build-dependencies] | ||
system-deps = "7" | ||
pkg-config = "0.3" | ||
|
||
[dependencies] | ||
libc = "0.2" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[cfg(not(docsrs))] | ||
use std::{env, fs::File, io::Write, path::Path}; | ||
|
||
#[cfg(not(docsrs))] | ||
pub fn main() { | ||
let deps = system_deps::Config::new() | ||
.probe() | ||
.expect("if this module is called, this is valid"); | ||
|
||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
let out_path = Path::new(&out_dir).join("param_spec.rs"); | ||
|
||
// Generating a link attribute is necessary in windows | ||
// (see https://rust-lang.github.io/rfcs/1717-dllimport.html#drawbacks) | ||
// even if we are already linking with system_deps. | ||
// We allow configuration of the library name from upstream dependents | ||
// using the alias variable in the pkgconfig file. | ||
let lib_name = deps | ||
.iter() | ||
.into_iter() | ||
.filter_map(|(_, l)| pkg_config::get_variable(&l.name, "alias").ok()) | ||
.find(|s| !s.is_empty()) | ||
.unwrap_or("gobject-2.0".into()); | ||
let link = if cfg!(target_os = "windows") { | ||
format!("#[link(name = \"{}\")]", lib_name) | ||
} else { | ||
"".into() | ||
}; | ||
|
||
let code = format!("{link} extern \"C\" {{ pub static g_param_spec_types: *const GType; }}"); | ||
let mut f = File::create(&out_path).unwrap(); | ||
f.write_all(code.as_bytes()).unwrap(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use glib_sys::GType; | ||
|
||
// `g_param_spec_types` extern binding generated by build.rs | ||
include!(concat!(env!("OUT_DIR"), "/param_spec.rs")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great if we could avoid such things because that makes the crate unusable with meson's cargo subproject support for the time being. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree, I'm not too happy with the current solution either. Ideally we could skip the code generation part and have the attribute read the link name from an env variable set by the build.rs, or something similar. However, it seems that derive attributes can't read constants yet.
|
||
|
||
/// # Safety | ||
/// This should be safe as long as the offset added to g_param_spec_types is in bounds. | ||
pub unsafe fn g_param_spec_types_get_type(offset: usize) -> GType { | ||
*g_param_spec_types.add(offset) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Generated by gir (https://github.com/gtk-rs/gir @ ae08ff966747) | ||
Generated by gir (https://github.com/gtk-rs/gir @ f0a4546b0a06) | ||
from gir-files (https://github.com/gtk-rs/gir-files @ 4d1189172a70) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check if e.g. gtk4-rs or gstreamer-rs are also accessing some library constant? That would have the same issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tested gstreamer-rs and I just checked gtk4-rs, and the only use seems to be on glib. At least on github no other crates seem to use this specific constant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not talking about this specific constant, but generally constants from shared libraries :) I thought in gstreamer-rs I used some but I can't remember exactly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do use constants, but they are generated by gir and are all defined as
pub const ...
. The thing with this one is that it is not added to gir files and that it is defined but not initialized in the header https://gitlab.gnome.org/GNOME/glib/-/blob/main/gobject/gparamspecs.h#L1147. So maybe that has to do with it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I think I found the difference between the
#[link]
attribute and adding them on the command line. Here is the code that generates libraries with the attribute (https://github.com/rust-lang/rust/blob/170d6cb845c8c3f0dcec5cdd4210df9ecf990244/compiler/rustc_metadata/src/native_libs.rs#L452), which setsforeign_modules
anddll_imports
. If we rename the libraries when they have the link attribute already set, they use the existing metadata and it works. However, when adding a library that is not mentioned in#[link]
, the dll imports is not set (https://github.com/rust-lang/rust/blob/170d6cb845c8c3f0dcec5cdd4210df9ecf990244/compiler/rustc_metadata/src/native_libs.rs#L541).I'm not sure how feasible it is to modify the behaviour of the linking there, because it needs access to the foreign items in the module to call dllimport, and it is doing that with the macro.