From 93604854232bcf6ce219bd40d39c2828fd0b81cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 15 Jan 2025 12:52:15 +0100 Subject: [PATCH] Move BREE calculation to project manager Currently the AbstractOsgiCompilerMojo contains code to calculate the wanted BREEs from the manifest and map them to available execution environments, but this can be useful on other places as well. This now extracts the part into the TychoProjectManager to make it mor general available. --- .../compiler/AbstractOsgiCompilerMojo.java | 51 +++---------------- .../tycho/core/TychoProjectManager.java | 45 ++++++++++++++++ .../tycho/core/maven/ToolchainProvider.java | 4 ++ 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java index ab42d0e5eb..8dc043cb6a 100644 --- a/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java +++ b/tycho-compiler-plugin/src/main/java/org/eclipse/tycho/compiler/AbstractOsgiCompilerMojo.java @@ -65,7 +65,6 @@ import org.eclipse.aether.resolution.DependencyResolutionException; import org.eclipse.jdt.internal.compiler.util.CtSym; import org.eclipse.jdt.internal.compiler.util.JRTUtil; -import org.eclipse.osgi.util.ManifestElement; import org.eclipse.tycho.ArtifactKey; import org.eclipse.tycho.ClasspathEntry; import org.eclipse.tycho.ClasspathEntry.AccessRule; @@ -98,13 +97,7 @@ import org.eclipse.tycho.model.classpath.M2ClasspathVariable; import org.eclipse.tycho.model.classpath.PluginDependenciesClasspathContainer; import org.eclipse.tycho.model.classpath.ProjectClasspathEntry; -import org.osgi.framework.Constants; -import org.osgi.framework.Filter; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.Version; -import org.osgi.framework.namespace.ExecutionEnvironmentNamespace; -import org.osgi.resource.Namespace; import copied.org.apache.maven.plugin.AbstractCompilerMojo; @@ -348,7 +341,7 @@ public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo impl @Component private MavenDependenciesResolver dependenciesResolver; - private StandardExecutionEnvironment[] manifestBREEs; + private ExecutionEnvironment[] manifestBREEs; private File currentOutputDirectory; @@ -458,45 +451,15 @@ protected void doFinish() throws MojoExecutionException { /** * Only public for tests purpose! */ - public StandardExecutionEnvironment[] getBREE() { + public ExecutionEnvironment[] getBREE() { if (currentRelease != null) { //if there is an explicit release set we know the release and there must be a suitable EE provided return new StandardExecutionEnvironment[] { ExecutionEnvironmentUtils .getExecutionEnvironment("JavaSE-" + currentRelease, toolchainManager, session, logger) }; } if (manifestBREEs == null) { - OsgiManifest manifest = bundleReader.loadManifest(project.getBasedir()); - manifestBREEs = Arrays.stream(manifest.getExecutionEnvironments()) - .map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session, logger)) - .toArray(StandardExecutionEnvironment[]::new); - if (manifestBREEs.length == 0) { - ManifestElement[] requireCapability = manifest.getManifestElements(Constants.REQUIRE_CAPABILITY); - if (requireCapability != null) { - List eeFilters = Arrays.stream(requireCapability) - .filter(element -> ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE - .equals(element.getValue())) // - .map(element -> element.getDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE)) // - .map(filterDirective -> { - try { - return FrameworkUtil.createFilter(filterDirective); - } catch (InvalidSyntaxException e) { - e.printStackTrace(); - return null; - } - }).filter(Objects::nonNull).toList(); - manifestBREEs = ExecutionEnvironmentUtils.getProfileNames(toolchainManager, session, logger) - .stream() // - .map(name -> name.split("-")) // - .map(segments -> Map.of(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE, - segments[0], "version", segments[1])) - .filter(eeCapability -> eeFilters.stream().anyMatch(filter -> filter.matches(eeCapability))) - .map(ee -> ee.get(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE) + '-' - + ee.get("version")) - .map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session, - logger)) - .toArray(StandardExecutionEnvironment[]::new); - } - } + manifestBREEs = tychoProjectManager.getExecutionEnvironments(project, session) + .toArray(ExecutionEnvironment[]::new); } return manifestBREEs; } @@ -828,7 +791,7 @@ private List getStrictBootClasspathAccessRules() throws MojoExecutio private void configureJavaHome(CompilerConfiguration compilerConfiguration) throws MojoExecutionException { if (useJDK == JDKUsage.BREE) { - StandardExecutionEnvironment[] brees = getBREE(); + ExecutionEnvironment[] brees = getBREE(); String toolchainId; if (brees.length > 0) { toolchainId = brees[0].getProfileName(); @@ -1155,9 +1118,9 @@ public String getReleaseLevel() throws MojoExecutionException { } private void checkTargetLevelCompatibleWithManifestBREEs(String effectiveTargetLevel, - StandardExecutionEnvironment[] manifestBREEs) throws MojoExecutionException { + ExecutionEnvironment[] manifestBREEs) throws MojoExecutionException { List incompatibleBREEs = new ArrayList<>(); - for (StandardExecutionEnvironment ee : manifestBREEs) { + for (ExecutionEnvironment ee : manifestBREEs) { if (!ee.isCompatibleCompilerTargetLevel(effectiveTargetLevel)) { incompatibleBREEs.add(ee.getProfileName() + " (assumes " + ee.getCompilerTargetLevelDefault() + ")"); } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java b/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java index b1c71d0280..18ff5f525f 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java @@ -15,6 +15,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -23,6 +24,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Stream; import javax.inject.Inject; @@ -38,11 +40,13 @@ import org.codehaus.plexus.logging.Logger; import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.eclipse.osgi.util.ManifestElement; import org.eclipse.tycho.ArtifactDescriptor; import org.eclipse.tycho.ArtifactKey; import org.eclipse.tycho.ClasspathEntry; import org.eclipse.tycho.DefaultArtifactKey; import org.eclipse.tycho.DependencyArtifacts; +import org.eclipse.tycho.ExecutionEnvironment; import org.eclipse.tycho.ExecutionEnvironmentConfiguration; import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.ResolvedArtifactKey; @@ -52,6 +56,7 @@ import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.classpath.ClasspathContributor; import org.eclipse.tycho.core.ee.ExecutionEnvironmentConfigurationImpl; +import org.eclipse.tycho.core.ee.ExecutionEnvironmentUtils; import org.eclipse.tycho.core.osgitools.AbstractTychoProject; import org.eclipse.tycho.core.osgitools.BundleReader; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; @@ -65,6 +70,12 @@ import org.eclipse.tycho.model.project.EclipseProject; import org.eclipse.tycho.p2maven.tmp.BundlesAction; import org.eclipse.tycho.targetplatform.TargetDefinition; +import org.osgi.framework.Constants; +import org.osgi.framework.Filter; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.namespace.ExecutionEnvironmentNamespace; +import org.osgi.resource.Namespace; import aQute.bnd.osgi.Processor; @@ -334,4 +345,38 @@ public Optional getTargetPlatform(MavenProject project) { } + public Stream getExecutionEnvironments(MavenProject project, MavenSession session) { + OsgiManifest manifest = bundleReader.loadManifest(project.getBasedir()); + ExecutionEnvironment[] manifestBREEs = Arrays.stream(manifest.getExecutionEnvironments()) + .map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session, logger)) + .toArray(ExecutionEnvironment[]::new); + if (manifestBREEs.length == 0) { + ManifestElement[] requireCapability = manifest.getManifestElements(Constants.REQUIRE_CAPABILITY); + if (requireCapability != null) { + List eeFilters = Arrays.stream(requireCapability) + .filter(element -> ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE + .equals(element.getValue())) // + .map(element -> element.getDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE)) // + .map(filterDirective -> { + try { + return FrameworkUtil.createFilter(filterDirective); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + return null; + } + }).filter(Objects::nonNull).toList(); + return ExecutionEnvironmentUtils.getProfileNames(toolchainManager, session, logger).stream() // + .map(name -> name.split("-")) // + .map(segments -> Map.of(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE, + segments[0], "version", segments[1])) + .filter(eeCapability -> eeFilters.stream().anyMatch(filter -> filter.matches(eeCapability))) + .map(ee -> ee.get(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE) + '-' + + ee.get("version")) + .map(ee -> ExecutionEnvironmentUtils.getExecutionEnvironment(ee, toolchainManager, session, + logger)); + } + } + return Arrays.stream(manifestBREEs); + } + } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java index 221af779f4..309df6cd03 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/ToolchainProvider.java @@ -68,6 +68,10 @@ public ToolchainProvider(MavenSession mavenSession) { this.mavenSession = mavenSession; } + public Optional getToolchain(String profileName) { + return getToolchain(JDKUsage.BREE, profileName).or(() -> getSystemToolchain()); + } + public Optional getToolchain(JDKUsage usage, String profileName) { if (usage == JDKUsage.SYSTEM) { return getSystemToolchain();