blob: b7a6897a29312a39cdb9b435b69d7fe6b095749d [file] [log] [blame]
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +01001extern crate cc;
Kristian H. Kristensen7c45ef12021-04-06 21:54:57 +00002extern crate wayland_scanner;
Kristian H. Kristensen974c7cd2021-01-24 12:50:23 +00003
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +01004use rayon::prelude::*;
Kristian H. Kristensene8d3b4d2021-02-05 16:39:09 +00005use std::env;
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +01006use std::fs::File;
7use std::io::ErrorKind;
8use std::io::Write;
Kristian H. Kristensen974c7cd2021-01-24 12:50:23 +00009use std::path::Path;
Kristian H. Kristensene8d3b4d2021-02-05 16:39:09 +000010use std::path::PathBuf;
Kristian H. Kristensenb54da802021-09-09 12:35:09 +000011use std::process::Command;
Kristian H. Kristensen974c7cd2021-01-24 12:50:23 +000012use wayland_scanner::*;
13
Kristian H. Kristensenb54da802021-09-09 12:35:09 +000014fn run_bindgen(header: &str, output: &str) {
15 let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
16
Kristian H. Kristensen1cea60c2021-09-17 12:41:35 +000017 let sysroot = env::var("SYSROOT").unwrap_or("/".to_string());
18
19 let status = Command::new("bindgen")
Kristian H. Kristensen129df192021-09-27 08:48:04 +000020 .args(&["-o", out_path.join(output).to_str().unwrap()])
21 .arg("--no-doc-comments")
22 .arg("--no-layout-tests")
23 .args(&["--new-type-alias", "wl_fixed_t"])
24 .args(&["--default-enum-style", "rust_non_exhaustive"])
25 .args(&["--bitfield-enum", "weston_keyboard_modifier"])
Kristian H. Kristensene1d57ee2021-11-10 23:33:29 +010026 .args(&["--bitfield-enum", "weston_activate_flag"])
Kristian H. Kristensen5ef03bd2022-02-23 16:45:36 +010027 .args(&["--bitfield-enum", "wl_event_mask"])
Kristian H. Kristensenb7d53512021-11-12 14:24:17 +010028 .args(&["--no-copy", "shell_surface"])
Kristian H. Kristensen129df192021-09-27 08:48:04 +000029 .arg(header)
30 .arg("--")
31 .arg(format!("--sysroot={}", sysroot))
Kristian H. Kristensen4cc76c12022-02-18 20:45:10 +010032 .arg("-I./wayland-server")
Kristian H. Kristensen129df192021-09-27 08:48:04 +000033 .arg("-I/usr/include/pixman-1")
Kristian H. Kristensen37e86ea2021-10-06 21:13:25 +000034 .arg("-I./weston/include")
Kristian H. Kristensen129df192021-09-27 08:48:04 +000035 .arg("-I./bindgen/block")
36 .status()
37 .unwrap();
Kristian H. Kristensen1cea60c2021-09-17 12:41:35 +000038
39 assert!(status.success())
Kristian H. Kristensenb54da802021-09-09 12:35:09 +000040}
41
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +010042struct ConfigHeader {
43 contents: String,
44}
45
46impl ConfigHeader {
47 fn new() -> ConfigHeader {
48 ConfigHeader {
49 contents: "/* automatically generated header */\n\n".to_string(),
50 }
51 }
52
53 fn define(&mut self, name: &str, value: impl std::fmt::Display) -> &mut Self {
54 self.contents
55 .push_str(&format!("#define {} {}\n", name, value.to_string()));
56
57 self
58 }
59
60 fn define_escaped(&mut self, name: &str, value: impl std::fmt::Display) -> &mut Self {
61 self.define(name, format!("\"{}\"", value));
62
63 self
64 }
65
66 fn build(&self, path: &str, name: &str) {
67 File::create(format!("{}/{}", path, name))
68 .unwrap()
69 .write(self.contents.as_bytes())
70 .unwrap();
71 }
72}
73
74fn generate_header_files(config: &Config) {
Lucas Berthouff52fdc2022-04-28 21:44:29 +000075 #[cfg(feature = "wayland-backend")]
76 {
77 ConfigHeader::new()
78 .define("HAVE_ACCEPT4", 1)
79 .define("HAVE_MEMFD_CREATE", 1)
80 .define_escaped("LIBWESTON_MODULEDIR", "/dev/null")
81 .define_escaped("DATADIR", &config.data_dir)
82 .define("_GNU_SOURCE", 1)
83 .define("_ALL_SOURCE", 1)
84 .define("MAJOR_IN_SYSMACROS", 1)
85 .build(&config.out_path, "config.h");
86 }
87 #[cfg(feature = "drm-backend")]
88 {
89 // TODO: some DRM GBM config might not be compatible with older version.
90 ConfigHeader::new()
91 .define("HAVE_ACCEPT4", 1)
92 .define("BUILD_DRM_COMPOSITOR", 1)
93 .define("BUILD_DRM_GBM", 1)
94 .define("HAVE_GBM_MODIFIERS", 1)
95 .define("HAVE_GBM_FD_IMPORT", 1)
96 .define("ENABLE_EGL", 1)
97 .define("HAVE_MEMFD_CREATE", 1)
98 .define_escaped("LIBWESTON_MODULEDIR", "/usr/lib64/libweston-9")
99 .define_escaped("DATADIR", &config.data_dir)
100 .define("_GNU_SOURCE", 1)
101 .define("_ALL_SOURCE", 1)
102 .define("MAJOR_IN_SYSMACROS", 1)
103 .build(&config.out_path, "config.h");
104 }
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100105
106 ConfigHeader::new()
107 .define("BUILD_ID", "c001bead")
108 .build(&config.out_path, "git-version.h");
109
110 std::fs::create_dir(format!("{}/libweston", config.out_path))
111 .or_else(|e| {
112 if e.kind() == ErrorKind::AlreadyExists {
113 Ok(())
114 } else {
115 Err(e)
116 }
117 })
118 .unwrap();
119
120 ConfigHeader::new()
121 .define("WESTON_VERSION_MAJOR", 9)
122 .define("WESTON_VERSION_MINOR", 0)
123 .define("WESTON_VERSION_MICRO", 0)
124 .define_escaped("WESTON_VERSION", "9.0.0")
125 .build(&config.out_path, "libweston/version.h");
126}
127
128fn generate_rust_protocol_code(config: &Config) {
Kristian H. Kristensencad53c42021-03-08 16:19:24 +0000129 let protocols = [
Kristian H. Kristensen7c45ef12021-04-06 21:54:57 +0000130 "wayland",
Kristian H. Kristensen999d8402021-10-13 13:40:29 +0000131 "xdg-shell",
Kristian H. Kristensen73a22cf2021-10-11 12:09:18 +0000132 "weston-screenshooter",
Kristian H. Kristensen7c45ef12021-04-06 21:54:57 +0000133 "linux-dmabuf-unstable-v1",
134 "croscomp",
135 "alpha-compositing-unstable-v1",
136 "aura-shell",
137 "cursor-shapes-unstable-v1",
138 "gaming-input-unstable-v2",
139 "keyboard-configuration-unstable-v1",
140 "keyboard-extension-unstable-v1",
141 "pointer-gestures-unstable-v1",
142 "remote-shell-unstable-v1",
143 "secure-output-unstable-v1",
144 "stylus-tools-unstable-v1",
145 "stylus-unstable-v2",
146 "vsync-feedback-unstable-v1",
Kristian H. Kristensencad53c42021-03-08 16:19:24 +0000147 ];
148
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100149 let out_dir = Path::new(&config.out_path);
150
151 protocols.par_iter().for_each(|e| {
Kristian H. Kristensen7c45ef12021-04-06 21:54:57 +0000152 let spec = Path::new("protocol").join(e).with_extension("xml");
153 let out = out_dir.join(e).with_extension("rs");
154 println!("cargo:rerun-if-changed={}", spec.to_str().unwrap());
Kristian H. Kristensen7c45ef12021-04-06 21:54:57 +0000155 generate_code_with_destructor_events(spec, out, Side::Server, &[("wl_callback", "done")]);
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100156 });
157}
158
159fn generate_protocol_code(config: &Config) -> Vec<String> {
160 let wp = "/usr/share/wayland-protocols";
161
162 let stable = ["viewporter", "presentation-time", "xdg-shell"];
163
164 let unstable = [
165 ("input-method", 1),
166 ("input-timestamps", 1),
167 ("linux-dmabuf", 1),
168 ("linux-explicit-synchronization", 1),
169 ("xdg-output", 1),
170 ("relative-pointer", 1),
171 ("pointer-constraints", 1),
172 ("fullscreen-shell", 1),
173 ];
174
175 let internal = [
Kristian H. Kristensen4cc76c12022-02-18 20:45:10 +0100176 "wayland",
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100177 "weston-content-protection",
178 "weston-touch-calibration",
179 "weston-debug",
180 "weston-direct-display",
181 "text-cursor-position",
182 ];
183
184 let protocols = stable
185 .iter()
186 .map(|name| {
187 (
188 format!("{}/stable/{}/{}.xml", wp, name, name),
189 format!("{}/{}", config.out_path, name),
190 )
191 })
192 .chain(unstable.iter().map(|(name, version)| {
193 (
194 format!(
195 "{}/unstable/{}/{}-unstable-v{}.xml",
196 wp, name, name, version
197 ),
198 format!("{}/{}-unstable-v{}", config.out_path, name, version),
199 )
200 }))
201 .chain(internal.iter().map(|name| {
202 (
203 format!("protocol/{}.xml", name),
204 format!("{}/{}", config.out_path, name),
205 )
206 }));
207
208 fn generate(xml: &str, output: &str) -> String {
209 for (t, suffix) in [
210 ("client-header", "client-protocol.h"),
211 ("server-header", "server-protocol.h"),
212 ("private-code", "protocol.c"),
213 ] {
214 let output_file = format!("{}-{}", output, suffix);
215 Command::new("wayland-scanner")
216 .args([t, &xml, &output_file])
217 .status()
218 .unwrap();
219 }
220
221 format!("{}-protocol.c", output)
Kristian H. Kristensencad53c42021-03-08 16:19:24 +0000222 }
Kristian H. Kristensene8d3b4d2021-02-05 16:39:09 +0000223
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100224 protocols
225 .map(|(xml, output)| generate(&xml, &output))
226 .collect()
227}
228
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100229struct Files {
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000230 files: Vec<String>,
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100231}
232
233impl Files {
234 fn new() -> Files {
235 Files { files: Vec::new() }
236 }
237
238 fn subdir<A: std::fmt::Display>(&mut self, prefix: &str, iter: impl IntoIterator<Item = A>) {
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000239 self.files
240 .extend(iter.into_iter().map(|f| format!("{}/{}", prefix, f)));
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100241 }
242
243 fn metadata(&self) {
244 for f in &self.files {
245 println!("cargo:rerun-if-changed={}", f);
246 }
247 }
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100248}
249
250fn build_libweston(config: &Config) {
251 generate_header_files(&config);
252
253 let protocol_c_files = generate_protocol_code(&config);
254
255 let mut builder = cc::Build::new();
256
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100257 let mut files = Files::new();
258
Lucas Berthou2827e3b2022-06-29 20:58:58 +0000259 let mut deps = vec!["libdrm", "pixman-1", "xkbcommon", "libffi", "libpng"];
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100260
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100261 files.subdir(
262 "weston/libweston",
263 [
264 "animation.c",
265 "bindings.c",
266 "clipboard.c",
267 "compositor.c",
268 "content-protection.c",
269 "data-device.c",
270 "input.c",
271 "linux-dmabuf.c",
272 "linux-explicit-synchronization.c",
273 "linux-sync-file.c",
274 "log.c",
275 "noop-renderer.c",
276 "pixel-formats.c",
277 "pixman-renderer.c",
278 "plugin-registry.c",
279 "screenshooter.c",
280 "timeline.c",
281 "touch-calibration.c",
282 "weston-log-wayland.c",
283 "weston-log-file.c",
284 "weston-log-flight-rec.c",
285 "weston-log.c",
286 "weston-direct-display.c",
287 "zoom.c",
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000288 ],
289 );
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100290 files.subdir(
291 "weston/shared",
292 [
293 "file-util.c",
294 "image-loader.c",
295 "matrix.c",
296 "os-compatibility.c",
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000297 ],
298 );
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100299
Kristian H. Kristensen4cc76c12022-02-18 20:45:10 +0100300 // Internal wayland-server
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100301 files.subdir(
Kristian H. Kristensen4cc76c12022-02-18 20:45:10 +0100302 "wayland-server",
303 [
304 "wayland-server.c",
305 "wayland-shm.c",
306 "event-loop.c",
307 "connection.c",
308 "wayland-os.c",
309 "wayland-util.c",
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000310 ],
311 );
Kristian H. Kristensen4cc76c12022-02-18 20:45:10 +0100312
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100313 #[cfg(feature = "wayland-backend")]
314 {
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100315 files.subdir("weston/libweston/backend-wayland", ["wayland.c"]);
316 files.subdir("weston/shared", ["frame.c", "cairo-util.c"]);
Lucas Berthou2827e3b2022-06-29 20:58:58 +0000317 deps.extend(["wayland-client", "wayland-cursor", "cairo"])
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100318 }
319
320 #[cfg(feature = "drm-backend")]
321 {
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100322 files.subdir(
323 "weston/libweston/backend-drm",
324 &[
325 "drm.c",
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000326 "drm-gbm.c",
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100327 "fb.c",
328 "modes.c",
329 "kms.c",
330 "state-helpers.c",
331 "state-propose.c",
332 "libbacklight.c",
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000333 ],
334 );
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100335 files.subdir(
336 "weston/libweston",
337 &[
338 "launcher-direct.c",
339 "launcher-util.c",
340 "launcher-weston-launch.c",
341 "libinput-device.c",
342 "libinput-seat.c",
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000343 "vertex-clipping.c",
344 ],
345 );
346 files.subdir(
347 "weston/libweston/renderer-gl",
348 &["egl-glue.c", "gl-renderer.c"],
349 );
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100350
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000351 deps.extend(["libinput", "libevdev", "libudev", "gbm", "egl", "glesv2"]);
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100352 }
353
Kristian H. Kristensenbe96d9a2022-03-10 16:06:56 +0100354 files.metadata();
355
356 builder
357 .cargo_metadata(false)
358 .files(files.files)
359 .files(protocol_c_files)
360 .extra_warnings(false)
361 .flag("-std=gnu99")
362 .include("weston/include")
363 .include("weston/libweston")
364 .include("weston")
365 .include(&config.out_path);
366
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100367 // builder.compile() normally prints these, but we need to list
368 // them now so that croscomp-libweston will appear before the
369 // pkg-config shared libraries on the link line.
370 println!("cargo:rustc-link-lib=static=croscomp-libweston");
371 println!("cargo:rustc-link-search=native={}", config.out_path);
372
373 for d in deps {
374 let lib = pkg_config::probe_library(d).unwrap();
Lucas Berthouff52fdc2022-04-28 21:44:29 +0000375 lib.include_paths.iter().for_each(|item| {
376 builder.include(&item);
377 });
378
379 // TODO: fix ebuild and includes instead of loop above
380 //builder.includes(&lib.include_paths);
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100381 }
382
383 builder.compile("croscomp-libweston");
384}
385
386struct Config {
387 out_path: String,
388 data_dir: String,
389}
390
391fn main() {
392 let config = Config {
393 out_path: env::var("OUT_DIR").unwrap(),
394 data_dir: "/usr/share".to_string(),
395 };
396
397 build_libweston(&config);
398
Kristian H. Kristensenb54da802021-09-09 12:35:09 +0000399 run_bindgen("/usr/include/linux/input.h", "input.rs");
Kristian H. Kristensen73599dc2021-10-07 20:05:00 +0000400 run_bindgen("./weston/croscomp_weston.h", "croscomp_weston.rs");
Kristian H. Kristensen8cc818f2021-02-12 22:52:36 +0000401
Kristian H. Kristensen431d00b2022-02-18 20:06:05 +0100402 generate_rust_protocol_code(&config);
Kristian H. Kristensen4dd859a2021-01-25 17:28:59 +0000403}