From da9e1a4aca2327294a233f83502440463ad13a01 Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Wed, 11 Apr 2018 20:50:25 -0700 Subject: [PATCH] Introduce fingerprint check for preview-dart-2 build flx. (#16484) * Introduce fingerprint check for preview-dart-2 build flx. * Don't count outputs into fingerpint. Refactor compilation code. --- packages/flutter_tools/lib/src/flx.dart | 75 +++++++++++++++++++------ 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/packages/flutter_tools/lib/src/flx.dart b/packages/flutter_tools/lib/src/flx.dart index f02ad9a4ab2..2c2bf3c5f7e 100644 --- a/packages/flutter_tools/lib/src/flx.dart +++ b/packages/flutter_tools/lib/src/flx.dart @@ -75,26 +75,69 @@ Future build({ DevFSContent kernelContent; if (!precompiledSnapshot && previewDart2) { - ensureDirectoryExists(applicationKernelFilePath); + final File fingerprintFile = fs.file('$depfilePath.fingerprint'); + final List inputPaths = [ + mainPath, + ]; - final String kernelBinaryFilename = await compile( - sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath), - incrementalCompilerByteStorePath: fs.path.absolute(getIncrementalCompilerByteStoreDirectory()), - mainPath: fs.file(mainPath).absolute.path, - outputFilePath: applicationKernelFilePath, - depFilePath: depfilePath, - trackWidgetCreation: trackWidgetCreation, - fileSystemRoots: fileSystemRoots, - fileSystemScheme: fileSystemScheme, - packagesPath: packagesPath, - ); - if (kernelBinaryFilename == null) { - throwToolExit('Compiler terminated unexpectedly on $mainPath'); + bool needBuild = true; + final List fingerprintFiles = [fingerprintFile, fs.file(depfilePath)] + ..addAll(inputPaths.map(fs.file)); + + Future makeFingerprint() async { + final Set compilerInputPaths = await readDepfile(depfilePath) + ..add(mainPath); + final Map properties = { + 'entryPoint': mainPath, + }; + return new Fingerprint.fromBuildInputs(properties, compilerInputPaths); } + + if (fingerprintFiles.every((File file) => file.existsSync())) { + try { + final String json = await fingerprintFile.readAsString(); + final Fingerprint oldFingerprint = new Fingerprint.fromJson(json); + if (oldFingerprint == await makeFingerprint()) { + needBuild = false; + printStatus('Skipping compilation. Fingerprint match.'); + } + } catch (e) { + printTrace('Rebuilding kernel file due to fingerprint check error: $e'); + } + } + + String kernelBinaryFilename; + if (needBuild) { + ensureDirectoryExists(applicationKernelFilePath); + kernelBinaryFilename = await compile( + sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath), + incrementalCompilerByteStorePath: fs.path.absolute(getIncrementalCompilerByteStoreDirectory()), + mainPath: fs.file(mainPath).absolute.path, + outputFilePath: applicationKernelFilePath, + depFilePath: depfilePath, + trackWidgetCreation: trackWidgetCreation, + fileSystemRoots: fileSystemRoots, + fileSystemScheme: fileSystemScheme, + packagesPath: packagesPath, + ); + if (kernelBinaryFilename == null) { + throwToolExit('Compiler failed on $mainPath'); + } + // Compute and record build fingerprint. + try { + final Fingerprint fingerprint = await makeFingerprint(); + await fingerprintFile.writeAsString(fingerprint.toJson()); + } catch (e, s) { + // Log exception and continue, this step is a performance improvement only. + printStatus('Error during compilation output fingerprinting: $e\n$s'); + } + } else { + kernelBinaryFilename = applicationKernelFilePath; + } + kernelContent = new DevFSFileContent(fs.file(kernelBinaryFilename)); + await fs.directory(getBuildDirectory()).childFile('frontend_server.d') .writeAsString('frontend_server.d: ${artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk)}\n'); - - kernelContent = new DevFSFileContent(fs.file(kernelBinaryFilename)); } final AssetBundle assets = await buildAssets(