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

Generate gir without link attribute #1508

Merged
merged 8 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gdk-pixbuf/src/auto/versions.txt
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)
1 change: 0 additions & 1 deletion gdk-pixbuf/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ impl ::std::fmt::Debug for GdkPixbufSimpleAnimIter {
}
}

#[link(name = "gdk_pixbuf-2.0")]
extern "C" {

//=========================================================================
Expand Down
2 changes: 1 addition & 1 deletion gdk-pixbuf/sys/versions.txt
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 changes: 1 addition & 1 deletion gio/src/auto/versions.txt
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)
1 change: 0 additions & 1 deletion gio/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9363,7 +9363,6 @@ impl ::std::fmt::Debug for GVolume {
}
}

#[link(name = "gio-2.0")]
extern "C" {

//=========================================================================
Expand Down
1 change: 0 additions & 1 deletion gio/sys/src/manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ mod windows_streams {
gboolean, GInputStream, GInputStreamClass, GOutputStream, GOutputStreamClass, GType,
};

#[link(name = "gio-2.0")]
extern "C" {
//=========================================================================
// GWin32InputStream
Expand Down
2 changes: 1 addition & 1 deletion gio/sys/versions.txt
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 changes: 1 addition & 1 deletion gir
1 change: 1 addition & 0 deletions glib/gobject-sys/Cargo.toml
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"
Expand Down
4 changes: 4 additions & 0 deletions glib/gobject-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

mod build_manual;

#[cfg(not(docsrs))]
use std::process;

Expand All @@ -14,4 +16,6 @@ fn main() {
println!("cargo:warning={s}");
process::exit(1);
}

build_manual::main();
}
33 changes: 33 additions & 0 deletions glib/gobject-sys/build_manual.rs
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
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Contributor Author

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 sets foreign_modules and dll_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.

// (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();
}
5 changes: 4 additions & 1 deletion glib/gobject-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

use glib_sys as glib;

mod manual;

pub use manual::*;

#[allow(unused_imports)]
use libc::{
c_char, c_double, c_float, c_int, c_long, c_short, c_uchar, c_uint, c_ulong, c_ushort, c_void,
Expand Down Expand Up @@ -1415,7 +1419,6 @@ impl ::std::fmt::Debug for GTypePlugin {
}
}

#[link(name = "gobject-2.0")]
extern "C" {

//=========================================================================
Expand Down
10 changes: 10 additions & 0 deletions glib/gobject-sys/src/manual.rs
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"));

Check failure on line 4 in glib/gobject-sys/src/manual.rs

View workflow job for this annotation

GitHub Actions / build

couldn't read /__w/gtk-rs-core/gtk-rs-core/target/debug/build/gobject-sys-0443df896c9d79c7/out/param_spec.rs: No such file or directory (os error 2)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

#[cfg_attr(target_os = "windows", link(name = env!("...")))]
extern "C" {
    pub static g_param_spec_types: *const GType;
}


/// # 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)
}
2 changes: 1 addition & 1 deletion glib/gobject-sys/versions.txt
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 changes: 1 addition & 1 deletion glib/src/auto/versions.txt
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)
8 changes: 1 addition & 7 deletions glib/src/param_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,13 @@ pub unsafe trait ParamSpecType:
{
}

#[link(name = "gobject-2.0")]
extern "C" {
pub static g_param_spec_types: *const ffi::GType;
}

macro_rules! define_param_spec {
($rust_type:ident, $ffi_type:path, $rust_type_offset:expr) => {
// Can't use get_type here as this is not a boxed type but another fundamental type
impl StaticType for $rust_type {
#[inline]
fn static_type() -> Type {
unsafe {
from_glib(*g_param_spec_types.add($rust_type_offset))
from_glib(gobject_ffi::g_param_spec_types_get_type($rust_type_offset))
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions glib/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2668,8 +2668,6 @@ impl ::std::fmt::Debug for GVariantType {
}
}

#[link(name = "gobject-2.0")]
#[link(name = "glib-2.0")]
extern "C" {

//=========================================================================
Expand Down
1 change: 0 additions & 1 deletion glib/sys/src/manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ mod win32 {
pub const G_WIN32_OS_WORKSTATION: GWin32OSType = 1;
pub const G_WIN32_OS_SERVER: GWin32OSType = 2;

#[link(name = "glib-2.0")]
extern "C" {
pub fn g_win32_check_windows_version(
major: c_int,
Expand Down
2 changes: 1 addition & 1 deletion glib/sys/versions.txt
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 changes: 1 addition & 1 deletion graphene/src/auto/versions.txt
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)
1 change: 0 additions & 1 deletion graphene/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ impl ::std::fmt::Debug for graphene_vec4_t {
}
}

#[link(name = "graphene-1.0")]
extern "C" {

//=========================================================================
Expand Down
2 changes: 1 addition & 1 deletion graphene/sys/versions.txt
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 changes: 1 addition & 1 deletion pango/src/auto/versions.txt
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)
1 change: 0 additions & 1 deletion pango/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,6 @@ impl ::std::fmt::Debug for PangoRenderer {
}
}

#[link(name = "pango-1.0")]
extern "C" {

//=========================================================================
Expand Down
2 changes: 1 addition & 1 deletion pango/sys/versions.txt
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 changes: 1 addition & 1 deletion pangocairo/src/auto/versions.txt
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)
1 change: 0 additions & 1 deletion pangocairo/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ impl ::std::fmt::Debug for PangoCairoFontMap {
}
}

#[link(name = "pangocairo-1.0")]
extern "C" {

//=========================================================================
Expand Down
2 changes: 1 addition & 1 deletion pangocairo/sys/versions.txt
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)
Loading