From d207c0fe09ea2fe8053d81d6a0ab7363f0f3e5e4 Mon Sep 17 00:00:00 2001 From: li_guoning Date: Thu, 17 Oct 2024 16:58:18 +0800 Subject: [PATCH 1/5] =?UTF-8?q?flutter=20build=20hap=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8F=82=E6=95=B0=20--target-platform=3Dohox-x64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li_guoning --- packages/flutter_tools/lib/src/build_info.dart | 6 +++--- packages/flutter_tools/lib/src/commands/build_app.dart | 2 +- packages/flutter_tools/lib/src/commands/build_hap.dart | 2 +- packages/flutter_tools/lib/src/commands/build_har.dart | 2 +- packages/flutter_tools/lib/src/commands/build_hsp.dart | 2 +- packages/flutter_tools/lib/src/compile.dart | 2 +- packages/flutter_tools/lib/src/ohos/ohos_device.dart | 2 ++ 7 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index 4bc6cfb270..f1b853be51 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -828,7 +828,7 @@ String getNameForTargetPlatform(TargetPlatform platform, {DarwinArch? darwinArch case TargetPlatform.ohos_arm64: return 'ohos-arm64'; case TargetPlatform.ohos_x64: - return 'ohos-x86'; + return 'ohos-x64'; } } @@ -922,7 +922,7 @@ OhosArch getOhosArchForName(String platform) { return OhosArch.armeabi_v7a; case 'ohos-arm64': return OhosArch.arm64_v8a; - case 'ohos-x86': + case 'ohos-x64': return OhosArch.x86_64; } throw Exception('Unsupported Ohos arch name "$platform"'); @@ -946,7 +946,7 @@ String getPlatformNameForOhosArch(OhosArch arch) { case OhosArch.arm64_v8a: return 'ohos-arm64'; case OhosArch.x86_64: - return 'ohos-x86'; + return 'ohos-x64'; } } diff --git a/packages/flutter_tools/lib/src/commands/build_app.dart b/packages/flutter_tools/lib/src/commands/build_app.dart index 7233fe78bf..b5923bfbd9 100644 --- a/packages/flutter_tools/lib/src/commands/build_app.dart +++ b/packages/flutter_tools/lib/src/commands/build_app.dart @@ -47,7 +47,7 @@ class BuildAppCommand extends BuildSubCommand { argParser.addMultiOption( 'target-platform', defaultsTo: const ['ohos-arm64'], - allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x86'], + allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x64'], help: 'The target platform for which the app is compiled.', ); } diff --git a/packages/flutter_tools/lib/src/commands/build_hap.dart b/packages/flutter_tools/lib/src/commands/build_hap.dart index effd903611..4adc519d22 100644 --- a/packages/flutter_tools/lib/src/commands/build_hap.dart +++ b/packages/flutter_tools/lib/src/commands/build_hap.dart @@ -47,7 +47,7 @@ class BuildHapCommand extends BuildSubCommand { argParser.addMultiOption( 'target-platform', defaultsTo: const ['ohos-arm64'], - allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x86'], + allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x64'], help: 'The target platform for which the app is compiled.', ); } diff --git a/packages/flutter_tools/lib/src/commands/build_har.dart b/packages/flutter_tools/lib/src/commands/build_har.dart index b9445beb43..0edbc0fc80 100644 --- a/packages/flutter_tools/lib/src/commands/build_har.dart +++ b/packages/flutter_tools/lib/src/commands/build_har.dart @@ -45,7 +45,7 @@ class BuildHarCommand extends BuildSubCommand { argParser.addMultiOption( 'target-platform', defaultsTo: const ['ohos-arm64'], - allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x86'], + allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x64'], help: 'The target platform for which the app is compiled.', ); } diff --git a/packages/flutter_tools/lib/src/commands/build_hsp.dart b/packages/flutter_tools/lib/src/commands/build_hsp.dart index 6fcaedb72a..9b8137409e 100644 --- a/packages/flutter_tools/lib/src/commands/build_hsp.dart +++ b/packages/flutter_tools/lib/src/commands/build_hsp.dart @@ -47,7 +47,7 @@ class BuildHspCommand extends BuildSubCommand { argParser.addMultiOption( 'target-platform', defaultsTo: const ['ohos-arm64'], - allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x86'], + allowed: ['ohos-arm64', 'ohos-arm', 'ohos-x64'], help: 'The target platform for which the app is compiled.', ); } diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart index 8e43ff5680..7a52be8ec7 100644 --- a/packages/flutter_tools/lib/src/compile.dart +++ b/packages/flutter_tools/lib/src/compile.dart @@ -285,7 +285,7 @@ class KernelCompiler { toMultiRootPath(dartPluginRegistrantFileUri, _fileSystemScheme, _fileSystemRoots, _fileSystem.path.separator == r'\'); } String? engineDartBinary; - if (globals.os.hostPlatform == HostPlatform.darwin_arm64) { + if (globals.platform.isMacOS) { final Artifacts? artifacts = globals.artifacts; if (artifacts is LocalEngineArtifacts) { final LocalEngineArtifacts localEngineArtifacts = artifacts; diff --git a/packages/flutter_tools/lib/src/ohos/ohos_device.dart b/packages/flutter_tools/lib/src/ohos/ohos_device.dart index fb82ee253f..14c90a39d2 100644 --- a/packages/flutter_tools/lib/src/ohos/ohos_device.dart +++ b/packages/flutter_tools/lib/src/ohos/ohos_device.dart @@ -424,6 +424,8 @@ class OhosDevice extends Device { return TargetPlatform.ohos_arm64; } else if (abilist.contains('x64')) { return TargetPlatform.ohos_x64; + } else if (abilist.contains('x86_64')) { + return TargetPlatform.ohos_x64; } else { return TargetPlatform.ohos_arm64; } -- Gitee From 3bd69f7e4674bc90c4de149f0fe8f5b4e8c8f0a7 Mon Sep 17 00:00:00 2001 From: li_guoning Date: Fri, 18 Oct 2024 16:17:59 +0800 Subject: [PATCH 2/5] hvigor --- packages/flutter_tools/lib/src/ohos/hvigor.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/flutter_tools/lib/src/ohos/hvigor.dart b/packages/flutter_tools/lib/src/ohos/hvigor.dart index bea955b09b..636b867122 100644 --- a/packages/flutter_tools/lib/src/ohos/hvigor.dart +++ b/packages/flutter_tools/lib/src/ohos/hvigor.dart @@ -332,6 +332,7 @@ void cleanAndCopyFlutterAsset( // clean flutter assets final String desFlutterAssetsPath = getProjectAssetsPath(ohosRootPath, ohosProject); + print('lign desFlutterAssetsPath: $desFlutterAssetsPath'); final Directory desAssets = globals.fs.directory(desFlutterAssetsPath); if (desAssets.existsSync()) { desAssets.deleteSync(recursive: true); @@ -343,10 +344,12 @@ void cleanAndCopyFlutterAsset( final String desAppSoPath = getAppSoPath(ohosRootPath, ohosBuildInfo.targetArchs.first, ohosProject); + print('lign desAppSoPath: $desAppSoPath'); if (ohosBuildInfo.buildInfo.isRelease || ohosBuildInfo.buildInfo.isProfile) { // copy app.so final String appSoPath = globals.fs.path.join(output, getNameForOhosArch(ohosBuildInfo.targetArchs.first), APP_SO_ORIGIN); + print('lign appSoPath: $appSoPath'); final File appSoFile = globals.localFileSystem.file(appSoPath); ensureParentExists(desAppSoPath); appSoFile.copySync(desAppSoPath); -- Gitee From 694377cddeae40e6dc48bfb2c571374078012ff1 Mon Sep 17 00:00:00 2001 From: li_guoning Date: Fri, 18 Oct 2024 17:15:54 +0800 Subject: [PATCH 3/5] gitignore --- .gitignore | 11 +++++++++++ dev/benchmarks/complex_layout/ohos/.gitignore | 8 -------- dev/benchmarks/macrobenchmarks/.gitignore | 9 +++++++++ dev/benchmarks/macrobenchmarks/ohos/.gitignore | 8 -------- .../templates/app_shared/.gitignore.tmpl | 9 +++++++++ .../templates/app_shared/ohos.tmpl/.gitignore | 8 -------- .../module/ohos/host_app_common/entry.tmpl/.gitignore | 8 -------- .../templates/module/ohos/host_config/.gitignore.tmpl | 9 --------- .../module_library/flutter_module.tmpl/.gitignore | 8 -------- .../flutter_tools/templates/package/.gitignore.tmpl | 9 +++++++++ .../templates/plugin_shared/.gitignore.tmpl | 9 +++++++++ 11 files changed, 47 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 8204a87e86..c58e012e51 100644 --- a/.gitignore +++ b/.gitignore @@ -107,6 +107,17 @@ unlinked_spec.ds **/macos/Flutter/ephemeral **/xcuserdata/ +# ohos +**/ohos/**/*.har +**/ohos/**/BuildProfile.ets +**/ohos/**/oh-package-lock.json5 +**/ohos/**/GeneratedPluginRegistrant.ets + +**/ohos/**/resources/rawfile/flutter_assets/ +**/ohos/**/libapp.so +**/ohos/**/libflutter.so +**/ohos/**/libvmservice_snapshot.so + # Windows **/windows/flutter/generated_plugin_registrant.cc **/windows/flutter/generated_plugin_registrant.h diff --git a/dev/benchmarks/complex_layout/ohos/.gitignore b/dev/benchmarks/complex_layout/ohos/.gitignore index 6ca13b3170..0e158c3f26 100644 --- a/dev/benchmarks/complex_layout/ohos/.gitignore +++ b/dev/benchmarks/complex_layout/ohos/.gitignore @@ -9,11 +9,3 @@ /.clang-format /.clang-tidy **/.test -*.har -**/BuildProfile.ets -**/oh-package-lock.json5 - -**/src/main/resources/rawfile/flutter_assets/ -**/libs/arm64-v8a/libapp.so -**/libs/arm64-v8a/libflutter.so -**/libs/arm64-v8a/libvmservice_snapshot.so diff --git a/dev/benchmarks/macrobenchmarks/.gitignore b/dev/benchmarks/macrobenchmarks/.gitignore index 394b6af32c..da298a0ce0 100644 --- a/dev/benchmarks/macrobenchmarks/.gitignore +++ b/dev/benchmarks/macrobenchmarks/.gitignore @@ -2,3 +2,12 @@ lib/generated_plugin_registrant.dart devtools_memory.json *.sksl.json vmservice.out + +ohos/**/*.har +ohos/**/BuildProfile.ets +ohos/**/oh-package-lock.json5 + +ohos/**/resources/rawfile/flutter_assets/ +ohos/**/libapp.so +ohos/**/libflutter.so +ohos/**/libvmservice_snapshot.so \ No newline at end of file diff --git a/dev/benchmarks/macrobenchmarks/ohos/.gitignore b/dev/benchmarks/macrobenchmarks/ohos/.gitignore index 6ca13b3170..0e158c3f26 100644 --- a/dev/benchmarks/macrobenchmarks/ohos/.gitignore +++ b/dev/benchmarks/macrobenchmarks/ohos/.gitignore @@ -9,11 +9,3 @@ /.clang-format /.clang-tidy **/.test -*.har -**/BuildProfile.ets -**/oh-package-lock.json5 - -**/src/main/resources/rawfile/flutter_assets/ -**/libs/arm64-v8a/libapp.so -**/libs/arm64-v8a/libflutter.so -**/libs/arm64-v8a/libvmservice_snapshot.so diff --git a/packages/flutter_tools/templates/app_shared/.gitignore.tmpl b/packages/flutter_tools/templates/app_shared/.gitignore.tmpl index 24476c5d1e..2d20242a38 100644 --- a/packages/flutter_tools/templates/app_shared/.gitignore.tmpl +++ b/packages/flutter_tools/templates/app_shared/.gitignore.tmpl @@ -42,3 +42,12 @@ app.*.map.json /android/app/debug /android/app/profile /android/app/release + +ohos/**/*.har +ohos/**/BuildProfile.ets +ohos/**/oh-package-lock.json5 + +ohos/**/resources/rawfile/flutter_assets/ +ohos/**/libapp.so +ohos/**/libflutter.so +ohos/**/libvmservice_snapshot.so \ No newline at end of file diff --git a/packages/flutter_tools/templates/app_shared/ohos.tmpl/.gitignore b/packages/flutter_tools/templates/app_shared/ohos.tmpl/.gitignore index 6ca13b3170..0e158c3f26 100644 --- a/packages/flutter_tools/templates/app_shared/ohos.tmpl/.gitignore +++ b/packages/flutter_tools/templates/app_shared/ohos.tmpl/.gitignore @@ -9,11 +9,3 @@ /.clang-format /.clang-tidy **/.test -*.har -**/BuildProfile.ets -**/oh-package-lock.json5 - -**/src/main/resources/rawfile/flutter_assets/ -**/libs/arm64-v8a/libapp.so -**/libs/arm64-v8a/libflutter.so -**/libs/arm64-v8a/libvmservice_snapshot.so diff --git a/packages/flutter_tools/templates/module/ohos/host_app_common/entry.tmpl/.gitignore b/packages/flutter_tools/templates/module/ohos/host_app_common/entry.tmpl/.gitignore index 63b3805e50..aa3c934840 100644 --- a/packages/flutter_tools/templates/module/ohos/host_app_common/entry.tmpl/.gitignore +++ b/packages/flutter_tools/templates/module/ohos/host_app_common/entry.tmpl/.gitignore @@ -5,11 +5,3 @@ /build /.cxx /.test -*.har -**/BuildProfile.ets -**/oh-package-lock.json5 - -**/src/main/resources/rawfile/flutter_assets/ -**/libs/arm64-v8a/libapp.so -**/libs/arm64-v8a/libflutter.so -**/libs/arm64-v8a/libvmservice_snapshot.so \ No newline at end of file diff --git a/packages/flutter_tools/templates/module/ohos/host_config/.gitignore.tmpl b/packages/flutter_tools/templates/module/ohos/host_config/.gitignore.tmpl index 6ca13b3170..b19f8d5b44 100644 --- a/packages/flutter_tools/templates/module/ohos/host_config/.gitignore.tmpl +++ b/packages/flutter_tools/templates/module/ohos/host_config/.gitignore.tmpl @@ -8,12 +8,3 @@ /.clangd /.clang-format /.clang-tidy -**/.test -*.har -**/BuildProfile.ets -**/oh-package-lock.json5 - -**/src/main/resources/rawfile/flutter_assets/ -**/libs/arm64-v8a/libapp.so -**/libs/arm64-v8a/libflutter.so -**/libs/arm64-v8a/libvmservice_snapshot.so diff --git a/packages/flutter_tools/templates/module/ohos/module_library/flutter_module.tmpl/.gitignore b/packages/flutter_tools/templates/module/ohos/module_library/flutter_module.tmpl/.gitignore index b418ad664c..36cebc08fe 100644 --- a/packages/flutter_tools/templates/module/ohos/module_library/flutter_module.tmpl/.gitignore +++ b/packages/flutter_tools/templates/module/ohos/module_library/flutter_module.tmpl/.gitignore @@ -4,11 +4,3 @@ /build /.cxx /.test -*.har -**/BuildProfile.ets -**/oh-package-lock.json5 - -**/src/main/resources/rawfile/flutter_assets/ -**/libs/arm64-v8a/libapp.so -**/libs/arm64-v8a/libflutter.so -**/libs/arm64-v8a/libvmservice_snapshot.so \ No newline at end of file diff --git a/packages/flutter_tools/templates/package/.gitignore.tmpl b/packages/flutter_tools/templates/package/.gitignore.tmpl index 96486fd930..6a0ef06365 100644 --- a/packages/flutter_tools/templates/package/.gitignore.tmpl +++ b/packages/flutter_tools/templates/package/.gitignore.tmpl @@ -28,3 +28,12 @@ migrate_working_dir/ .dart_tool/ .packages build/ + +ohos/**/*.har +ohos/**/BuildProfile.ets +ohos/**/oh-package-lock.json5 + +ohos/**/resources/rawfile/flutter_assets/ +ohos/**/libapp.so +ohos/**/libflutter.so +ohos/**/libvmservice_snapshot.so \ No newline at end of file diff --git a/packages/flutter_tools/templates/plugin_shared/.gitignore.tmpl b/packages/flutter_tools/templates/plugin_shared/.gitignore.tmpl index 96486fd930..6a0ef06365 100644 --- a/packages/flutter_tools/templates/plugin_shared/.gitignore.tmpl +++ b/packages/flutter_tools/templates/plugin_shared/.gitignore.tmpl @@ -28,3 +28,12 @@ migrate_working_dir/ .dart_tool/ .packages build/ + +ohos/**/*.har +ohos/**/BuildProfile.ets +ohos/**/oh-package-lock.json5 + +ohos/**/resources/rawfile/flutter_assets/ +ohos/**/libapp.so +ohos/**/libflutter.so +ohos/**/libvmservice_snapshot.so \ No newline at end of file -- Gitee From 8f3456fe2b87191642b8a18cd80141a0d4d53a8c Mon Sep 17 00:00:00 2001 From: li_guoning Date: Wed, 23 Oct 2024 09:36:56 +0000 Subject: [PATCH 4/5] update packages/flutter_tools/lib/src/ohos/hvigor.dart. Signed-off-by: li_guoning --- packages/flutter_tools/lib/src/ohos/hvigor.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/ohos/hvigor.dart b/packages/flutter_tools/lib/src/ohos/hvigor.dart index 636b867122..bea955b09b 100644 --- a/packages/flutter_tools/lib/src/ohos/hvigor.dart +++ b/packages/flutter_tools/lib/src/ohos/hvigor.dart @@ -332,7 +332,6 @@ void cleanAndCopyFlutterAsset( // clean flutter assets final String desFlutterAssetsPath = getProjectAssetsPath(ohosRootPath, ohosProject); - print('lign desFlutterAssetsPath: $desFlutterAssetsPath'); final Directory desAssets = globals.fs.directory(desFlutterAssetsPath); if (desAssets.existsSync()) { desAssets.deleteSync(recursive: true); @@ -344,12 +343,10 @@ void cleanAndCopyFlutterAsset( final String desAppSoPath = getAppSoPath(ohosRootPath, ohosBuildInfo.targetArchs.first, ohosProject); - print('lign desAppSoPath: $desAppSoPath'); if (ohosBuildInfo.buildInfo.isRelease || ohosBuildInfo.buildInfo.isProfile) { // copy app.so final String appSoPath = globals.fs.path.join(output, getNameForOhosArch(ohosBuildInfo.targetArchs.first), APP_SO_ORIGIN); - print('lign appSoPath: $appSoPath'); final File appSoFile = globals.localFileSystem.file(appSoPath); ensureParentExists(desAppSoPath); appSoFile.copySync(desAppSoPath); -- Gitee From 163f45aca94c046caf5eaaba404884f131582877 Mon Sep 17 00:00:00 2001 From: li_guoning Date: Wed, 23 Oct 2024 09:38:08 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20pack?= =?UTF-8?q?ages/flutter=5Ftools/lib/src/ohos/hvigor.dart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter_tools/lib/src/ohos/hvigor.dart | 776 ------------------ 1 file changed, 776 deletions(-) delete mode 100644 packages/flutter_tools/lib/src/ohos/hvigor.dart diff --git a/packages/flutter_tools/lib/src/ohos/hvigor.dart b/packages/flutter_tools/lib/src/ohos/hvigor.dart deleted file mode 100644 index bea955b09b..0000000000 --- a/packages/flutter_tools/lib/src/ohos/hvigor.dart +++ /dev/null @@ -1,776 +0,0 @@ -/* -* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import 'dart:io'; - -import 'package:json5/json5.dart'; -import 'package:process/process.dart'; - -import '../artifacts.dart'; -import '../base/common.dart'; -import '../base/file_system.dart'; -import '../base/logger.dart'; -import '../base/os.dart'; -import '../base/platform.dart' as base_platform; -import '../base/process.dart'; -import '../base/terminal.dart'; -import '../base/utils.dart'; -import '../build_info.dart'; -import '../build_system/build_system.dart'; -import '../build_system/targets/ohos.dart'; -import '../cache.dart'; -import '../globals.dart' as globals; -import '../project.dart'; -import '../reporting/reporting.dart'; -import 'application_package.dart'; -import 'hvigor_utils.dart'; -import 'ohos_builder.dart'; -import 'ohos_plugins_manager.dart'; - -const String OHOS_DTA_FILE_NAME = 'icudtl.dat'; - -const String FLUTTER_ASSETS_PATH = 'flutter_assets'; - -const String APP_SO_ORIGIN = 'app.so'; - -const String APP_SO = 'libapp.so'; - -const String HAR_FILE_NAME = 'flutter.har'; - -final bool isWindows = globals.platform.isWindows; - -String getHvigorwFile() => isWindows ? 'hvigorw.bat' : 'hvigorw'; - -void checkPlatformEnvironment(String environment, Logger? logger) { - final String? environmentConfig = Platform.environment[environment]; - if (environmentConfig == null) { - throwToolExit( - 'error:current platform environment $environment have not set'); - } else { - logger?.printStatus( - 'current platform environment $environment = $environmentConfig'); - } -} - -void copyFlutterAssets(String orgPath, String desPath, Logger? logger) { - logger?.printTrace('copy from "$orgPath" to "$desPath"'); - final LocalFileSystem localFileSystem = globals.localFileSystem; - copyDirectory( - localFileSystem.directory(orgPath), localFileSystem.directory(desPath)); -} - -/// eg:entry/src/main/resources/rawfile -String getProjectAssetsPath(String ohosRootPath, OhosProject ohosProject) { - return globals.fs.path.join(ohosProject.flutterModuleDirectory.path, - 'src/main/resources/rawfile', FLUTTER_ASSETS_PATH); -} - -/// eg:entry/src/main/resources/rawfile/flutter_assets/ -String getDatPath(String ohosRootPath, OhosProject ohosProject) { - return globals.fs.path.join( - getProjectAssetsPath(ohosRootPath, ohosProject), OHOS_DTA_FILE_NAME); -} - -/// eg:entry/libs/arm64-v8a/libapp.so -String getAppSoPath( - String ohosRootPath, OhosArch ohosArch, OhosProject ohosProject) { - return globals.fs.path.join(ohosProject.flutterModuleDirectory.path, 'libs', - getNameForOhosArch(ohosArch), APP_SO); -} - -String getHvigorwPath(String ohosRootPath, {bool checkMod = false}) { - final String hvigorwPath = - globals.fs.path.join(ohosRootPath, getHvigorwFile()); - if (globals.fs.file(hvigorwPath).existsSync()) { - if (checkMod) { - final OperatingSystemUtils operatingSystemUtils = globals.os; - final File file = globals.localFileSystem.file(hvigorwPath); - operatingSystemUtils.chmod(file, '755'); - } - return hvigorwPath; - } else { - return 'hvigorw'; - } -} - -String getAbsolutePath(FlutterProject flutterProject, String path) { - if (globals.fs.path.isRelative(path)) { - return globals.fs.path.join(flutterProject.directory.path, path); - } - return path; -} - -/// ohpm should init first -Future ohpmInstall( - {required ProcessUtils processUtils, - required String workingDirectory, - Logger? logger}) async { - final List cleanCmd = ['ohpm', 'clean']; - final List installCmd = ['ohpm', 'install', '--all']; - processUtils.runSync(cleanCmd, - workingDirectory: workingDirectory, throwOnError: true); - processUtils.runSync(installCmd, - workingDirectory: workingDirectory, throwOnError: true); -} - -/// 根据来源,替换关键字,输出target文件 -void replaceKey(File file, File target, String key, String value) { - String content = file.readAsStringSync(); - content = content.replaceAll(key, value); - target.writeAsStringSync(content); -} - -///hvigorw任务 -Future hvigorwTask(List taskCommand, - {required ProcessUtils processUtils, - required String workPath, - required String hvigorwPath, - Logger? logger}) async { - final RunResult result = processUtils.runSync(taskCommand, - workingDirectory: workPath, throwOnError: true); - return result.exitCode; -} - -Future assembleHap( - {required ProcessUtils processUtils, - required String ohosRootPath, - required String hvigorwPath, - required String flavor, - required String buildMode, - Logger? logger}) async { - final List command = [ - hvigorwPath, - // 'clean', - 'assembleHap', - '-p', - 'product=$flavor', - '-p', - 'buildMode=$buildMode', - '--no-daemon', - ]; - return hvigorwTask(command, - processUtils: processUtils, - workPath: ohosRootPath, - hvigorwPath: hvigorwPath, - logger: logger); -} - -Future assembleApp( - {required ProcessUtils processUtils, - required String ohosRootPath, - required String hvigorwPath, - required String flavor, - required String buildMode, - Logger? logger}) async { - final List command = [ - hvigorwPath, - // 'clean', - 'assembleApp', - '-p', - 'product=$flavor', - '-p', - 'buildMode=$buildMode', - '--no-daemon', - ]; - return hvigorwTask(command, - processUtils: processUtils, - workPath: ohosRootPath, - hvigorwPath: hvigorwPath, - logger: logger); -} - -Future assembleHar( - {required ProcessUtils processUtils, - required String workPath, - required String hvigorwPath, - required String moduleName, - required String buildMode, - String product = 'default', - Logger? logger}) async { - final List command = [ - hvigorwPath, - // 'clean', - '--mode', - 'module', - '-p', - 'module=$moduleName', - '-p', - 'product=$product', - 'assembleHar', - '--no-daemon', - ]; - return hvigorwTask(command, - processUtils: processUtils, - workPath: workPath, - hvigorwPath: hvigorwPath, - logger: logger); -} - -Future assembleHsp( - {required ProcessUtils processUtils, - required String workPath, - required String hvigorwPath, - required String moduleName, - required String flavor, - required String buildMode, - Logger? logger}) async { - final List command = [ - hvigorwPath, - // 'clean', - '--mode', - 'module', - '-p', - 'module=$moduleName', - '-p', - 'product=$flavor', - '-p', - 'buildMode=$buildMode', - 'assembleHsp', - '--no-daemon', - ]; - return hvigorwTask(command, - processUtils: processUtils, - workPath: workPath, - hvigorwPath: hvigorwPath, - logger: logger); -} - -/// flutter构建 -Future flutterAssemble(FlutterProject flutterProject, - OhosBuildInfo ohosBuildInfo, String targetFile) async { - late String targetName; - if (ohosBuildInfo.buildInfo.isDebug) { - targetName = 'debug_ohos_application'; - } else if (ohosBuildInfo.buildInfo.isProfile) { - // eg:ohos_aot_bundle_profile_ohos-arm64 - targetName = - 'ohos_aot_bundle_profile_${getPlatformNameForOhosArch(ohosBuildInfo.targetArchs.first)}'; - } else { - // eg:ohos_aot_bundle_release_ohos-arm64 - targetName = - 'ohos_aot_bundle_release_${getPlatformNameForOhosArch(ohosBuildInfo.targetArchs.first)}'; - } - final List selectTarget = - ohosTargets.where((Target e) => targetName == e.name).toList(); - if (selectTarget.isEmpty) { - throwToolExit('do not found compare target.'); - } else if (selectTarget.length > 1) { - throwToolExit('more than one target match.'); - } - final Target target = selectTarget[0]; - - final Status status = - globals.logger.startProgress('Compiling $targetName for the Ohos...'); - String output = globals.fs.directory(getOhosBuildDirectory()).path; - // If path is relative, make it absolute from flutter project. - output = getAbsolutePath(flutterProject, output); - try { - final BuildResult result = await globals.buildSystem.build( - target, - Environment( - projectDir: globals.fs.currentDirectory, - outputDir: globals.fs.directory(output), - buildDir: flutterProject.directory - .childDirectory('.dart_tool') - .childDirectory('flutter_build'), - defines: { - ...ohosBuildInfo.buildInfo.toBuildSystemEnvironment(), - kTargetFile: targetFile, - kTargetPlatform: getNameForTargetPlatform(TargetPlatform.ohos), - }, - artifacts: globals.artifacts!, - fileSystem: globals.fs, - logger: globals.logger, - processManager: globals.processManager, - platform: globals.platform, - usage: globals.flutterUsage, - cacheDir: globals.cache.getRoot(), - engineVersion: globals.artifacts!.isLocalEngine - ? null - : globals.flutterVersion.engineRevision, - flutterRootDir: globals.fs.directory(Cache.flutterRoot), - generateDartPluginRegistry: true, - )); - if (!result.success) { - for (final ExceptionMeasurement measurement in result.exceptions.values) { - globals.printError( - 'Target ${measurement.target} failed: ${measurement.exception}', - stackTrace: measurement.fatal ? measurement.stackTrace : null, - ); - } - throwToolExit('Failed to compile application for the Ohos.'); - } else { - return output; - } - } on Exception catch (err) { - throwToolExit(err.toString()); - } finally { - status.stop(); - } -} - -/// 清理和拷贝flutter产物和资源 -void cleanAndCopyFlutterAsset( - OhosProject ohosProject, - OhosBuildInfo ohosBuildInfo, - Logger? logger, - String ohosRootPath, - String output) { - logger?.printTrace('copy flutter assets to project start'); - // clean flutter assets - final String desFlutterAssetsPath = - getProjectAssetsPath(ohosRootPath, ohosProject); - final Directory desAssets = globals.fs.directory(desFlutterAssetsPath); - if (desAssets.existsSync()) { - desAssets.deleteSync(recursive: true); - } - - /// copy flutter assets - copyFlutterAssets(globals.fs.path.join(output, FLUTTER_ASSETS_PATH), - desFlutterAssetsPath, logger); - - final String desAppSoPath = - getAppSoPath(ohosRootPath, ohosBuildInfo.targetArchs.first, ohosProject); - if (ohosBuildInfo.buildInfo.isRelease || ohosBuildInfo.buildInfo.isProfile) { - // copy app.so - final String appSoPath = globals.fs.path.join(output, - getNameForOhosArch(ohosBuildInfo.targetArchs.first), APP_SO_ORIGIN); - final File appSoFile = globals.localFileSystem.file(appSoPath); - ensureParentExists(desAppSoPath); - appSoFile.copySync(desAppSoPath); - } else { - final File appSo = globals.fs.file(desAppSoPath); - if (appSo.existsSync()) { - appSo.deleteSync(); - } - } - logger?.printTrace('copy flutter assets to project end'); -} - -/// 清理和拷贝flutter运行时 -void cleanAndCopyFlutterRuntime( - OhosProject ohosProject, - OhosBuildInfo ohosBuildInfo, - Logger? logger, - String ohosRootPath, - OhosBuildData ohosBuildData) { - logger?.printTrace('copy flutter runtime to project start'); - // copy ohos font-family support - if (ohosBuildInfo.buildInfo.isDebug) { - final String flutterSdk = globals.fsUtils.escapePath(Cache.flutterRoot!); - final File ohosDta = globals.localFileSystem.file(globals.fs.path.join( - flutterSdk, - 'packages', - 'flutter_tools', - 'templates', - 'app_shared', - 'ohos.tmpl', - 'dta', - OHOS_DTA_FILE_NAME)); - final String copyDes = getDatPath(ohosRootPath, ohosProject); - ohosDta.copySync(copyDes); - } - - // 复制 flutter.har - final String localEngineHarPath = globals.artifacts!.getArtifactPath( - Artifact.flutterEngineHar, - platform: getTargetPlatformForName( - getPlatformNameForOhosArch(ohosBuildInfo.targetArchs.first)), - mode: ohosBuildInfo.buildInfo.mode, - ); - final String desHarPath = - globals.fs.path.join(ohosRootPath, 'har', HAR_FILE_NAME); - ensureParentExists(desHarPath); - final File originHarFile = globals.localFileSystem.file(localEngineHarPath); - originHarFile.copySync(desHarPath); - logger?.printTrace('copy from "$localEngineHarPath" to "$desHarPath"'); - logger?.printTrace('copy flutter runtime to project end'); -} - -void ensureParentExists(String path) { - final Directory directory = globals.localFileSystem.file(path).parent; - if (!directory.existsSync()) { - directory.createSync(recursive: true); - } -} - -class OhosHvigorBuilder implements OhosBuilder { - OhosHvigorBuilder({ - required Logger logger, - required ProcessManager processManager, - required FileSystem fileSystem, - required Artifacts artifacts, - required Usage usage, - required HvigorUtils hvigorUtils, - required base_platform.Platform platform, - }) : _logger = logger, - _fileSystem = fileSystem, - _artifacts = artifacts, - _usage = usage, - _hvigorUtils = hvigorUtils, - _fileSystemUtils = - FileSystemUtils(fileSystem: fileSystem, platform: platform), - _processUtils = - ProcessUtils(logger: logger, processManager: processManager); - - final Logger _logger; - final ProcessUtils _processUtils; - final FileSystem _fileSystem; - final Artifacts _artifacts; - final Usage _usage; - final HvigorUtils _hvigorUtils; - final FileSystemUtils _fileSystemUtils; - - late OhosProject _ohosProject; - late String _ohosRootPath; - late OhosBuildData _ohosBuildData; - - void parseData(FlutterProject flutterProject, Logger? logger) { - _ohosProject = flutterProject.ohos; - _ohosRootPath = _ohosProject.ohosRoot.path; - _ohosBuildData = OhosBuildData.parseOhosBuildData(_ohosProject, logger); - } - - /// build hap - @override - Future buildHap({ - required FlutterProject project, - required OhosBuildInfo ohosBuildInfo, - required String target, - }) async { - _logger.printStatus('start hap build...'); - - if (!project.ohos.ohosBuildData.moduleInfo.hasEntryModule) { - throwToolExit( - "this ohos project don't have a entry module, can't build to a hap file."); - } - final Status status = _logger.startProgress( - 'Running Hvigor task assembleHap...', - ); - - updateProjectVersion(project, ohosBuildInfo.buildInfo); - await addPluginsModules(project); - await addFlutterModuleAndPluginsSrcOverrides(project); - - await buildApplicationPipeLine(project, ohosBuildInfo, target: target); - - final String hvigorwPath = getHvigorwPath(_ohosRootPath, checkMod: true); - - /// 生成所有 plugin 的 har - await assembleHars(_processUtils, project, ohosBuildInfo, _logger); - await assembleHsps(_processUtils, project, ohosBuildInfo, _logger); - - await removePluginsModules(project); - await addFlutterModuleAndPluginsOverrides(project); - // ohosProject.deleteOhModulesCache(); - await ohpmInstall( - processUtils: _processUtils, - workingDirectory: _ohosRootPath, - logger: _logger, - ); - - /// invoke hvigow task generate hap file. - final int errorCode = await assembleHap( - processUtils: _processUtils, - ohosRootPath: _ohosRootPath, - hvigorwPath: hvigorwPath, - flavor: getFlavor( - _ohosProject.getBuildProfileFile(), ohosBuildInfo.buildInfo.flavor), - buildMode: ohosBuildInfo.buildInfo.modeName, - logger: _logger); - status.stop(); - if (errorCode != 0) { - throwToolExit('assembleHap error! please check log.'); - } - - final File buildProfile = project.ohos.getBuildProfileFile(); - final String buildProfileConfig = buildProfile.readAsStringSync(); - final dynamic obj = JSON5.parse(buildProfileConfig); - // ignore: avoid_dynamic_calls - final dynamic signingConfigs = obj['app']?['signingConfigs']; - if (signingConfigs is List && signingConfigs.isEmpty) { - _logger.printError( - '请通过DevEco Studio打开ohos工程后配置调试签名(File -> Project Structure -> Signing Configs 勾选Automatically generate signature)'); - } else { - final BuildInfo buildInfo = ohosBuildInfo.buildInfo; - final File bundleFile = OhosProject.getSignedFile( - modulePath: _ohosProject.mainModuleDirectory.path, - moduleName: _ohosProject.mainModuleName, - flavor: getFlavor(_ohosProject.getBuildProfileFile(), buildInfo.flavor), - throwOnMissing: true, - ); - final String appSize = (buildInfo.mode == BuildMode.debug) - ? '' // Don't display the size when building a debug variant. - : ' (${getSizeAsMB(bundleFile.lengthSync())})'; - _logger.printStatus( - '${_logger.terminal.successMark} Built ${_fileSystem.path.relative(bundleFile.path)}$appSize.', - color: TerminalColor.green, - ); - } - } - - Future flutterBuildPre(FlutterProject flutterProject, - OhosBuildInfo ohosBuildInfo, String target) async { - /** - * 1. execute flutter assemble - * 2. copy flutter asset to flutter module - * 3. copy flutter runtime - * 4. ohpm install - */ - - final String output = - await flutterAssemble(flutterProject, ohosBuildInfo, target); - - cleanAndCopyFlutterAsset( - _ohosProject, ohosBuildInfo, _logger, _ohosRootPath, output); - - cleanAndCopyFlutterRuntime( - _ohosProject, ohosBuildInfo, _logger, _ohosRootPath, _ohosBuildData); - - // ohpm install for all modules - // ohosProject.deleteOhModulesCache(); - await ohpmInstall( - processUtils: _processUtils, - workingDirectory: _ohosRootPath, - logger: _logger, - ); - } - - @override - Future buildHar({ - required FlutterProject project, - required OhosBuildInfo ohosBuildInfo, - required String target, - }) async { - if (!project.isModule || - !project.ohos.flutterModuleDirectory.existsSync()) { - throwToolExit('current project is not module or has not pub get'); - } - - final Status status = _logger.startProgress( - 'Running Hvigor task assembleHar...', - ); - - await addPluginsModules(project); - await addFlutterModuleAndPluginsSrcOverrides(project); - - parseData(project, _logger); - - await flutterBuildPre(project, ohosBuildInfo, target); - - /// 生成 module 和所有 plugin 的 har - await assembleHars(_processUtils, project, ohosBuildInfo, _logger); - await assembleHsps(_processUtils, project, ohosBuildInfo, _logger); - - await removePluginsModules(project); - await addFlutterModuleAndPluginsOverrides(project); - status.stop(); - printHowToConsumeHar(logger: _logger); - } - - /// Prints how to consume the har from a host app. - void printHowToConsumeHar({ - Logger? logger, - }) { - logger?.printStatus('\nConsuming the Module', emphasis: true); - logger?.printStatus(''' - 1. Open ${globals.fs.path.join('', 'oh-package.json5')} - 2. Add flutter_module to the dependencies list: - - "dependencies": { - "@ohos/flutter_module": "file:path/to/har/flutter_module.har" - } - - 3. Override flutter and plugins dependencies: - - "overrides" { - "@ohos/flutter_ohos": "file:path/to/har/flutter.har", - } - '''); - } - - @override - Future buildHsp({ - required FlutterProject project, - required OhosBuildInfo ohosBuildInfo, - required String target, - }) { - // TODO: implement buildHsp - throw UnimplementedError(); - } - - @override - Future buildApp({ - required FlutterProject project, - required OhosBuildInfo ohosBuildInfo, - required String target, - }) async { - final Status status = _logger.startProgress( - 'Running Hvigor task assembleApp...', - ); - updateProjectVersion(project, ohosBuildInfo.buildInfo); - await buildApplicationPipeLine(project, ohosBuildInfo, target: target); - - final String hvigorwPath = getHvigorwPath(_ohosRootPath, checkMod: true); - - /// invoke hvigow task generate hap file. - final int errorCode1 = await assembleApp( - processUtils: _processUtils, - ohosRootPath: _ohosRootPath, - flavor: getFlavor( - _ohosProject.getBuildProfileFile(), ohosBuildInfo.buildInfo.flavor), - hvigorwPath: hvigorwPath, - buildMode: ohosBuildInfo.buildInfo.modeName, - logger: _logger); - status.stop(); - if (errorCode1 != 0) { - throwToolExit('assembleApp error! please check log.'); - } - final BuildInfo buildInfo = ohosBuildInfo.buildInfo; - final File bundleFile = OhosProject.getSignedFile( - modulePath: _ohosProject.mainModuleDirectory.path, - moduleName: _ohosProject.mainModuleName, - flavor: getFlavor(_ohosProject.getBuildProfileFile(), buildInfo.flavor), - type: OhosFileType.app, - throwOnMissing: true, - ); - final String appSize = (buildInfo.mode == BuildMode.debug) - ? '' // Don't display the size when building a debug variant. - : ' (${getSizeAsMB(bundleFile.lengthSync())})'; - _logger.printStatus( - '${_logger.terminal.successMark} Built ${_fileSystem.path.relative(bundleFile.path)}$appSize.', - color: TerminalColor.green, - ); - } - - Future buildApplicationPipeLine( - FlutterProject flutterProject, OhosBuildInfo ohosBuildInfo, - {required String target}) async { - if (!flutterProject.ohos.ohosBuildData.moduleInfo.hasEntryModule) { - throwToolExit( - "this ohos project don't have a entry module , can't build to a application."); - } - - parseData(flutterProject, _logger); - - /// 检查plugin的har构建 - await checkOhosPluginsDependencies(flutterProject); - - await flutterBuildPre(flutterProject, ohosBuildInfo, target); - - if (_ohosProject.isRunWithModuleHar) { - await assembleHars(_processUtils, flutterProject, ohosBuildInfo, _logger); - await assembleHsps(_processUtils, flutterProject, ohosBuildInfo, _logger); - - /// har文件拷贝后,需要重新install - // ohosProject.deleteOhModulesCache(); - await ohpmInstall( - processUtils: _processUtils, - workingDirectory: _ohosProject.mainModuleDirectory.path, - logger: _logger); - } - } - - String _moduleNameWithFlavor(List modules, String? flavor) { - return modules - .map((OhosModule module) => OhosModule.fromModulePath( - modulePath: module.srcPath, - flavor: getFlavor( - globals.fs.file(globals.fs.path - .join(module.srcPath, 'build-profile.json5')), - flavor, - ), - )) - .map((OhosModule module) => '${module.name}@${module.flavor}') - .join(','); - } - - /// 生成所有 plugin 的 har - Future assembleHars( - ProcessUtils processUtils, - FlutterProject project, - OhosBuildInfo ohosBuildInfo, - Logger? logger, - ) async { - final String ohosProjectPath = project.ohos.ohosRoot.path; - final List modules = _ohosBuildData.harModules; - if (modules.isEmpty) { - return; - } - - // compile hars. parallel compilation. - final String hvigorwPath = getHvigorwPath(ohosProjectPath, checkMod: true); - final String moduleName = - _moduleNameWithFlavor(modules, ohosBuildInfo.buildInfo.flavor); - final int errorCode = await assembleHar( - processUtils: processUtils, - workPath: ohosProjectPath, - moduleName: moduleName, - hvigorwPath: hvigorwPath, - buildMode: ohosBuildInfo.buildInfo.modeName, - logger: logger); - if (errorCode != 0) { - throwToolExit('Oops! assembleHars failed! please check log.'); - } - - // copy hars - for (final OhosModule module in modules) { - final File originHar = globals.fs.file(globals.fs.path.join( - module.srcPath, - 'build', - 'default', - 'outputs', - module.flavor, - '${module.name}.har')); - if (!originHar.existsSync()) { - throwToolExit('Oops! Failed to find: ${originHar.path}'); - } - final String desPath = - globals.fs.path.join(ohosProjectPath, 'har', '${module.name}.har'); - ensureParentExists(desPath); - originHar.copySync(desPath); - } - } - - Future assembleHsps( - ProcessUtils processUtils, - FlutterProject project, - OhosBuildInfo ohosBuildInfo, - Logger? logger, - ) async { - final String ohosProjectPath = project.ohos.ohosRoot.path; - final List modules = _ohosBuildData.moduleInfo.moduleList - .where((OhosModule element) => element.type == OhosModuleType.shared) - .toList(); - if (modules.isEmpty) { - return; - } - final String hvigorwPath = getHvigorwPath(ohosProjectPath, checkMod: true); - final String moduleName = - _moduleNameWithFlavor(modules, ohosBuildInfo.buildInfo.flavor); - final int errorCode = await assembleHsp( - processUtils: processUtils, - workPath: ohosProjectPath, - moduleName: moduleName, - hvigorwPath: hvigorwPath, - flavor: getFlavor( - project.ohos.getBuildProfileFile(), ohosBuildInfo.buildInfo.flavor), - buildMode: ohosBuildInfo.buildInfo.modeName, - logger: logger); - if (errorCode != 0) { - throwToolExit('Oops! assembleHsps failed! please check log.'); - } - } -} -- Gitee