2023-04-06 16:43:18 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.nio.file.Path;
|
2023-04-24 11:57:38 +00:00
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.Arrays;
|
2023-10-16 15:23:18 +02:00
|
|
|
import java.util.List;
|
2023-04-24 11:57:38 +00:00
|
|
|
|
|
|
|
|
import com.adacore.libadalang.Libadalang;
|
2023-04-06 16:43:18 +02:00
|
|
|
|
2023-09-01 14:36:10 +02:00
|
|
|
public class ProjectManager {
|
2023-04-06 16:43:18 +02:00
|
|
|
|
2024-03-04 17:28:48 +01:00
|
|
|
/** The directory which contains the projects */
|
2023-04-06 16:43:18 +02:00
|
|
|
private static String projectPath;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the section header with the given name
|
|
|
|
|
*
|
|
|
|
|
* @param name The name of the section
|
|
|
|
|
*/
|
|
|
|
|
private static void header(String name) {
|
|
|
|
|
System.out.println("--- " + name + " ---");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the footer of a section
|
|
|
|
|
*
|
|
|
|
|
* @param name The name of the section
|
|
|
|
|
*/
|
|
|
|
|
private static void footer(String name) {
|
|
|
|
|
System.out.println("----" + "-".repeat(name.length()) + "----\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-04 17:56:13 +01:00
|
|
|
* Util function to display all information about a project manager.
|
|
|
|
|
*/
|
|
|
|
|
private static void projectInfo(
|
|
|
|
|
Libadalang.ProjectManager project,
|
|
|
|
|
String subproject
|
|
|
|
|
) {
|
|
|
|
|
// Display the project manager diagnostics if any
|
|
|
|
|
List<String> diagnostics = project.getDiagnostics();
|
|
|
|
|
if (diagnostics.size() > 0) {
|
|
|
|
|
System.out.println("Error during project opening:");
|
|
|
|
|
System.out.println(" " + diagnostics);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] files = project.getFiles(
|
|
|
|
|
Libadalang.SourceFileMode.ROOT_PROJECT,
|
|
|
|
|
subproject == null ? null : new String[] {subproject}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Create an analysis context with the project unit provider
|
|
|
|
|
Libadalang.UnitProvider unitProvider =
|
|
|
|
|
project.getProvider(subproject);
|
|
|
|
|
|
2024-05-23 10:52:27 +02:00
|
|
|
// Create an event handler
|
|
|
|
|
Libadalang.EventHandler eh = Libadalang.EventHandler.create(
|
|
|
|
|
(ctx, name, from, found, notFoundIsError) -> {
|
|
|
|
|
if (!found && notFoundIsError) {
|
|
|
|
|
System.out.println(
|
|
|
|
|
"Cannot find file " + new File(name).getName()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
null);
|
|
|
|
|
|
2024-03-04 17:56:13 +01:00
|
|
|
try(
|
2025-09-15 14:54:56 +00:00
|
|
|
Libadalang.AnalysisContext context =
|
|
|
|
|
Libadalang.AnalysisContext.create(
|
2024-03-04 17:56:13 +01:00
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
unitProvider,
|
2024-05-23 10:52:27 +02:00
|
|
|
eh,
|
2024-03-04 17:56:13 +01:00
|
|
|
true,
|
|
|
|
|
8
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
for(String file : files) {
|
2025-09-15 14:54:56 +00:00
|
|
|
Libadalang.AnalysisUnit unit =
|
|
|
|
|
context.getUnitFromFile(file);
|
2024-03-04 17:56:13 +01:00
|
|
|
System.out.println("File " + unit.getFileName(false));
|
|
|
|
|
System.out.println(" root = " + unit.getRoot());
|
2024-05-23 10:52:27 +02:00
|
|
|
System.out.println(
|
|
|
|
|
" deps = " +
|
|
|
|
|
Arrays.toString(
|
|
|
|
|
((Libadalang.CompilationUnit) unit.getRoot())
|
|
|
|
|
.pUnitDependencies()
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-03-04 17:56:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 10:52:27 +02:00
|
|
|
/** Simply open the given GPR file and display its source files. */
|
|
|
|
|
private static void openProject(String gprFile) {
|
|
|
|
|
openProject(
|
|
|
|
|
gprFile,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open the given GPR file with a subproject to use, then display its
|
|
|
|
|
* source files.
|
|
|
|
|
*/
|
|
|
|
|
private static void openProject(
|
|
|
|
|
String gprFile,
|
|
|
|
|
String subproject
|
|
|
|
|
) {
|
|
|
|
|
openProject(
|
|
|
|
|
gprFile,
|
|
|
|
|
subproject,
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open a GPR project and list its source files.
|
|
|
|
|
*
|
|
|
|
|
* @param gprFile The gpr file
|
|
|
|
|
* @param subproject The subproject to use. If null, use the root project
|
|
|
|
|
* in the given project path
|
2025-09-15 14:54:56 +00:00
|
|
|
* @param scenarioVariables Scenario variables to apply during project
|
2024-05-23 10:52:27 +02:00
|
|
|
* opening
|
|
|
|
|
* @param lookInProjectPath If the function should look for the GPR file
|
|
|
|
|
*/
|
|
|
|
|
private static void openProject(
|
|
|
|
|
String gprFile,
|
|
|
|
|
String subproject,
|
2025-09-15 14:54:56 +00:00
|
|
|
String[] scenarioVariables,
|
2024-05-23 10:52:27 +02:00
|
|
|
boolean lookInProjectPath
|
|
|
|
|
) {
|
|
|
|
|
openProject(
|
|
|
|
|
gprFile,
|
|
|
|
|
subproject,
|
|
|
|
|
null,
|
|
|
|
|
scenarioVariables,
|
|
|
|
|
lookInProjectPath
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 17:56:13 +01:00
|
|
|
/**
|
|
|
|
|
* Open a gpr project and list its source files.
|
2023-04-06 16:43:18 +02:00
|
|
|
*
|
|
|
|
|
* @param gprFile The gpr file
|
2023-07-27 17:23:43 +02:00
|
|
|
* @param subproject The subproject to use. If null, use the root project
|
2024-05-23 10:52:27 +02:00
|
|
|
* in the given project path
|
|
|
|
|
* @param configFile A configuration file to open the project with
|
2025-09-15 14:54:56 +00:00
|
|
|
* @param scenarioVariables Scenario variables to apply during project
|
2024-05-23 10:52:27 +02:00
|
|
|
* opening
|
|
|
|
|
* @param lookInProjectPath If the function should look for the GPR file
|
2023-04-06 16:43:18 +02:00
|
|
|
*/
|
|
|
|
|
private static void openProject(
|
|
|
|
|
String gprFile,
|
2024-05-23 10:52:27 +02:00
|
|
|
String subproject,
|
|
|
|
|
String configFile,
|
2025-09-15 14:54:56 +00:00
|
|
|
String[] scenarioVariables,
|
2024-05-23 10:52:27 +02:00
|
|
|
boolean lookInProjectPath
|
2024-09-24 11:50:15 +02:00
|
|
|
) {
|
|
|
|
|
openProject(
|
|
|
|
|
gprFile,
|
|
|
|
|
subproject,
|
|
|
|
|
configFile,
|
|
|
|
|
scenarioVariables,
|
|
|
|
|
lookInProjectPath,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void openProject(
|
|
|
|
|
String gprFile,
|
|
|
|
|
String subproject,
|
|
|
|
|
String configFile,
|
2025-09-15 14:54:56 +00:00
|
|
|
String[] scenarioVariables,
|
2024-09-24 11:50:15 +02:00
|
|
|
boolean lookInProjectPath,
|
|
|
|
|
boolean adaOnly
|
2023-04-06 16:43:18 +02:00
|
|
|
) {
|
2024-05-23 10:52:27 +02:00
|
|
|
String headerMsg = "Open " + Paths.get(gprFile).getFileName();
|
2023-07-27 17:23:43 +02:00
|
|
|
if (subproject != null) {
|
|
|
|
|
headerMsg += " (" + subproject + ")";
|
|
|
|
|
}
|
2024-05-23 10:52:27 +02:00
|
|
|
if (configFile != null) {
|
|
|
|
|
headerMsg += " with config " + configFile;
|
|
|
|
|
}
|
2024-09-24 11:50:15 +02:00
|
|
|
if (adaOnly) {
|
|
|
|
|
headerMsg += " | Ada only";
|
|
|
|
|
}
|
2023-07-27 17:23:43 +02:00
|
|
|
header(headerMsg);
|
2023-04-06 16:43:18 +02:00
|
|
|
|
2023-04-12 17:10:33 +02:00
|
|
|
// Resolve the project file if needed
|
2023-04-06 16:43:18 +02:00
|
|
|
if(lookInProjectPath) {
|
2024-05-23 10:52:27 +02:00
|
|
|
gprFile = Paths.get(projectPath, gprFile).toString();
|
|
|
|
|
configFile = configFile == null ?
|
|
|
|
|
null :
|
|
|
|
|
Paths.get(projectPath, configFile).toString();
|
2023-04-06 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-15 14:54:56 +00:00
|
|
|
try (
|
|
|
|
|
Libadalang.ProjectOptions options =
|
|
|
|
|
new Libadalang.ProjectOptions();
|
2023-04-24 11:57:38 +00:00
|
|
|
) {
|
2025-09-15 14:54:56 +00:00
|
|
|
options.addSwitch(Libadalang.ProjectOption.P, gprFile);
|
|
|
|
|
if (configFile != null) {
|
|
|
|
|
options.addSwitch(Libadalang.ProjectOption.CONFIG, configFile);
|
|
|
|
|
}
|
|
|
|
|
if (scenarioVariables != null) {
|
|
|
|
|
for (int i = 0; i < scenarioVariables.length; ++i) {
|
|
|
|
|
options.addSwitch(
|
|
|
|
|
Libadalang.ProjectOption.X,
|
|
|
|
|
scenarioVariables[i]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try(
|
|
|
|
|
Libadalang.ProjectManager project =
|
|
|
|
|
new Libadalang.ProjectManager(options, adaOnly)
|
|
|
|
|
) {
|
|
|
|
|
projectInfo(project, subproject);
|
|
|
|
|
} catch (Libadalang.ProjectManagerException e) {
|
|
|
|
|
System.out.println(e.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
footer(headerMsg);
|
|
|
|
|
}
|
2023-04-06 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test opening a valid project.
|
|
|
|
|
*/
|
|
|
|
|
private static void testValid() {
|
2024-05-23 10:52:27 +02:00
|
|
|
openProject("p1.gpr");
|
2023-04-12 17:10:33 +02:00
|
|
|
openProject(
|
|
|
|
|
"p2.gpr",
|
2024-05-23 10:52:27 +02:00
|
|
|
null,
|
2025-09-15 14:54:56 +00:00
|
|
|
new String[] {
|
|
|
|
|
"SRC_DIR=src2_1",
|
|
|
|
|
"USELESS=useless"
|
2023-04-12 17:10:33 +02:00
|
|
|
},
|
2024-05-23 10:52:27 +02:00
|
|
|
true
|
2023-04-12 17:10:33 +02:00
|
|
|
);
|
|
|
|
|
openProject(
|
|
|
|
|
"p2.gpr",
|
2024-05-23 10:52:27 +02:00
|
|
|
null,
|
2025-09-15 14:54:56 +00:00
|
|
|
new String[] {"SRC_DIR=src2_2"},
|
2024-05-23 10:52:27 +02:00
|
|
|
true
|
2023-04-12 17:10:33 +02:00
|
|
|
);
|
2023-04-06 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-15 14:54:56 +00:00
|
|
|
/**
|
|
|
|
|
* Test loading a project with a null GPROptions argument.
|
|
|
|
|
*/
|
|
|
|
|
private static void testNullOptions() {
|
|
|
|
|
final String headerMsg = "Null GPROptions argument";
|
|
|
|
|
header (headerMsg);
|
|
|
|
|
try(
|
|
|
|
|
Libadalang.ProjectManager project =
|
|
|
|
|
new Libadalang.ProjectManager(null, false);
|
|
|
|
|
) {
|
|
|
|
|
System.out.println("... unexpected success...");
|
|
|
|
|
} catch (Libadalang.ProjectManagerException e) {
|
|
|
|
|
System.out.println("ProjectManagerException:");
|
|
|
|
|
System.out.println(e.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
footer(headerMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 16:43:18 +02:00
|
|
|
/**
|
|
|
|
|
* Test opening an invalid project.
|
|
|
|
|
*/
|
|
|
|
|
private static void testInvalid() {
|
2024-05-23 10:52:27 +02:00
|
|
|
openProject("invalid.gpr");
|
2023-04-06 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test opening an inexistant project.
|
|
|
|
|
*/
|
|
|
|
|
private static void testInexistant() {
|
2024-05-23 10:52:27 +02:00
|
|
|
openProject("idonotexist.gpr", null, null, false);
|
2023-07-27 17:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test opening an aggregate project.
|
|
|
|
|
*/
|
|
|
|
|
private static void testAggregate() {
|
2024-05-23 10:52:27 +02:00
|
|
|
openProject("agg.gpr", "nosuchsubproject");
|
|
|
|
|
openProject("agg.gpr", "p1");
|
|
|
|
|
openProject("agg.gpr", "p2");
|
2023-04-06 16:43:18 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-16 15:23:18 +02:00
|
|
|
private static void testNoSuchTarget() {
|
2024-05-23 10:52:27 +02:00
|
|
|
openProject("nosuchtarget.gpr");
|
2023-10-16 15:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-04 17:56:13 +01:00
|
|
|
/**
|
|
|
|
|
* Test the opening of an implicit project.
|
|
|
|
|
*/
|
|
|
|
|
private static void testImplicit() {
|
|
|
|
|
String headerMsg = "Open implicit project";
|
|
|
|
|
header(headerMsg);
|
2025-09-15 14:54:56 +00:00
|
|
|
|
|
|
|
|
Libadalang.ProjectOptions options = new Libadalang.ProjectOptions();
|
|
|
|
|
options.addSwitch(Libadalang.ProjectOption.NO_PROJECT);
|
2024-03-04 17:56:13 +01:00
|
|
|
try (
|
|
|
|
|
Libadalang.ProjectManager project =
|
2025-09-15 14:54:56 +00:00
|
|
|
new Libadalang.ProjectManager(options, false)
|
2024-03-04 17:56:13 +01:00
|
|
|
) {
|
|
|
|
|
projectInfo(project, null);
|
|
|
|
|
} catch (Libadalang.ProjectManagerException e) {
|
|
|
|
|
System.out.println(e.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
footer(headerMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 10:52:27 +02:00
|
|
|
private static void testConfigFile() {
|
|
|
|
|
openProject(
|
|
|
|
|
"p1.gpr",
|
|
|
|
|
null,
|
|
|
|
|
"other_naming.cgpr",
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 08:58:39 +00:00
|
|
|
private static void testValidConfigFile() {
|
|
|
|
|
openProject("for_cgpr.gpr");
|
2024-05-23 10:52:27 +02:00
|
|
|
openProject(
|
2024-08-22 08:58:39 +00:00
|
|
|
Paths.get(projectPath, "for_cgpr.gpr").toString(),
|
2024-05-23 10:52:27 +02:00
|
|
|
null,
|
2024-08-22 08:58:39 +00:00
|
|
|
"custom.cgpr",
|
2024-05-23 10:52:27 +02:00
|
|
|
null,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void testEmptyConfigFile() {
|
|
|
|
|
openProject(
|
|
|
|
|
"p1.gpr",
|
|
|
|
|
null,
|
|
|
|
|
"empty.cgpr",
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void testInexistantConfigFile() {
|
|
|
|
|
openProject(
|
|
|
|
|
Paths.get(projectPath, "p1.gpr").toString(),
|
|
|
|
|
null,
|
|
|
|
|
"idonotexist.cgpr",
|
|
|
|
|
null,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 11:50:15 +02:00
|
|
|
private static void testAdaOnly() {
|
|
|
|
|
openProject(
|
|
|
|
|
"ada_only.gpr",
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
openProject(
|
|
|
|
|
"ada_only.gpr",
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
true,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 16:43:18 +02:00
|
|
|
/**
|
|
|
|
|
* Run the tests
|
|
|
|
|
*/
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
projectPath = args[0];
|
|
|
|
|
testValid();
|
2025-09-15 14:54:56 +00:00
|
|
|
testNullOptions();
|
2023-04-06 16:43:18 +02:00
|
|
|
testInvalid();
|
|
|
|
|
testInexistant();
|
2023-07-27 17:23:43 +02:00
|
|
|
testAggregate();
|
2023-10-16 15:23:18 +02:00
|
|
|
testNoSuchTarget();
|
2024-03-04 17:56:13 +01:00
|
|
|
testImplicit();
|
2024-05-23 10:52:27 +02:00
|
|
|
testConfigFile();
|
2024-08-22 08:58:39 +00:00
|
|
|
testValidConfigFile();
|
2024-05-23 10:52:27 +02:00
|
|
|
testEmptyConfigFile();
|
|
|
|
|
testInexistantConfigFile();
|
2024-09-24 11:50:15 +02:00
|
|
|
testAdaOnly();
|
2023-04-06 16:43:18 +02:00
|
|
|
}
|
2023-04-24 11:57:38 +00:00
|
|
|
}
|