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

0.20 backports #1603

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 22 additions & 22 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions book/src/tutorial/high_level_rust_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ v1_52 = ["ffi/v1_52", "v1_50"]
The lib.rs file will not be replaced when you run gir.
All the code that gir will generate for us is going to be in src/auto.
We need to include all `auto` files in our library.
Also it needs to include sys create so auto files can use it.
To do so, let's update the `src/lib.rs` file as follows:

```rust
#![cfg_attr(docsrs, feature(doc_cfg))]

use ffi;
pub use auto::*;
mod auto;
```
Expand Down
28 changes: 10 additions & 18 deletions generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files")
DEFAULT_GIR_DIRECTORY = Path("./gir/")
DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / "target/release/gir"


def run_command(command, folder=None):
Expand All @@ -30,8 +29,13 @@ async def spawn_process(exe, args):
return stdout, stderr


async def spawn_gir(gir_exe, args):
stdout, stderr = await spawn_process(gir_exe, args)
async def spawn_gir(conf, args):
if conf.gir_path:
stdout, stderr = await spawn_process(conf.gir_path, args)
else:
cargo_args = ["run", "--release", "--quiet", "--manifest-path", DEFAULT_GIR_DIRECTORY / "Cargo.toml", "--"]
stdout, stderr = await spawn_process("cargo", cargo_args + args)

# Gir doesn't print anything to stdout. If it does, this is likely out of
# order with stderr, unless the printer/logging flushes in between.
assert not stdout, "`gir` printed unexpected stdout: {}".format(stdout)
Expand All @@ -40,10 +44,6 @@ async def spawn_gir(gir_exe, args):
return ""


def update_workspace():
return run_command(["cargo", "build", "--release"], "gir")


def ask_yes_no_question(question, conf):
question = "{} [y/N] ".format(question)
if conf.yes:
Expand Down Expand Up @@ -71,12 +71,6 @@ def update_submodule(submodule_path, conf):
return False


def build_gir():
print("=> Building gir...")
update_workspace()
print("<= Done!")


async def regenerate_crate_docs(conf, crate_dir, base_gir_args):
doc_path = "docs.md"
# Generate into docs.md instead of the default vendor.md
Expand All @@ -99,7 +93,7 @@ async def regenerate_crate_docs(conf, crate_dir, base_gir_args):
logs += "==> Regenerating documentation for `{}` into `{}`...\n".format(
crate_dir, doc_path
)
logs += await spawn_gir(conf.gir_path, doc_args)
logs += await spawn_gir(conf, doc_args)

logs += "==> Embedding documentation from `{}` into `{}`...\n".format(
doc_path, crate_dir
Expand Down Expand Up @@ -139,7 +133,7 @@ def regen_crates(path, conf):

async def regenerate_crate(path, args):
return "==> Regenerating `{}`...\n".format(path) + await spawn_gir(
conf.gir_path, args
conf, args
)

processes.append(regenerate_crate(path, args))
Expand Down Expand Up @@ -194,7 +188,6 @@ def parse_args():
)
parser.add_argument(
"--gir-path",
default=DEFAULT_GIR_PATH,
type=file_path,
help="Path of the gir executable to run",
)
Expand Down Expand Up @@ -228,9 +221,8 @@ async def main():
if not conf.gir_files_paths:
update_submodule(DEFAULT_GIR_FILES_DIRECTORY, conf)

if conf.gir_path == DEFAULT_GIR_PATH:
if conf.gir_path is None:
update_submodule(DEFAULT_GIR_DIRECTORY, conf)
build_gir()

