GT-3328 corrected sleighArgs.txt use with module dependency paths

This commit is contained in:
ghidra1 2019-11-19 15:11:30 -05:00
parent 083b5f61cc
commit 1b1240a41b
15 changed files with 188 additions and 80 deletions

View file

@ -7,8 +7,6 @@
apply from: "$rootProject.projectDir/gradle/nativeProject.gradle"
*****************************************************************************************/
/*****************************************************************************************
*
* Create a configuration so the a dependency can be declared on the the software modeling
@ -26,21 +24,70 @@ dependencies {
/*****************************************************************************************
*
* Task to write sleigh compiler args to build/data/sleighArgs.txt for use with sleigh
* external sleigh compiler.
* Sleigh compile options to be written to sleighArgs.txt in support of the following
* use cases:
* - Ant build using data/build.xml (development and distribution)
* - Eclipse Sleigh launcher (development only)
* - Ghidra runtime language rebuild (SleighLanguage.reloadLanguage; development and distribution)
* - Distribution build (sleighCompile task; development layout)
*
* This list may be added to or replaced by a specific processor project/module.
*
* Example: MIPS processor module dependency within a slaspec specified as:
*
* @include "$(BaseDir)$(MIPS)/data/language/maips.sinc
*
* with the corresponding MIPS definition specified within the sleighCompileOptions
* list specified within the module's build.gradle file:
*
* sleighCompileOptions.add "-DMIPS=%%MIPS%%"
* -or-
* sleighCompileOptions = [
* "-l",
* "-DMIPS=%%MIPS%%"
* ]
*
*****************************************************************************************/
ext.sleighCompileOptions = [ ]
/*****************************************************************************************
*
* Check for invalid sleighCompileOptions
*
*****************************************************************************************/
def checkSleighCompileOptions() {
sleighCompileOptions.each { a ->
assert !(a.startsWith("-a") || a.startsWith("-i")) : "Invalid sleighCompileOption: ${a}"
}
}
/*****************************************************************************************
*
* Task to write sleigh compiler args for use with sleigh compiler.
* Due to the possible presence of module dependency paths two different sleighArgs.txt
* files are produced: one for development layout (build/tmp/sleighArgs.txt) and
* one for distribution layout ([build/]data/sleighArgs.txt). When invoking the
* Sleigh compiler and using a sleighArgs.txt file the appropriate 'BaseDir' property
* must be specified. Withing a distribution install 'BaseDir' must specifiy the
* path to the install directory while in a development layout 'BaseDir' must specify
* the repos root directory which contains the 'ghidra' repo directory.
*
*****************************************************************************************/
task saveSleighArgs {
def sleighArgsFile = file("build/data/sleighArgs.txt")
outputs.files sleighArgsFile
def sleighArgsDevFile = file("build/tmp/sleighArgs.txt")
outputs.files sleighArgsFile, sleighArgsDevFile
outputs.upToDateWhen { false }
doLast {
checkSleighCompileOptions()
sleighArgsFile.withWriter { out->
project.sleighCompile.args.each { a->
// don't save -a option
if (!"-a".equals(a)) {
out.println a
}
sleighCompileOptions.each { a->
out.println resolveSleighArg(a, false)
}
}
sleighArgsDevFile.withWriter { out->
sleighCompileOptions.each { a->
out.println resolveSleighArg(a, true)
}
}
}
@ -50,6 +97,7 @@ rootProject.prepDev.dependsOn(saveSleighArgs)
apply plugin: 'base'
clean {
delete file("build/data/sleighArgs.txt")
delete file("build/tmp/sleighArgs.txt")
}
/*****************************************************************************************
@ -94,6 +142,7 @@ rootProject.assembleDistribution {
*
*****************************************************************************************/
task sleighCompile (type: JavaExec) {
dependsOn saveSleighArgs
group = rootProject.GHIDRA_GROUP
description " Compiles all the sleigh languages. [processorUtils.gradle]\n"
@ -104,7 +153,12 @@ task sleighCompile (type: JavaExec) {
// Delay adding the directory argument until the first part of the execution phase, so
// that any extra args added by a project override will be added to the arg list before
// these arguments.
// NOTE: projects should no longer add arguments to this task and should instead
// add such args to the sleighCompileOptions list.
doFirst {
args "-i"
args "./build/tmp/sleighArgs.txt"
args "-DBaseDir=${getProjectReposRootPath()}"
args '-a'
args './data/languages'
}
@ -115,11 +169,9 @@ task sleighCompile (type: JavaExec) {
// The task that copies the common files to the distribution folder must depend on
// the sleigh tasks before executing.
rootProject.assembleDistribution.dependsOn sleighCompile
rootProject.assembleDistribution.dependsOn saveSleighArgs
// Add in this projects sleighCompile to the allSleighCompile task
rootProject.allSleighCompile.dependsOn sleighCompile
rootProject.allSleighCompile.dependsOn saveSleighArgs
/*****************************************************************************************
*
@ -154,3 +206,57 @@ sleighCompile.outputs.files (taskOutputs)
// define the sleigh compile inputs to saveSleighArgs to limit task creation to language modules
saveSleighArgs.inputs.files (taskInputs)
/*****************************************************************************************
*
* Gets the absolute repos root directory path with a trailing File separator.
* This path may be used for specifying 'BaseDir' to the sleigh compiler within a
* development layout.
*
*****************************************************************************************/
def getProjectReposRootPath() {
def rootPath = rootProject.projectDir.absolutePath
int index = rootPath.lastIndexOf(File.separator)
rootPath = rootPath.substring(0, index+1)
return rootPath
}
/*****************************************************************************************
*
* Filter a sleigh compiler argument replacing any project/module reference of the form
* %%MODULE%% witha that MODULE's relative path. If useDevPath is true the path will
* include the containing repo directory (e.g., ghidra/Ghidra/...), otherwise the
* path should start at the application root 'Ghidra/'. Only a single replacement per
* arg is supported.
*
* This mechanism relies on the relative depth of a language module project within a
* repository directory hierarchy. In general language module projects must reside
* within the directory Ghidra/Processors.
*
*****************************************************************************************/
def resolveSleighArg(String arg, boolean useDevPath) {
int index = arg.indexOf("%%")
if (index < 0) {
return arg
}
String newArg = arg.substring(0, index)
String tail = arg.substring(index+2)
index = tail.indexOf("%%")
assert index > 0 : "Badly formed sleigh path-replacment option: ${arg}"
String moduleName = tail.substring(0, index)
tail = tail.substring(index+2)
def moduleProject = project(":${moduleName}")
def modulePath
if (useDevPath) {
// first path element is the containing repo directory
modulePath = moduleProject.projectDir.absolutePath
modulePath = modulePath.substring(getProjectReposRootPath().length())
}
else {
// first path element is the Ghidra directory
modulePath = getZipPath(moduleProject)
}
newArg += modulePath
newArg += tail
return newArg
}