-
Notifications
You must be signed in to change notification settings - Fork 30
/
build.gradle
499 lines (409 loc) · 18.9 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/***
* CERT Kaiju
* Copyright 2021 Carnegie Mellon University.
*
* NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING
* INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY
* MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER
* INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR
* MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL.
* CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT
* TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
*
* Released under a BSD (SEI)-style license, please see LICENSE.md or contact [email protected] for full terms.
*
* [DISTRIBUTION STATEMENT A] This material has been approved for public release and unlimited distribution.
* Please see Copyright notice for non-US Government use and distribution.
*
* Carnegie Mellon (R) and CERT (R) are registered in the U.S. Patent and Trademark Office by Carnegie Mellon University.
*
* This Software includes and/or makes use of the following Third-Party Software subject to its own license:
* 1. OpenJDK (http://openjdk.java.net/legal/gplv2+ce.html) Copyright 2021 Oracle.
* 2. Ghidra (https://github.com/NationalSecurityAgency/ghidra/blob/master/LICENSE) Copyright 2021 National Security Administration.
* 3. GSON (https://github.com/google/gson/blob/master/LICENSE) Copyright 2020 Google.
* 4. JUnit (https://github.com/junit-team/junit5/blob/main/LICENSE.md) Copyright 2020 JUnit Team.
* 5. Gradle (https://github.com/gradle/gradle/blob/master/LICENSE) Copyright 2021 Gradle Inc.
* 6. markdown-gradle-plugin (https://github.com/kordamp/markdown-gradle-plugin/blob/master/LICENSE.txt) Copyright 2020 Andres Almiray.
* 7. Z3 (https://github.com/Z3Prover/z3/blob/master/LICENSE.txt) Copyright 2021 Microsoft Corporation.
* 8. jopt-simple (https://github.com/jopt-simple/jopt-simple/blob/master/LICENSE.txt) Copyright 2021 Paul R. Holser, Jr.
*
* DM21-0792
*/
// Builds a Ghidra Extension for a given Ghidra installation.
//
// An absolute path to the Ghidra installation directory must be supplied either by setting the
// GHIDRA_INSTALL_DIR environment variable or Gradle project property:
//
// > export GHIDRA_INSTALL_DIR=<Absolute path to Ghidra>
// > gradle
//
// or
//
// > gradle -PGHIDRA_INSTALL_DIR=<Absolute path to Ghidra>
//
// Gradle should be invoked from the directory of the project to build. Please see the
// application.gradle.version property in <GHIDRA_INSTALL_DIR>/Ghidra/application.properties
// for the correction version of Gradle to use for the Ghidra installation you specify.
//apply plugin: 'java' // adds 'test' task
//apply plugin: MarkdownPlugin // adds a markdownToHtml task, see buildSrc/ directory
plugins {
id 'java'
id 'kaiju.gradle.ghidrahtml'
id "de.undercouch.download" version "5.5.0" // used to download z3 source zip archive
id "io.github.tomtzook.gradle-cmake" version "1.2.2" // used to build z3 automatically
id 'com.adarshr.test-logger' version '4.0.0' // used because gradle sucks at showing test output
}
//----------------------START "DO NOT MODIFY" SECTION------------------------------
def ghidraInstallDir
if (System.env.GHIDRA_INSTALL_DIR) {
ghidraInstallDir = System.env.GHIDRA_INSTALL_DIR
}
else if (project.hasProperty("GHIDRA_INSTALL_DIR")) {
ghidraInstallDir = project.getProperty("GHIDRA_INSTALL_DIR")
}
if (ghidraInstallDir) {
apply from: new File(ghidraInstallDir).getCanonicalPath() + "/support/buildExtension.gradle"
}
else {
throw new GradleException("GHIDRA_INSTALL_DIR is not defined!")
}
//----------------------END "DO NOT MODIFY" SECTION-------------------------------
OperatingSystem os = org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem;
// change this to set the version of Z3 we're using
String z3Version = "4.12.1"
def autocatsDir
if (System.env.KAIJU_AUTOCATS_DIR) {
autocatsDir = System.env.KAIJU_AUTOCATS_DIR
}
else if (project.hasProperty("KAIJU_AUTOCATS_DIR")) {
autocatsDir = project.getProperty("KAIJU_AUTOCATS_DIR")
}
// allow installation using:
// > gradle -PGHIDRA_INSTALL_DIR=/path/to/ghidra install
// you can also allow removal of the any previous Kaiju install prior to
// reinstalling by adding -PKAIJU_AUTO_REMOVE
import org.gradle.util.VersionNumber
// Gradle 6.4+ is needed to compile the markdown gradle plugin written in groovy
// Ghidra 10 requires gradle 6.8 or above
if (VersionNumber.parse( gradle.gradleVersion ) <= VersionNumber.parse ("6.8.0")) {
throw new GradleException("CERT Kaiju requires at least gradle version 6.8.0. Please update gradle and retry.")
}
// Export GHIDRA_10_4 and _11_1 for conditional compilation
def ghidraVersion = project.ext.ghidra_version;
compileJava {
options.compilerArgs << '-AGHIDRA_10_4=' + (VersionNumber.parse( ghidraVersion ) >= VersionNumber.parse("10.4")) \
<< '-AGHIDRA_11_1=' + (VersionNumber.parse( ghidraVersion ) >= VersionNumber.parse("11.1"))
}
compileTestJava {
options.compilerArgs << '-AGHIDRA_10_4=' + (VersionNumber.parse( ghidraVersion ) >= VersionNumber.parse("10.4")) \
<< '-AGHIDRA_11_1=' + (VersionNumber.parse( ghidraVersion ) >= VersionNumber.parse("11.1"))
}
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'systems.manifold:manifold-preprocessor:2023.1.28'
testAnnotationProcessor 'systems.manifold:manifold-preprocessor:2023.1.28'
testImplementation(
'junit:junit:4.12', // needed on classpath to support junit 5 vintage
'org.junit.jupiter:junit-jupiter-api:5.9.2',
'org.junit.jupiter:junit-jupiter-engine:5.9.2',
'org.junit.jupiter:junit-jupiter-migrationsupport:5.9.2'
)
testRuntimeOnly(
'org.junit.jupiter:junit-jupiter-engine:5.9.2',
'org.junit.vintage:junit-vintage-engine:5.9.2'
)
}
tasks.withType(JavaCompile) {
// If you DO NOT define a module-info.java file:
options.compilerArgs += ['-Xplugin:Manifold']
}
// don't include files not needed for runtime!
buildExtension.exclude 'azure-pipelines.yml'
buildExtension.exclude '*.bak'
buildExtension.exclude 'buildSrc/**'
buildExtension.exclude '3rdparty/**'
buildExtension.exclude '.github/**'
buildExtension.exclude '.vscode/**'
task printGhidraDir {
println 'Using Ghidra install directory ' + ghidraInstallDir
println 'Using Java ' + targetCompatibility + ' for Ghidra ' + ghidraVersion
println 'Using AUTOCATS test directory ' + autocatsDir
println 'Targeting '+ os.getDisplayName() + ' (' + System.properties['os.name'].toLowerCase() + '-' + System.getProperty('os.arch') + ')'
}
task copyToLib(type: Copy) {
outputs.file file("lib/gson-2.8.6.jar")
if (!file("lib/gson-2.8.6.jar").exists()) {
configurations {
gsonDependency
}
dependencies {
gsonDependency group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}
from configurations.gsonDependency
into "lib"
}
outputs.file file("lib/jopt-simple-5.0.4.jar")
if (!file("lib/jopt-simple-5.0.4.jar").exists()) {
configurations {
joptDependency
}
dependencies {
joptDependency group: 'net.sf.jopt-simple', name: 'jopt-simple', version: '5.0.4'
}
from configurations.joptDependency
into "lib"
}
// This will copy com.microsoft.z3.jar without knowing the architecture directory
from fileTree('build/cmake/z3') {
include '**/com.microsoft.z3.jar'
}
into 'lib'
eachFile {
it.path = it.name
}
includeEmptyDirs = false
}
// BEGIN: if user gives flag, skip building Z3
if (!project.hasProperty('KAIJU_SKIP_Z3_BUILD')) {
// download task uses plugin:
// https://github.com/michel-kraemer/gradle-download-task
task downloadZ3Source(type: Download) {
src 'https://github.com/Z3Prover/z3/archive/refs/tags/z3-4.12.1.zip'
dest new File('3rdparty', 'z3-4.12.1.zip')
overwrite false
}
task downloadAndUnzipZ3Source(dependsOn: downloadZ3Source, type: Copy) {
from zipTree(downloadZ3Source.dest)
into '3rdparty'
filesMatching("**") {
it.path = it.path.replace("z3-z3-4.12.1", "z3")
}
includeEmptyDirs = true
doLast {
// the zip creates these directories even though renamed?
// so just clear it during this task
if (file('3rdparty/z3-z3-4.12.1').exists()) {
def z3Dir2 = new File('3rdparty/z3-z3-4.12.1')
z3Dir2.deleteDir()
}
}
onlyIf {
!file('3rdparty/z3/CMakeLists.txt').exists()
}
}
buildExtension.dependsOn(downloadAndUnzipZ3Source)
task cleanZ3Source(type: Delete) {
doLast {
def z3Dir = new File('3rdparty/z3')
z3Dir.deleteDir()
}
}
tasks.clean.dependsOn(cleanZ3Source)
// BEGIN: cmake z3 build section
// based on: https://github.com/tomtzook/gradle-cmake
cmake {
targets {
z3 {
cmakeLists.set(file('3rdparty/z3/CMakeLists.txt'))
targetMachines.add(machines.host)
// we could use ninja instead of make
// but for cross-compatibility reasons,
// we're letting the default run
if (os.isLinux()) {
try {
def result = exec {
ignoreExitValue = true
commandLine 'ninja', '--version'
}
generator.set(generators.ninja)
} catch (Exception e) {
// do nothing, just let the default generator run
}
}
// pass options to cmake
cmakeArgs.add('-DCMAKE_BUILD_TYPE=Release')
cmakeArgs.add('-DZ3_BUILD_LIBZ3_SHARED=true')
cmakeArgs.add('-DZ3_BUILD_JAVA_BINDINGS=true')
cmakeArgs.add('-DZ3_USE_LIB_GMP=false')
cmakeArgs.add('-DZ3_LINK_TIME_OPTIMIZATION=true')
cmakeArgs.add('-DZ3_BUILD_EXECUTABLE=false')
cmakeArgs.add('-DZ3_BUILD_TEST_EXECUTABLES=false')
cmakeArgs.add('-DZ3_ENABLE_EXAMPLE_TARGETS=false')
//cmakeArgs.add('-DZ3_INCLUDE_GIT_HASH=false')
//cmakeArgs.add('-DZ3_INCLUDE_GIT_DESCRIBE=false')
// if wanted to pass options directly to make/ninja, would also use:
// generatorArgs.add()
}
}
}
tasks.clean.dependsOn(tasks.cmakeClean)
// This is our task for building z3. It's only really needed to work around a
// bug in the gradle cmake plugin that always causes linking to occur, which is
// slow because LTO. So we dynamically decide whether to set tasks.cmakeBuild as
// a dependency depending on whether z3 appears to be built.
task buildZ3 {
if (!(file('build/cmake/z3/linux-amd64/libz3.so').exists() ||
file('build/cmake/z3/windows-amd64/libz3.dll').exists() ||
file('build/cmake/z3/osx-amd64/libz3.dylib').exists())) {
dependsOn(tasks.cmakeBuild)
}
}
tasks.build.dependsOn(tasks.buildZ3)
buildExtension.dependsOn(tasks.buildZ3)
copyToLib.dependsOn(tasks.buildZ3)
project.getTasks().matching(
(task)-> {
task.getName().contains("cmakez3") || task.getName().contains("z3_");
}
).configureEach(
(task)-> {
task.dependsOn(downloadAndUnzipZ3Source);
}
);
if (os.isLinux()) {
task copyZ3toOsLinux(type: Copy) {
from ("build/cmake/z3/linux-amd64/libz3.so")
from ("build/cmake/z3/linux-amd64/libz3java.so")
into "os/linux_x86_64"
}
copyZ3toOsLinux.dependsOn(tasks.buildZ3)
copyToLib.dependsOn(copyZ3toOsLinux)
}
if (os.isWindows()) {
task copyZ3toOsWin(type: Copy) {
from ("build/cmake/z3/windows-amd64/libz3.dll")
from ("build/cmake/z3/windows-amd64/libz3java.dll")
into "os/win_x86_64"
}
copyZ3toOsWin.dependsOn(tasks.buildZ3)
copyToLib.dependsOn(copyZ3toOsWin)
}
if (os.isMacOsX()) {
task copyZ3toOsMac(type: Copy) {
from ("build/cmake/z3/osx-amd64/libz3.dylib")
from ("build/cmake/z3/osx-amd64/libz3java.dylib")
into "os/mac_x86_64"
}
copyZ3toOsMac.dependsOn(tasks.buildZ3)
copyToLib.dependsOn(copyZ3toOsMac)
}
// END: cmake z3 build section
}
// END
copyDependencies.dependsOn(copyToLib)
buildExtension.dependsOn(copyToLib)
compileJava.dependsOn(copyToLib)
indexHelp.dependsOn(copyToLib)
indexHelp.dependsOn(markdownToGhidraHtml)
buildHelp.dependsOn(markdownToGhidraHtml)
zipSource.dependsOn(markdownToGhidraHtml)
processResources.dependsOn(markdownToGhidraHtml)
markdownToGhidraHtml.sourceDir = file('docs/')
markdownToGhidraHtml.outputDir = file('src/main/help/help/topics/kaiju/')
task uninstallPreviousOOAnalyzerPlugin {
group = "Installation"
description = "Removes a previous OOAnalyzerPlugin installation, or notifies user of previous installation"
doLast {
// check if a previous OOAnalyzer installation exists
if (file(ghidraInstallDir + '/Ghidra/Extensions/' + 'OOAnalyzerPlugin/').exists()) {
// automatically remove past install if user specifies
if (project.hasProperty('KAIJU_AUTO_REMOVE')) {
//delete ghidraInstallDir + '/Ghidra/Extensions/' + 'OOAnalyzerPlugin/'
def ooDir = new File(ghidraInstallDir + '/Ghidra/Extensions/' + 'OOAnalyzerPlugin/')
ooDir.deleteDir()
logger.quiet("SUCCESS! Previous standalone OOAnalyzerPlugin installation was removed automatically by gradle.")
} else {
throw new GradleException("FATAL ERROR! A previous installation of the standalone OOAnalyzerPlugin was detected. It is recommended that you either manually remove this installation before re-installing Kaiju, or re-run this gradle installation script with the -PKAIJU_AUTO_REMOVE option. Please note that the standalone plugin is now deprecated; we recommend you use the latest version included with Kaiju instead.")
}
}
}
outputs.upToDateWhen { false } // ensures never cached, always runs if user enables
dependsOn buildExtension
}
task uninstallPreviousKaiju {
group = "Installation"
description = "Removes a previous Kaiju installation, or notifies user of previous installation"
doLast {
// check first if a previous Kaiju installation exists
if (file(ghidraInstallDir + '/Ghidra/Extensions/' + 'kaiju/').exists()) {
// automatically remove past install if user specifies
if (project.hasProperty('KAIJU_AUTO_REMOVE')) {
//delete ghidraInstallDir + '/Ghidra/Extensions/' + 'kaiju/'
def kaijuDir = new File(ghidraInstallDir + '/Ghidra/Extensions/' + 'kaiju/')
kaijuDir.deleteDir()
logger.quiet("SUCCESS! Previous Kaiju installation was removed automatically by gradle.")
} else {
throw new GradleException("FATAL ERROR! A previous installation of Kaiju was detected. It is recommended that you either manually remove this installation before re-installing Kaiju, or re-run this gradle installation script with the -PKAIJU_AUTO_REMOVE option.")
}
}
}
outputs.upToDateWhen { false } // ensures never cached, always runs if user enables
dependsOn uninstallPreviousOOAnalyzerPlugin
}
task copyZip(type: Copy) {
group = "Installation"
description = "Copies zip contents into special Extensions directory so user may enable or disable easily with reinstalling from gradle each time."
from "${buildExtension.destinationDirectory}/${buildExtension.archiveBaseName}.${buildExtension.archiveExtension}"
into ghidraInstallDir + '/Extensions/Ghidra'
dependsOn uninstallPreviousKaiju
}
task installZip(type: Exec) {
group = "Installation"
description = "Installs the zip package in installed Ghidra's default extension directory"
workingDir ghidraInstallDir + '/Ghidra/Extensions'
commandLine 'unzip', '-o', "${buildExtension.destinationDirectory.getAsFile().get().getAbsolutePath()}/${buildExtension.archiveBaseName.get()}.${buildExtension.archiveExtension.get()}"
dependsOn copyZip
mustRunAfter copyZip
}
task install() {
dependsOn installZip
group = "Installation"
description = "Alternate name for installZip task"
//title = "Install zip"
doLast {
// Empty action list
logger.quiet("Installed CERT Kaiju from zip!")
}
}
testlogger {
showPassedStandardStreams = false
slowThreshold = 5000
}
test {
jvmArgs = ['-Djava.awt.headless=true',
// add-exports needed prior to Ghidra 10.3 to address sun.awt not exported in JDK 17+
'--add-exports=java.desktop/sun.awt=ALL-UNNAMED',
// add the kaiju os directories to load z3 library to run tests
'-Djava.library.path=' + ghidraInstallDir + '/Ghidra/Extensions/kaiju/lib/:'+ ghidraInstallDir + '/Ghidra/Extensions/kaiju/os/linux_x86_64/:' + ghidraInstallDir + '/Ghidra/Extensions/kaiju/os/mac_x86_64/:' + ghidraInstallDir + '/Ghidra/Extensions/kaiju/os/win_x86_64/']
useJUnitPlatform()
maxHeapSize = '2G'
failFast = false
// Use testlogger config/plugin instead.
// testLogging {
// events "passed", "skipped", "failed"
// exceptionFormat = 'full'
// showStackTraces = true
// }
beforeSuite { descriptor ->
// check that user has specified the property AND set it (not empty)
if (!autocatsDir || autocatsDir == '') {
throw new GradleException("FATAL ERROR! Kaiju requires specification of the directory where the AUTOCATS tests may be found. Please re-run the tests with the -PKAIJU_AUTOCATS_DIR=<path/to/autocats/dir> command option. Please consult the README.md for more information on AUTOCATS and how to obtain the AUTOCATS tests.")
}
// check that the specified directory actually exists
if (!file(autocatsDir).exists()) {
throw new GradleException("FATAL ERROR! The AUTOCATS path provided to Kaiju does not appear to exist. Please re-check the path then retry the -PKAIJU_AUTOCATS_DIR=<path/to/autocats/dir> command option. You may wish to provide the full absolute path to reduce errors. Please consult the README.md for more information on AUTOCATS and how to obtain the AUTOCATS tests.")
}
// TODO: how can we check if this path is actually an AUTOCATS path?
// for now, just passing along the path and hoping for the best!
}
// only pass on the environment variable if was specified
// this doesn't matter if it wasn't set, because the beforeSuite will execute
// and abort if it wasn't set before running tests, and if you're not
// running tests then it doesn't matter if it was set or not
if (project.hasProperty('KAIJU_AUTOCATS_DIR')) {
environment "KAIJU_AUTOCATS_DIR", project.property('KAIJU_AUTOCATS_DIR')
}
outputs.upToDateWhen { false } // ensures never cached, always runs if user enables
}