qtbridge_build_utils/
qt_build.rs1use std::fmt::Display;
5use std::path::{Path, PathBuf};
6use std::process::Command;
7use qt_build_utils::{QtBuild, QtTool, QtToolQtPaths};
8
9#[doc(hidden)]
10pub struct QtInstallation {
11 pub qt_paths: QtToolQtPaths,
12 qt_build: QtBuild,
13}
14
15#[doc(hidden)]
16impl Default for QtInstallation {
17 fn default() -> Self {
18 let qt = QtBuild::new(vec!["Core".to_owned()])
19 .expect("Could not find Qt installation");
20 let qt_paths = qt.qtpaths();
21 Self { qt_paths, qt_build: qt }
22 }
23}
24
25#[doc(hidden)]
26impl QtInstallation {
27 pub fn include_dirs(&self, modules: impl IntoIterator<Item: Display>, add_private: bool) -> Vec<String> {
28 let qtbase_include_dir = PathBuf::from(self.qt_paths.query("QT_INSTALL_HEADERS").expect("qtpaths failed to query QT_INSTALL_HEADERS"));
29 let qt_version = self.qt_paths.query("QT_VERSION").expect("qtpaths failed to query QT_VERSION");
30
31 if self.is_qt_framework_installation() {
43 let qt_libs_dir = self.qt_paths.query("QT_INSTALL_LIBS").expect("qtpaths failed to query QT_INSTALL_LIBS");
44 let frameworks_path = PathBuf::from(qt_libs_dir);
45
46 let mut dirs: Vec<String> = modules.into_iter()
47 .flat_map(|module| {
48 let module_name = format!("Qt{module}");
49 let headers = frameworks_path
50 .join(format!("{module_name}.framework"))
51 .join("Headers");
52 if add_private {
53 let version_path = headers.join(&qt_version);
54 vec![
55 version_path.to_string_lossy().to_string(),
57 version_path.join(&module_name).to_string_lossy().to_string(),
59 headers.to_string_lossy().to_string(),
61 ]
62 } else {
63 vec![headers.to_string_lossy().to_string()]
65 }
66 })
67 .collect();
68 dirs.push(qtbase_include_dir.to_string_lossy().to_string());
70 return dirs;
71 }
72
73 modules.into_iter()
74 .flat_map(|module| {
75 let module_name = format!("Qt{module}");
76 let module_path = qtbase_include_dir.join(&module_name);
77 if add_private {
78 let module_version_path = module_path.join(&qt_version);
79 let private_module_path = module_version_path.join(&module_name);
80
81 vec![module_version_path, private_module_path, module_path]
82 } else {
83 vec![module_path]
84 }
85 })
86 .chain(std::iter::once(qtbase_include_dir.clone()))
87 .map(|path| path.to_string_lossy().to_string())
88 .collect()
89 }
90
91
92 pub fn configure_builder(&self, builder: &mut cc::Build) {
93 self.set_platform_specific_flags(builder);
94 }
95
96 fn set_platform_specific_flags(&self, builder: &mut cc::Build) {
97 if self.is_qt_framework_installation() {
98 let qt_libs_dir = self.qt_paths.query("QT_INSTALL_LIBS").expect("qtpaths failed to query QT_INSTALL_LIBS");
99 builder.flag(format!("-F{}", qt_libs_dir));
100 }
101 }
102
103 fn is_qt_framework_installation(&self) -> bool {
104 if !cfg!(target_os = "macos") {
105 return false;
106 }
107 let qt_libs_dir = self.qt_paths.query("QT_INSTALL_LIBS").expect("qtpaths failed to query QT_INSTALL_LIBS");
108 PathBuf::from(qt_libs_dir).join("QtCore.framework").exists()
109 }
110
111 pub fn link_modules(&self, modules: impl IntoIterator<Item: Display>) {
112 let qt_libs_dir = self.qt_paths.query("QT_INSTALL_LIBS").expect("qtpaths failed to query QT_INSTALL_LIBS");
113
114 if self.is_qt_framework_installation() {
115 println!("cargo::rustc-link-search=framework={qt_libs_dir}");
117 modules.into_iter()
118 .for_each(|module| println!("cargo::rustc-link-lib=framework=Qt{module}"));
119 return;
120 }
121
122 println!("cargo::rustc-link-search={qt_libs_dir}");
123
124 modules.into_iter()
125 .for_each(|module| {
126 println!("cargo::rustc-link-lib=Qt6{module}")
127 });
128 }
129
130 pub fn run_moc(&self, input: &Path, output: &Path) {
131 let moc_path = self.qt_build.installation()
132 .try_find_tool(QtTool::Moc)
133 .expect("Could not find moc executable");
134
135 println!("cargo::rerun-if-changed={}", input.display());
136
137 let result = Command::new(&moc_path)
138 .arg(input)
139 .arg("-o")
140 .arg(output)
141 .output()
142 .unwrap_or_else(|_| panic!("Failed to run moc for {}", input.display()));
143
144 if !result.status.success() {
145 panic!(
146 "moc failed for {}:\n{}",
147 input.display(),
148 String::from_utf8_lossy(&result.stderr)
149 );
150 }
151 }
152}
153
154pub fn get_cxx_qt_lib_manifest_dir() -> Result<PathBuf, String> {
155 const MANIFEST_VAR_NAME: &str = "DEP_CXX_QT_LIB_CXX_QT_MANIFEST_PATH";
156 let manifest_path_var = std::env::var(MANIFEST_VAR_NAME)
157 .map_err(|err| format!("Failed to get environment variable '{MANIFEST_VAR_NAME}'. Error: '{err}'"))?;
158 let path = PathBuf::from(&manifest_path_var)
159 .parent()
160 .ok_or_else(|| format!("Failed to get parent of manifest path '{manifest_path_var}'"))?
161 .to_owned();
162 Ok(path)
163}
164
165pub fn get_cxx_qt_lib_include_path() -> Result<PathBuf, String> {
166 let path = get_cxx_qt_lib_manifest_dir()?
170 .join("include");
171 Ok(path)
172}