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

import '../../artifacts.dart';
import '../../base/file_system.dart';
7
import '../../build_info.dart';
8
import '../../globals.dart' as globals;
9
import '../build_system.dart';
10
import '../depfile.dart';
11 12 13
import '../exceptions.dart';
import 'assets.dart';
import 'dart.dart';
14
import 'icon_tree_shaker.dart';
15

16 17 18 19 20 21 22 23 24 25 26
/// The only files/subdirectories we care out.
const List<String> _kLinuxArtifacts = <String>[
  'libflutter_linux_glfw.so',
  'flutter_export.h',
  'flutter_messenger.h',
  'flutter_plugin_registrar.h',
  'flutter_glfw.h',
  'icudtl.dat',
  'cpp_client_wrapper_glfw/',
];

27
/// Copies the Linux desktop embedding files to the copy directory.
28 29
class UnpackLinuxDebug extends Target {
  const UnpackLinuxDebug();
30 31

  @override
32
  String get name => 'unpack_linux_debug';
33 34 35 36 37 38 39

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

  @override
40 41 42 43 44
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => <String>[
    'linux_engine_sources.d'
45 46 47 48 49 50
  ];

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

  @override
51
  Future<void> build(Environment environment) async {
52
    final String basePath = globals.artifacts.getArtifactPath(Artifact.linuxDesktopPath);
53 54
    final List<File> inputs = <File>[];
    final List<File> outputs = <File>[];
55
    final String outputPrefix = globals.fs.path.join(
56 57 58 59 60 61 62
      environment.projectDir.path,
      'linux',
      'flutter',
      'ephemeral',
    );
    // The native linux artifacts are composed of 6 files and a directory (listed above)
    // which need to be copied to the target directory.
63
    for (final String artifact in _kLinuxArtifacts) {
64
      final String entityPath = globals.fs.path.join(basePath, artifact);
65
      // If this artifact is a file, just copy the source over.
66 67
      if (globals.fs.isFileSync(entityPath)) {
        final String outputPath = globals.fs.path.join(
68
          outputPrefix,
69
          globals.fs.path.relative(entityPath, from: basePath),
70
        );
71
        final File destinationFile = globals.fs.file(outputPath);
72 73 74
        if (!destinationFile.parent.existsSync()) {
          destinationFile.parent.createSync(recursive: true);
        }
75
        final File inputFile = globals.fs.file(entityPath);
76 77 78 79 80 81 82
        inputFile.copySync(destinationFile.path);
        inputs.add(inputFile);
        outputs.add(destinationFile);
        continue;
      }
      // If the artifact is the directory cpp_client_wrapper, recursively
      // copy every file from it.
83
      for (final File input in globals.fs.directory(entityPath)
84 85
          .listSync(recursive: true)
          .whereType<File>()) {
86
        final String outputPath = globals.fs.path.join(
87
          outputPrefix,
88
          globals.fs.path.relative(input.path, from: basePath),
89
        );
90
        final File destinationFile = globals.fs.file(outputPath);
91 92 93
        if (!destinationFile.parent.existsSync()) {
          destinationFile.parent.createSync(recursive: true);
        }
94
        final File inputFile = globals.fs.file(input);
95 96 97
        inputFile.copySync(destinationFile.path);
        inputs.add(inputFile);
        outputs.add(destinationFile);
98 99
      }
    }
100 101
    final Depfile depfile = Depfile(inputs, outputs);
    depfile.writeToFile(environment.buildDir.childFile('linux_engine_sources.d'));
102 103
  }
}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

/// Creates a debug bundle for the Linux desktop target.
class DebugBundleLinuxAssets extends Target {
  const DebugBundleLinuxAssets();

  @override
  String get name => 'debug_bundle_linux_assets';

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

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.dill'),
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/linux.dart'),
122
    Source.pattern('{PROJECT_DIR}/pubspec.yaml'),
123
    ...IconTreeShaker.inputs,
124 125 126 127 128 129 130
  ];

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

131 132 133 134 135
  @override
  List<String> get depfiles => const <String>[
    'flutter_assets.d',
  ];

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  @override
  Future<void> build(Environment environment) async {
    if (environment.defines[kBuildMode] == null) {
      throw MissingDefineException(kBuildMode, 'debug_bundle_linux_assets');
    }
    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);
    }
153 154
    final Depfile depfile = await copyAssets(environment, outputDirectory);
    depfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
155 156
  }
}