1 /*
  2  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation. Amazon designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  */
 21 
 22 dependencies {
 23     compile project(path: ':openjdksrc', configuration: 'archives')
 24     compile project(path: ':prebuild', configuration: 'cacerts')
 25 }
 26 
 27 if (project.correttoArch == 'x86') {
 28     project.correttoCommonFlags += '--with-target-bits=32'
 29 }
 30 
 31 def imageDir = "$buildRoot/build/${project.jdkImageName}/images"
 32 def jdkResultingImage = "${imageDir}/jdk"
 33 def testResultingImage = "${imageDir}/test"
 34 
 35 // deps
 36 def depsMap = [:]
 37 project.configurations.compile.getFiles().each { depsMap[it.getName()] = it }
 38 /**
 39  * Create a local copy of the source tree in our
 40  * build root -- this is required since OpenJDK's
 41  * build wants to occur inside the source tree,
 42  * and we don't want to tamper with someone
 43  * else's tree.
 44  */
 45 task copySource(type: Exec) {
 46     if (!file(buildRoot).exists()) {
 47         file(buildRoot).mkdirs()
 48     }
 49     workingDir '/usr/bin'
 50     commandLine 'rsync', '-am',
 51             '--exclude=pre-build',
 52             '--exclude=installers',
 53             '--exclude=corretto-build',
 54             "${project.rootDir}/", buildRoot
 55 }
 56 
 57 /**
 58  * Scan the patches folder for any .patch that needs
 59  * to be applied before start building.
 60  */
 61 task applyPatches() {
 62     dependsOn copySource
 63     doLast {
 64         fileTree('patches').matching {
 65             include '*.patch'
 66         }.each { f ->
 67             ant.patch(patchfile: f, dir: "$buildRoot", strip: 0)
 68         }
 69     }
 70 }
 71 
 72 task configureBuild(type: Exec) {
 73     dependsOn project.configurations.compile
 74     dependsOn applyPatches
 75     workingDir "$buildRoot"
 76 
 77     // Platform specific flags
 78     def command = ['bash', 'configure',
 79             "--with-cacerts-file=${depsMap[caCerts]}",
 80             "--with-zlib=system"
 81     ]
 82     // Common flags
 83     command += project.correttoCommonFlags
 84     commandLine command.flatten()
 85 
 86 }
 87 
 88 task executeBuild(type: Exec) {
 89     dependsOn configureBuild
 90     workingDir "$buildRoot"
 91     commandLine 'make', 'images'
 92     outputs.dir jdkResultingImage
 93 }
 94 
 95 task createTestImage(type: Exec) {
 96     dependsOn executeBuild
 97     workingDir "$buildRoot"
 98     commandLine 'make','test-image'
 99 }
100 
101 task bundleThirdPartyBinaries {
102     dependsOn executeBuild
103     doLast {
104         // If property set, copy async profiler binaries to bundle
105         if (project.asyncProfilerBinariesPath != "") {
106             if (!file(project.asyncProfilerBinariesPath).isDirectory()) {
107                 throw new GradleException("asyncProfilerBinariesPath is not a valid directory: ${project.asyncProfilerBinariesPath}")
108             }
109             copy {
110                 from project.asyncProfilerBinariesPath
111                 include 'bin/**', 'lib/**'
112                 into jdkResultingImage
113             }
114             copy {
115                 from project.asyncProfilerLegal
116                 into "${jdkResultingImage}/legal"
117             }
118         }
119     }
120 }
121 
122 task fixCrossCompileLinking(type: Exec) {
123     dependsOn createTestImage
124     /* When cross-compiling for aarch64, gcc incorrectly links against
125      * libz.so, provided by the -devel packages, instead of libz.so.1
126      * This can lead to runtime errors of the form
127      * javac: error while loading shared libraries: libz.so: cannot open shared object file: No such file or directory
128      * As a workaround, use patchelf to update all references to libz.so to the expected libz.so.1
129      *
130      * See also:
131      * - https://github.com/AppImage/AppImageKit/issues/964
132      * - https://github.com/AppImage/AppImageKit/issues/1092
133      * - https://github.com/electron-userland/electron-builder/issues/7835
134      * - https://github.com/CollaboraOnline/richdocumentscode/issues/68
135      * - https://github.com/Sienci-Labs/gsender/issues/420
136      */
137     if (project.hasProperty("corretto.crossCompile")) {
138         commandLine 'bash', '-c', "find ${imageDir} -type f -exec patchelf --replace-needed libz.so libz.so.1 {} \\; 2>/dev/null"
139     } else {
140         commandLine 'bash', '-c', "echo 'Not a cross-compiled build, no linker fixup required'"
141     }
142 }
143 
144 task packageTestImage(type: Tar) {
145     dependsOn fixCrossCompileLinking
146     description = 'Package test results'
147     archiveFileName = "${project.correttoTestImageArchiveName}.tar.gz"
148     compression = Compression.GZIP
149     from(testResultingImage) {
150         include '**'
151     }
152     into project.correttoTestImageArchiveName
153 }
154 
155 task packageDebugSymbols(type: Tar) {
156     description = 'Package debug results'
157     dependsOn packageTestImage
158     archiveFileName = "${project.correttoDebugSymbolsArchiveName}.tar.gz"
159     compression = Compression.GZIP
160     from(jdkResultingImage) {
161         include 'bin/*.diz'
162         include 'lib/*.diz'
163         include 'lib/server/*.diz'
164         into project.correttoDebugSymbolsArchiveName
165     }
166 }
167 
168 task packageBuildResults(type: Tar) {
169     description = 'Compresses the JDK image and puts the results in build/distributions.'
170     dependsOn packageDebugSymbols
171     dependsOn bundleThirdPartyBinaries
172     archiveFileName = "${project.correttoJdkArchiveName}.tar.gz"
173     compression = Compression.GZIP
174     from(buildRoot) {
175         include project.rootFiles
176         into project.correttoJdkArchiveName
177     }
178     from(jdkResultingImage) {
179         include 'bin/**'
180         include 'conf/**'
181         include 'include/**'
182         include 'jmods/**'
183         include 'lib/**'
184         include 'man/man1/**'
185         include 'release'
186         exclude '**/*.diz'
187         into project.correttoJdkArchiveName
188     }
189 
190     // Copy legal directory specifically to set permission correctly.
191     // See https://github.com/corretto/corretto-11/issues/129
192     from("${jdkResultingImage}/legal") {
193         include '**'
194         fileMode = 0444
195         into "${project.correttoJdkArchiveName}/legal"
196     }
197 }
198 
199 artifacts {
200     archives packageBuildResults
201 }