Skip to content

Commit 065e777

Browse files
committed
feat(generator): discover Qt include/framework paths dynamically; add PYTHONQT_FRAMEWORK
Add `getFrameworkDirectories` to collect framework/library search roots from: - `.` (cwd), - `PYTHONQT_FRAMEWORK` (path-list, validated), - `--framework-paths` CLI arg (validated), - `${QTDIR}/lib/<Module>` when present, plus `${QTDIR}/lib`.
1 parent bc488b4 commit 065e777

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

generator/main.cpp

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,81 @@ namespace
145145
return includes;
146146
}
147147

148+
QStringList getFrameworkDirectories(const QString &commandLineFrameworks)
149+
{
150+
QStringList frameworks;
151+
frameworks << QStringLiteral(".");
152+
153+
const QChar pathSplitter = QDir::listSeparator();
154+
155+
// From env var PYTHONQT_FRAMEWORK
156+
const QString envFramework = qEnvironmentVariable("PYTHONQT_FRAMEWORK");
157+
if (!envFramework.isEmpty()) {
158+
QStringList envFrameworks = envFramework.split(pathSplitter, Qt::SkipEmptyParts);
159+
for(const QString& framework: qAsConst(envFrameworks)) {
160+
if (isDir(framework)) {
161+
frameworks << framework;
162+
}
163+
else {
164+
qWarning() << "Framework path" << framework << "does not exist, ignoring.";
165+
}
166+
}
167+
}
168+
169+
// CLI-provided framework paths
170+
if (!commandLineFrameworks.isEmpty()) {
171+
const QStringList cliFrameworks = commandLineFrameworks.split(QDir::listSeparator(), Qt::SkipEmptyParts);
172+
for (const QString& framework : cliFrameworks) {
173+
if (isDir(framework)) {
174+
frameworks << QDir::cleanPath(framework);
175+
}
176+
else {
177+
qWarning() << "Framework path" << framework << "does not exist, ignoring.";
178+
}
179+
}
180+
}
181+
182+
// Prefer QLibraryInfo (works without QTDIR)
183+
QString qtLib = QLibraryInfo::location(QLibraryInfo::LibrariesPath);
184+
if (!isDir(qtLib)) {
185+
// Fallback to QTDIR/include
186+
const QString qtDir = qEnvironmentVariable("QTDIR");
187+
if (qtDir.isEmpty() || !isDir(qtDir)) {
188+
const QString reason = QStringLiteral("The QTDIR environment variable ")
189+
+ (qtDir.isEmpty()
190+
? "is not set."
191+
: "points to a non-existing directory.");
192+
qWarning() << reason << "This may cause problems with finding the necessary framework files.";
193+
} else {
194+
qtLib = joinPath(qtDir, QStringLiteral("lib"));
195+
}
196+
}
197+
if (!qtLib.isEmpty() && isDir(qtLib)) {
198+
qInfo() << "Using Qt frameworks at:" << qtLib;
199+
// Check for <lib>/<Module>.framework bundles
200+
for (const QString& qtModule : std::as_const(candidatesQtModules)) {
201+
const QString qtModuleFramework = QDir(qtLib).filePath(qtModule + ".framework");
202+
if (QDir(qtModuleFramework).exists()) {
203+
frameworks << qtModuleFramework;
204+
}
205+
}
206+
frameworks << qtLib;
207+
}
208+
209+
return frameworks;
210+
}
211+
148212
bool
149-
preprocess(const QString& sourceFile, const QString& targetFile, const QStringList& includePaths)
213+
preprocess(const QString& sourceFile, const QString& targetFile, const QStringList& includePaths, const QStringList& frameworkPaths)
150214
{
151215
simplecpp::DUI dui; // settings
152216

153217
for(const QString& include : includePaths) {
154218
dui.addIncludePath(QDir::toNativeSeparators(include).toStdString());
155219
}
220+
for(const QString& framework : frameworkPaths) {
221+
dui.addFrameworkPath(QDir::toNativeSeparators(framework).toStdString());
222+
}
156223
dui.defines.push_back("__cplusplus=1");
157224
dui.defines.push_back("__STDC__");
158225
dui.std = "c++20";
@@ -378,6 +445,7 @@ int main(int argc, char *argv[])
378445
}
379446
printf("Determined Qt version is %d.%d.%d\n", qtVersion >> 16, (qtVersion >> 8) & 0xFF, qtVersion & 0xFF);
380447
}
448+
QStringList frameworkPaths = getFrameworkDirectories(args.value("framework-paths"));
381449

382450
printf("Parsing typesystem file [%s]\n", qPrintable(typesystemFileName));
383451
fflush(stdout);
@@ -393,7 +461,7 @@ int main(int argc, char *argv[])
393461
qPrintable(pp_file), qPrintable(fileName), qPrintable(args.value("include-paths")));
394462
ReportHandler::setContext("Preprocess");
395463

396-
if (!preprocess(fileName, pp_file, includePaths)) {
464+
if (!preprocess(fileName, pp_file, includePaths, frameworkPaths)) {
397465
fprintf(stderr, "Preprocessor failed on file: '%s'\n", qPrintable(fileName));
398466
return 1;
399467
}
@@ -434,6 +502,9 @@ void displayHelp(GeneratorSet* generatorSet) {
434502
" --no-suppress-warnings \n"
435503
" --output-directory=[dir] \n"
436504
" --include-paths=<path>[%c<path>%c...] \n"
505+
#ifdef Q_OS_MACOS
506+
" --framework-paths=<path>[%c<path>%c...] \n"
507+
#endif
437508
" --qt-version=x.y.z \n"
438509
" --print-stdout \n",
439510
path_splitter, path_splitter);

0 commit comments

Comments
 (0)