btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / compilers / corretto / mk-corretto.nix
blob0de05fb0f6c876511d94d7f4172bed3e09f1a914
2   jdk,
3   version,
4   src,
5   lib,
6   stdenv,
7   gradle,
8   extraConfig ? [ ],
9   rsync,
10   runCommand,
11   testers,
14 # Each Corretto version is based on a corresponding OpenJDK version. So
15 # building Corretto is more or less the same as building OpenJDK. Hence, the
16 # Corretto derivation overrides the corresponding OpenJDK derivation in order
17 # to have access to all the version-specific fixes for the various OpenJDK
18 # builds. However, Corretto uses `gradle` as build tool (which in turn will
19 # invoke `make`). The configure/build phases are adapted as needed.
21 let
22   pname = "corretto";
24 # The version scheme is different between OpenJDK & Corretto.
25 # See https://github.com/corretto/corretto-17/blob/release-17.0.8.8.1/build.gradle#L40
26 # "major.minor.security.build.revision"
27 jdk.overrideAttrs (
28   finalAttrs: oldAttrs: {
29     inherit pname version src;
30     name = "${pname}-${version}";
32     nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [
33       jdk
34       gradle
35       rsync
36     ];
38     dontConfigure = true;
40     postPatch =
41       let
42         extra_config = builtins.concatStringsSep " " extraConfig;
43       in
44       ''
45         # The rpm/deb task definitions require a Gradle plugin which we don't
46         # have and so the build fails. We'll simply remove them here because
47         # they are not needed anyways.
48         rm -rf installers/linux/universal/{rpm,deb}
50         # `/usr/bin/rsync` is invoked to copy the source tree. We don't have that.
51         for file in $(find installers -name "build.gradle"); do
52           substituteInPlace $file --replace-warn "workingDir '/usr/bin'" "workingDir '.'"
53         done
55         gradleFlagsArray+=(-Pcorretto.extra_config="${extra_config}")
56       '';
58     # since we dontConfigure, we must run this manually
59     preBuild = "gradleConfigureHook";
61     # The Linux installer is placed at linux/universal/tar whereas the MacOS
62     # one is at mac/tar.
63     gradleBuildTask =
64       if stdenv.hostPlatform.isDarwin then
65         ":installers:mac:tar:build"
66       else
67         ":installers:linux:universal:tar:packageBuildResults";
69     postBuild =
70       ''
71         # Prepare for the installPhase so that it looks like if a normal
72         # OpenJDK had been built.
73         dir=build/jdkImageName/images
74         mkdir -p $dir
75         file=$(find ./installers -name 'amazon-corretto-${version}*.tar.gz')
76         tar -xzf $file -C $dir
77         mv $dir/amazon-corretto-* $dir/jdk
78       ''
79       + oldAttrs.postBuild or "";
81     installPhase =
82       oldAttrs.installPhase
83       + ''
84         # The installPhase will place everything in $out/lib/openjdk and
85         # reference through symlinks. We don't rewrite the installPhase but at
86         # least move the folder to convey that this is not OpenJDK anymore.
87         mv $out/lib/openjdk $out/lib/corretto
88         ln -s $out/lib/corretto $out/lib/openjdk
89       '';
91     passthru =
92       let
93         pkg = finalAttrs.finalPackage;
94       in
95       oldAttrs.passthru
96       // {
97         tests = {
98           version = testers.testVersion { package = pkg; };
99           vendor = runCommand "${pname}-vendor" { nativeBuildInputs = [ pkg ]; } ''
100             output=$(${pkg.meta.mainProgram} -XshowSettings:properties -version 2>&1 | grep vendor)
101             grep -Fq "java.vendor = Amazon.com Inc." - <<< "$output" && touch $out
102           '';
103           compiler = runCommand "${pname}-compiler" { nativeBuildInputs = [ pkg ]; } ''
104             cat << EOF  > Main.java
105             class Main {
106                 public static void main(String[] args) {
107                     System.out.println("Hello, World!");
108                 }
109             }
110             EOF
111             ${pkg}/bin/javac Main.java
112             ${pkg}/bin/java Main | grep -q "Hello, World!" && touch $out
113           '';
114         };
115       };
117     # Some of the OpenJDK derivation set their `pos` by hand. We need to
118     # overwrite this in order to point to Corretto, not OpenJDK.
119     pos = __curPos;
120     meta =
121       with lib;
122       oldAttrs.meta
123       // {
124         homepage = "https://aws.amazon.com/corretto";
125         license = licenses.gpl2Only;
126         description = "Amazon's distribution of OpenJDK";
127         maintainers = with maintainers; [ rollf ];
128       };
129   }