Merge pull request #240715 from FlafyDev/flutter-cache-drv-2

flutter: Separate cache and unwrapped derivations #2
This commit is contained in:
Ilan Joselevich 2023-07-05 03:03:27 +03:00 committed by GitHub
commit d625c36563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 77 deletions

View File

@ -3,68 +3,13 @@
, patches
, dart
, src
, includedEngineArtifacts ? {
common = [
"flutter_patched_sdk"
"flutter_patched_sdk_product"
];
platform = { };
}
, lib
, callPackage
, stdenv
, runCommandLocal
, symlinkJoin
, lndir
, git
, which
}:
let
engineArtifactDirectory =
let
engineArtifacts = callPackage ./engine-artifacts { inherit engineVersion; };
in
runCommandLocal "flutter-engine-artifacts-${version}" { }
(
let
mkCommonArtifactLinkCommand = { artifact }:
''
mkdir -p $out/common
${lndir}/bin/lndir -silent ${artifact} $out/common
'';
mkPlatformArtifactLinkCommand = { artifact, os, architecture, variant ? null }:
let
artifactDirectory = "${os}-${architecture}${lib.optionalString (variant != null) "-${variant}"}";
in
''
mkdir -p $out/${artifactDirectory}
${lndir}/bin/lndir -silent ${artifact} $out/${artifactDirectory}
'';
in
''
${
builtins.concatStringsSep "\n"
((map (name: mkCommonArtifactLinkCommand {
artifact = engineArtifacts.common.${name};
}) (if includedEngineArtifacts ? common then includedEngineArtifacts.common else [ ])) ++
(builtins.foldl' (commands: os: commands ++
(builtins.foldl' (commands: architecture: commands ++
(builtins.foldl' (commands: variant: commands ++
(map (artifact: mkPlatformArtifactLinkCommand {
inherit artifact os architecture variant;
}) engineArtifacts.platform.${os}.${architecture}.variants.${variant}))
(map (artifact: mkPlatformArtifactLinkCommand {
inherit artifact os architecture;
}) engineArtifacts.platform.${os}.${architecture}.base)
includedEngineArtifacts.platform.${os}.${architecture}))
[] (builtins.attrNames includedEngineArtifacts.platform.${os})))
[] (builtins.attrNames (if includedEngineArtifacts ? platform then includedEngineArtifacts.platform else { }))))
}
''
);
unwrapped =
stdenv.mkDerivation {
name = "flutter-${version}-unwrapped";
@ -134,7 +79,6 @@ let
mkdir -p $out
cp -r . $out
ln -sf ${dart} $out/bin/cache/dart-sdk
ln -sf ${engineArtifactDirectory} $out/bin/cache/artifacts/engine
runHook postInstall
'';
@ -153,7 +97,7 @@ let
'';
passthru = {
inherit dart;
inherit dart engineVersion;
# The derivation containing the original Flutter SDK files.
# When other derivations wrap this one, any unmodified files
# found here should be included as-is, for tooling compatibility.

View File

@ -35,3 +35,19 @@ index dd80b1e46e..8e54517765 100644
if (!devToolsDir.existsSync()) {
throw Exception('Could not find directory at ${devToolsDir.path}');
}
diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart
index 1c31c1b5db..76c7210d3b 100644
--- a/packages/flutter_tools/lib/src/cache.dart
+++ b/packages/flutter_tools/lib/src/cache.dart
@@ -529,6 +529,11 @@ class Cache {
/// Return the top-level directory in the cache; this is `bin/cache`.
Directory getRoot() {
+ const Platform platform = LocalPlatform();
+ if (platform.environment.containsKey('FLUTTER_CACHE_DIR')) {
+ return _fileSystem.directory(platform.environment['FLUTTER_CACHE_DIR']);
+ }
+
if (_rootOverride != null) {
return _fileSystem.directory(_fileSystem.path.join(_rootOverride!.path, 'bin', 'cache'));
} else {

View File

@ -4,7 +4,19 @@
, flutter
, supportsLinuxDesktop ? stdenv.hostPlatform.isLinux
, supportsAndroid ? stdenv.hostPlatform.isx86_64
, includedEngineArtifacts ? null
, includedEngineArtifacts ? {
common = [
"flutter_patched_sdk"
"flutter_patched_sdk_product"
];
platform = {
android = lib.optionalAttrs supportsAndroid
((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
linux = lib.optionalAttrs supportsLinuxDesktop
(lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
(architecture: [ "debug" "profile" "release" ]));
};
}
, extraPkgConfigPackages ? [ ]
, extraLibraries ? [ ]
, extraIncludes ? [ ]
@ -32,30 +44,73 @@
, cmake
, ninja
, clang
, lndir
, symlinkJoin
}:
let
flutterWithArtifacts = flutter.override {
includedEngineArtifacts = if includedEngineArtifacts != null then includedEngineArtifacts else {
common = [
"flutter_patched_sdk"
"flutter_patched_sdk_product"
];
platform = {
android = lib.optionalAttrs supportsAndroid
((lib.genAttrs [ "arm" "arm64" "x64" ] (architecture: [ "profile" "release" ])) // { x86 = [ "jit-release" ]; });
linux = lib.optionalAttrs supportsLinuxDesktop
(lib.genAttrs ((lib.optional stdenv.hostPlatform.isx86_64 "x64") ++ (lib.optional stdenv.hostPlatform.isAarch64 "arm64"))
(architecture: [ "debug" "profile" "release" ]));
};
};
engineArtifacts = callPackage ./engine-artifacts { inherit (flutter) engineVersion; };
mkCommonArtifactLinkCommand = { artifact }:
''
mkdir -p $out/artifacts/engine/common
lndir -silent ${artifact} $out/artifacts/engine/common
'';
mkPlatformArtifactLinkCommand = { artifact, os, architecture, variant ? null }:
let
artifactDirectory = "${os}-${architecture}${lib.optionalString (variant != null) "-${variant}"}";
in
''
mkdir -p $out/artifacts/engine/${artifactDirectory}
lndir -silent ${artifact} $out/artifacts/engine/${artifactDirectory}
'';
engineArtifactDirectory =
runCommandLocal "flutter-engine-artifacts-${flutter.version}" { nativeBuildInputs = [ lndir ]; }
(
builtins.concatStringsSep "\n"
((map
(name: mkCommonArtifactLinkCommand {
artifact = engineArtifacts.common.${name};
})
(includedEngineArtifacts.common or [ ])) ++
(builtins.foldl'
(commands: os: commands ++
(builtins.foldl'
(commands: architecture: commands ++
(builtins.foldl'
(commands: variant: commands ++
(map
(artifact: mkPlatformArtifactLinkCommand {
inherit artifact os architecture variant;
})
engineArtifacts.platform.${os}.${architecture}.variants.${variant}))
(map
(artifact: mkPlatformArtifactLinkCommand {
inherit artifact os architecture;
})
engineArtifacts.platform.${os}.${architecture}.base)
includedEngineArtifacts.platform.${os}.${architecture}))
[ ]
(builtins.attrNames includedEngineArtifacts.platform.${os})))
[ ]
(builtins.attrNames (includedEngineArtifacts.platform or { }))))
);
cacheDir = symlinkJoin {
name = "flutter-cache-dir";
paths = [
engineArtifactDirectory
"${flutter}/bin/cache"
];
};
# By default, Flutter stores downloaded files (such as the Pub cache) in the SDK directory.
# Wrap it to ensure that it does not do that, preferring home directories instead.
# The sh file `$out/bin/internal/shared.sh` runs when launching Flutter and calls `"$FLUTTER_ROOT/bin/cache/` instead of our environment variable `FLUTTER_CACHE_DIR`.
# We do not patch it since the script doesn't require engine artifacts(which are the only thing not added by the unwrapped derivation), so it shouldn't fail, and patching it will just be harder to maintain.
immutableFlutter = writeShellScript "flutter_immutable" ''
export PUB_CACHE=''${PUB_CACHE:-"$HOME/.pub-cache"}
${flutterWithArtifacts}/bin/flutter "$@"
export FLUTTER_CACHE_DIR=${cacheDir}
${flutter}/bin/flutter "$@"
'';
# Tools that the Flutter tool depends on.
@ -105,12 +160,12 @@ in
{
nativeBuildInputs = [ makeWrapper ];
passthru = flutterWithArtifacts.passthru // {
inherit (flutterWithArtifacts) version;
unwrapped = flutterWithArtifacts;
passthru = flutter.passthru // {
inherit (flutter) version;
unwrapped = flutter;
};
inherit (flutterWithArtifacts) meta;
inherit (flutter) meta;
} ''
for path in ${builtins.concatStringsSep " " (builtins.foldl' (paths: pkg: paths ++ (map (directory: "'${pkg}/${directory}/pkgconfig'") ["lib" "share"])) [ ] pkgConfigPackages)}; do
addToSearchPath FLUTTER_PKG_CONFIG_PATH "$path"