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 // Tasks can refer to version information using the project.version
 23 // property. For an example of its usage, see ':installers:linux:rpm:buildLogic'
 24 // where the version numbers are used to populate a templated RPM spec file.
 25 
 26 import org.apache.tools.ant.taskdefs.condition.Os
 27 
 28 allprojects {
 29     apply plugin: 'base'
 30 
 31     configurations {
 32         compile {
 33             transitive = false
 34         }
 35     }
 36 
 37     version = {
 38         def full = file('version.txt').text.trim()
 39         def tokens = full.tokenize(".")
 40         [full    : full,
 41          major   : tokens[0],
 42          minor   : tokens[1],
 43          security: tokens[2],
 44          build   : tokens[3],
 45          revision: tokens[4],
 46          upstream: "${tokens[0]}.${tokens[1]}.${tokens[2]}.${tokens[3]}".toString()]
 47     }.call()
 48     buildDir = 'corretto-build'
 49 
 50     ext {
 51         buildRoot = file("$buildDir/buildRoot")
 52         distributionDir = file("$buildDir/distributions")
 53         packageInfo = [
 54                 url       : "https://github.com/corretto/corretto-${project.version.major}",
 55                 vendor    : 'Amazon',
 56                 packager  : 'Amazon',
 57                 description  : 'Amazon Corretto\'s packaging of the runtime core elements of the OpenJDK code.',
 58                 license   : 'ASL 1.1 and ASL 2.0 and BSD and BSD with advertising and GPL+ and GPLv2 and GPLv2 with ' +
 59                         'exceptions and IJG and LGPLv2+ and MIT and MPLv2.0 and Public Domain and W3C and zlib and ' +
 60                         'ISC and FTL and RSA.',
 61                 maintainer: 'Amazon Corretto Team <corretto-team@amazon.com>',
 62                 buildHost : 'build.amazon.com'
 63         ]
 64         jdkTools = ['java', 'keytool', 'rmid', 'rmiregistry', 'jjs', 'pack200', 'unpack200', 'javac', 'jaotc', 'jlink',
 65             'jmod', 'jhsdb', 'jar', 'jarsigner', 'javadoc', 'javap', 'jcmd', 'jconsole', 'jdb', 'jdeps', 'jdeprscan',
 66             'jimage', 'jinfo', 'jmap', 'jps', 'jrunscript', 'jshell', 'jstack', 'jstat', 'jstatd', 'rmic', 'serialver']
 67         // artifact names
 68         caCerts = "cacerts"
 69         sourceTar = "amazon-corretto-source-${project.version.full}.tar.gz"
 70 
 71         configurationFiles = [
 72             'conf/logging.properties',
 73             'conf/net.properties',
 74             'conf/sound.properties',
 75             'conf/security/policy/limited/default_local.policy',
 76             'conf/security/policy/limited/exempt_local.policy',
 77             'conf/security/policy/limited/default_US_export.policy',
 78             'conf/security/policy/unlimited/default_local.policy',
 79             'conf/security/policy/unlimited/default_US_export.policy',
 80             'conf/security/java.security',
 81             'conf/management/jmxremote.access',
 82             'conf/management/management.properties',
 83             'lib/security/blocked.certs',
 84             'lib/security/public_suffix_list.dat'
 85             ];
 86 
 87         correttoCommonFlags = [
 88                 "--with-freetype=bundled",
 89                 '--with-jvm-features=zgc',
 90                 "--with-version-build=${project.version.build}",
 91                 '--with-version-pre=',
 92                 "--with-vendor-bug-url=https://github.com/corretto/corretto-${project.version.major}/issues/",
 93                 "--with-vendor-vm-bug-url=https://github.com/corretto/corretto-${project.version.major}/issues/",
 94                 '--with-vendor-name=Amazon.com Inc.',
 95                 '--with-vendor-url=https://aws.amazon.com/corretto/',
 96                 "--with-vendor-version-string=Corretto-${project.version.full}"
 97         ]
 98 
 99         // If this is a certified release build, exclude the README.JAVASE file
100         excludeReadmeJavaSE = Boolean.parseBoolean(project.findProperty("corretto.excludeReadmeJavaSE"))
101         rootFiles = ['ADDITIONAL_LICENSE_INFO', 'ASSEMBLY_EXCEPTION', 'LICENSE', 'README.md', 'commitId.txt', 'version.txt']
102         if (!excludeReadmeJavaSE) {
103             rootFiles += 'README.JAVASE'
104         }
105 
106         // If the version opt is empty, then the JDK will set to  'adhoc.<username>.<base dir name>'
107         versionOpt = project.findProperty("corretto.versionOpt") ?: "LTS" // Set the LTS when needed
108         correttoCommonFlags += ["--with-version-opt=${versionOpt}"]
109 
110         def versionDate = project.findProperty("corretto.versionDate")
111         if (versionDate) {
112             correttoCommonFlags += ["--with-version-date=${versionDate}"]
113         }
114 
115         def gtest = project.findProperty("corretto.gtest")
116         if (gtest) {
117             correttoCommonFlags += ["--with-gtest=${gtest}"]
118         }
119 
120         // This should be public so we can know if we should build the micro-benchmarks
121         jmh = project.findProperty("corretto.jmh")
122         if (jmh) {
123             correttoCommonFlags += ["--with-jmh=${jmh}"]
124         }
125 
126         // If asyncProfilerBinariesPath provided, store for easy reference when bundling
127         asyncProfilerBinariesPath = project.findProperty("asyncProfilerBinariesPath") ?: ""
128         asyncProfilerLegal = "${rootDir}/installers/legal/async-profiler.md"
129 
130         // Valid value: null, release, debug, fastdebug, slowdebug
131         correttoDebugLevel = "release" // Default: release
132         switch(project.findProperty("corretto.debug_level")) {
133             case null:
134             case "release":
135                 correttoCommonFlags += ['--with-debug-level=release', '--with-native-debug-symbols=zipped']
136                 break
137             case "fastdebug":
138                 correttoDebugLevel = "fastdebug"
139                 correttoCommonFlags += ['--with-debug-level=fastdebug', '--with-native-debug-symbols=zipped']
140                 break
141             case "debug":
142             case "slowdebug":
143                 correttoDebugLevel = "slowdebug"
144                 correttoCommonFlags += ['--with-debug-level=slowdebug', '--with-native-debug-symbols=external']
145                 break
146             default:
147                 throw new RuntimeException("Invalid corretto.debug_level")
148         }
149 
150         // customized flags. Identify the index of double dashes as the start of a flag
151         String extraConfig = project.findProperty("corretto.extra_config")
152         def lastIndex = -1
153         if (extraConfig != null) {
154             for (int index = extraConfig.indexOf("--"); index >= 0; index = extraConfig.indexOf("--", index + 1)) {
155                 if (lastIndex != -1) {
156                     correttoCommonFlags += extraConfig.substring(lastIndex, index).trim()
157                 }
158                 lastIndex = index
159             }
160             if (lastIndex != -1) {
161                 correttoCommonFlags += extraConfig.substring(lastIndex).trim()
162             }
163         }
164 
165         // Determine the system architecture
166         def jdkArch = ""
167         def os = ""
168         if (Os.isFamily(Os.FAMILY_MAC)) {
169             os = 'macosx'
170             nativeArch = ['uname', '-m'].execute().text.trim()
171             if (nativeArch == "arm64") {
172                 jdkArch = "aarch64"
173             } else {
174                 jdkArch = "x86_64"
175             }
176         } else if (Os.isFamily(Os.FAMILY_UNIX)) {
177             os = 'linux'
178             jdkArch = ['uname', '-m'].execute().text.trim()
179             // `uname -m` returns the host arch in a linux x86 docker instance. Pass a flag
180             // to enable
181             if (project.hasProperty("x86") && Boolean.parseBoolean(project.getProperty('x86'))) {
182                 jdkArch = "x86"
183             }
184         } else if (Os.isFamily(Os.FAMILY_WINDOWS)) {
185             os = 'windows'
186             def arch = System.getenv('PROCESSOR_ARCHITECTURE')
187             if (arch == 'AMD64') {
188                 jdkArch = "x86_64"
189             } else { //x86
190                 throw new GradleException("${arch} is not suported")
191             }
192         } else {
193             throw new GradleException("OS is not supported")
194         }
195 
196         // Ext property: correttoArch
197         switch (jdkArch) {
198             case 'x86':
199             case 'aarch64':
200                 correttoArch = jdkArch
201                 break
202             case 'x86_64':
203                 correttoArch = 'x64'
204                 break
205             default:
206                 throw new GradleException("${jdkArch} is not supported")
207         }
208 
209         // Call toString explicitly to avoid lazy evaluation
210         jdkImageName = "${os}-${jdkArch}-server-${correttoDebugLevel}".toString()
211         // no debug level suffix for release build
212         if (correttoDebugLevel == "release") {
213             correttoJdkArchiveName = "amazon-corretto-${project.version.full}-${os}-${correttoArch}".toString()
214         } else {
215             correttoJdkArchiveName =
216                     "amazon-corretto-${project.version.full}-${os}-${correttoArch}-${correttoDebugLevel}".toString()
217         }
218         correttoDebugSymbolsArchiveName = "amazon-corretto-debugsymbols-${project.version.full}-${os}-${correttoArch}".toString()
219         correttoTestImageArchiveName = "amazon-corretto-testimage-${project.version.full}-${os}-${correttoArch}".toString()
220     }
221 }
222 
223 project(':prebuild') {
224     apply plugin: 'java'
225 
226     configurations {
227         cacerts
228     }
229 
230     ext {
231         cacertDir = "$distributionDir/${project.caCerts}"
232     }
233 
234     def preBuildSrc = "src"
235     def classPath = "classes"
236     def generateToolMain = "build.tools.generatecacerts.GenerateCacerts"
237 
238     task copyToolSrc(type: Copy) {
239         description = 'Copy utility tool source to the project root'
240         from fileTree("$rootDir/make/jdk/src/classes") {
241             include 'build/tools/generatecacerts/*'
242         }
243         into preBuildSrc
244     }
245 
246     task buildTool(type: JavaCompile) {
247         dependsOn copyToolSrc
248         source = fileTree(dir: preBuildSrc, include: '**/*.java')
249         destinationDirectory = file(classPath)
250         classpath = files(classPath)
251     }
252 
253     task generateJdkCacerts(type: JavaExec) {
254         dependsOn buildTool
255         def jdkCaDir = "$rootDir/src/java.base/share/data/cacerts"
256 
257         description = 'Generate Cacerts from JDK source'
258         classpath = files(classPath)
259 
260         // See commit for [JDK-8275252](https://github.com/corretto/corretto-jdk/commit/bd2b41dd7062c50f3aaebec2137d5fdd9546c120)
261         jvmArgs = ['-Dkeystore.pkcs12.certProtectionAlgorithm=NONE', '-Dkeystore.pkcs12.macAlgorithm=NONE']
262         mainClass = generateToolMain
263         args = [jdkCaDir, project.caCerts]
264     }
265 
266     task importAmazonCacerts(type: Exec) {
267         dependsOn generateJdkCacerts
268         // Default password for JSSE key store
269         def keystore_password = "changeit"
270         commandLine 'keytool', '-importkeystore', '-noprompt',
271                 '-srckeystore', "$rootDir/amazon-cacerts",
272                 '-srcstorepass', keystore_password,
273                 '-destkeystore', caCerts,
274                 '-deststorepass', ''
275     }
276 
277     task copyCacerts(type: Copy) {
278         dependsOn importAmazonCacerts
279         from caCerts
280         into distributionDir
281     }
282 
283     artifacts {
284         cacerts file: file("$distributionDir/${caCerts}"), builtBy: copyCacerts
285     }
286 }
287 
288 project(':openjdksrc') {
289     /**
290      * Compresses a snapshot of the source code used to perform the build.
291      */
292     task sourceDistributionTarball(type: Tar) {
293         description = 'Assemble source files required for building and distributing Corretto.'
294         compression = Compression.GZIP
295         archiveFileName = sourceTar
296         def sourceTarRootFiles = ['LICENSE',
297                     'ADDITIONAL_LICENSE_INFO',
298                     'README',
299                     'README.md',
300                     'ASSEMBLY_EXCEPTION',
301                     'commitId.txt',
302                     'version.txt',
303                     'amazon-cacerts',
304                     'configure',
305                     'Makefile',
306                     'bin/**',
307                     'doc/**',
308                     'make/**',
309                     'src/**',
310                     'test/**']
311         if (!excludeReadmeJavaSE) {
312             sourceTarRootFiles += 'README.JAVASE'
313         }
314 
315         from fileTree(rootDir) {
316             include sourceTarRootFiles
317         }
318     }
319 
320     artifacts {
321         archives sourceDistributionTarball
322     }
323 }