···11+{ jdk
22+, version
33+, src
44+, lib
55+, stdenv
66+, gradle
77+, rsync
88+, runCommand
99+, testers
1010+}:
1111+1212+# Each Corretto version is based on a corresponding OpenJDK version. So
1313+# building Corretto is more or less the same as building OpenJDK. Hence, the
1414+# Corretto derivation overrides the corresponding OpenJDK derivation in order
1515+# to have access to all the version-specific fixes for the various OpenJDK
1616+# builds. However, Corretto uses `gradle` as build tool (which in turn will
1717+# invoke `make`). The configure/build phases are adapted as needed.
1818+1919+let
2020+ pname = "corretto";
2121+ # The version scheme is different between OpenJDK & Corretto.
2222+ # See https://github.com/corretto/corretto-17/blob/release-17.0.8.8.1/build.gradle#L40
2323+ # "major.minor.security.build.revision"
2424+in
2525+jdk.overrideAttrs (finalAttrs: oldAttrs: {
2626+ inherit pname version src;
2727+ name = "${pname}-${version}";
2828+2929+ nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [ jdk gradle rsync ];
3030+3131+ dontConfigure = true;
3232+3333+ postPatch = ''
3434+ # The rpm/deb task definitions require a Gradle plugin which we don't
3535+ # have and so the build fails. We'll simply remove them here because
3636+ # they are not needed anyways.
3737+ rm -rf installers/linux/universal/{rpm,deb}
3838+3939+ # `/usr/bin/rsync` is invoked to copy the source tree. We don't have that.
4040+ for file in $(find installers -name "build.gradle"); do
4141+ substituteInPlace $file --replace "workingDir '/usr/bin'" "workingDir '.'"
4242+ done
4343+ '';
4444+4545+4646+ buildPhase =
4747+ let
4848+ # The Linux installer is placed at linux/universal/tar whereas the MacOS
4949+ # one is at mac/tar.
5050+ task =
5151+ if stdenv.isDarwin then
5252+ ":installers:mac:tar:packageBuildResults"
5353+ else ":installers:linux:universal:tar:packageBuildResults";
5454+ in
5555+ ''
5656+ runHook preBuild
5757+5858+ # Corretto's actual built is triggered via `gradle`.
5959+ gradle --console=plain --no-daemon ${task}
6060+6161+ # Prepare for the installPhase so that it looks like if a normal
6262+ # OpenJDK had been built.
6363+ dir=build/jdkImageName/images
6464+ mkdir -p $dir
6565+ file=$(find ./installers -name 'amazon-corretto-${version}*.tar.gz')
6666+ tar -xzf $file -C $dir
6767+ mv $dir/amazon-corretto-* $dir/jdk
6868+6969+ runHook postBuild
7070+ '';
7171+7272+ installPhase = oldAttrs.installPhase + ''
7373+ # The installPhase will place everything in $out/lib/openjdk and
7474+ # reference through symlinks. We don't rewrite the installPhase but at
7575+ # least move the folder to convey that this is not OpenJDK anymore.
7676+ mv $out/lib/openjdk $out/lib/corretto
7777+ ln -s $out/lib/corretto $out/lib/openjdk
7878+ '';
7979+8080+ passthru =
8181+ let
8282+ pkg = finalAttrs.finalPackage;
8383+ in
8484+ oldAttrs.passthru // {
8585+ tests = {
8686+ version = testers.testVersion {
8787+ package = pkg;
8888+ };
8989+ vendor = runCommand "${pname}-vendor" { nativeBuildInputs = [ pkg ]; } ''
9090+ output=$(${pkg.meta.mainProgram} -XshowSettings:properties -version 2>&1 | grep vendor)
9191+ grep -Fq "java.vendor = Amazon.com Inc." - <<< "$output" && touch $out
9292+ '';
9393+ compiler = runCommand "${pname}-compiler" { nativeBuildInputs = [ pkg ]; } ''
9494+ cat << EOF > Main.java
9595+ class Main {
9696+ public static void main(String[] args) {
9797+ System.out.println("Hello, World!");
9898+ }
9999+ }
100100+ EOF
101101+ ${pkg}/bin/javac Main.java
102102+ ${pkg}/bin/java Main | grep -q "Hello, World!" && touch $out
103103+ '';
104104+ };
105105+ };
106106+107107+ meta = with lib; {
108108+ homepage = "https://aws.amazon.com/corretto";
109109+ license = licenses.gpl2Only;
110110+ description = "Amazon's distribution of OpenJDK";
111111+ platforms = jdk.meta.platforms;
112112+ mainProgram = "java";
113113+ maintainers = with maintainers; [ rollf ];
114114+ };
115115+})