windows.dart 12.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'package:meta/meta.dart';

9 10
import '../../artifacts.dart';
import '../../base/file_system.dart';
11
import '../../build_info.dart';
12
import '../build_system.dart';
13 14 15
import '../depfile.dart';
import '../exceptions.dart';
import 'assets.dart';
16
import 'common.dart';
17 18 19 20 21 22 23 24 25 26 27 28
import 'desktop.dart';
import 'icon_tree_shaker.dart';

/// The only files/subdirectories we care about.
const List<String> _kWindowsArtifacts = <String>[
  'flutter_windows.dll',
  'flutter_windows.dll.exp',
  'flutter_windows.dll.lib',
  'flutter_windows.dll.pdb',
  'flutter_export.h',
  'flutter_messenger.h',
  'flutter_plugin_registrar.h',
29
  'flutter_texture_registrar.h',
30 31 32
  'flutter_windows.h',
];

33 34 35 36 37 38 39 40 41 42 43 44
const List<String> _kWindowsUwpArtifacts = <String>[
  'flutter_windows_winuwp.dll',
  'flutter_windows_winuwp.dll.exp',
  'flutter_windows_winuwp.dll.lib',
  'flutter_windows_winuwp.dll.pdb',
  'flutter_export.h',
  'flutter_messenger.h',
  'flutter_plugin_registrar.h',
  'flutter_texture_registrar.h',
  'flutter_windows.h',
];

45
const String _kWindowsDepfile = 'windows_engine_sources.d';
46
const String _kWindowsUwpDepfile = 'windows_uwp_engine_sources.d';
47 48

/// Copies the Windows desktop embedding files to the copy directory.
49 50 51 52 53 54 55 56 57 58 59 60
class UnpackWindows extends Target {
  const UnpackWindows();

  @override
  String get name => 'unpack_windows';

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/windows.dart'),
  ];

  @override
61 62 63 64
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => const <String>[_kWindowsDepfile];
65 66 67 68 69

  @override
  List<Target> get dependencies => const <Target>[];

  @override
70
  Future<void> build(Environment environment) async {
71 72 73 74
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
    final String engineSourcePath = environment.artifacts
      .getArtifactPath(
        Artifact.windowsDesktopPath,
75
        platform: TargetPlatform.windows_x64,
76 77 78 79 80
        mode: buildMode,
      );
    final String clientSourcePath = environment.artifacts
      .getArtifactPath(
        Artifact.windowsCppClientWrapper,
81
        platform: TargetPlatform.windows_x64,
82 83
        mode: buildMode,
      );
84 85
    final Directory outputDirectory = environment.fileSystem.directory(
      environment.fileSystem.path.join(
86 87 88
        environment.projectDir.path,
        'windows',
        'flutter',
89 90 91 92 93 94
        'ephemeral',
      ),
    );
    final Depfile depfile = unpackDesktopArtifacts(
      fileSystem: environment.fileSystem,
      artifacts: _kWindowsArtifacts,
95
      engineSourcePath: engineSourcePath,
96
      outputDirectory: outputDirectory,
97
      clientSourcePaths: <String>[clientSourcePath],
98 99
      icuDataPath: environment.artifacts.getArtifactPath(
        Artifact.icuData,
100
        platform: TargetPlatform.windows_x64
101
      )
102 103 104 105 106 107 108 109 110 111 112 113
    );
    final DepfileService depfileService = DepfileService(
      fileSystem: environment.fileSystem,
      logger: environment.logger,
    );
    depfileService.writeToFile(
      depfile,
      environment.buildDir.childFile(_kWindowsDepfile),
    );
  }
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

/// Copies the Windows desktop embedding files to the copy directory.
class UnpackWindowsUwp extends Target {
  const UnpackWindowsUwp();

