Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move BREE calculation to project manager #4600

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -348,7 +341,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 @@ -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<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 @@ -828,7 +791,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 @@ -1155,9 +1118,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 @@ -334,4 +345,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
Loading