Skip to content

Commit 7978b53

Browse files
Use shorter paths in the OUT_DIR
As described in KDAB#1237, on Windows we're sometimes hitting the 260 character limit for filepaths. This is mostly due to a lot of nesting by Qt Creator, Corrosion and Cargo who all include the target triple and other configuration information in the build path. However, we can at least reduce the character count a little bit on our part. For example, for qqmlengine.cxxqt.h, the path length is now reduced by ~40 characters from: out/cxx-qt-build/target/crates/cxx-qt-lib/include/cxx-qt-lib-internals/src/qml/qqmlengine.cxxqt.h to now: out/cxxqtbuild/include/private/src/qml/qqmlengine.cxxqt.h
1 parent 45d4bf5 commit 7978b53

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

crates/cxx-qt-build/src/dir.rs

+44-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ pub(crate) const INCLUDE_VERB: &str = "create symlink";
2121
#[cfg(not(unix))]
2222
pub(crate) const INCLUDE_VERB: &str = "deep copy files";
2323

24+
pub(crate) fn gen() -> PathBuf {
25+
// Use a short name due to the Windows file path limit!
26+
out().join("cxxqtgen")
27+
}
28+
2429
// Clean a directory by removing it and recreating it.
2530
pub(crate) fn clean(path: impl AsRef<Path>) -> Result<()> {
2631
let result = std::fs::remove_dir_all(&path);
@@ -37,14 +42,47 @@ pub(crate) fn clean(path: impl AsRef<Path>) -> Result<()> {
3742

3843
/// The target directory, namespaced by crate
3944
pub(crate) fn crate_target() -> PathBuf {
40-
target().join("crates").join(crate_name())
45+
let path = target();
46+
if is_exporting() {
47+
path.join("crates").join(crate_name())
48+
} else {
49+
// If we're not exporting, use a shortened path
50+
// The paths for files in the OUT_DIR can get pretty long, especially if combined with
51+
// Corrosion/CMake.
52+
// This is an issue, as Windows has a maximum path length of 260 characters.
53+
// The OUT_DIR is already namespaced by crate name, so we don't need to prefix again.
54+
// See also: https://github.com/KDAB/cxx-qt/issues/1237
55+
path
56+
}
4157
}
4258

4359
/// The target directory, namespaced by plugin
4460
pub(crate) fn module_target(module_uri: &str) -> PathBuf {
45-
target()
46-
.join("qml_modules")
47-
.join(module_name_from_uri(module_uri))
61+
module_export(module_uri).unwrap_or_else(|| {
62+
out()
63+
// Use a short name due to the Windows file path limit!
64+
.join("cxxqtqml")
65+
.join(module_name_from_uri(module_uri))
66+
})
67+
}
68+
69+
/// The export directory, namespaced by QML module
70+
///
71+
/// In conctrast to the crate_export directory, this is `Some` for downstream dependencies as well.
72+
/// This allows CMake to import QML modules from dependencies.
73+
///
74+
/// TODO: This may conflict if two dependencies are building QML modules with the same name!
75+
/// We should probably include a lockfile here to avoid this.
76+
pub(crate) fn module_export(module_uri: &str) -> Option<PathBuf> {
77+
// In contrast to crate_export, we don't need to check for the specific crate here.
78+
// QML modules should always be exported.
79+
env::var("CXX_QT_EXPORT_DIR")
80+
.ok()
81+
.map(PathBuf::from)
82+
.map(|dir| {
83+
dir.join("qml_modules")
84+
.join(module_name_from_uri(module_uri))
85+
})
4886
}
4987

5088
/// The target directory or another directory where we can write files that will be shared
@@ -54,7 +92,8 @@ pub(crate) fn target() -> PathBuf {
5492
return export;
5593
}
5694

57-
out().join("cxx-qt-build").join("target")
95+
// Use a short name due to the Windows file path limit!
96+
out().join("cxxqtbuild")
5897
}
5998

6099
/// The export directory, if one was specified through the environment.

crates/cxx-qt-build/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn generate_cxxqt_cpp_files(
261261
header_dir: impl AsRef<Path>,
262262
include_prefix: &str,
263263
) -> Vec<GeneratedCppFilePaths> {
264-
let cxx_qt_dir = dir::out().join("cxx-qt-gen");
264+
let cxx_qt_dir = dir::gen();
265265
std::fs::create_dir_all(&cxx_qt_dir).expect("Failed to create cxx-qt-gen directory!");
266266
std::fs::write(cxx_qt_dir.join("include-prefix.txt"), include_prefix).expect("");
267267

crates/cxx-qt-lib-extras/build.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn main() {
8080
});
8181
println!("cargo::rerun-if-changed=src/assertion_utils.h");
8282

83-
builder
84-
.include_prefix("cxx-qt-lib-extras-internals")
85-
.build();
83+
// Use a short name due to the Windows file path limit!
84+
// We don't re-export these headers anyway.
85+
builder.include_prefix("private").build();
8686
}

0 commit comments

Comments
 (0)