  @override
  String get name => 'unpack_windows_uwp';

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/windows.dart'),
  ];

  @override
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => const <String>[_kWindowsUwpDepfile];

  @override
  List<Target> get dependencies => const <Target>[];

  @override
  Future<void> build(Environment environment) async {
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
    final String engineSourcePath = environment.artifacts
      .getArtifactPath(
141
        Artifact.windowsUwpDesktopPath,
142 143 144 145 146
        platform: TargetPlatform.windows_x64,
        mode: buildMode,
      );
    final String clientSourcePath = environment.artifacts
      .getArtifactPath(
147
        Artifact.windowsUwpCppClientWrapper,
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        platform: TargetPlatform.windows_x64,
        mode: buildMode,
      );
    final Directory outputDirectory = environment.fileSystem.directory(
      environment.fileSystem.path.join(
        environment.projectDir.path,
        'winuwp',
        'flutter',
        'ephemeral',
      ),
    );
    final Depfile depfile = unpackDesktopArtifacts(
      fileSystem: environment.fileSystem,
      artifacts: _kWindowsUwpArtifacts,
      engineSourcePath: engineSourcePath,
      outputDirectory: outputDirectory,
      clientSourcePaths: <String>[clientSourcePath],
      icuDataPath: environment.artifacts.getArtifactPath(
        Artifact.icuData,
        platform: TargetPlatform.windows_x64
      )
    );
    // Copy flutter_windows.h into flutter directory as well.
    final File flutterWindows = outputDirectory.childFile('flutter_windows.h');
    final File flutterWindowsDest = flutterWindows.parent.parent.childFile('flutter_windows.h');
    flutterWindows.copySync(flutterWindowsDest.path);
    depfile.outputs.add(flutterWindowsDest);
    //

    final DepfileService depfileService = DepfileService(
      fileSystem: environment.fileSystem,
      logger: environment.logger,
    );
    depfileService.writeToFile(
      depfile,
      environment.buildDir.childFile(_kWindowsUwpDepfile),
    );
  }
}

188 189 190
/// Creates a bundle for the Windows desktop target.
abstract class BundleWindowsAssets extends Target {
  const BundleWindowsAssets();
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
    UnpackWindows(),
  ];

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/windows.dart'),
    Source.pattern('{PROJECT_DIR}/pubspec.yaml'),
    ...IconTreeShaker.inputs,
  ];

  @override
  List<String> get depfiles => const <String>[
    'flutter_assets.d',
  ];

  @override
  Future<void> build(Environment environment) async {
    if (environment.defines[kBuildMode] == null) {
213
      throw MissingDefineException(kBuildMode, 'bundle_windows_assets');
214 215 216 217 218 219 220 221 222 223 224 225
    }
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
    final Directory outputDirectory = environment.outputDir
      .childDirectory('flutter_assets');
    if (!outputDirectory.existsSync()) {
      outputDirectory.createSync();
    }

    // Only copy the kernel blob in debug mode.
    if (buildMode == BuildMode.debug) {
      environment.buildDir.childFile('app.dill')
        .copySync(outputDirectory.childFile('kernel_blob.bin').path);
226
    }
227 228 229
    final Depfile depfile = await copyAssets(
      environment,
      outputDirectory,
230
      targetPlatform: TargetPlatform.windows_x64,
231
    );
232 233 234 235 236 237 238 239
    final DepfileService depfileService = DepfileService(
      fileSystem: environment.fileSystem,
      logger: environment.logger,
    );
    depfileService.writeToFile(
      depfile,
      environment.buildDir.childFile('flutter_assets.d'),
    );
240 241
  }
}
242

243 244 245 246 247 248 249 250 251 252 253 254

/// Creates a bundle for the Windows desktop target.
abstract class BundleWindowsAssetsUwp extends BundleWindowsAssets {
  const BundleWindowsAssetsUwp();

  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
    UnpackWindowsUwp(),
  ];
}

255 256 257
/// A wrapper for AOT compilation that copies app.so into the output directory.
class WindowsAotBundle extends Target {
  /// Create a [WindowsAotBundle] wrapper for [aotTarget].
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  const WindowsAotBundle(this.aotTarget, {@required this.uwp});

  /// The [AotElfBase] subclass that produces the app.so.
  final AotElfBase aotTarget;

  /// Whether this is the UWP target.
  final bool uwp;

  @override
  String get name => uwp ? 'windows_uwp_aot_bundle' : 'windows_aot_bundle';

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];

  @override
  List<Source> get outputs => uwp ?
    const <Source>[
      Source.pattern('{OUTPUT_DIR}/winuwp/app.so'),
    ] :
    const <Source>[
      Source.pattern('{OUTPUT_DIR}/windows/app.so'),
    ];

  @override
  List<Target> get dependencies => <Target>[
    aotTarget,
  ];

  @override
  Future<void> build(Environment environment) async {
    final File outputFile = environment.buildDir.childFile('app.so');
    final Directory outputDirectory = environment.outputDir.childDirectory(uwp ? 'winuwp' : 'windows');
    if (!outputDirectory.existsSync()) {
      outputDirectory.createSync(recursive: true);
    }
    outputFile.copySync(outputDirectory.childFile('app.so').path);
  }
}

