Skip to content

Commit 074f343

Browse files
committed
Make only the qttypes/build.rs do the finding qt logic
downstream crates can just use metadata provided by qttypes
1 parent 19cf07f commit 074f343

File tree

6 files changed

+54
-164
lines changed

6 files changed

+54
-164
lines changed

examples/graph/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ build = "build.rs"
77

88
[dependencies]
99
qmetaobject = { path = "../../qmetaobject"}
10-
cstr = "0.1.4"
10+
qttypes = { path = "../../qttypes"} # required to get the DEP_QT_* env variables
11+
cstr = "0.2"
1112
cpp = "0.5"
1213

1314
[build-dependencies]

examples/graph/build.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,12 @@ OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1616
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
1818
extern crate cpp_build;
19-
use std::process::Command;
20-
21-
fn qmake_query(var: &str) -> String {
22-
String::from_utf8(
23-
Command::new("qmake")
24-
.args(&["-query", var])
25-
.output()
26-
.expect("Failed to execute qmake. Make sure 'qmake' is in your path")
27-
.stdout,
28-
)
29-
.expect("UTF-8 conversion failed")
30-
}
3119

3220
fn main() {
33-
let qt_include_path = qmake_query("QT_INSTALL_HEADERS");
34-
let qt_library_path = qmake_query("QT_INSTALL_LIBS");
35-
21+
let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap();
3622
cpp_build::Config::new()
3723
.include(qt_include_path.trim())
3824
.include(qt_include_path.trim().to_owned() + "/QtQuick")
3925
.include(qt_include_path.trim().to_owned() + "/QtCore")
4026
.build("src/main.rs");
41-
42-
let macos_lib_search = if cfg!(target_os = "macos") { "=framework" } else { "" };
43-
let macos_lib_framework = if cfg!(target_os = "macos") { "" } else { "5" };
44-
45-
println!("cargo:rustc-link-search{}={}", macos_lib_search, qt_library_path.trim());
46-
println!("cargo:rustc-link-lib{}=Qt{}Widgets", macos_lib_search, macos_lib_framework);
47-
println!("cargo:rustc-link-lib{}=Qt{}Gui", macos_lib_search, macos_lib_framework);
48-
println!("cargo:rustc-link-lib{}=Qt{}Core", macos_lib_search, macos_lib_framework);
49-
println!("cargo:rustc-link-lib{}=Qt{}Quick", macos_lib_search, macos_lib_framework);
50-
println!("cargo:rustc-link-lib{}=Qt{}Qml", macos_lib_search, macos_lib_framework);
5127
}

qmetaobject/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ repository = "https://github.com/woboq/qmetaobject-rs"
1313
[features]
1414
default = ["log"]
1515
chrono_qdatetime = ["qttypes/chrono"]
16-
webengine = []
16+
webengine = ["qttypes/qtwebengine"]
1717

1818
[dependencies]
19-
qttypes = { path = "../qttypes", version = "0.1.4" }
19+
qttypes = { path = "../qttypes", version = "0.1.4", features = ["qtquick"] }
2020
qmetaobject_impl = { path = "../qmetaobject_impl", version = "=0.1.4"}
2121
lazy_static = "1.0"
2222
cpp = "0.5.4"
2323
log = { version = "0.4", optional = true }
2424

2525
[build-dependencies]
2626
cpp_build = "0.5.4"
27-
semver = "0.9"
27+
semver = "0.11"
2828

2929
[dev-dependencies]
30-
cstr = "0.1"
30+
cstr = "0.2"
3131
if_rust_version = "1"
3232
tempfile = "^3"
3333

qmetaobject/build.rs

Lines changed: 10 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -15,108 +15,29 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO
1515
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1616
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
18-
extern crate cpp_build;
18+
1919
use semver::Version;
20-
use std::io::prelude::*;
21-
use std::io::BufReader;
22-
use std::path::Path;
23-
use std::process::Command;
2420

