182 QCommandLineParser parser;
183 parser.setApplicationDescription(
"Qt HarmonyOS Deployment Tool"_L1);
185 QCommandLineOption inputOption(
"input"_L1,
"JSON configuration file"_L1,
"file"_L1);
186 QCommandLineOption outputOption(
"output"_L1,
"Output directory"_L1,
"dir"_L1);
187 QCommandLineOption hvigorOption(
"hvigor"_L1,
"Path to hvigorw"_L1,
"path"_L1);
188 QCommandLineOption installOption(
"install"_L1,
"Install to device"_L1);
189 QCommandLineOption releaseOption(
"release"_L1,
"Build release configuration"_L1);
190 QCommandLineOption verboseOption(
"verbose"_L1,
"Verbose output"_L1);
191 QCommandLineOption noBuildOption(
"no-build"_L1,
"Skip building"_L1);
192 QCommandLineOption testBundleOption(
"test-bundle"_L1,
"Enable test bundle mode"_L1);
193 QCommandLineOption depfileOption(
"depfile"_L1,
"Dependency file output"_L1,
"path"_L1);
194 QCommandLineOption depfileBaseOption(
"depfile-base"_L1,
"Base directory for depfile paths"_L1,
"dir"_L1);
195 QCommandLineOption signingCertPathOption(
"signing-cert-path"_L1,
196 "Path to the .cer file"_L1,
"path"_L1);
197 QCommandLineOption signingProfileOption(
"signing-profile"_L1,
198 "Path to the .p7b profile"_L1,
"path"_L1);
199 QCommandLineOption signingStoreFileOption(
"signing-store-file"_L1,
200 "Path to the .p12 keystore"_L1,
"path"_L1);
201 QCommandLineOption signingKeyAliasOption(
"signing-key-alias"_L1,
202 "Key alias inside the keystore"_L1,
"alias"_L1);
203 QCommandLineOption signingKeyPasswordOption(
"signing-key-password"_L1,
204 "Encrypted key password"_L1,
"pwd"_L1);
205 QCommandLineOption signingStorePasswordOption(
"signing-store-password"_L1,
206 "Encrypted keystore password"_L1,
"pwd"_L1);
207 QCommandLineOption signingPlainKeyPasswordOption(
"signing-plain-key-password"_L1,
208 "Plain text key password"_L1,
"pwd"_L1);
209 QCommandLineOption signingPlainStorePasswordOption(
"signing-plain-store-password"_L1,
210 "Plain text keystore password"_L1,
"pwd"_L1);
211 QCommandLineOption signingAlgOption(
"signing-alg"_L1,
212 "Signature algorithm (default SHA256withECDSA)"_L1,
"alg"_L1);
213 QCommandLineOption helpOption(
"help"_L1,
"Show help"_L1);
215 parser.addOption(inputOption);
216 parser.addOption(outputOption);
217 parser.addOption(hvigorOption);
218 parser.addOption(installOption);
219 parser.addOption(releaseOption);
220 parser.addOption(verboseOption);
221 parser.addOption(noBuildOption);
222 parser.addOption(testBundleOption);
223 parser.addOption(depfileOption);
224 parser.addOption(depfileBaseOption);
225 parser.addOption(signingCertPathOption);
226 parser.addOption(signingProfileOption);
227 parser.addOption(signingStoreFileOption);
228 parser.addOption(signingKeyAliasOption);
229 parser.addOption(signingKeyPasswordOption);
230 parser.addOption(signingStorePasswordOption);
231 parser.addOption(signingPlainKeyPasswordOption);
232 parser.addOption(signingPlainStorePasswordOption);
233 parser.addOption(signingAlgOption);
234 parser.addOption(helpOption);
236 if (!parser.parse(arguments)) {
237 fprintf(stderr,
"%s\n", qPrintable(parser.errorText()));
241 if (parser.isSet(helpOption)) {
246 if (!parser.isSet(inputOption)) {
247 fprintf(stderr,
"Error: --input option is required\n");
252 options->inputFile = parser.value(inputOption);
253 options->outputDirectory = parser.value(outputOption);
254 options->hvigorPath = parser.value(hvigorOption);
257 options
->verbose = parser.isSet(verboseOption);
260 options->depFilePath = parser.value(depfileOption);
261 options->depFileBase = parser.value(depfileBaseOption);
262 options->signingCertPath = parser.value(signingCertPathOption);
263 options->signingProfile = parser.value(signingProfileOption);
264 options->signingStoreFile = parser.value(signingStoreFileOption);
265 options->signingKeyAlias = parser.value(signingKeyAliasOption);
266 options->signingKeyPassword = parser.value(signingKeyPasswordOption);
267 options->signingStorePassword = parser.value(signingStorePasswordOption);
268 options->signingPlainKeyPassword = parser.value(signingPlainKeyPasswordOption);
269 options->signingPlainStorePassword = parser.value(signingPlainStorePasswordOption);
270 options->signingAlg = parser.value(signingAlgOption);
302 QFile inputFile(options->inputFile);
303 if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
304 fprintf(stderr,
"Failed to open input file: %s\n", qPrintable(options->inputFile));
308 QJsonParseError parseError;
309 QJsonDocument doc = QJsonDocument::fromJson(inputFile.readAll(), &parseError);
311 fprintf(stderr,
"Failed to parse JSON: %s\n", qPrintable(parseError.errorString()));
315 QJsonObject obj = doc.object();
317 options->applicationBinary = obj[
"application-binary"_L1].toString();
318 options->harmonyOsPackageSourceDirectory = obj[
"harmonyos-package-source-directory"_L1].toString();
319 options->harmonyOsAppName = obj[
"harmonyos-app-name"_L1].toString();
320 options->harmonyOsAppBundleName = obj[
"harmonyos-app-bundle-name"_L1].toString();
321 options->sdkRoot = obj[
"sdk-root"_L1].toString();
322 options->ndkRoot = obj[
"ndk-root"_L1].toString();
325 const QJsonValue rootPathValue = obj[
"qml-root-path"_L1];
326 if (rootPathValue.isArray()) {
327 for (
const QJsonValue &v : rootPathValue.toArray()) {
328 const QString s = v.toString();
330 options->qmlRootPaths.append(s);
333 const QString s = rootPathValue.toString();
335 options->qmlRootPaths.append(s);
340 options->qtLibsDirectory = obj[
"qtLibsDirectory"_L1].toString();
341 options->qtPluginsDirectory = obj[
"qtPluginsDirectory"_L1].toString();
342 options->qtQmlDirectory = obj[
"qtQmlDirectory"_L1].toString();
343 options->qtLibExecsDirectory = obj[
"qtLibExecsDirectory"_L1].toString();
344 options->qtHostDirectory = obj[
"qtHostDirectory"_L1].toString();
346 QJsonArray extraLibsDirsArray = obj[
"extra-libs-dirs"_L1].toArray();
347 for (
const QJsonValue &value : extraLibsDirsArray)
348 options->extraLibsDirs.append(value.toString());
351 if (obj[
"test-bundle"_L1].toBool())
353 options->testBinariesDirectory = obj[
"test-binaries-directory"_L1].toString();
354 QJsonArray excludeArray = obj[
"test-exclude-list"_L1].toArray();
355 for (
const QJsonValue &value : excludeArray)
356 options->testExcludeList.append(value.toString());
357 const QJsonArray rawfilesArray = obj[
"test-rawfiles"_L1].toArray();
358 for (
const QJsonValue &value : rawfilesArray) {
359 const QJsonObject rawfile = value.toObject();
360 QString destination = QDir::cleanPath(rawfile[
"destination"_L1].toString());
361 if (destination ==
"."_L1)
363 if (QDir::isAbsolutePath(destination) || destination ==
".."_L1
364 || destination.startsWith(
"../"_L1)) {
365 fprintf(stderr,
"Test rawfile destination is outside the rawfile directory: %s\n",
366 qPrintable(destination));
369 options->testRawfiles.append({ rawfile[
"source"_L1].toString(), destination });
373 QJsonArray projectLibsArray = obj[
"project-libraries"_L1].toArray();
374 for (
const QJsonValue &value : projectLibsArray)
375 options->projectLibraries.append(value.toString());
378 QJsonArray importPathsArray = obj[
"qml-import-paths"_L1].toArray();
379 for (
const QJsonValue &value : importPathsArray)
380 options->qmlImportPaths.append(value.toString());
383 QJsonArray pluginsImportPathsArray = obj[
"plugins-import-paths"_L1].toArray();
384 for (
const QJsonValue &value : pluginsImportPathsArray)
385 options->pluginsImportPaths.append(value.toString());
388 QJsonArray archArray = obj[
"harmonyos-target-arch"_L1].toArray();
389 for (
const QJsonValue &value : archArray)
390 options->targetArchs.append(value.toString());
391 if (options->targetArchs.isEmpty())
392 options->targetArchs.append(
"arm64-v8a"_L1);
395 options->permissions = obj[
"permissions"_L1].toArray();
398 options->harmonyOsAppVendor = obj[
"harmonyos-app-vendor"_L1].toString();
400 options->harmonyOsAppVersionName = obj[
"harmonyos-app-version-name"_L1].toString();
401 options->harmonyOsAppLabel = obj[
"harmonyos-app-label"_L1].toString();
402 options->harmonyOsAppIcon = obj[
"harmonyos-app-icon"_L1].toString();
405 options->harmonyOsCompatibleSdkVersion =
406 obj[
"harmonyos-compatible-sdk-version"_L1].toString();
407 options->harmonyOsTargetSdkVersion =
408 obj[
"harmonyos-target-sdk-version"_L1].toString();
409 options->harmonyOsCompileSdkVersion =
410 obj[
"harmonyos-compile-sdk-version"_L1].toString();
413 if (options->harmonyOsTargetSdkVersion.isEmpty()
414 && options->harmonyOsCompileSdkVersion.isEmpty()) {
415 options->harmonyOsTargetSdkVersion = sdkVersion(options->sdkRoot);
421 const QJsonArray extraPluginsArray =
422 obj[
"harmonyos-extra-plugins"_L1].toArray();
423 for (
const QJsonValue &v : extraPluginsArray) {
424 const QString s = v.toString();
426 options->extraPlugins.append(s);
432 options->harmonyOsModuleDescription = obj[
"harmonyos-module-description"_L1].toString();
433 const QJsonArray deviceTypesArray = obj[
"harmonyos-module-device-types"_L1].toArray();
434 for (
const QJsonValue &value : deviceTypesArray)
435 options->harmonyOsModuleDeviceTypes.append(value.toString());
436 options->harmonyOsAbilityOrientation = obj[
"harmonyos-ability-orientation"_L1].toString();
440 if (options->applicationBinary.isEmpty()) {
441 fprintf(stderr,
"Error: 'application-binary' not specified in JSON\n");
449 if (!QFile::exists(options->applicationBinary)) {
450 fprintf(stderr,
"Error: application binary does not exist: %s\n",
451 qPrintable(options->applicationBinary));
452 fprintf(stderr,
" Build the project before running harmonydeployqt.\n");
459 if (options->harmonyOsAppBundleName.isEmpty())
460 options->harmonyOsAppBundleName =
"org.qtproject.autotests"_L1;
461 if (options->harmonyOsAppName.isEmpty())
462 options->harmonyOsAppName =
"QtAutoTests"_L1;
466 if (options->harmonyOsPackageSourceDirectory.isEmpty()) {
471 searchPath = QDir::cleanPath(options->qtLibsDirectory);
472 }
else if (!options->applicationBinary.isEmpty()) {
473 QFileInfo appInfo(options->applicationBinary);
474 searchPath = QDir::cleanPath(appInfo.absolutePath());
477 if (searchPath.isEmpty()) {
478 fprintf(stderr,
"Error: 'harmonyos-package-source-directory' not specified in JSON\n");
479 fprintf(stderr,
" and could not auto-detect template location (no search path)\n");
484 fprintf(stdout,
"Searching for template starting from: %s\n", qPrintable(searchPath));
487 for (
int i = 0; i < 10; ++i) {
489 QString templatePath = searchPath +
"/share/qt6/src/harmonyos/templates"_L1;
491 fprintf(stdout,
" Checking: %s ... %s\n", qPrintable(templatePath),
492 QDir(templatePath).exists() ?
"FOUND" :
"not found");
494 if (QDir(templatePath).exists()) {
495 options->harmonyOsPackageSourceDirectory = std::move(templatePath);
500 templatePath = searchPath +
"/src/harmonyos/templates"_L1;
502 fprintf(stdout,
" Checking: %s ... %s\n", qPrintable(templatePath),
503 QDir(templatePath).exists() ?
"FOUND" :
"not found");
505 if (QDir(templatePath).exists()) {
506 options->harmonyOsPackageSourceDirectory = std::move(templatePath);
511 const auto lastSlash = searchPath.lastIndexOf(
'/'_L1);
512 if (lastSlash <= 0) {
514 fprintf(stdout,
" Reached root directory\n");
517 searchPath.resize(lastSlash);
520 if (options->harmonyOsPackageSourceDirectory.isEmpty()) {
521 fprintf(stderr,
"Error: 'harmonyos-package-source-directory' not specified in JSON\n");
522 fprintf(stderr,
" and could not auto-detect template location\n");
523 fprintf(stderr,
" Please specify the path to the HarmonyOS application template\n");
526 fprintf(stdout,
"Auto-detected template: %s\n", qPrintable(options->harmonyOsPackageSourceDirectory));
530 if (options->harmonyOsAppName.isEmpty()) {
531 fprintf(stderr,
"Error: 'harmonyos-app-name' not specified in JSON\n");
535 if (options->harmonyOsAppBundleName.isEmpty()) {
536 fprintf(stderr,
"Error: 'harmonyos-app-bundle-name' not specified in JSON\n");
541 if (options->outputDirectory.isEmpty()) {
543 options->outputDirectory = QDir::currentPath() +
"/harmonyos-tests-bundle"_L1;
545 QFileInfo appInfo(options->applicationBinary);
546 options->outputDirectory = QDir::currentPath() +
"/"_L1 +
547 appInfo.completeBaseName() +
"-harmonyos"_L1;
552 fprintf(stdout,
"Configuration loaded:\n");
554 fprintf(stdout,
" Mode: test bundle\n");
555 fprintf(stdout,
" Test binaries directory: %s\n", qPrintable(options->testBinariesDirectory));
556 if (!options->testExcludeList.isEmpty())
557 fprintf(stdout,
" Exclude list: %s\n", qPrintable(options->testExcludeList.join(
", "_L1)));
559 fprintf(stdout,
" Application binary: %s\n", qPrintable(options->applicationBinary));
561 fprintf(stdout,
" Template directory: %s\n", qPrintable(options->harmonyOsPackageSourceDirectory));
562 fprintf(stdout,
" App name: %s\n", qPrintable(options->harmonyOsAppName));
563 fprintf(stdout,
" Bundle name: %s\n", qPrintable(options->harmonyOsAppBundleName));
564 fprintf(stdout,
" Output directory: %s\n", qPrintable(options->outputDirectory));
565 fprintf(stdout,
" Target architectures: %s\n", qPrintable(options->targetArchs.join(
", "_L1)));
736 fprintf(stdout,
"Copying template from %s to %s\n",
737 qPrintable(options.harmonyOsPackageSourceDirectory),
738 qPrintable(options.outputDirectory));
742 QDir templateDir(options.harmonyOsPackageSourceDirectory);
743 if (!templateDir.exists()) {
744 fprintf(stderr,
"Template directory does not exist: %s\n",
745 qPrintable(options.harmonyOsPackageSourceDirectory));
752 const QString moduleManifest =
753 options.harmonyOsPackageSourceDirectory +
"/entry/src/main/module.json5"_L1;
754 if (!QFile::exists(moduleManifest)) {
756 "'%s' is not a valid HarmonyOS package template: missing "
757 "entry/src/main/module.json5\n",
758 qPrintable(options.harmonyOsPackageSourceDirectory));
763 QDir outputDir(options.outputDirectory);
764 if (outputDir.exists()) {
766 fprintf(stdout,
"Output directory already exists, will overwrite files\n");
772 const QString buildProfilePath = options.outputDirectory +
"/build-profile.json5"_L1;
773 QString signingConfigs;
774 if (keepSigningConfigs) {
775 QFile buildProfile(buildProfilePath);
776 if (buildProfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
778 json5ArrayValue(QString::fromUtf8(buildProfile.readAll()),
"signingConfigs"_L1);
783 if (!copyRecursively(options.harmonyOsPackageSourceDirectory, options.outputDirectory, options.verbose))
792 for (
const char *relPath : {
"AppScope/app.json5",
"entry/src/main/module.json5",
793 "build-profile.json5" }) {
794 const QString src = options.harmonyOsPackageSourceDirectory
795 + QLatin1Char(
'/') + QLatin1String(relPath);
796 const QString dst = options.outputDirectory + QLatin1Char(
'/') + QLatin1String(relPath);
797 if (QFile::exists(src) && !copyFileIfNewer(src, dst, options.verbose,
true))
801 if (!signingConfigs.isEmpty() && signingConfigs !=
"[]"_L1) {
802 QFile buildProfile(buildProfilePath);
803 if (!buildProfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
804 fprintf(stderr,
"Failed to open %s for reading\n", qPrintable(buildProfilePath));
807 QString content = QString::fromUtf8(buildProfile.readAll());
808 buildProfile.close();
809 const auto emptySigningConfigs =
"\"signingConfigs\": []"_L1;
810 if (!content.contains(emptySigningConfigs)) {
812 "Warning: the template's build-profile.json5 does not contain %s, so the"
813 " existing signingConfigs are not kept\n",
814 emptySigningConfigs.data());
816 content.replace(emptySigningConfigs,
"\"signingConfigs\": "_L1 + signingConfigs);
817 if (!buildProfile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
818 fprintf(stderr,
"Failed to open %s for writing\n", qPrintable(buildProfilePath));
821 const QByteArray data = content.toUtf8();
822 if (buildProfile.write(data) != data.size()) {
823 fprintf(stderr,
"Failed to write %s\n", qPrintable(buildProfilePath));
827 fprintf(stdout,
" Kept signingConfigs of the existing build-profile.json5\n");
832 fprintf(stdout,
"Template copied successfully\n");
905 fprintf(stdout,
"Customizing template files\n");
908 QString qtAppConstantsPath = options.outputDirectory +
"/entry/src/main/ets/common/QtAppConstants.ets"_L1;
909 QFile qtAppConstantsFile(qtAppConstantsPath);
911 if (qtAppConstantsFile.exists()) {
912 if (!qtAppConstantsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
913 fprintf(stderr,
"Failed to open QtAppConstants.ets for reading\n");
917 QString content = QString::fromUtf8(qtAppConstantsFile.readAll());
918 qtAppConstantsFile.close();
924 appLibName =
"libtst_placeholder.so"_L1;
926 QFileInfo appInfo(options.applicationBinary);
927 appLibName = appInfo.fileName();
930 content.replace(QRegularExpression(
"APP_LIBRARY_NAME = '[^']*'"_L1),
931 "APP_LIBRARY_NAME = '"_L1 + appLibName +
"'"_L1);
933 if (!qtAppConstantsFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
934 fprintf(stderr,
"Failed to open QtAppConstants.ets for writing\n");
938 qtAppConstantsFile.write(content.toUtf8());
939 qtAppConstantsFile.close();
942 fprintf(stdout,
" Updated QtAppConstants.ets with app name: %s\n", qPrintable(appLibName));
952 QString iconValue = options.harmonyOsAppIcon;
953 if (!iconValue.isEmpty() && !iconValue.startsWith(
"$media:"_L1)) {
954 QFileInfo iconInfo(iconValue);
955 if (!iconInfo.exists() || !iconInfo.isFile()) {
956 fprintf(stderr,
"App icon does not exist: %s\n", qPrintable(iconValue));
959 QString safeStem = iconInfo.completeBaseName();
960 for (QChar &c : safeStem) {
961 if (!c.isLetterOrNumber() && c != QLatin1Char(
'_'))
962 c = QLatin1Char(
'_');
964 const QString destFileName = iconInfo.suffix().isEmpty()
966 : safeStem +
"."_L1 + iconInfo.suffix();
967 const QStringList destDirs = {
968 options.outputDirectory +
"/AppScope/resources/base/media"_L1,
969 options.outputDirectory +
"/entry/src/main/resources/base/media"_L1,
971 for (
const QString &destDir : destDirs) {
972 QDir().mkpath(destDir);
973 const QString destPath = destDir +
"/"_L1 + destFileName;
974 if (!copyFileIfNewer(iconValue, destPath, options.verbose)) {
975 fprintf(stderr,
"Failed to copy app icon to: %s\n", qPrintable(destPath));
979 iconValue =
"$media:"_L1 + safeStem;
986 QString appJsonPath = options.outputDirectory +
"/AppScope/app.json5"_L1;
987 QFile appJsonFile(appJsonPath);
989 if (appJsonFile.exists()) {
990 if (!appJsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
991 fprintf(stderr,
"Failed to open app.json5 for reading\n");
994 QString content = QString::fromUtf8(appJsonFile.readAll());
997 auto replaceStringField =
998 [&content](QLatin1StringView key,
const QString &value) {
1002 QRegularExpression(
"\""_L1 + key +
"\":\\s*\"[^\"]*\""_L1),
1003 "\""_L1 + key +
"\": \""_L1 + jsonStringEscape(value) +
"\""_L1);
1006 replaceStringField(
"bundleName"_L1, options.harmonyOsAppBundleName);
1007 replaceStringField(
"vendor"_L1, options.harmonyOsAppVendor);
1008 replaceStringField(
"versionName"_L1, options.harmonyOsAppVersionName);
1015 if (options.harmonyOsAppLabel.startsWith(
"$string:"_L1))
1016 replaceStringField(
"label"_L1, options.harmonyOsAppLabel);
1017 replaceStringField(
"icon"_L1, iconValue);
1021 QRegularExpression(
"\"versionCode\":\\s*\\d+"_L1),
1022 "\"versionCode\": "_L1 + QString::number(options.harmonyOsAppVersionCode));
1025 if (!appJsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
1026 fprintf(stderr,
"Failed to open app.json5 for writing\n");
1029 appJsonFile.write(content.toUtf8());
1030 appJsonFile.close();
1033 fprintf(stdout,
" Updated app.json5 (bundle: %s)\n",
1034 qPrintable(options.harmonyOsAppBundleName));
1042 const QString displayLabel =
1043 (!options.harmonyOsAppLabel.isEmpty()
1044 && !options.harmonyOsAppLabel.startsWith(
"$string:"_L1))
1045 ? options.harmonyOsAppLabel
1046 : options.harmonyOsAppName;
1051 QJsonArray transformedPermissions;
1052 QList<PromotedReason> promotedReasons;
1053 QSet<QString> seenPromotedIds;
1054 for (
const QJsonValue &value : std::as_const(options.permissions)) {
1055 if (!value.isObject()) {
1056 transformedPermissions.append(value);
1059 QJsonObject entry = value.toObject();
1060 if (entry.contains(
"reason"_L1)) {
1061 const QString reason = entry[
"reason"_L1].toString();
1062 if (reasonNeedsPromotion(reason)) {
1063 const QString permName = entry[
"name"_L1].toString();
1064 const QString stringId = synthesizePermissionReasonId(permName);
1065 entry[
"reason"_L1] = QString(
"$string:"_L1 + stringId);
1066 if (!seenPromotedIds.contains(stringId)) {
1067 promotedReasons.append({stringId, reason});
1068 seenPromotedIds.insert(stringId);
1072 transformedPermissions.append(entry);
1078 QStringList locales = QStringList() <<
"base"_L1 <<
"en_US"_L1 <<
"zh_CN"_L1;
1080 for (
const QString &locale : locales) {
1081 QString stringJsonPath = options.outputDirectory +
"/entry/src/main/resources/"_L1 +
1082 locale +
"/element/string.json"_L1;
1083 QFile stringJsonFile(stringJsonPath);
1085 if (!stringJsonFile.exists())
1088 if (!stringJsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
1089 fprintf(stderr,
"Failed to open %s string.json for reading\n", qPrintable(locale));
1093 const QByteArray bytes = stringJsonFile.readAll();
1094 stringJsonFile.close();
1096 QJsonParseError parseErr;
1097 QJsonDocument doc = QJsonDocument::fromJson(bytes, &parseErr);
1098 if (parseErr.error != QJsonParseError::NoError || !doc.isObject()) {
1099 fprintf(stderr,
"Failed to parse %s string.json: %s\n",
1100 qPrintable(locale), qPrintable(parseErr.errorString()));
1103 QJsonObject root = doc.object();
1104 QJsonArray strings = root[
"string"_L1].toArray();
1107 QSet<QString> existingNames;
1108 for (qsizetype i = 0; i < strings.size(); ++i) {
1109 QJsonObject e = strings[i].toObject();
1110 const QString name = e[
"name"_L1].toString();
1111 existingNames.insert(name);
1112 if (name ==
"QAbility_label"_L1) {
1113 e[
"value"_L1] = displayLabel;
1119 for (
const PromotedReason &p : std::as_const(promotedReasons)) {
1120 if (existingNames.contains(p.id))
1123 e[
"name"_L1] = p.id;
1124 e[
"value"_L1] = p.value;
1126 existingNames.insert(p.id);
1128 root[
"string"_L1] = strings;
1129 doc.setObject(root);
1131 if (!stringJsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
1132 fprintf(stderr,
"Failed to open %s string.json for writing\n", qPrintable(locale));
1136 stringJsonFile.write(doc.toJson(QJsonDocument::Indented));
1137 stringJsonFile.close();
1139 if (options.verbose) {
1141 " Updated %s string.json (label: %s, +%lld promoted permission reasons)\n",
1142 qPrintable(locale), qPrintable(displayLabel),
1143 static_cast<
long long>(promotedReasons.size()));
1148 QString appScopeStringPath = options.outputDirectory +
"/AppScope/resources/base/element/string.json"_L1;
1149 QFile appScopeStringFile(appScopeStringPath);
1151 if (appScopeStringFile.exists()) {
1152 if (!appScopeStringFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
1153 fprintf(stderr,
"Failed to open AppScope string.json for reading\n");
1157 QString content = QString::fromUtf8(appScopeStringFile.readAll());
1158 appScopeStringFile.close();
1161 QRegularExpression appNameRegex(
"(\"name\":\\s*\"app_name\"[^}]*\"value\":\\s*)\"[^\"]*\""_L1);
1162 content.replace(appNameRegex,
"\\1\""_L1 + jsonStringEscape(displayLabel) +
"\""_L1);
1164 if (!appScopeStringFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
1165 fprintf(stderr,
"Failed to open AppScope string.json for writing\n");
1169 appScopeStringFile.write(content.toUtf8());
1170 appScopeStringFile.close();
1173 fprintf(stdout,
" Updated AppScope string.json with app name: %s\n",
1174 qPrintable(displayLabel));
1179 QString moduleJsonPath = options.outputDirectory +
"/entry/src/main/module.json5"_L1;
1180 QFile moduleJsonFile(moduleJsonPath);
1182 if (moduleJsonFile.exists()) {
1183 if (!moduleJsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
1184 fprintf(stderr,
"Failed to open module.json5 for reading\n");
1188 QString content = QString::fromUtf8(moduleJsonFile.readAll());
1189 moduleJsonFile.close();
1195 const QString descriptionSentinel =
"%%INSERT_MODULE_DESCRIPTION%%"_L1;
1196 const QString descriptionValue = options.harmonyOsModuleDescription.isEmpty()
1197 ?
"$string:module_desc"_L1
1198 : jsonStringEscape(options.harmonyOsModuleDescription);
1199 content.replace(descriptionSentinel, descriptionValue);
1202 const QString deviceTypesSentinel =
"/* %%INSERT_DEVICE_TYPES%% */"_L1;
1203 QStringList deviceTypes = options.harmonyOsModuleDeviceTypes;
1204 if (deviceTypes.isEmpty())
1205 deviceTypes = QStringList{
"phone"_L1,
"tablet"_L1,
"2in1"_L1 };
1206 QStringList quotedDeviceTypes;
1207 quotedDeviceTypes.reserve(deviceTypes.size());
1208 for (
const QString &dt : std::as_const(deviceTypes))
1209 quotedDeviceTypes.append(
"\""_L1 + dt +
"\""_L1);
1210 content.replace(deviceTypesSentinel, quotedDeviceTypes.join(
", "_L1));
1219 const QString orientationSentinelLine =
1220 " /* %%INSERT_ABILITY_ORIENTATION%% */\n"_L1;
1221 QString orientationReplacement;
1222 if (!options.harmonyOsAbilityOrientation.isEmpty()) {
1223 if (isValidHarmonyOsAbilityOrientation(options.harmonyOsAbilityOrientation)) {
1224 orientationReplacement =
" \"orientation\": \""_L1
1225 + options.harmonyOsAbilityOrientation
1229 "Warning: Ignoring unknown harmonyos-ability-orientation value '%s'\n",
1230 qPrintable(options.harmonyOsAbilityOrientation));
1233 content.replace(orientationSentinelLine, orientationReplacement);
1239 if (!iconValue.isEmpty()) {
1241 QRegularExpression(
"\"icon\":\\s*\"\\$media:layered_image\""_L1),
1242 "\"icon\": \""_L1 + iconValue +
"\""_L1);
1249 const QString sentinel =
"/* %%INSERT_PERMISSIONS%% */"_L1;
1250 QString permissionsFragment;
1251 if (!transformedPermissions.isEmpty()) {
1252 QStringList entryStrings;
1253 entryStrings.reserve(transformedPermissions.size());
1254 for (
const QJsonValue &value : std::as_const(transformedPermissions)) {
1255 if (!value.isObject())
1257 const QJsonObject entry = value.toObject();
1261 const QByteArray pretty =
1262 QJsonDocument(entry).toJson(QJsonDocument::Indented).trimmed();
1263 const QStringList lines = QString::fromUtf8(pretty).split(QLatin1Char(
'\n'));
1264 QStringList indented;
1265 indented.reserve(lines.size());
1266 for (
const QString &line : lines)
1267 indented.append(
" "_L1 + line);
1268 entryStrings.append(indented.join(QLatin1Char(
'\n')));
1270 if (!entryStrings.isEmpty()) {
1271 permissionsFragment =
"\n"_L1 + entryStrings.join(
",\n"_L1) +
"\n "_L1;
1274 content.replace(sentinel, permissionsFragment);
1276 if (!moduleJsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
1277 fprintf(stderr,
"Failed to open module.json5 for writing\n");
1281 moduleJsonFile.write(content.toUtf8());
1282 moduleJsonFile.close();
1285 fprintf(stdout,
" Updated module.json5 description\n");
1286 fprintf(stdout,
" Injected %lld permissions into module.json5\n",
1287 static_cast<
long long>(transformedPermissions.size()));
1300 const bool anySdkVersionSet =
1301 !options.harmonyOsCompatibleSdkVersion.isEmpty()
1302 || !options.harmonyOsTargetSdkVersion.isEmpty()
1303 || !options.harmonyOsCompileSdkVersion.isEmpty();
1305 const QString buildProfilePath =
1306 options.outputDirectory +
"/build-profile.json5"_L1;
1307 QFile buildProfileFile(buildProfilePath);
1308 if (anySdkVersionSet && buildProfileFile.exists()) {
1309 if (!buildProfileFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
1310 fprintf(stderr,
"Failed to open build-profile.json5 for reading\n");
1313 QString content = QString::fromUtf8(buildProfileFile.readAll());
1314 buildProfileFile.close();
1316 if (!options.harmonyOsCompatibleSdkVersion.isEmpty()) {
1319 "\"compatibleSdkVersion\":\\s*\"[^\"]*\""_L1),
1320 "\"compatibleSdkVersion\": \""_L1
1321 + jsonStringEscape(options.harmonyOsCompatibleSdkVersion)
1325 const QString targetSentinel =
"/* %%INSERT_TARGET_SDK_VERSION%% */"_L1;
1326 const QString targetReplacement =
1327 options.harmonyOsTargetSdkVersion.isEmpty()
1329 :
"\"targetSdkVersion\": \""_L1
1330 + jsonStringEscape(options.harmonyOsTargetSdkVersion)
1332 content.replace(targetSentinel, targetReplacement);
1334 const QString compileSentinel =
"/* %%INSERT_COMPILE_SDK_VERSION%% */"_L1;
1335 const QString compileReplacement =
1336 options.harmonyOsCompileSdkVersion.isEmpty()
1338 :
"\"compileSdkVersion\": \""_L1
1339 + jsonStringEscape(options.harmonyOsCompileSdkVersion)
1341 content.replace(compileSentinel, compileReplacement);
1343 if (!buildProfileFile.open(
1344 QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
1345 fprintf(stderr,
"Failed to open build-profile.json5 for writing\n");
1348 buildProfileFile.write(content.toUtf8());
1349 buildProfileFile.close();
1353 " Updated build-profile.json5 SDK versions"
1354 " (compatible=%s, target=%s, compile=%s)\n",
1355 qPrintable(options.harmonyOsCompatibleSdkVersion),
1356 qPrintable(options.harmonyOsTargetSdkVersion),
1357 qPrintable(options.harmonyOsCompileSdkVersion));
1363 fprintf(stdout,
"Template customization completed\n");
1570 if (options.testBinariesDirectory.isEmpty()) {
1571 fprintf(stderr,
"Error: 'test-binaries-directory' not specified for test bundle mode\n");
1575 if (!QDir(options.testBinariesDirectory).exists()) {
1576 fprintf(stderr,
"Error: test-binaries-directory does not exist: %s\n",
1577 qPrintable(options.testBinariesDirectory));
1582 fprintf(stdout,
"Scanning for test binaries in %s\n", qPrintable(options.testBinariesDirectory));
1585 QStringList foundHelpers;
1586 QSet<QString> helperNames;
1590 const QStringList excludeDirs = { QFileInfo(options.outputDirectory).absoluteFilePath() };
1591 scanTestBinariesDir(options.testBinariesDirectory, options.testExcludeList, excludeDirs,
1592 found, foundHelpers, helperNames);
1594 if (found.isEmpty()) {
1595 fprintf(stderr,
"Warning: No test binaries (libtst_*.so) found in %s\n",
1596 qPrintable(options.testBinariesDirectory));
1601 fprintf(stdout,
"Found %lld test binaries\n",
static_cast<
long long>(found.size()));
1602 if (!foundHelpers.isEmpty())
1603 fprintf(stdout,
"Found %lld test helper libraries\n",
1604 static_cast<
long long>(foundHelpers.size()));
1608 for (
const QString &arch : options.targetArchs) {
1609 QString archLibPath = options.outputDirectory +
"/entry/libs/"_L1 + arch;
1610 QDir().mkpath(archLibPath);
1612 for (
const QString &testBinary : found) {
1613 QFileInfo testInfo(testBinary);
1614 QString destPath = archLibPath +
"/"_L1 + testInfo.fileName();
1616 if (options.verbose)
1617 fprintf(stdout,
" Copying test binary: %s\n", qPrintable(testInfo.fileName()));
1619 if (!copyFileIfNewer(testBinary, destPath, options.verbose)) {
1620 fprintf(stderr,
"Failed to copy test binary: %s\n", qPrintable(testBinary));
1625 for (
const QString &helperLib : foundHelpers) {
1626 QFileInfo helperInfo(helperLib);
1627 QString destPath = archLibPath +
"/"_L1 + helperInfo.fileName();
1629 if (options.verbose)
1630 fprintf(stdout,
" Copying test helper lib: %s\n", qPrintable(helperInfo.fileName()));
1632 if (!copyFileIfNewer(helperLib, destPath, options.verbose)) {
1633 fprintf(stderr,
"Failed to copy test helper lib: %s\n", qPrintable(helperLib));
1640 for (
const QString &testBinary : found) {
1641 QString fileName = QFileInfo(testBinary).fileName();
1642 if (!bundledBinaries.contains(fileName))
1643 bundledBinaries.append(fileName);
1647 if (!options.depFilePath.isEmpty()) {
1648 for (
const QString &testBinary : found)
1649 dependenciesForDepfile << testBinary;
1650 for (
const QString &helperLib : foundHelpers)
1651 dependenciesForDepfile << helperLib;
1689 if (options.qtLibsDirectory.isEmpty())
1692 if (!QDir(options.qtLibsDirectory).exists()) {
1694 fprintf(stdout,
"Qt libs directory not found, skipping: %s\n",
1695 qPrintable(options.qtLibsDirectory));
1701 fprintf(stdout,
"Copying all Qt libraries from %s\n", qPrintable(options.qtLibsDirectory));
1703 QDir libsDir(options.qtLibsDirectory);
1704 const QFileInfoList entries = libsDir.entryInfoList({
"*.so"_L1,
"*.so.*"_L1}, QDir::Files);
1706 for (
const QFileInfo &entry : entries) {
1707 for (
const QString &arch : options.targetArchs) {
1708 QString destPath = options.outputDirectory +
"/entry/libs/"_L1 + arch +
"/"_L1 + entry.fileName();
1709 QDir().mkpath(QFileInfo(destPath).absolutePath());
1710 if (!copyFileIfNewer(entry.filePath(), destPath, options.verbose))
1713 if (!options.depFilePath.isEmpty())
1714 dependenciesForDepfile << entry.filePath();
1718 for (
const QString &arch : options.targetArchs) {
1719 QString stdCppPath = findStdCppLibrary(options, arch);
1720 if (!stdCppPath.isEmpty()) {
1721 QString destPath = options.outputDirectory +
"/entry/libs/"_L1 + arch +
"/libc++_shared.so"_L1;
1722 if (options.verbose)
1723 fprintf(stdout,
" Copying C++ standard library for %s\n", qPrintable(arch));
1724 if (!copyFileIfNewer(stdCppPath, destPath, options.verbose))
1726 if (!options.depFilePath.isEmpty())
1727 dependenciesForDepfile << stdCppPath;
1732 for (
const QString &extraDir : options.extraLibsDirs) {
1734 if (!dir.exists()) {
1735 if (options.verbose)
1736 fprintf(stdout,
"Extra libs dir not found, skipping: %s\n", qPrintable(extraDir));
1740 if (options.verbose)
1741 fprintf(stdout,
"Copying extra libraries from %s\n", qPrintable(extraDir));
1743 const QFileInfoList entries = dir.entryInfoList({
"*.so"_L1,
"*.so.*"_L1}, QDir::Files);
1744 for (
const QFileInfo &entry : entries) {
1748 const QString soname = readElfSoname(options, entry.filePath());
1749 const QString deployName = (!soname.isEmpty() && soname != entry.fileName())
1750 ? soname : entry.fileName();
1751 for (
const QString &arch : options.targetArchs) {
1752 QString destPath = options.outputDirectory +
"/entry/libs/"_L1 + arch +
"/"_L1 + deployName;
1753 QDir().mkpath(QFileInfo(destPath).absolutePath());
1754 if (!copyFileIfNewer(entry.filePath(), destPath, options.verbose))
1757 if (!options.depFilePath.isEmpty())
1758 dependenciesForDepfile << entry.filePath();
2460 fprintf(stdout,
"Detecting Qt library dependencies\n");
2463 QStringList toProcess;
2464 toProcess.append(options.applicationBinary);
2468 for (
const QString &projectLib : options.projectLibraries)
2469 toProcess.append(projectLib);
2471 QList<QtDependency> qtDependencies;
2472 QStringList detectedQtModules;
2473 bool needsStdCpp =
false;
2475 while (!toProcess.isEmpty()) {
2476 QString currentLib = toProcess.takeFirst();
2478 if (processedLibs.contains(currentLib))
2481 processedLibs.insert(currentLib);
2484 fprintf(stdout,
" Analyzing: %s\n", qPrintable(QFileInfo(currentLib).fileName()));
2486 QStringList deps = readElfDependencies(options, currentLib);
2488 for (
const QString &dep : deps) {
2490 if (dep ==
"libc++_shared.so"_L1) {
2496 if (isSystemLibrary(dep))
2500 if (!dep.startsWith(
"libQt6"_L1) && !dep.startsWith(
"libqohos"_L1)) {
2502 QString extraDepPath = findExtraDepLibrary(options, dep);
2503 if (extraDepPath.isEmpty())
2508 if (!processedLibs.contains(extraDepPath) && !toProcess.contains(extraDepPath)) {
2509 if (options.verbose)
2510 fprintf(stdout,
" Found extra dep: %s\n", qPrintable(dep));
2511 QtDependency extraDep;
2512 extraDep.relativePath =
"lib/"_L1 + dep;
2513 extraDep.absolutePath = extraDepPath;
2514 qtDependencies.append(extraDep);
2515 toProcess.append(extraDepPath);
2521 if (dep.startsWith(
"libQt6"_L1)) {
2522 QString moduleName = dep.mid(6);
2523 if (moduleName.endsWith(
".so"_L1))
2525 if (!moduleName.isEmpty() && !detectedQtModules.contains(moduleName))
2526 detectedQtModules.append(moduleName);
2529 QString depPath = findQtLibrary(options, dep);
2530 if (depPath.isEmpty()) {
2531 if (options.verbose)
2532 fprintf(stdout,
" Warning: Could not find Qt library: %s\n", qPrintable(dep));
2536 if (processedLibs.contains(depPath))
2539 if (options.verbose)
2540 fprintf(stdout,
" Found dependency: %s\n", qPrintable(dep));
2543 qtDep.relativePath =
"lib/"_L1 + dep;
2544 qtDep.absolutePath = depPath;
2545 qtDependencies.append(std::move(qtDep));
2548 toProcess.append(std::move(depPath));
2553 fprintf(stdout,
"Found %lld Qt library dependencies\n",
static_cast<
long long>(qtDependencies.size()));
2555 fprintf(stdout,
"C++ standard library required\n");
2560 for (
const QString &arch : options.targetArchs) {
2561 QString archLibPath = options.outputDirectory +
"/entry/libs/"_L1 + arch;
2562 QString stdCppPath = findStdCppLibrary(options, arch);
2563 if (stdCppPath.isEmpty()) {
2564 fprintf(stderr,
"Warning: Could not find C++ standard library for %s\n", qPrintable(arch));
2566 QString destPath = archLibPath +
"/libc++_shared.so"_L1;
2567 if (options.verbose)
2568 fprintf(stdout,
" Copying C++ standard library for %s\n", qPrintable(arch));
2569 if (!copyFileIfNewer(stdCppPath, destPath, options.verbose)) {
2571 "Failed to copy C++ standard library to: %s\n",
2572 qPrintable(destPath));
2577 if (!options.depFilePath.isEmpty())
2578 dependenciesForDepfile << stdCppPath;
2587 for (
const QtDependency &dep : qtDependencies) {
2588 QFileInfo libInfo(dep.relativePath);
2589 if (!copyFileToArchitectures(options, dep.absolutePath, libInfo.fileName(),
false))
2592 if (!options.depFilePath.isEmpty())
2593 dependenciesForDepfile << dep.absolutePath;
2824 QList<QmlImportInfo> imports;
2826 if (options.qmlRootPaths.isEmpty()) {
2829 "No QML root path specified, skipping QML import scanning\n");
2834 fprintf(stdout,
"Scanning for QML imports\n");
2837 QStringList searchPaths;
2838 if (!options.qtLibExecsDirectory.isEmpty())
2839 searchPaths.append(options.qtLibExecsDirectory);
2842 if (!options.qtHostDirectory.isEmpty())
2843 searchPaths.append(options.qtHostDirectory +
"/bin"_L1);
2845 QString qmlImportScannerPath =
2846 QStandardPaths::findExecutable(
"qmlimportscanner"_L1, searchPaths);
2849 if (qmlImportScannerPath.isEmpty()) {
2850 QDir dir(QFileInfo(options.applicationBinary).absolutePath());
2852 for (
int i = 0; i < 10; ++i) {
2853 qmlImportScannerPath = QStandardPaths::findExecutable(
2854 "qmlimportscanner"_L1,
2855 {dir.absoluteFilePath(
"libexec"_L1), dir.absoluteFilePath(
"bin"_L1)});
2857 if (!qmlImportScannerPath.isEmpty())
2865 if (qmlImportScannerPath.isEmpty()) {
2868 "Warning: qmlimportscanner not found, skipping QML import scanning\n");
2873 fprintf(stdout,
" Using qmlimportscanner: %s\n",
2874 qPrintable(qmlImportScannerPath));
2877 QStringList importPaths;
2880 for (
const QString &path : options.qmlImportPaths)
2881 if (QFile::exists(path))
2882 importPaths.append(path);
2887 QFileInfo appBinary(options.applicationBinary);
2888 QString appBuildDir = appBinary.absolutePath();
2889 if (QDir(appBuildDir).exists())
2890 importPaths.append(appBuildDir);
2893 if (!options.qtQmlDirectory.isEmpty() &&
2894 QDir(options.qtQmlDirectory).exists()) {
2895 importPaths.append(options.qtQmlDirectory);
2898 QFileInfo appBinary(options.applicationBinary);
2899 QDir qtDir(appBinary.absolutePath());
2900 for (
int i = 0; i < 10; ++i) {
2901 QString qmlDir = qtDir.absoluteFilePath(
"qml"_L1);
2902 if (QDir(qmlDir).exists()) {
2903 importPaths.append(qmlDir);
2907 qmlDir = qtDir.absoluteFilePath(
"qtbase/../qml"_L1);
2908 if (QDir(qmlDir).exists()) {
2909 importPaths.append(QDir::cleanPath(qmlDir));
2917 if (importPaths.isEmpty()) {
2918 fprintf(stderr,
"Warning: No QML import paths found\n");
2924 QStringList arguments;
2925 for (
const QString &rootPath : options.qmlRootPaths)
2926 arguments <<
"-rootPath"_L1 << rootPath;
2928 for (
const QString &importPath : importPaths)
2929 arguments <<
"-importPath"_L1 << importPath;
2932 fprintf(stdout,
" Root paths:\n");
2933 for (
const QString &rootPath : options.qmlRootPaths)
2934 fprintf(stdout,
" %s\n", qPrintable(rootPath));
2935 fprintf(stdout,
" Import paths:\n");
2936 for (
const QString &path : importPaths)
2937 fprintf(stdout,
" %s\n", qPrintable(path));
2942 process.start(qmlImportScannerPath, arguments);
2944 if (!process.waitForFinished(30000)) {
2946 process.waitForFinished(2000);
2947 fprintf(stderr,
"Error: qmlimportscanner timed out after 30 seconds\n");
2948 fprintf(stderr,
" Command: %s %s\n", qPrintable(qmlImportScannerPath),
2949 qPrintable(arguments.join(u' ')));
2950 const QByteArray errorOutput = process.readAllStandardError();
2951 if (!errorOutput.isEmpty())
2952 fprintf(stderr,
"%s\n", errorOutput.constData());
2958 if (process.exitStatus() != QProcess::NormalExit) {
2959 fprintf(stderr,
"Error: qmlimportscanner crashed\n");
2960 fprintf(stderr,
"%s\n", process.readAllStandardError().constData());
2966 if (process.exitCode() != 0) {
2967 fprintf(stderr,
"Error: qmlimportscanner failed with exit code %d\n",
2968 process.exitCode());
2969 fprintf(stderr,
"%s\n", process.readAllStandardError().constData());
2976 QByteArray output = process.readAllStandardOutput();
2977 QJsonDocument doc = QJsonDocument::fromJson(output);
2979 if (!doc.isArray()) {
2980 fprintf(stderr,
"Error: Invalid JSON output from qmlimportscanner\n");
2986 QJsonArray array = doc.array();
2987 for (
const QJsonValue &value : array) {
2988 if (!value.isObject())
2991 QJsonObject obj = value.toObject();
2993 info.name = obj[
"name"_L1].toString();
2994 info.path = obj[
"path"_L1].toString();
2995 info.type = obj[
"type"_L1].toString();
2997 if (obj.contains(
"plugin"_L1))
2998 info.plugin = obj[
"plugin"_L1].toString();
3000 if (obj.contains(
"pluginIsOptional"_L1))
3001 info.pluginIsOptional = obj[
"pluginIsOptional"_L1].toBool();
3003 if (obj.contains(
"prefer"_L1))
3004 info.prefer = obj[
"prefer"_L1].toString();
3007 if (obj.contains(
"components"_L1)) {
3008 QJsonArray componentsArray = obj[
"components"_L1].toArray();
3009 for (
const QJsonValue &comp : componentsArray)
3010 info.components.append(comp.toString());
3014 if (obj.contains(
"scripts"_L1)) {
3015 QJsonArray scriptsArray = obj[
"scripts"_L1].toArray();
3016 for (
const QJsonValue &script : scriptsArray)
3017 info.scripts.append(script.toString());
3021 if (info.path.isEmpty()) {
3022 if (options.verbose)
3023 fprintf(stdout,
" Warning: Could not resolve QML import: %s\n",
3024 qPrintable(info.name));
3029 if (info.type !=
"module"_L1)
3032 if (options.verbose)
3033 fprintf(stdout,
" Found QML import: %s at %s\n", qPrintable(info.name),
3034 qPrintable(info.path));
3036 imports.append(info);
3081 const QList<QmlImportInfo> &imports,
3082 QSet<QString> &processedLibs)
3084 if (imports.isEmpty())
3088 fprintf(stdout,
"Copying QML imports\n");
3092 QString qmlDestBase = hapQmlDir(options);
3093 QDir().mkpath(qmlDestBase);
3096 QStringList qmlPluginsToScan;
3098 for (
const QmlImportInfo &import : imports) {
3099 if (options.verbose)
3100 fprintf(stdout,
" Copying QML module: %s\n", qPrintable(import.name));
3104 QString relativePath;
3107 if (!options.qtQmlDirectory.isEmpty() && import.path.startsWith(options.qtQmlDirectory)) {
3109 relativePath = import.path.mid(options.qtQmlDirectory.length());
3110 if (relativePath.startsWith(
'/'_L1))
3111 relativePath = relativePath.mid(1);
3115 relativePath = import.name;
3116 relativePath.replace(
'.'_L1,
'/'_L1);
3119 QString destModuleDir = qmlDestBase +
"/"_L1 + relativePath;
3120 QDir().mkpath(destModuleDir);
3123 QString qmldirSrc = import.path +
"/qmldir"_L1;
3124 QString qmldirDest = destModuleDir +
"/qmldir"_L1;
3125 if (QFile::exists(qmldirSrc)) {
3126 if (copyFileIfNewer(qmldirSrc, qmldirDest, options.verbose)) {
3127 if (options.verbose)
3128 fprintf(stdout,
" Copied qmldir\n");
3131 if (!options.depFilePath.isEmpty())
3132 dependenciesForDepfile << qmldirSrc;
3134 fprintf(stderr,
"Warning: Failed to copy qmldir for %s\n",
3135 qPrintable(import.name));
3142 bool qmlFilesAreEmbedded = import.prefer.startsWith(
":/"_L1);
3144 if (!import.plugin.isEmpty()) {
3146 QString pluginFileName =
"lib"_L1 + import.plugin +
".so"_L1;
3147 QString pluginSrc = import.path +
"/"_L1 + pluginFileName;
3149 if (QFile::exists(pluginSrc)) {
3151 for (
const QString &arch : options.targetArchs) {
3152 QString pluginDest = options.outputDirectory +
"/entry/libs/"_L1 + arch
3153 +
"/"_L1 + pluginFileName;
3155 if (copyFileIfNewer(pluginSrc, pluginDest, options.verbose)) {
3156 if (options.verbose)
3157 fprintf(stdout,
" Copied plugin to libs/%s: %s\n",
3158 qPrintable(arch), qPrintable(pluginFileName));
3159 processedLibs.insert(pluginSrc);
3162 if (!qmlPluginsToScan.contains(pluginSrc))
3163 qmlPluginsToScan.append(pluginSrc);
3166 if (!options.depFilePath.isEmpty())
3167 dependenciesForDepfile << pluginSrc;
3168 }
else if (!import.pluginIsOptional) {
3169 fprintf(stderr,
"Warning: Failed to copy required plugin: %s\n",
3170 qPrintable(pluginFileName));
3173 }
else if (!import.pluginIsOptional) {
3174 if (options.verbose)
3175 fprintf(stdout,
" Warning: Required plugin not found: %s\n",
3176 qPrintable(pluginFileName));
3181 if (!qmlFilesAreEmbedded) {
3182 for (
const QString &component : import.components) {
3183 QFileInfo compInfo(component);
3184 if (!compInfo.exists()) {
3185 if (options.verbose)
3186 fprintf(stdout,
" Warning: Component file not found: %s\n",
3187 qPrintable(component));
3191 QString relativePath = component.mid(import.path.length());
3192 if (relativePath.startsWith(
'/'_L1))
3193 relativePath = relativePath.mid(1);
3195 QString destFile = destModuleDir +
"/"_L1 + relativePath;
3196 QFileInfo destInfo(destFile);
3197 QDir().mkpath(destInfo.absolutePath());
3199 if (copyFileIfNewer(component, destFile, options.verbose)) {
3200 if (options.verbose)
3201 fprintf(stdout,
" Copied component: %s\n", qPrintable(compInfo.fileName()));
3204 if (!options.depFilePath.isEmpty())
3205 dependenciesForDepfile << component;
3210 for (
const QString &script : import.scripts) {
3211 QFileInfo scriptInfo(script);
3212 if (!scriptInfo.exists())
3215 QString relativePath = script.mid(import.path.length());
3216 if (relativePath.startsWith(
'/'_L1))
3217 relativePath = relativePath.mid(1);
3219 QString destFile = destModuleDir +
"/"_L1 + relativePath;
3220 QFileInfo destInfo(destFile);
3221 QDir().mkpath(destInfo.absolutePath());
3223 if (copyFileIfNewer(script, destFile, options.verbose)) {
3224 if (options.verbose)
3225 fprintf(stdout,
" Copied script: %s\n", qPrintable(scriptInfo.fileName()));
3228 if (!options.depFilePath.isEmpty())
3229 dependenciesForDepfile << script;
3233 if (options.verbose)
3234 fprintf(stdout,
" Skipping QML files (embedded in resources)\n");
3239 if (!qmlPluginsToScan.isEmpty()) {
3241 fprintf(stdout,
"Scanning QML plugin dependencies\n");
3243 QStringList toProcess = qmlPluginsToScan;
3244 while (!toProcess.isEmpty()) {
3245 QString pluginPath = toProcess.takeFirst();
3248 fprintf(stdout,
" Scanning plugin: %s\n", qPrintable(QFileInfo(pluginPath).fileName()));
3250 QStringList deps = readElfDependencies(options, pluginPath);
3251 for (
const QString &dep : deps) {
3253 if (isSystemLibrary(dep))
3258 if (dep.startsWith(
"libQt6"_L1)) {
3259 depPath = findQtLibrary(options, dep);
3260 if (depPath.isEmpty()) {
3261 if (options.verbose)
3262 fprintf(stdout,
" Warning: Could not find Qt library: %s\n", qPrintable(dep));
3265 }
else if (!options.extraLibsDirs.isEmpty()) {
3266 depPath = findExtraDepLibrary(options, dep);
3267 if (depPath.isEmpty())
3273 if (processedLibs.contains(depPath))
3276 if (options.verbose)
3277 fprintf(stdout,
" Found dependency: %s\n", qPrintable(dep));
3280 for (
const QString &arch : options.targetArchs) {
3281 QString destPath = options.outputDirectory +
"/entry/libs/"_L1 + arch +
"/"_L1 + dep;
3282 if (copyFileIfNewer(depPath, destPath, options.verbose)) {
3283 if (options.verbose)
3284 fprintf(stdout,
" Copied %s to libs/%s\n", qPrintable(dep), qPrintable(arch));
3287 if (!options.depFilePath.isEmpty())
3288 dependenciesForDepfile << depPath;
3292 processedLibs.insert(depPath);
3294 toProcess.append(depPath);
3300 fprintf(stdout,
"QML imports copied successfully\n");
3328 const char *envName;
3329 const char *cliFlag;
3333 QString resolved()
const
3335 if (!cliValue.isEmpty())
3339 QString label()
const
3341 return QString::fromLatin1(cliFlag) +
" / "_L1 + QString::fromLatin1(envName);
3345 Field required[] = {
3346 {
"QT_HARMONYOS_SIGNING_CERT_PATH",
"--signing-cert-path", options.signingCertPath,
3347 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_CERT_PATH") },
3348 {
"QT_HARMONYOS_SIGNING_PROFILE",
"--signing-profile", options.signingProfile,
3349 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_PROFILE") },
3350 {
"QT_HARMONYOS_SIGNING_STORE_FILE",
"--signing-store-file", options.signingStoreFile,
3351 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_STORE_FILE") },
3352 {
"QT_HARMONYOS_SIGNING_KEY_ALIAS",
"--signing-key-alias", options.signingKeyAlias,
3353 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_KEY_ALIAS") },
3357 Field passwords[][2] = {
3358 { {
"QT_HARMONYOS_SIGNING_KEY_PASSWORD",
"--signing-key-password",
3359 options.signingKeyPassword, qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_KEY_PASSWORD") },
3360 {
"QT_HARMONYOS_SIGNING_PLAIN_KEY_PASSWORD",
"--signing-plain-key-password",
3361 options.signingPlainKeyPassword,
3362 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_PLAIN_KEY_PASSWORD") } },
3363 { {
"QT_HARMONYOS_SIGNING_STORE_PASSWORD",
"--signing-store-password",
3364 options.signingStorePassword,
3365 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_STORE_PASSWORD") },
3366 {
"QT_HARMONYOS_SIGNING_PLAIN_STORE_PASSWORD",
"--signing-plain-store-password",
3367 options.signingPlainStorePassword,
3368 qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_PLAIN_STORE_PASSWORD") } },
3371 QString signAlg = options.signingAlg.isEmpty()
3372 ? qEnvironmentVariable(
"QT_HARMONYOS_SIGNING_ALG")
3373 : options.signingAlg;
3375 bool anySet = !signAlg.isEmpty();
3376 for (
const Field &f : required)
3377 anySet = anySet || !f.resolved().isEmpty();
3378 for (
const auto &forms : passwords)
3379 anySet = anySet || !forms[0].resolved().isEmpty() || !forms[1].resolved().isEmpty();
3383 QStringList missing;
3384 for (
const Field &f : required) {
3385 if (f.resolved().isEmpty())
3386 missing << f.label();
3388 for (
const auto &forms : passwords) {
3389 if (forms[0].resolved().isEmpty() && forms[1].resolved().isEmpty())
3390 missing << forms[0].label() +
" or "_L1 + forms[1].label();
3392 if (!missing.isEmpty()) {
3393 fprintf(stderr,
"Error: HAP signing requested, but the following required input(s)\n"
3394 " are missing (neither CLI flag nor env var was supplied):\n");
3395 for (
const QString &name : missing)
3396 fprintf(stderr,
" %s\n", qPrintable(name));
3400 for (
const auto &forms : passwords) {
3401 if (!forms[0].resolved().isEmpty() && !forms[1].resolved().isEmpty()) {
3402 fprintf(stderr,
"Error: both %s and %s are set; pass only one of them\n",
3403 qPrintable(forms[0].label()), qPrintable(forms[1].label()));
3408 const bool encryptedKeyPassword = !passwords[0][0].resolved().isEmpty();
3409 const bool encryptedStorePassword = !passwords[1][0].resolved().isEmpty();
3410 if (encryptedKeyPassword != encryptedStorePassword) {
3412 "Error: the key password and the keystore password must both be\n"
3413 " hvigor-encrypted or both plain text.\n");
3419 signing->certPath = required[0].resolved();
3420 signing->profile = required[1].resolved();
3421 signing->storeFile = required[2].resolved();
3422 signing->keyAlias = required[3].resolved();
3423 signing->keyPassword = passwords[0][encryptedKeyPassword ? 0 : 1].resolved();
3424 signing->storePassword = passwords[1][encryptedStorePassword ? 0 : 1].resolved();
3425 signing->signAlg = signAlg.isEmpty() ?
"SHA256withECDSA"_L1 : signAlg;
3427 const std::pair<
const char *, QString> files[] = {
3428 { required[0].cliFlag, signing->certPath },
3429 { required[1].cliFlag, signing->profile },
3430 { required[2].cliFlag, signing->storeFile },
3432 for (
const auto &file : files) {
3433 if (!QFile::exists(file.second)) {
3434 fprintf(stderr,
"Error: signing input %s does not exist: %s\n", file.first,
3435 qPrintable(file.second));
3441 fprintf(stdout,
"HAP signing enabled\n");
3442 fprintf(stdout,
" certpath: %s\n", qPrintable(signing->certPath));
3443 fprintf(stdout,
" profile: %s\n", qPrintable(signing->profile));
3444 fprintf(stdout,
" storeFile: %s\n", qPrintable(signing->storeFile));
3445 fprintf(stdout,
" keyAlias: %s\n", qPrintable(signing->keyAlias));
3446 fprintf(stdout,
" signAlg: %s\n", qPrintable(signing->signAlg));
3447 fprintf(stdout,
" passwords: %s\n",
3732 fprintf(stdout,
"Skipping HAP build (--no-build specified)\n");
3737 QString hvigorPath = options.hvigorPath;
3739 if (hvigorPath.isEmpty()) {
3740 QByteArray envHvigor = qgetenv(
"QT_HARMONYOS_HVIGOR");
3741 if (!envHvigor.isEmpty())
3742 hvigorPath = QString::fromLocal8Bit(envHvigor);
3745 if (hvigorPath.isEmpty()) {
3747 QString candidate = options.outputDirectory +
"/hvigorw"_L1;
3748 if (QFile::exists(candidate))
3749 hvigorPath = std::move(candidate);
3752 if (hvigorPath.isEmpty()) {
3753 fprintf(stderr,
"Warning: No hvigor path specified, skipping build\n");
3754 fprintf(stderr,
"Use --hvigor <path> or set QT_HARMONYOS_HVIGOR\n");
3759 fprintf(stdout,
"Building HarmonyOS HAP package\n");
3762 if (!QFile::exists(hvigorPath)) {
3763 fprintf(stderr,
"Error: hvigorw not found at: %s\n", qPrintable(hvigorPath));
3769 QString buildTask =
"assembleHap"_L1;
3770 QString buildMode = options.releaseMode ?
"release"_L1 :
"debug"_L1;
3773 fprintf(stdout,
" Build mode: %s\n", qPrintable(buildMode));
3774 fprintf(stdout,
" Running: %s %s\n", qPrintable(hvigorPath), qPrintable(buildTask));
3779 process.setWorkingDirectory(options.outputDirectory);
3780 process.setProcessChannelMode(QProcess::MergedChannels);
3782 QStringList arguments;
3783 arguments << buildTask <<
"-p"_L1 << (
"buildMode="_L1 + buildMode);
3786 arguments <<
"--no-daemon"_L1;
3788 process.start(hvigorPath, arguments);
3790 if (!process.waitForStarted()) {
3791 fprintf(stderr,
"Failed to start hvigorw\n");
3796 while (process.state() != QProcess::NotRunning) {
3797 if (!process.waitForReadyRead(1000)) {
3799 if (process.state() == QProcess::NotRunning)
3805 QByteArray output = process.readAll();
3806 fprintf(stdout,
"%s", output.constData());
3813 QByteArray output = process.readAll();
3814 if (!output.isEmpty()) {
3815 fprintf(stdout,
"%s", output.constData());
3820 if (process.exitCode() != 0) {
3821 fprintf(stderr,
"hvigorw failed with exit code %d\n", process.exitCode());
3823 fprintf(stderr,
"Run with --verbose to see build output\n");
3828 fprintf(stdout,
"HAP build completed successfully\n");
3831 if (hapOutputPath) {
3832 QString hapSearchPath = options.outputDirectory +
"/entry/build/default/outputs/default"_L1;
3833 QDir hapDir(hapSearchPath);
3835 if (hapDir.exists()) {
3836 QStringList hapFiles = hapDir.entryList(QStringList() <<
"*.hap"_L1, QDir::Files);
3839 for (qsizetype i = 0; i < hapFiles.size(); ++i) {
3840 if (hapFiles.at(i).endsWith(
"-unsigned.hap"_L1)) {
3841 hapFiles.move(i, 0);
3845 if (!hapFiles.isEmpty()) {
3846 *hapOutputPath = hapDir.absoluteFilePath(hapFiles.first());
3848 fprintf(stdout,
" Generated HAP: %s\n", qPrintable(*hapOutputPath));
3874 if (builtHapPath.isEmpty()) {
3875 fprintf(stderr,
"Error: Cannot install - HAP file path not found\n");
3881 QString hapPath = builtHapPath;
3882 if (hapPath.endsWith(
"-unsigned.hap"_L1)) {
3883 QString signedPath = hapPath;
3884 signedPath.replace(signedPath.size() - 13, 13,
"-signed.hap"_L1);
3885 const QFileInfo signedInfo(signedPath);
3886 if (signedInfo.exists()
3887 && signedInfo.lastModified() >= QFileInfo(hapPath).lastModified()) {
3888 hapPath = signedPath;
3893 fprintf(stdout,
"Installing HAP to device\n");
3895 QString hdcPath = qEnvironmentVariable(
"QT_HARMONYOS_HDC");
3896 if (hdcPath.isEmpty())
3897 hdcPath = QStandardPaths::findExecutable(
"hdc"_L1, sdkToolchainsDirs(options));
3898 if (hdcPath.isEmpty())
3902 QProcess checkDevices;
3903 checkDevices.start(hdcPath, QStringList() <<
"list"_L1 <<
"targets"_L1);
3904 if (!checkDevices.waitForFinished(5000)) {
3905 fprintf(stderr,
"Error: Failed to check for connected devices with %s\n",
3906 qPrintable(hdcPath));
3911 QString devicesOutput = QString::fromUtf8(checkDevices.readAllStandardOutput());
3912 if (devicesOutput.contains(
"[Fail]"_L1)) {
3913 fprintf(stderr,
"Error: Failed to list devices:\n%s\n", qPrintable(devicesOutput));
3916 if (devicesOutput.trimmed().isEmpty()
3917 || devicesOutput.contains(
"[Empty]"_L1, Qt::CaseInsensitive)) {
3918 fprintf(stderr,
"Error: No HarmonyOS devices connected\n");
3919 fprintf(stderr,
"Connect a device and ensure USB debugging is enabled\n");
3924 fprintf(stdout,
" Connected devices:\n%s\n", qPrintable(devicesOutput));
3928 fprintf(stdout,
" Uninstalling old version (if exists)\n");
3931 uninstall.start(hdcPath, QStringList() <<
"uninstall"_L1 << options.harmonyOsAppBundleName);
3932 uninstall.waitForFinished(10000);
3937 fprintf(stdout,
" Installing: %s\n", qPrintable(hapPath));
3940 install.setProcessChannelMode(QProcess::MergedChannels);
3942 install.start(hdcPath, QStringList() <<
"install"_L1 << QDir::toNativeSeparators(hapPath));
3944 if (!install.waitForFinished(60000)) {
3945 fprintf(stderr,
"Error: Installation timed out\n");
3949 QString installOutput = QString::fromUtf8(install.readAll());
3951 if (install.exitCode() != 0 || installOutput.contains(
"[Fail]"_L1)
3952 || installOutput.contains(
"failed to install"_L1)) {
3953 fprintf(stderr,
"Error: Installation failed\n");
3954 fprintf(stderr,
"%s\n", qPrintable(installOutput));
3959 fprintf(stdout,
" Installation output:\n%s\n", qPrintable(installOutput));
3963 fprintf(stdout,
" Launching application\n");
3966 launch.setProcessChannelMode(QProcess::MergedChannels);
3967 QStringList launchArgs;
3968 launchArgs <<
"shell"_L1 <<
"aa"_L1 <<
"start"_L1
3969 <<
"-a"_L1 << mainAbilityName(options)
3970 <<
"-b"_L1 << options.harmonyOsAppBundleName;
3972 launch.start(hdcPath, launchArgs);
3973 launch.waitForFinished(5000);
3975 const QString launchOutput = QString::fromUtf8(launch.readAll());
3976 if (launch.exitCode() != 0 || launchOutput.contains(
"[Fail]"_L1)
3977 || launchOutput.contains(
"error:"_L1)) {
3979 fprintf(stderr,
"Warning: Failed to launch application:\n%s\n", qPrintable(launchOutput));
3980 fprintf(stdout,
"Successfully installed application on device\n");
3984 fprintf(stdout,
"Successfully installed and launched application on device\n");
3991 QCoreApplication app(argc, argv);
3992 QCoreApplication::setApplicationName(
"harmonydeployqt"_L1);
3993 QCoreApplication::setApplicationVersion(
"1.0"_L1);
3996 options.timer.start();
3998 if (!parseCommandLine(app.arguments(), &options))
4002 fprintf(stdout,
"Qt HarmonyOS Deployment Tool\n");
4003 fprintf(stdout,
"==============================\n\n");
4015 const QList<TestQmlModule> testQmlModules =
4016 options.testBundleMode ? findTestQmlModules(options) : QList<TestQmlModule>{};
4018 if (options.testBundleMode && !verifyUniqueTestQmlModuleDeployDirs(testQmlModules))
4021 if (options.testBundleMode && !verifyUniqueTestRawfilePaths(options.testRawfiles))
4025 fprintf(stdout,
"\nDeployment process started...\n");
4027 QString hapOutputPath;
4031 fprintf(stderr,
"Failed to copy template\n");
4037 fprintf(stderr,
"Failed to customize template\n");
4042 QStringList bundledBinaries;
4044 if (!copyTestBinaries(options, bundledBinaries)) {
4045 fprintf(stderr,
"Failed to copy test binaries\n");
4049 fprintf(stderr,
"Failed to copy Qt libraries\n");
4053 fprintf(stderr,
"Failed to copy Qt plugins\n");
4057 fprintf(stderr,
"Failed to copy QML modules\n");
4060 if (!copyTestQmlModules(testQmlModules, options)) {
4061 fprintf(stderr,
"Failed to copy test QML modules\n");
4065 fprintf(stderr,
"Failed to copy test rawfiles\n");
4070 fprintf(stderr,
"Failed to copy application binary\n");
4074 fprintf(stderr,
"Failed to copy project libraries\n");
4077 QSet<QString> processedLibs;
4078 if (!detectAndCopyDependencies(options, processedLibs)) {
4079 fprintf(stderr,
"Failed to detect and copy dependencies\n");
4082 bool qmlScanOk =
true;
4083 QList<QmlImportInfo> qmlImports = scanQmlImports(options, &qmlScanOk);
4085 fprintf(stderr,
"Failed to scan QML imports\n");
4089 fprintf(stderr,
"Failed to copy QML files\n");
4092 if (!copyQmlImports(options, qmlImports, processedLibs)) {
4093 fprintf(stderr,
"Failed to copy QML imports\n");
4096 if (!copyPlugins(options, processedLibs)) {
4097 fprintf(stderr,
"Failed to copy plugins\n");
4103 fprintf(stderr,
"Failed to copy extra plugins\n");
4108 fprintf(stderr,
"Failed to inject signing configuration\n");
4113 if (!buildHap(options, &hapOutputPath)) {
4114 fprintf(stderr,
"Failed to build HAP\n");
4118 if (!signHap(options, signing, &hapOutputPath)) {
4119 fprintf(stderr,
"Failed to sign HAP\n");
4125 if (!writeTestBinariesList(options, bundledBinaries)) {
4126 fprintf(stderr,
"Failed to write binaries.txt\n");
4133 if (!options.depFilePath.isEmpty()) {
4134 const QString unsignedHapPath = options.outputDirectory
4135 +
"/entry/build/default/outputs/default/entry-default-unsigned.hap"_L1;
4136 if (!writeDepfile(options, unsignedHapPath))
4137 fprintf(stderr,
"Warning: Failed to write dependency file\n");
4142 if (!installToDevice(options, hapOutputPath)) {
4143 fprintf(stderr,
"Failed to install to device\n");
4148 fprintf(stdout,
"\n==============================================\n");
4149 fprintf(stdout,
"Deployment completed successfully!\n");
4150 fprintf(stdout,
"==============================================\n");
4151 fprintf(stdout,
"Project location: %s\n", qPrintable(options.outputDirectory));
4153 if (!hapOutputPath.isEmpty())
4154 fprintf(stdout,
"HAP package: %s\n", qPrintable(hapOutputPath));
4157 fprintf(stdout,
"\nTotal time: %lld ms\n", options.timer.elapsed());