1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32configurations {
33    antlr3
34    jflex
35    proguard
36}
37
38ext.antlrSource = 'src/main/antlr3'
39ext.antlrOutput = file("${buildDir}/generated-sources/antlr3")
40
41ext.jflexSource = "src/main/jflex"
42ext.jflexOutput = file("${buildDir}/generated-sources/jflex")
43
44ext.testAntlrSource = 'src/test/antlr3'
45ext.testAntlrOutput = file("${buildDir}/generated-test-sources/antlr3")
46
47sourceSets.main.java.srcDir antlrOutput
48sourceSets.main.java.srcDir jflexOutput
49
50sourceSets.test.java.srcDir testAntlrOutput
51
52idea {
53    module {
54        excludeDirs -= buildDir
55        if (buildDir.exists()) {
56            excludeDirs.addAll(buildDir.listFiles())
57        }
58        for (sourceDir in (sourceDirs + testSourceDirs)) {
59            excludeDirs.remove(sourceDir);
60            while ((sourceDir = sourceDir.getParentFile()) != null) {
61                excludeDirs.remove(sourceDir);
62            }
63        }
64    }
65}
66
67dependencies {
68    compile project(':util')
69    compile project(':dexlib2')
70    compile depends.antlr_runtime
71    compile depends.commons_cli
72
73    testCompile depends.junit
74
75    antlr3 depends.antlr
76    jflex depends.jflex
77    proguard depends.proguard
78}
79
80task generateParserAntlrSource(type: JavaExec) {
81    inputs.dir file(antlrSource)
82    outputs.dir file(antlrOutput)
83
84    mkdir(antlrOutput)
85    def grammars = fileTree(antlrSource).include('**/smaliParser.g')
86
87    classpath = configurations.antlr3
88    main = 'org.antlr.Tool'
89    args '-fo', relativePath("${antlrOutput}/org/jf/smali")
90    args grammars.files
91}
92
93task generateTreeWalkerAntlrSource(type: JavaExec) {
94    inputs.dir file(antlrSource)
95    outputs.dir file(antlrOutput)
96
97    mkdir(antlrOutput)
98    def grammars = fileTree(antlrSource).include('**/smaliTreeWalker.g')
99
100    classpath = configurations.antlr3
101    main = 'org.antlr.Tool'
102    args '-fo', relativePath("${antlrOutput}/org/jf/smali")
103    args grammars.files
104}
105
106task generateTestAntlrSource(type: JavaExec) {
107    inputs.dir file(testAntlrSource)
108    outputs.dir file(testAntlrOutput)
109
110    mkdir(testAntlrOutput)
111    def grammars = fileTree(testAntlrSource).include('**/*.g')
112
113    classpath = configurations.antlr3
114    main = 'org.antlr.Tool'
115    args '-fo', relativePath("${testAntlrOutput}/org/jf/smali")
116    args grammars.files.join(' ')
117}
118
119task generateJflexSource(type: JavaExec) {
120    inputs.dir file(jflexSource)
121    outputs.dir file(jflexOutput)
122
123    mkdir(jflexOutput)
124    def grammars = fileTree(jflexSource).include('**/*.flex')
125
126    classpath = configurations.jflex
127    main = 'JFlex.Main'
128    args '-q'
129    args '-d', relativePath("${jflexOutput}/org/jf/smali")
130    args grammars.files.join(' ')
131    environment LC_ALL: "en_US"
132}
133
134compileJava.dependsOn generateParserAntlrSource, generateTreeWalkerAntlrSource, generateJflexSource
135compileTestJava.dependsOn generateTestAntlrSource
136
137processResources.inputs.property('version', version)
138processResources.expand('version': version)
139
140// This is the jar that gets uploaded to maven
141jar {
142    baseName = 'maven'
143}
144
145// Build a separate jar that contains all dependencies
146task fatJar(type: Jar, dependsOn: jar) {
147    from sourceSets.main.output
148    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
149
150    manifest {
151        attributes("Main-Class": "org.jf.smali.main")
152    }
153
154    doLast {
155        if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
156            ant.symlink(link: file("${destinationDir}/smali.jar"), resource: archivePath, overwrite: true)
157        }
158    }
159}
160tasks.getByPath('build').dependsOn(fatJar)
161
162uploadArchives {
163    repositories.mavenDeployer {
164        pom.project {
165            description 'smali is an assembler for dalvik bytecode'
166            scm {
167                url 'https://github.com/JesusFreke/smali/tree/master/smali'
168            }
169        }
170    }
171}
172
173task sourcesJar(type: Jar) {
174    classifier = 'sources'
175    from sourceSets.main.allJava
176}
177
178artifacts {
179    archives sourcesJar
180}
181
182task proguard(type: JavaExec, dependsOn: fatJar) {
183    def outFile = fatJar.destinationDir.getPath() + '/' + fatJar.baseName + '-' + fatJar.version + '-small' + '.' + fatJar.extension
184    inputs.file fatJar.archivePath
185    outputs.file outFile
186
187    classpath = configurations.proguard
188    main = 'proguard.ProGuard'
189    args "-injars ${fatJar.archivePath}(!**/TestStringTemplate*.class)"
190    args "-outjars ${outFile}"
191    args "-libraryjars ${System.properties['java.home']}/lib/rt.jar"
192    args '-dontobfuscate'
193    args '-dontoptimize'
194    args '-keep public class org.jf.smali.main { public static void main(java.lang.String[]); }'
195    args '-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }'
196    args '-dontwarn com.google.common.**'
197    args '-dontnote com.google.common.**'
198}
199
200tasks.getByPath(':release').dependsOn(proguard)
201