/// A wrapper for AOT compilation that copies app.so into the output directory.
class WindowsUwpAotBundle extends Target {
  /// Create a [WindowsAotBundle] wrapper for [aotTarget].
  const WindowsUwpAotBundle(this.aotTarget);
303 304 305 306 307

  /// The [AotElfBase] subclass that produces the app.so.
  final AotElfBase aotTarget;

  @override
308
  String get name => 'windows_uwp_aot_bundle';
309 310 311 312 313 314 315 316

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];

  @override
  List<Source> get outputs => const <Source>[
317
    Source.pattern('{OUTPUT_DIR}/winuwp/app.so'),
318 319 320 321 322 323 324 325 326 327
  ];

  @override
  List<Target> get dependencies => <Target>[
    aotTarget,
  ];

  @override
  Future<void> build(Environment environment) async {
    final File outputFile = environment.buildDir.childFile('app.so');
328
    final Directory outputDirectory = environment.outputDir.childDirectory('winuwp');
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    if (!outputDirectory.existsSync()) {
      outputDirectory.createSync(recursive: true);
    }
    outputFile.copySync(outputDirectory.childFile('app.so').path);
  }
}

class ReleaseBundleWindowsAssets extends BundleWindowsAssets {
  const ReleaseBundleWindowsAssets();

  @override
  String get name => 'release_bundle_windows_assets';

  @override
  List<Source> get outputs => const <Source>[];

  @override
  List<Target> get dependencies => <Target>[
    ...super.dependencies,
348
    const WindowsAotBundle(AotElfRelease(TargetPlatform.windows_x64), uwp: false),
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  ];
}

class ProfileBundleWindowsAssets extends BundleWindowsAssets {
  const ProfileBundleWindowsAssets();

  @override
  String get name => 'profile_bundle_windows_assets';

  @override
  List<Source> get outputs => const <Source>[];

  @override
  List<Target> get dependencies => <Target>[
    ...super.dependencies,
364
    const WindowsAotBundle(AotElfProfile(TargetPlatform.windows_x64), uwp: false),
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  ];
}

class DebugBundleWindowsAssets extends BundleWindowsAssets {
  const DebugBundleWindowsAssets();

  @override
  String get name => 'debug_bundle_windows_assets';

  @override
  List<Source> get inputs => <Source>[
    const Source.pattern('{BUILD_DIR}/app.dill'),
  ];

  @override
  List<Source> get outputs => <Source>[
    const Source.pattern('{OUTPUT_DIR}/flutter_assets/kernel_blob.bin'),
  ];
}
384 385 386 387 388 389 390 391 392 393 394 395 396

class ReleaseBundleWindowsAssetsUwp extends BundleWindowsAssetsUwp {
  const ReleaseBundleWindowsAssetsUwp();

  @override
  String get name => 'release_bundle_windows_assets_uwp';

  @override
  List<Source> get outputs => const <Source>[];

  @override
  List<Target> get dependencies => <Target>[
    ...super.dependencies,
397
    const WindowsAotBundle(AotElfRelease(TargetPlatform.windows_uwp_x64), uwp: true),
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  ];
}

class ProfileBundleWindowsAssetsUwp extends BundleWindowsAssetsUwp {
  const ProfileBundleWindowsAssetsUwp();

  @override
  String get name => 'profile_bundle_windows_assets_uwp';

  @override
  List<Source> get outputs => const <Source>[];

  @override
  List<Target> get dependencies => <Target>[
    ...super.dependencies,
413
    const WindowsAotBundle(AotElfProfile(TargetPlatform.windows_uwp_x64), uwp: true),
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  ];
}

class DebugBundleWindowsAssetsUwp extends BundleWindowsAssetsUwp {
  const DebugBundleWindowsAssetsUwp();

  @override
  String get name => 'debug_bundle_windows_assets_uwp';

  @override
  List<Source> get inputs => <Source>[
    const Source.pattern('{BUILD_DIR}/app.dill'),
  ];

  @override
  List<Source> get outputs => <Source>[
    const Source.pattern('{OUTPUT_DIR}/flutter_assets/kernel_blob.bin'),
  ];
}