print("=> Regenerating crates...")
for path in conf.path:
Expand Down
26 changes: 13 additions & 13 deletions src/analysis/ffi_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
Type::Basic(fund) => {
use crate::library::Basic::*;
let inner = match fund {
None => "libc::c_void",
None => "std::ffi::c_void",
Boolean => return Ok(use_glib_if_needed(env, "ffi::gboolean").into()),
Int8 => "i8",
UInt8 => "u8",
Expand All @@ -82,18 +82,18 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
UInt32 => "u32",
Int64 => "i64",
UInt64 => "u64",
Char => "libc::c_char",
UChar => "libc::c_uchar",
Short => "libc::c_short",
UShort => "libc::c_ushort",
Int => "libc::c_int",
UInt => "libc::c_uint",
Long => "libc::c_long",
ULong => "libc::c_ulong",
Char => "std::ffi::c_char",
UChar => "std::ffi::c_uchar",
Short => "std::ffi::c_short",
UShort => "std::ffi::c_ushort",
Int => "std::ffi::c_int",
UInt => "std::ffi::c_uint",
Long => "std::ffi::c_long",
ULong => "std::ffi::c_ulong",
Size => "libc::size_t",
SSize => "libc::ssize_t",
Float => "libc::c_float",
Double => "libc::c_double",
Float => "std::ffi::c_float",
Double => "std::ffi::c_double",
TimeT => "libc::time_t",
OffT => "libc::off_t",
DevT => "libc::dev_t",
Expand All @@ -102,8 +102,8 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
SockLenT => "libc::socklen_t",
UidT => "libc::uid_t",
UniChar => "u32",
Utf8 => "libc::c_char",
Filename => "libc::c_char",
Utf8 => "std::ffi::c_char",
Filename => "std::ffi::c_char",
Type => return Ok(use_glib_if_needed(env, "ffi::GType").into()),
IntPtr => "libc::intptr_t",
UIntPtr => "libc::uintptr_t",
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/sys/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ fn fill_empty(root: &mut Table, env: &Env, crate_name: &str) {
fn fill_in(root: &mut Table, env: &Env) {
{
let package = upsert_table(root, "package");
set_string(package, "build", "build.rs");
package
.entry("build")
.or_insert_with(|| Value::String("build.rs".into()));
// set_string(package, "version", "0.2.0");
}

Expand Down
13 changes: 0 additions & 13 deletions src/codegen/sys/lib_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ pub fn generate(env: &Env) {
save_to_file(&path, env.config.make_backup, |w| generate_lib(w, env));
}

fn write_link_attr(w: &mut dyn Write, shared_libs: &[String]) -> Result<()> {
for it in shared_libs {
writeln!(
w,
"#[link(name = \"{}\")]",
shared_lib_name_to_link_name(it)
)?;
}

Ok(())
}

fn generate_lib(w: &mut dyn Write, env: &Env) -> Result<()> {
general::start_comments(w, &env.config)?;
statics::begin(w)?;
Expand Down Expand Up @@ -89,7 +77,6 @@ fn generate_lib(w: &mut dyn Write, env: &Env) -> Result<()> {
}

if !env.namespaces.main().shared_libs.is_empty() {
write_link_attr(w, &env.namespaces.main().shared_libs)?;
writeln!(w, "extern \"C\" {{")?;
functions::generate_enums_funcs(w, env, &enums)?;
functions::generate_bitfields_funcs(w, env, &bitfields)?;
Expand Down
7 changes: 4 additions & 3 deletions src/codegen/sys/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ pub fn after_extern_crates(w: &mut dyn Write) -> Result<()> {
let v = vec![
"",
"#[allow(unused_imports)]",
"use libc::{c_int, c_char, c_uchar, c_float, c_uint, c_double,",
" c_short, c_ushort, c_long, c_ulong,",
" c_void, size_t, ssize_t, time_t, off_t, intptr_t, uintptr_t, FILE};",
"use std::ffi::{c_int, c_char, c_uchar, c_float, c_uint, c_double,",
" c_short, c_ushort, c_long, c_ulong, c_void};",
"#[allow(unused_imports)]",
"use libc::{size_t, ssize_t, time_t, off_t, intptr_t, uintptr_t, FILE};",
"#[cfg(unix)]",
"#[allow(unused_imports)]",
"use libc::{dev_t, gid_t, pid_t, socklen_t, uid_t};",
Expand Down
29 changes: 0 additions & 29 deletions src/nameutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
pub(crate) fn set_crate_name_overrides(overrides: HashMap<String, String>) {
unsafe {
assert!(
CRATE_NAME_OVERRIDES.is_none(),

Check warning on line 10 in src/nameutil.rs

View workflow job for this annotation

GitHub Actions / Build/Test (ubuntu-latest, nightly)

creating a shared reference to mutable static is discouraged

Check warning on line 10 in src/nameutil.rs

View workflow job for this annotation

GitHub Actions / Build/Test (ubuntu-latest, nightly)

creating a shared reference to mutable static is discouraged
"Crate name overrides already set"
);
CRATE_NAME_OVERRIDES = Some(overrides);
Expand Down Expand Up @@ -129,25 +129,6 @@
name.to_string().replace(['-', '.'], "_")
}

pub fn shared_lib_name_to_link_name(name: &str) -> &str {
let mut s = name;

if s.starts_with("lib") {
s = &s[3..];
}

if let Some(offset) = s.rfind(".so") {
s = &s[..offset];
} else if let Some(offset) = s.rfind(".dll") {
s = &s[..offset];
if let Some(offset) = s.rfind('-') {
s = &s[..offset];
}
}

s
}

pub fn use_glib_type(env: &crate::env::Env, import: &str) -> String {
format!(
"{}::{}",
Expand Down Expand Up @@ -253,14 +234,4 @@
fn lib_name_to_toml_works() {
assert_eq!(lib_name_to_toml("gstreamer-1.0"), "gstreamer_1_0");
}

#[test]
fn shared_lib_name_to_link_name_works() {
assert_eq!(shared_lib_name_to_link_name("libgtk-4-1.dll"), "gtk-4");
assert_eq!(shared_lib_name_to_link_name("libatk-1.0.so.0"), "atk-1.0");
assert_eq!(
shared_lib_name_to_link_name("libgdk_pixbuf-2.0.so.0"),
"gdk_pixbuf-2.0"
);
}
}
Loading