Merge remote-tracking branch 'origin/GP-5125_ryanmkurtz_ghidradev'

(Closes #7047)
This commit is contained in:
Ryan Kurtz 2024-11-14 06:41:53 -05:00
commit 95c63ec84d
2 changed files with 35 additions and 55 deletions

View File

@ -32,6 +32,8 @@ change with future releases.
## Change History ## Change History
__4.0.1:__ __4.0.1:__
* New Ghidra module projects now contain a default `README.md` file. * New Ghidra module projects now contain a default `README.md` file.
* Fixed a bug that prevented an imported module source project from being discovered by Ghidra when
launched with the project's run/debug configuration.
__4.0.0:__ __4.0.0:__
* GhidraDev has been upgraded to be compatible with Ghidra 11.2 and later. It is not backwards * GhidraDev has been upgraded to be compatible with Ghidra 11.2 and later. It is not backwards

View File

@ -62,13 +62,14 @@ public class GhidraModuleUtils {
} }
} }
/** private static String defaultOutputPath = "bin/main";
* Stores a source folder and its corresponding output folder
* private static Map<String, String> sourceToOutputMap =
* @param sourceFolder The source folder Map.ofEntries(Map.entry("src/main/java", defaultOutputPath),
* @param outputFolder The output folder Map.entry("src/main/help", defaultOutputPath),
*/ Map.entry("src/main/resources", defaultOutputPath),
private record SourceFolderInfo(IFolder sourceFolder, IFolder outputFolder) {} Map.entry("src/test/java", "bin/test"),
Map.entry("ghidra_scripts", "bin/scripts"));
/** /**
* Creates a new Ghidra module project with the given name. * Creates a new Ghidra module project with the given name.
@ -97,28 +98,17 @@ public class GhidraModuleUtils {
runConfigMemory, ghidraLayout, jythonInterpreterName, monitor); runConfigMemory, ghidraLayout, jythonInterpreterName, monitor);
IProject project = javaProject.getProject(); IProject project = javaProject.getProject();
// Create source directories // Set default output location
List<SourceFolderInfo> sourceFolderInfos = new ArrayList<>(); javaProject.setOutputLocation(project.getFolder(defaultOutputPath).getFullPath(), monitor);
sourceFolderInfos.add(new SourceFolderInfo(project.getFolder("src/main/java"),
project.getFolder("bin/main"))); // Create source directories and add them to the project's classpath
sourceFolderInfos.add(new SourceFolderInfo(project.getFolder("src/main/help"),
project.getFolder("bin/main")));
sourceFolderInfos.add(new SourceFolderInfo(project.getFolder("src/main/resources"),
project.getFolder("bin/main")));
sourceFolderInfos.add(new SourceFolderInfo(project.getFolder("src/test/java"),
project.getFolder("bin/test")));
sourceFolderInfos.add(new SourceFolderInfo(project.getFolder("ghidra_scripts"),
project.getFolder("bin/scripts")));
for (SourceFolderInfo sourceFolderInfo : sourceFolderInfos) {
GhidraProjectUtils.createFolder(sourceFolderInfo.sourceFolder(), monitor);
}
// Put the source directories in the project's classpath
List<IClasspathEntry> classpathEntries = new LinkedList<>(); List<IClasspathEntry> classpathEntries = new LinkedList<>();
for (SourceFolderInfo sourceFolderInfo : sourceFolderInfos) { for (String sourcePath : sourceToOutputMap.keySet()) {
classpathEntries IFolder sourceFolder = project.getFolder(sourcePath);
.add(JavaCore.newSourceEntry(sourceFolderInfo.sourceFolder().getFullPath(), IFolder outputFolder = project.getFolder(sourceToOutputMap.get(sourcePath));
new IPath[0], sourceFolderInfo.outputFolder().getFullPath())); GhidraProjectUtils.createFolder(sourceFolder, monitor);
classpathEntries.add(JavaCore.newSourceEntry(sourceFolder.getFullPath(), new IPath[0],
outputFolder.getFullPath()));
} }
GhidraProjectUtils.addToClasspath(javaProject, classpathEntries, monitor); GhidraProjectUtils.addToClasspath(javaProject, classpathEntries, monitor);
@ -256,42 +246,30 @@ public class GhidraModuleUtils {
createRunConfig, runConfigMemory, ghidraLayout, jythonInterpreterName, monitor); createRunConfig, runConfigMemory, ghidraLayout, jythonInterpreterName, monitor);
IProject project = javaProject.getProject(); IProject project = javaProject.getProject();
// Find source directory paths // Set default output location
List<IPath> sourcePaths = new ArrayList<>(); IFolder defaultOutputFolder = project.getFolder(defaultOutputPath);
IFolder srcFolder = project.getFolder("src"); javaProject.setOutputLocation(defaultOutputFolder.getFullPath(), monitor);
List<IFolder> srcSubFolders = getSubFolders(srcFolder);
if (!srcSubFolders.isEmpty()) {
for (IFolder srcSubFolder : srcSubFolders) {
List<IFolder> subSubFolders = getSubFolders(srcSubFolder);
if (!subSubFolders.isEmpty()) {
sourcePaths.addAll(subSubFolders.stream().map(e -> e.getFullPath()).toList());
}
else {
sourcePaths.add(srcSubFolder.getFullPath());
}
}
}
else {
sourcePaths.add(srcFolder.getFullPath());
}
// Find jar file paths // Put the source and jar paths in the project's classpath
List<IPath> jarPaths = new ArrayList<>(); List<IClasspathEntry> classpathEntries = new LinkedList<>();
for (String sourcePath : sourceToOutputMap.keySet()) {
IFolder sourceFolder = project.getFolder(sourcePath);
IFolder outputFolder = project.getFolder(sourceToOutputMap.get(sourcePath));
GhidraProjectUtils.createFolder(sourceFolder, monitor);
classpathEntries.add(JavaCore.newSourceEntry(sourceFolder.getFullPath(), new IPath[0],
outputFolder.getFullPath()));
}
IFolder libFolder = project.getFolder("lib"); IFolder libFolder = project.getFolder("lib");
if (libFolder.exists()) { if (libFolder.exists()) {
for (IResource resource : libFolder.members()) { for (IResource resource : libFolder.members()) {
if (resource.getType() == IResource.FILE && if (resource.getType() == IResource.FILE &&
resource.getFileExtension().equals("jar")) { resource.getFileExtension().equals("jar")) {
jarPaths.add(resource.getFullPath()); classpathEntries
.add(JavaCore.newLibraryEntry(resource.getFullPath(), null, null));
} }
} }
} }
GhidraProjectUtils.addToClasspath(javaProject, classpathEntries, monitor);
// Put the source and jar paths in the project's classpath
List<IClasspathEntry> cp = new ArrayList<>();
cp.addAll(sourcePaths.stream().map(e -> JavaCore.newSourceEntry(e)).toList());
cp.addAll(jarPaths.stream().map(e -> JavaCore.newLibraryEntry(e, null, null)).toList());
GhidraProjectUtils.addToClasspath(javaProject, cp, monitor);
// Update language ant properties file // Update language ant properties file
GhidraModuleUtils.writeAntProperties(project, ghidraLayout); GhidraModuleUtils.writeAntProperties(project, ghidraLayout);