1// Script used to build ADT plugins, IDE and monitor
2// There are 2 major tasks done by this plugin:
3//    copydeps: copies tools/base, tools/swt and prebuilt jars into plugins' libs folder
4//    buildEclipse: builds Eclipse by running Tycho from the commandline
5//
6// Usage: $ ANDROID_SRC/tools/gradlew -i -b /path/to/this/build.gradle copydeps|buildEclipse
7
8// get tools/base version
9apply from: "../../tools/buildSrc/base/version.gradle"
10
11repositories {
12    maven { url '../../prebuilts/tools/common/m2/repository' }
13    maven { url '../../out/repo' }
14}
15
16ext {
17    // list of plugins whose manifest should be examined to identify dependencies
18    adtPlugins = new File(projectDir, 'plugins').listFiles().findAll { it.name.startsWith("com.android") }
19
20    //
21    def eclipseBuildDeps = new File(projectDir, "../../prebuilts/eclipse-build-deps").getCanonicalFile()
22    targetComponents = [
23            "platform": new File(eclipseBuildDeps, "platform/org.eclipse.platform-4.2.2.zip"),
24            "cdt"     : new File(eclipseBuildDeps, "cdt/cdt-master-8.0.2.zip"),
25            "emf"     : new File(eclipseBuildDeps, "emf/emf-xsd-Update-2.9.1.zip"),
26            "jdt"     : new File(eclipseBuildDeps, "jdt/org.eclipse.jdt.source-4.2.2.zip"),
27            "wtp"     : new File(eclipseBuildDeps, "wtp/wtp-repo-R-3.3.2-20120210195245.zip"),
28            "gef"     : new File(eclipseBuildDeps, "gef/GEF-Update-3.9.1.zip"),
29            "pde"     : new File(eclipseBuildDeps, "pde/org.eclipse.pde-3.8.zip"),
30            "egit"    : new File(eclipseBuildDeps, "egit/org.eclipse.egit.repository-2.2.0.201212191850-r.zip"),
31    ]
32
33    buildNumber = System.getenv("BUILD_NUMBER")
34    if (buildNumber == null) {
35        buildNumber = "SNAPSHOT"
36    }
37}
38
39// a mapping from the library names as used inside the plugin's MANIFEST.MF to the Maven artifact id
40def artifacts = [
41        // tools/base and tools/swt dependencies
42        'manifest-merger'     : "com.android.tools.build:manifest-merger:$ext.baseVersion",
43        'ddmlib'              : "com.android.tools.ddms:ddmlib:$ext.baseVersion",
44        'ddmuilib'            : "com.android.tools.ddms:ddmuilib:$ext.baseVersion",
45        'layoutlib-api'       : "com.android.tools.layoutlib:layoutlib-api:$ext.baseVersion",
46        'lint-api'            : "com.android.tools.lint:lint-api:$ext.baseVersion",
47        'lint-checks'         : "com.android.tools.lint:lint-checks:$ext.baseVersion",
48        'asset-studio'        : "com.android.tools:asset-studio:$ext.baseVersion",
49        'annotations'         : "com.android.tools:annotations:$ext.baseVersion",
50        'common'              : "com.android.tools:common:$ext.baseVersion",
51        'dvlib'               : "com.android.tools:dvlib:$ext.baseVersion",
52        'hierarchyviewer2lib' : "com.android.tools:hierarchyviewer2lib:$ext.baseVersion",
53        'ninepatch'           : "com.android.tools:ninepatch:$ext.baseVersion",
54        'rule-api'            : "com.android.tools:rule-api:$ext.baseVersion",
55        'sdk-common'          : "com.android.tools:sdk-common:$ext.baseVersion",
56        'sdklib'              : "com.android.tools:sdklib:$ext.baseVersion",
57        'sdkstats'            : "com.android.tools:sdkstats:$ext.baseVersion",
58        'sdkuilib'            : "com.android.tools:sdkuilib:$ext.baseVersion",
59        'swtmenubar'          : "com.android.tools:swtmenubar:$ext.baseVersion",
60        'testutils'           : "com.android.tools:testutils:$ext.baseVersion",
61        'traceview'           : "com.android.tools:traceview:$ext.baseVersion",
62        'uiautomatorviewer'   : "com.android.tools:uiautomatorviewer:$ext.baseVersion",
63
64        // prebuilts
65        'ant-glob'                         : 'com.android.tools.external:ant-glob:1.0',
66        'asm-5.0.3'                        : 'org.ow2.asm:asm:5.0.3',
67        'asm-analysis-5.0.3'               : 'org.ow2.asm:asm-analysis:5.0.3',
68        'asm-tree-5.0.3'                   : 'org.ow2.asm:asm-tree:5.0.3',
69        'commons-codec-1.4'                : 'commons-codec:commons-codec:1.4',
70        'commons-compress-1.0'             : 'org.apache.commons:commons-compress:1.8.1',
71        'commons-logging-1.1.1'            : 'commons-logging:commons-logging:1.1.1',
72        'easymock'                         : 'org.easymock:easymock:3.3',
73        'freemarker-2.3.20'                : 'org.freemarker:freemarker:2.3.20',
74        'guava-17.0'                       : 'com.google.guava:guava:17.0',
75        'host-libprotobuf-java-2.3.0-lite' : 'com.android.tools.external:libprotobuf-java-lite:2.3.0',
76        'httpclient-4.1.1'                 : 'org.apache.httpcomponents:httpclient:4.1.1',
77        'httpcore-4.1'                     : 'org.apache.httpcomponents:httpcore:4.1',
78        'httpmime-4.1'                     : 'org.apache.httpcomponents:httpmime:4.1',
79        'jcommon-1.0.12'                   : 'jfree:jcommon:1.0.12',
80        'jfreechart-1.0.9'                 : 'jfree:jfreechart:1.0.9',
81        'jfreechart-swt-1.0.9'             : 'jfree:jfreechart-swt:1.0.9',
82        'kxml2-2.3.0'                      : 'net.sf.kxml:kxml2:2.3.0',
83        'liblzf-1.0'                       : 'com.android.tools.external:liblzf:1.0',
84        'lombok-ast-0.2.3'                 : 'com.android.tools.external.lombok:lombok-ast:0.2.3',
85        'propertysheet'                    : 'com.android.tools.external:propertysheet:1.0',
86]
87
88configurations {
89    compile
90}
91
92dependencies {
93    compile artifacts.values()
94}
95
96task copydeps << {
97    // get the resolved dependencies from the compile configuration
98    def resolvedDependencies = configurations.compile.resolvedConfiguration.firstLevelModuleDependencies
99
100    // generate a map from "xy.jar" -> "/path/to/xy-1.0.jar"
101    def artifactMap = [:]
102    resolvedDependencies.each { dependency ->
103        def dependencyId = dependency.getName()
104        def artifactName = artifacts.find{ it.value == dependencyId}?.key
105
106        // get the jar file corresponding to the dependency
107        def artifact = getArtifact(dependency)
108        artifactMap.put(artifactName + ".jar", artifact)
109    }
110
111    project.adtPlugins.each { File pluginFile ->
112        def manifestDeps = getManifestDependencies(new File(pluginFile, "META-INF/MANIFEST.MF"))
113        logger.info("Dependencies for " + pluginFile.toString() + ": " + manifestDeps.join(","))
114
115        File dest = new File(pluginFile, "libs")
116        if (!manifestDeps.isEmpty() && !dest.isDirectory()) {
117            dest.mkdirs()
118        }
119
120        manifestDeps.each {
121            if (!artifactMap.containsKey(it)) {
122                throw new RuntimeException("No resolved artifact for: " + it + ", required for: "
123                        + pluginFile.getPath())
124            }
125
126            String destName = artifactMap.get(it)
127            logger.info("\tCopying " + destName + " to " + dest)
128            ant.copy(file: destName, tofile: new File(dest, it))
129        }
130    }
131}
132
133// unzip eclipse prebuilts into the out folder to create a target platform for the build
134task unzipTargetPlatform << {
135    File targetDir = new File(projectDir, "../../out/host/maven/target").getCanonicalFile()
136    targetDir.mkdirs()
137
138    project.targetComponents.each { String k, File v ->
139        File d = new File(targetDir, k)
140        logger.info("Unzipping " + v.getPath() + " into: " + d.getPath())
141        ant.unzip(src: v, dest: d)
142    }
143}
144
145task buildEclipse(type: Exec, dependsOn: unzipTargetPlatform) {
146    def maven = new File(projectDir, "../../prebuilts/eclipse/maven/apache-maven-3.2.1/bin/mvn").getCanonicalFile()
147    def androidOut = new File(projectDir, "../../out").getCanonicalPath()
148    environment("M2_HOME", maven.getParentFile().getParentFile().getCanonicalPath())
149    workingDir projectDir
150    commandLine maven.getCanonicalPath(), "-s", "settings.xml", "-DforceContextQualifier=$project.buildNumber", "-DANDROID_OUT=$androidOut", "package"
151}
152
153private File getArtifact(ResolvedDependency dependency) {
154    if (dependency.moduleArtifacts.size() != 1) {
155        String msg = String.format("Each dependency is expected to map to a single jar file, " +
156                "but %s maps to the following artifacts: %s",
157                dependency,
158                dependency.moduleArtifacts.collect { it.file })
159        throw new RuntimeException(msg);
160    }
161
162    return dependency.moduleArtifacts.iterator().next().file
163}
164
165// parse a plugin's manifest file and return the list of jar dependencies expected to be
166// bundled inside
167private List<String> getManifestDependencies(File manifest) {
168    if (manifest == null || !manifest.exists()) {
169        return []
170    }
171
172    def entries = []
173
174    def fis = new FileInputStream(manifest)
175    try {
176        java.util.jar.Manifest m = new java.util.jar.Manifest(fis)
177        def classPath = m.getMainAttributes().getValue("Bundle-ClassPath")
178        if (classPath == null) {
179            return []
180        }
181
182        classPath.split(',').each {
183            if (!it.equals(".")) {
184                if (!it.startsWith("libs/") || !it.endsWith(".jar")) {
185                    throw new RuntimeException(
186                            "Unexpected classpath entry: " + it + " in file: " + manifest)
187                }
188
189                entries.add(it.substring("libs/".length()))
190            }
191        }
192    } finally {
193        fis.close()
194    }
195
196    return entries
197}
198