shared-settings.gradle (8919B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 6 import org.yaml.snakeyaml.Yaml 7 8 import java.time.format.DateTimeFormatter 9 10 buildscript { 11 if (!gradle.root.hasProperty("mozconfig")){ 12 apply from: file('./gradle/mozconfig.gradle') 13 } else { 14 gradle.ext.mozconfig = gradle.root.mozconfig 15 } 16 17 repositories { 18 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository -> 19 maven { 20 url = repository 21 if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) { 22 allowInsecureProtocol = true 23 } 24 } 25 } 26 } 27 28 dependencies { 29 classpath 'org.yaml:snakeyaml:2.2' 30 } 31 } 32 33 if (!gradle.root.hasProperty("mozconfig")){ 34 apply from: file('./gradle/mozconfig.gradle') 35 } else { 36 gradle.ext.mozconfig = gradle.root.mozconfig 37 } 38 39 // Synchronized library configuration for all modules 40 // This "componentsVersion" number is defined in "version.txt" and should follow 41 // semantic versioning (MAJOR.MINOR.PATCH). See https://semver.org/ 42 class Config { 43 44 public final String componentsVersion 45 public final String componentsGroupId 46 public final Integer jvmTargetCompatibility 47 public final Integer compileSdkMajorVersion 48 public final Integer compileSdkMinorVersion 49 public final Integer minSdkVersion 50 public final Integer targetSdkVersion 51 public final String ndkVersion 52 53 54 Config( 55 String componentsVersion, 56 String componentsGroupId, 57 Integer jvmTargetCompatibility, 58 Integer compileSdkMajorVersion, 59 Integer compileSdkMinorVersion, 60 Integer minSdkVersion, 61 Integer targetSdkVersion, 62 String ndkVersion 63 ) { 64 this.componentsVersion = componentsVersion 65 this.componentsGroupId = componentsGroupId 66 this.jvmTargetCompatibility = jvmTargetCompatibility 67 this.compileSdkMajorVersion = compileSdkMajorVersion 68 this.compileSdkMinorVersion = compileSdkMinorVersion 69 this.minSdkVersion = minSdkVersion 70 this.targetSdkVersion = targetSdkVersion 71 this.ndkVersion = ndkVersion 72 } 73 } 74 75 def setupProject(name, path, description) { 76 settings.include(":$name") 77 78 project(":$name").projectDir = new File(gradle.mozconfig.topsrcdir, "mobile/android/android-components/${path}") 79 80 // project(...) gives us a skeleton project that we can't set ext.* on 81 gradle.beforeProject { project -> 82 // However, the "afterProject" listener iterates over every project and gives us the actual project 83 // So, once we filter for the project we care about, we can set whatever we want 84 if (project.path == ":$name") { 85 project.ext.description = description 86 } 87 } 88 } 89 90 91 // Mimic Python: open(os.path.join(buildconfig.topobjdir, 'buildid.h')).readline().split()[2] 92 def getBuildId() { 93 if (System.env.MOZ_BUILD_DATE) { 94 if (System.env.MOZ_BUILD_DATE.length() == 14) { 95 return System.env.MOZ_BUILD_DATE 96 } 97 logger.warn("Ignoring invalid MOZ_BUILD_DATE: ${System.env.MOZ_BUILD_DATE}") 98 } 99 return file("${gradle.mozconfig.topobjdir}/buildid.h").getText('utf-8').split()[2] 100 } 101 102 // Return a manifest version string that respects the Firefox version format, 103 // see: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/version#version_format 104 def getManifestVersionString(componentsVersion) { 105 // We assume that the `version.txt` file will always contain a version 106 // string with at least two parts separated with a dot. Below, we extract 107 // each part, and we make sure that there is no letter, e.g. `"0a2"` would 108 // become `"0"`. 109 String[] parts = componentsVersion.split("\\.").collect { 110 part -> part.split("a|b")[0] 111 }; 112 113 // Per https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/version, 114 // each part can have up to 9 digits. Note the single `H` when formatting the output avoid 115 // leading zeros, which are not allowed. 116 def buildDate = LocalDateTime.parse(getBuildId(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) 117 def dateAndTime = buildDate.format(DateTimeFormatter.ofPattern("YYYYMMdd.Hmmss")); 118 119 return "${parts[0]}.${parts[1]}.${dateAndTime}"; 120 } 121 122 def yaml = new Yaml() 123 def buildConfigFile = new File(gradle.mozconfig.topsrcdir, "mobile/android/android-components/.buildconfig.yml") 124 def buildconfig = yaml.load(buildConfigFile.newInputStream()) 125 126 // Setup the parent ":components" project 127 settings.include(":components") 128 project(":components").projectDir = new File(gradle.mozconfig.topsrcdir, "mobile/android/android-components/components") 129 130 buildconfig.projects.each { project -> 131 // If we're building A-C, set up all projects, otherwise exclude samples e.g., if we're building Fenix or Focus. 132 // The samples are not relevant for the Fenix and Focus builds. 133 if (rootDir.toString().contains("android-components") || !project.key.startsWith("samples")) { 134 setupProject(project.key, project.value.path, project.value.description) 135 } 136 } 137 138 gradle.projectsLoaded { -> 139 def versionFile = new File(gradle.mozconfig.topsrcdir, "mobile/android/version.txt") 140 String version = versionFile.text.stripTrailing() 141 142 def configDataFile = new File(gradle.mozconfig.topsrcdir, "mobile/android/android-components/.config.yml") 143 def configData = yaml.load(configDataFile.newInputStream()) 144 145 if (gradle.rootProject.hasProperty("nightlyVersion")) { 146 version = gradle.rootProject.nightlyVersion 147 } else if (gradle.rootProject.hasProperty("local")) { 148 // To support local auto-publication workflow, we use a version prefix we wouldn't normally encounter. 149 version = "0.0.1" 150 } else if (gradle.hasProperty("localProperties.branchBuild.android-components.version")) { 151 version = gradle.getProperty("localProperties.branchBuild.android-components.version") 152 } 153 // get the ndk version from the external build environment. 154 def ndkVersion = "${gradle.mozconfig.substs.ANDROID_NDK_MAJOR_VERSION}.${gradle.mozconfig.substs.ANDROID_NDK_MINOR_VERSION}" 155 156 // Wait until root project is "loaded" before we set "config" 157 // Note that since this is set on "rootProject.ext", it will be "in scope" during the evaluation of all projects' 158 // gradle files. This means that they can just access "config.<value>", and it'll function properly 159 gradle.rootProject.ext.config = new Config( 160 version, 161 configData.componentsGroupId, 162 configData.jvmTargetCompatibility, 163 configData.compileSdkMajorVersion, 164 configData.compileSdkMinorVersion, 165 configData.minSdkVersion, 166 configData.targetSdkVersion, 167 ndkVersion 168 ) 169 170 gradle.rootProject.ext.buildConfig = buildconfig 171 172 // Define a reusable task for updating the version in manifest.json for modules that package 173 // a web extension. We automate this to make sure we never forget to update the version, either 174 // in local development or for releases. In both cases, we want to make sure the latest version 175 // of all extensions (including their latest changes) are installed on first start-up. 176 gradle.rootProject.allprojects { 177 ext.updateExtensionVersion = { task, extDir -> 178 configure(task) { 179 from extDir 180 include 'manifest.template.json' 181 rename { 'manifest.json' } 182 into extDir 183 184 def values = ['version': getManifestVersionString(rootProject.ext.config.componentsVersion)] 185 inputs.properties(values) 186 expand(values) 187 } 188 } 189 } 190 191 // Initialize all project buildDirs to be in ${topobjdir} to follow 192 // conventions of mozilla-central build system. 193 gradle.rootProject.allprojects { project -> 194 def topSrcPath = file(gradle.mozconfig.topsrcdir).toPath() 195 def topObjPath = file(gradle.mozconfig.topobjdir).toPath() 196 197 def sourcePath = project.getBuildFile().toPath().getParent() 198 def relativePath = topSrcPath.relativize(sourcePath) 199 200 if (relativePath.startsWith("..")) { 201 // The project doesn't appear to be in topsrcdir so leave the 202 // buildDir alone. 203 } else { 204 // Transplant the project path into "${topobjdir}/gradle/build". 205 // This is consistent with existing gradle / taskcluster 206 // configurations but less consistent with the result of the 207 // non-gradle build system. 208 project.layout.buildDirectory.set(topObjPath.resolve("gradle/build").resolve(relativePath).toFile()) 209 } 210 } 211 }