From be79a86721a712b8ff8fd62f4bd42496ffc1955b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Mon, 18 Dec 2023 07:27:27 +0100 Subject: [PATCH] Compute a final target platform for target projects --- .../org/eclipse/tycho/TargetPlatform.java | 4 + .../tycho/core/DependencyResolver.java | 2 +- .../core/osgitools/AbstractTychoProject.java | 2 +- .../core/osgitools/OsgiBundleProject.java | 2 +- .../core/osgitools/TargetPlatformProject.java | 2 +- .../DefaultTargetPlatformService.java | 124 ++++++++++-------- .../p2resolver/P2DependencyResolver.java | 2 +- 7 files changed, 76 insertions(+), 62 deletions(-) diff --git a/tycho-api/src/main/java/org/eclipse/tycho/TargetPlatform.java b/tycho-api/src/main/java/org/eclipse/tycho/TargetPlatform.java index 99b2572379..ffbb72eb09 100644 --- a/tycho-api/src/main/java/org/eclipse/tycho/TargetPlatform.java +++ b/tycho-api/src/main/java/org/eclipse/tycho/TargetPlatform.java @@ -33,6 +33,10 @@ public interface TargetPlatform { * Key under which the final target platform is stored in the reactor project instances. */ String FINAL_TARGET_PLATFORM_KEY = "org.eclipse.tycho.core.TychoConstants/targetPlatform"; + + /** + * Key under which the preliminary target platform is stored in the reactor project instances. + */ String PRELIMINARY_TARGET_PLATFORM_KEY = "org.eclipse.tycho.core.TychoConstants/dependencyOnlyTargetPlatform"; /** diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java index 736d659ce0..96acc2708e 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/DependencyResolver.java @@ -33,7 +33,7 @@ public interface DependencyResolver { PomDependencyCollector resolvePomDependencies(MavenSession session, MavenProject project); - public TargetPlatform computePreliminaryTargetPlatform(MavenSession session, MavenProject project); + public TargetPlatform getPreliminaryTargetPlatform(MavenSession session, MavenProject project); /** * @param targetPlatform diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java index 7fb65b8e67..8a4a0cf2ae 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/AbstractTychoProject.java @@ -80,7 +80,7 @@ public DependencyArtifacts getDependencyArtifacts(ReactorProject reactorProject) } MavenSession mavenSession = getMavenSession(reactorProject); MavenProject mavenProject = getMavenProject(reactorProject); - TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession, + TargetPlatform preliminaryTargetPlatform = dependencyResolver.getPreliminaryTargetPlatform(mavenSession, mavenProject); TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(mavenProject); DependencyResolverConfiguration resolverConfiguration = configuration.getDependencyResolverConfiguration(); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java index 162a8b8afe..5c51557638 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/OsgiBundleProject.java @@ -711,7 +711,7 @@ public Collection getAdditionalRequirements() { return resolverConfiguration.getAdditionalRequirements(); } }; - TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession, + TargetPlatform preliminaryTargetPlatform = dependencyResolver.getPreliminaryTargetPlatform(mavenSession, mavenProject); return dependencyResolver.resolveDependencies(mavenSession, mavenProject, preliminaryTargetPlatform, testResolverConfiguration, configuration.getEnvironments()); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/TargetPlatformProject.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/TargetPlatformProject.java index f9c2bba376..fca2c72aab 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/TargetPlatformProject.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/TargetPlatformProject.java @@ -78,7 +78,7 @@ public DependencyArtifacts getDependencyArtifacts(ReactorProject reactorProject) DefaultDependencyArtifacts artifacts = new DefaultDependencyArtifacts(reactorProject); MavenSession mavenSession = getMavenSession(reactorProject); MavenProject mavenProject = getMavenProject(reactorProject); - TargetPlatform targetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession, + TargetPlatform targetPlatform = dependencyResolver.getPreliminaryTargetPlatform(mavenSession, mavenProject); if (targetPlatform instanceof P2TargetPlatform p2) { Set installableUnits = p2.getInstallableUnits(); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java index 3c16ed915d..cadd674cb2 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformService.java @@ -16,6 +16,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -27,7 +28,13 @@ import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.Logger; +import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; +import org.eclipse.tycho.ArtifactKey; import org.eclipse.tycho.DependencyResolutionException; +import org.eclipse.tycho.IllegalArtifactReferenceException; +import org.eclipse.tycho.PackagingType; import org.eclipse.tycho.ReactorProject; import org.eclipse.tycho.ReactorProjectIdentities; import org.eclipse.tycho.TargetPlatform; @@ -35,12 +42,13 @@ import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.core.DependencyResolver; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; +import org.eclipse.tycho.core.resolver.target.SupplierMetadataRepository; import org.eclipse.tycho.p2.repository.GAV; import org.eclipse.tycho.p2.repository.PublishingRepository; import org.eclipse.tycho.p2.target.facade.PomDependencyCollector; import org.eclipse.tycho.p2.target.facade.TargetPlatformFactory; -import org.eclipse.tycho.p2resolver.PreliminaryTargetPlatformImpl; import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager; +import org.eclipse.tycho.targetplatform.P2TargetPlatform; @Component(role = TargetPlatformService.class) public class DefaultTargetPlatformService implements TargetPlatformService { @@ -63,6 +71,9 @@ public class DefaultTargetPlatformService implements TargetPlatformService { @Requirement private TargetPlatformFactory tpFactory; + @Requirement + private IProvisioningAgent agent; + @Override public Optional getTargetPlatform() throws DependencyResolutionException { MavenSession session = legacySupport.getSession(); @@ -78,61 +89,67 @@ public Optional getTargetPlatform() throws DependencyResolutionE @Override public Optional getTargetPlatform(ReactorProject project) throws DependencyResolutionException { - synchronized (project) { - Object contextValue = project.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY); - if (contextValue instanceof TargetPlatform) { - return Optional.of((TargetPlatform) contextValue); - } - MavenSession session = legacySupport.getSession(); - if (repositoryManager == null || session == null) { - return Optional.empty(); - } - List upstreamProjects = getReferencedTychoProjects(project); - PomDependencyCollector pomDependenciesCollector = dependencyResolver.resolvePomDependencies(session, - project.adapt(MavenProject.class)); - TargetPlatform finalTargetPlatform = computeFinalTargetPlatform(project, upstreamProjects, - pomDependenciesCollector); - return Optional.ofNullable(finalTargetPlatform); + Object contextValue = project.getContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY); + if (contextValue instanceof TargetPlatform) { + return Optional.of((TargetPlatform) contextValue); + } + if (repositoryManager == null || project.adapt(MavenSession.class) == null) { + return Optional.empty(); } + return Optional.ofNullable(computeFinalTargetPlatform(project)); } - /** - * Computes the (immutable) target platform with final p2 metadata and attaches it to the given - * project. - * - * @param project - * the reactor project to compute the target platform for. - * @param upstreamProjects - * Other projects in the reactor which have already been built and may be referenced - * by the given project. - */ - private TargetPlatform computeFinalTargetPlatform(ReactorProject project, - List upstreamProjects, PomDependencyCollector pomDependencyCollector) { - synchronized (project) { - PreliminaryTargetPlatformImpl preliminaryTargetPlatform = getRegisteredPreliminaryTargetPlatform(project); - if (preliminaryTargetPlatform == null) { - MavenSession session = project.adapt(MavenSession.class); - if (session == null) { - session = legacySupport.getSession(); - if (session == null) { - return null; - } + private TargetPlatform computeFinalTargetPlatform(ReactorProject project) { + return project.computeContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, () -> { + MavenProject mavenProject = project.adapt(MavenProject.class); + MavenSession mavenSession = project.adapt(MavenSession.class); + TargetPlatform preliminaryTargetPlatform = dependencyResolver.getPreliminaryTargetPlatform(mavenSession, + mavenProject); + if (PackagingType.TYPE_ECLIPSE_TARGET_DEFINITION.equals(mavenProject.getPackaging())) { + IMetadataRepository metadataRepository; + if (preliminaryTargetPlatform instanceof P2TargetPlatform p2) { + metadataRepository = new SupplierMetadataRepository(agent, + () -> p2.getInstallableUnits().iterator()); + } else { + metadataRepository = new SupplierMetadataRepository(agent, () -> Collections.emptyIterator()); } - MavenProject mavenProject = project.adapt(MavenProject.class); - if (mavenProject == null) { - return null; - } - preliminaryTargetPlatform = (PreliminaryTargetPlatformImpl) dependencyResolver - .computePreliminaryTargetPlatform(session, mavenProject); + return new TargetPlatform() { - } - List upstreamProjectResults = getBuildResults(upstreamProjects); - TargetPlatform result = tpFactory.createTargetPlatformWithUpdatedReactorContent(preliminaryTargetPlatform, - upstreamProjectResults, pomDependencyCollector); + @Override + public ArtifactKey resolveArtifact(String type, String id, String versionRef) + throws IllegalArtifactReferenceException, DependencyResolutionException { + return preliminaryTargetPlatform.resolveArtifact(type, id, versionRef); + } - project.setContextValue(TargetPlatform.FINAL_TARGET_PLATFORM_KEY, result); - return result; - } + @Override + public boolean isFileAlreadyAvailable(ArtifactKey artifactKey) { + return preliminaryTargetPlatform.isFileAlreadyAvailable(artifactKey); + } + + @Override + public IMetadataRepository getMetadataRepository() { + return metadataRepository; + } + + @Override + public IArtifactRepository getArtifactRepository() { + return preliminaryTargetPlatform.getArtifactRepository(); + } + + @Override + public File getArtifactLocation(ArtifactKey artifact) { + return preliminaryTargetPlatform.getArtifactLocation(artifact); + } + }; + } else { + List upstreamProjects = getReferencedTychoProjects(project); + PomDependencyCollector pomDependenciesCollector = dependencyResolver + .resolvePomDependencies(mavenSession, project.adapt(MavenProject.class)); + List upstreamProjectResults = getBuildResults(upstreamProjects); + return tpFactory.createTargetPlatformWithUpdatedReactorContent(preliminaryTargetPlatform, + upstreamProjectResults, pomDependenciesCollector); + } + }); } private List getBuildResults(List projects) { @@ -143,13 +160,6 @@ private List getBuildResults(List getReferencedTychoProjects(ReactorProject reactorProject) throws DependencyResolutionException { List result = new ArrayList<>(); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java index cab618fc58..f69b059655 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java @@ -204,7 +204,7 @@ protected Collection getDependencyMetadata(final MavenSessi } @Override - public TargetPlatform computePreliminaryTargetPlatform(MavenSession mavenSession, MavenProject mavenProject) { + public TargetPlatform getPreliminaryTargetPlatform(MavenSession mavenSession, MavenProject mavenProject) { ReactorProject reactorProject = DefaultReactorProject.adapt(mavenProject); return reactorProject.computeContextValue(TargetPlatform.PRELIMINARY_TARGET_PLATFORM_KEY, () -> { logger.debug("Computing preliminary target platform for " + mavenProject);