25-
fn qmake_query(var: &str) -> String {
26-
let qmake = std::env::var("QMAKE").unwrap_or("qmake".to_string());
27-
String::from_utf8(
28-
Command::new(qmake)
29-
.env("QT_SELECT", "qt5")
30-
.args(&["-query", var])
31-
.output()
32-
.expect("Failed to execute qmake. Make sure 'qmake' is in your path")
33-
.stdout,
34-
)
35-
.expect("UTF-8 conversion failed")
36-
}
21+
fn main() {
22+
eprintln!("cargo:warning={:?}", std::env::vars().collect::<Vec<_>>());
3723

38-
// qreal is a double, unless QT_COORD_TYPE says otherwise:
39-
// https://doc.qt.io/qt-5/qtglobal.html#qreal-typedef
40-
fn detect_qreal_size(qt_include_path: &str) {
41-
let path = Path::new(qt_include_path).join("QtCore").join("qconfig.h");
42-
let f = std::fs::File::open(&path).expect(&format!("Cannot open `{:?}`", path));
43-
let b = BufReader::new(f);
24+
let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap();
25+
let qt_library_path = std::env::var("DEP_QT_LIBRARY_PATH").unwrap();
26+
let qt_version = std::env::var("DEP_QT_VERSION")
27+
.unwrap()
28+
.parse::<Version>()
29+
.expect("Parsing Qt version failed");
4430

45-
// Find declaration of QT_COORD_TYPE
46-
for line in b.lines() {
47-
let line = line.expect("qconfig.h is valid UTF-8");
48-
if line.contains("QT_COORD_TYPE") {
49-
if line.contains("float") {
50-
println!("cargo:rustc-cfg=qreal_is_float");
51-
return;
52-
} else {
53-
panic!("QT_COORD_TYPE with unknown declaration {}", line);
54-
}
55-
}
56-
}
57-
}
58-
59-
fn main() {
60-
let qt_include_path = qmake_query("QT_INSTALL_HEADERS");
61-
let qt_library_path = qmake_query("QT_INSTALL_LIBS");
62-
let qt_version =
63-
qmake_query("QT_VERSION").parse::<Version>().expect("Parsing Qt version failed");
6431
let mut config = cpp_build::Config::new();
65-
6632
if cfg!(target_os = "macos") {
6733
config.flag("-F");
6834
config.flag(qt_library_path.trim());
6935
}
36+
config.include(qt_include_path.trim()).build("src/lib.rs");
7037

7138
for minor in 7..=15 {
7239
if qt_version >= Version::new(5, minor, 0) {
7340
println!("cargo:rustc-cfg=qt_{}_{}", 5, minor);
7441
}
7542
}
76-
77-
detect_qreal_size(&qt_include_path.trim());
78-
79-
config.include(qt_include_path.trim()).build("src/lib.rs");
80-
81-
let macos_lib_search = if cfg!(target_os = "macos") { "=framework" } else { "" };
82-
let macos_lib_framework = if cfg!(target_os = "macos") { "" } else { "5" };
83-
84-
let debug = std::env::var("DEBUG").ok().map_or(false, |s| s == "true");
85-
let windows_dbg_suffix = if debug {
86-
if cfg!(target_os = "windows") {
87-
println!("cargo:rustc-link-lib=msvcrtd");
88-
"d"
89-
} else {
90-
""
91-
}
92-
} else {
93-
""
94-
};
95-
println!("cargo:rustc-link-search{}={}", macos_lib_search, qt_library_path.trim());
96-
println!(
97-
"cargo:rustc-link-lib{}=Qt{}Widgets{}",
98-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
99-
);
100-
println!(
101-
"cargo:rustc-link-lib{}=Qt{}Gui{}",
102-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
103-
);
104-
println!(
105-
"cargo:rustc-link-lib{}=Qt{}Core{}",
106-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
107-
);
108-
println!(
109-
"cargo:rustc-link-lib{}=Qt{}Quick{}",
110-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
111-
);
112-
println!(
113-
"cargo:rustc-link-lib{}=Qt{}Qml{}",
114-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
115-
);
116-
if cfg!(feature = "webengine") {
117-
println!(
118-
"cargo:rustc-link-lib{}=Qt{}WebEngine{}",
119-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
120-
);
121-
}
12243
}

qttypes/Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ version = "0.1.4"
44
edition = "2018"
55
authors = ["Olivier Goffart <ogoffart@woboq.com>"]
66
build = "build.rs"
7-
description = "Manually maintained building for Qt basic types"
7+
description = "Manually maintained buildings for Qt value types"
88
readme = "README.md"
99
license = "MIT"
10-
keywords = ["Qt"]
10+
keywords = ["Qt", "QtCore", "QtGui"]
1111
repository = "https://github.com/woboq/qmetaobject-rs"
12+
links = "qt"
13+
14+
[features]
15+
# Link against QtQuick
16+
qtquick = []
17+
# Link against QtWebEngine
18+
qtwebengine = []
1219

1320
[dependencies]
1421
cpp = "0.5.6"
1522
chrono = { version = "0.4", optional = true }
1623

1724
[build-dependencies]
1825
cpp_build = "0.5.6"
19-
semver = "0.11"
2026

2127
[package.metadata.docs.rs]
2228
dependencies = [ "qtbase5-dev", "qtdeclarative5-dev" ]

qttypes/build.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO
1515
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1616
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
18-
extern crate cpp_build;
19-
use semver::Version;
18+
2019
use std::io::prelude::*;
2120
use std::io::BufReader;
2221
use std::path::Path;
@@ -59,64 +58,51 @@ fn detect_qreal_size(qt_include_path: &str) {
5958
fn main() {
6059
let qt_include_path = qmake_query("QT_INSTALL_HEADERS");
6160
let qt_library_path = qmake_query("QT_INSTALL_LIBS");
62-
let qt_version =
63-
qmake_query("QT_VERSION").parse::<Version>().expect("Parsing Qt version failed");
61+
let qt_version = qmake_query("QT_VERSION");
62+
6463
let mut config = cpp_build::Config::new();
6564

6665
if cfg!(target_os = "macos") {
6766
config.flag("-F");
6867
config.flag(qt_library_path.trim());
6968
}
7069

71-
for minor in 7..=15 {
72-
if qt_version >= Version::new(5, minor, 0) {
73-
println!("cargo:rustc-cfg=qt_{}_{}", 5, minor);
74-
}
75-
}
76-
7770
detect_qreal_size(&qt_include_path.trim());
7871

7972
config.include(qt_include_path.trim()).build("src/lib.rs");
8073

74+
println!("cargo:VERSION={}", qt_version.trim());
75+
println!("cargo:LIBRARY_PATH={}", qt_library_path.trim());
76+
println!("cargo:INCLUDE_PATH={}", qt_include_path.trim());
77+
8178
let macos_lib_search = if cfg!(target_os = "macos") { "=framework" } else { "" };
8279
let macos_lib_framework = if cfg!(target_os = "macos") { "" } else { "5" };
8380

8481
let debug = std::env::var("DEBUG").ok().map_or(false, |s| s == "true");
85-
let windows_dbg_suffix = if debug {
86-
if cfg!(target_os = "windows") {
87-
println!("cargo:rustc-link-lib=msvcrtd");
88-
"d"
89-
} else {
90-
""
91-
}
82+
let windows_dbg_suffix = if debug && cfg!(target_os = "windows") {
83+
println!("cargo:rustc-link-lib=msvcrtd");
84+
"d"
9285
} else {
9386
""
9487
};
9588
println!("cargo:rustc-link-search{}={}", macos_lib_search, qt_library_path.trim());
96-
println!(
97-
"cargo:rustc-link-lib{}=Qt{}Widgets{}",
98-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
99-
);
100-
println!(
101-
"cargo:rustc-link-lib{}=Qt{}Gui{}",
102-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
103-
);
104-
println!(
105-
"cargo:rustc-link-lib{}=Qt{}Core{}",
106-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
107-
);
108-
println!(
109-
"cargo:rustc-link-lib{}=Qt{}Quick{}",
110-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
111-
);
112-
println!(
113-
"cargo:rustc-link-lib{}=Qt{}Qml{}",
114-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
115-
);
116-
if cfg!(feature = "webengine") {
89+
90+
let link_lib = |lib: &str| {
11791
println!(
118-
"cargo:rustc-link-lib{}=Qt{}WebEngine{}",
119-
macos_lib_search, macos_lib_framework, windows_dbg_suffix
120-
);
121-
}
92+
"cargo:rustc-link-lib{search}=Qt{vers}{lib}{suffix}",
93+
search = macos_lib_search,
94+
vers = macos_lib_framework,
95+
lib = lib,
96+
suffix = windows_dbg_suffix
97+
)
98+
};
99+
link_lib("Core");
100+
link_lib("Gui");
101+
link_lib("Widgets");
102+
#[cfg(feature = "qtquick")]
103+
link_lib("Quick");
104+
#[cfg(feature = "qtquick")]
105+
link_lib("Qml");
106+
#[cfg(feature = "qtwebengine")]
107+
link_lib("WebEngine");
122108
}

0 commit comments

Comments
 (0)