Skip to content

Commit

Permalink
Move BREE calculation to project manager
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 9360485)
  • Loading branch information
laeubi committed Jan 15, 2025
1 parent 2b0b879 commit 82eb25d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -363,7 +356,7 @@ public abstract class AbstractOsgiCompilerMojo extends AbstractCompilerMojo impl
@Component
private MavenDependenciesResolver dependenciesResolver;

private StandardExecutionEnvironment[] manifestBREEs;
private ExecutionEnvironment[] manifestBREEs;

private File currentOutputDirectory;

Expand Down Expand Up @@ -473,45 +466,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<Filter> 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;
}
Expand Down Expand Up @@ -861,7 +824,7 @@ private List<AccessRule> 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();
Expand Down Expand Up @@ -1188,9 +1151,9 @@ public String getReleaseLevel() throws MojoExecutionException {
}

private void checkTargetLevelCompatibleWithManifestBREEs(String effectiveTargetLevel,
StandardExecutionEnvironment[] manifestBREEs) throws MojoExecutionException {
ExecutionEnvironment[] manifestBREEs) throws MojoExecutionException {
List<String> incompatibleBREEs = new ArrayList<>();
for (StandardExecutionEnvironment ee : manifestBREEs) {
for (ExecutionEnvironment ee : manifestBREEs) {
if (!ee.isCompatibleCompilerTargetLevel(effectiveTargetLevel)) {
incompatibleBREEs.add(ee.getProfileName() + " (assumes " + ee.getCompilerTargetLevelDefault() + ")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -336,4 +347,38 @@ public Optional<TargetPlatform> getTargetPlatform(MavenProject project) {

}

public Stream<ExecutionEnvironment> 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<Filter> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public ToolchainProvider(MavenSession mavenSession) {
this.mavenSession = mavenSession;
}

public Optional<OSGiJavaToolchain> getToolchain(String profileName) {
return getToolchain(JDKUsage.BREE, profileName).or(() -> getSystemToolchain());
}

public Optional<OSGiJavaToolchain> getToolchain(JDKUsage usage, String profileName) {
if (usage == JDKUsage.SYSTEM) {
return getSystemToolchain();
Expand Down

0 comments on commit 82eb25d

Please sign in to comment.