Debian Codegraph @

book/src/introduction.md

raw
1 # Introduction
2 [gir] is a tool to automatically generate safe wrappers for a C library with [GObject introspection](https://gi.readthedocs.io/en/latest/) information.
3 In order to use it you need the `.gir` file containing the introspection data for the library you want to create the bindings for, as well as the `.gir` files for all its dependencies.
4 Have a look at the tutorial if you don't know how to [find the .gir files](tutorial/finding_gir_files.md).
5 If your library does not provide a `.gir` file, unfortunately you cannot use [gir], but maybe you can try [rust-bindgen](https://github.com/rust-lang/rust-bindgen).
6
7 This book contains a tutorial on how to use [gir].
8 As an example we will create the bindings for [Pango](https://docs.gtk.org/Pango/).
9 In many cases you will be able to follow the same steps with your library.
10 If you are already familiar with [gir] and you just want to look up details about the configuration files, feel free to skip ahead to the documentation of the [configuration files](config/introduction.md).
11
12 ## General steps
13 [gir] tries to make it as simple as possible to generate a safe wrapper for your C library.
14 The process can be divided into four steps that correspond to the four operating modes gir has.
15
16 - Generating unsafe bindings:
17 In this step, the low-level FFI bindings are created from the supplied `*.gir` files.
18 These are essentially direct calls into the related C library and are unsafe.
19 The resulting crate is typically appended with -sys.
20 The operating mode is `sys`.
21
22 - Generating a safe wrapper:
23 Next, another crate for a layer on top of these unsafe (sys) bindings is created, which makes them safe for use in general Rust.
24 The operating mode is `normal`.
25
26 - Checking for missing types/methods:
27 The operating mode `not_bound` allows you to see the detected types/methods that will not be generated for whatever reasons.
28
29 - Adding documentation:
30 After the safe wrapper is created, gir can even generate the documentation for us.
31 Use the operating mode `doc` to do so.
32
33
34 ## Regenerating the bindings and wrapper
35 In order to generate the bindings and the wrapper for the first time, the above-mentioned steps should be followed.
36 When you want to regenerate the crates because e.g.
37 the library was updated, you can simplify the process by running the helper script `./generator.py`.
38 The script detects `Gir.toml` configurations in the current directory and subdirectories (or the paths passed on the command-line) and generates "normal" or "sys" crates for it.
39 Alternatively `--embed-docs` can be passed to prepare source-code for a documentation built by moving all documentation into it.
40 For a complete overview of available options, pass `--help`.
41
42 ## GIR format reference
43 It can always be useful to look at the [reference](https://gi.readthedocs.io/en/latest/annotations/giannotations.html) or [schema](https://gitlab.gnome.org/GNOME/gobject-introspection/blob/main/docs/gir-1.2.rnc).
44
45 ## Contact us
46 If you use [gir] on another library and it fails and you can't figure out why, don't hesitate to [contact us](https://gtk-rs.org/contact)!
47
48 [gir]: https://github.com/gtk-rs/gir