Debian Codegraph @

book/src/tutorial/sys_library.md

raw
1 # Generating the FFI library
2 We have installed [gir], set up our repo and have all the `.gir` files we need.
3 Now let's work on the unsafe bindings of the -sys crate.
4 Let's change into the directory of the sys crate.
5
6 ```sh
7 cd pango/pango-sys
8 ```
9
10 ## The Gir.toml file
11 The first step is to let [gir] know what to generate.
12 This is what the `Gir.toml` file that we created inside the `pango-sys` folder is for.
13 This file will not be replaced when we run gir again.
14 The file is currently empty so let's add the following to it:
15
16 ```toml
17 [options]
18 library = "Pango"
19 version = "1.0"
20 min_cfg_version = "1.0"
21 target_path = "."
22 girs_directories = ["../../gir-files/"]
23 work_mode = "sys"
24 single_version_file = true
25 ```
26
27 * `library` stands for the name of the library we want to generate.
28 * `version` stands for the version of the library to be used.
29 * `min_cfg_version` will be the minimum version supported by the generated bindings.
30 * `target_path` stands for the location where the files will be generated.
31 * `girs_directories` stands for the location of the `.gir` files.
32 * `work_mode` stands for the mode gir is using.
33 The options here are `sys` and `normal`.
34 * `single_version_file` is a very useful option when you have a lot of generated files (like we'll have).
35 Instead of generating the gir hash commit used for the generation in the header of all generated files, it'll just write it inside one file, removing `git diff` noise **a lot**.
36
37 You can find out the values for `library` and `version` by looking at the name of the .gir file of your library.
38 In our case it is called Pango-1.0.gir.
39 This tells us that the `library` is Pango and the `version` is 1.0.
40 If you don't know what value to use for `min_cfg_version`, use the same as you use for `version`.
41 If not all needed `.gir` files reside in `../../gir-files/`, you can add the path to the other files by changing `girs_directories`.
42 If for example you also have `.gir` files in the root of your project folder, change it to `girs_directories = ["../../gir-files/", "../.."]`.
43 Because we are generating the unsafe bindings, we use the `sys` work mode.
44
45 Let's generate the `sys` crate now:
46
47 ```sh
48 gir -o .
49 ```
50
51 You should now see new files and a new folder.
52
53 * `build.rs`
54 * `Cargo.toml`
55 * `Gir.toml`
56 * `src/lib.rs`
57 * `tests/`
58
59 Now let's try to build it:
60
61 ```sh
62 cargo build
63 ```
64
65 Surprise.
66 It doesn't build at all and you should see a lot of errors.
67 Well, that was expected.
68 We need to add some dependencies in order to make it work.
69 Have a look at the errors of the compiler to find out which are missing.
70 In our example, the compiler throws the following errors:
71 ```rust
72 use of undeclared crate or module `glib`
73 use of undeclared crate or module `gobject`
74 ```
75 The dependencies need to be added to the `external_libraries` part.
76 The names of the dependencies are the same as in the .gir files and not what the packages to install the libraries might be called.
77 The compiler told us that the `GLib` and the `GObject` dependencies are missing.
78 Let's update our `Gir.toml` file to fix it:
79
80 ```toml
81 [options]
82 library = "Pango"
83 version = "1.0"
84 min_cfg_version = "1.0"
85 target_path = "."
86 girs_directories = ["../../gir-files/"]
87 work_mode = "sys"
88
89 external_libraries = [
90 "GLib",
91 "GObject",
92 ]
93 ```
94
95 If one of your .gir files changed or you want to use an updated version of gir to generate the code, there is no need to delete the Cargo.toml or the Cargo.lock files before you regenerate the code.
96 Because we made some changes to the Gir.toml file, we have to run gir again.
97 Changing the content of the external_libraries array means that additional dependencies have to be added.
98 gir does this automatically for you, but only if there is no Cargo.toml and no Cargo.lock file present.
99 Just remove the `Cargo.*` files and run gir again and the additional dependencies will be added.
100 If you made any manual changes to the file, you would have to do these changes again.
101 After regenerating the code, we build the crate to see if the errors are gone.
102 ```sh
103 rm Cargo.*
104 gir -o .
105 cargo build
106 ```
107 When executing the above commands, there should not be any errors and everything should work fine.
108 Just to be sure everything was correctly generated, we can run some tests (graciously generated by [gir] as well):
109 ```sh
110 cargo test
111 ```
112 Normally, all tests passed.
113 If you get an error when running those tests, it's very likely that the `sys` generation is invalid and/or incomplete.
114
115 ## The Gir.toml file
116 This file was automatically generated but it will not be replaced when we run gir again.
117 Make sure to look at your Cargo.toml to optionally add more information to it.
118 If there are any `[features]`, you should try building and testing with these features activated as well.
119 If you'd like, you can also set the default features.
120 This can be useful for example if you want to always activate the newest version unless the user of the crate specifies an older version.
121
122 For our example, we now have a working `sys` crate containing all functions and objects definition.
123 We are done here and can go back to the folder of the wrapper crate:
124 ```sh
125 cd ..
126 ```
127 Time to generate the high-level Rust API!
128
129
130 [gir]: https://github.com/gtk-rs/gir