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