Skip to content

Commit

Permalink
Compute a final target platform for target projects
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Dec 18, 2023
1 parent d252b9c commit be79a86
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 62 deletions.
4 changes: 4 additions & 0 deletions tycho-api/src/main/java/org/eclipse/tycho/TargetPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public Collection<IRequirement> getAdditionalRequirements() {
return resolverConfiguration.getAdditionalRequirements();
}
};
TargetPlatform preliminaryTargetPlatform = dependencyResolver.computePreliminaryTargetPlatform(mavenSession,
TargetPlatform preliminaryTargetPlatform = dependencyResolver.getPreliminaryTargetPlatform(mavenSession,
mavenProject);
return dependencyResolver.resolveDependencies(mavenSession, mavenProject, preliminaryTargetPlatform,
testResolverConfiguration, configuration.getEnvironments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IInstallableUnit> installableUnits = p2.getInstallableUnits();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,20 +28,27 @@
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;
import org.eclipse.tycho.TargetPlatformService;
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 {
Expand All @@ -63,6 +71,9 @@ public class DefaultTargetPlatformService implements TargetPlatformService {
@Requirement
private TargetPlatformFactory tpFactory;

@Requirement
private IProvisioningAgent agent;

@Override
public Optional<TargetPlatform> getTargetPlatform() throws DependencyResolutionException {
MavenSession session = legacySupport.getSession();
Expand All @@ -78,61 +89,67 @@ public Optional<TargetPlatform> getTargetPlatform() throws DependencyResolutionE

@Override
public Optional<TargetPlatform> 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<ReactorProjectIdentities> 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<? extends ReactorProjectIdentities> 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<PublishingRepository> 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<ReactorProjectIdentities> upstreamProjects = getReferencedTychoProjects(project);
PomDependencyCollector pomDependenciesCollector = dependencyResolver
.resolvePomDependencies(mavenSession, project.adapt(MavenProject.class));
List<PublishingRepository> upstreamProjectResults = getBuildResults(upstreamProjects);
return tpFactory.createTargetPlatformWithUpdatedReactorContent(preliminaryTargetPlatform,
upstreamProjectResults, pomDependenciesCollector);
}
});
}

private List<PublishingRepository> getBuildResults(List<? extends ReactorProjectIdentities> projects) {
Expand All @@ -143,13 +160,6 @@ private List<PublishingRepository> getBuildResults(List<? extends ReactorProject
return results;
}

private PreliminaryTargetPlatformImpl getRegisteredPreliminaryTargetPlatform(ReactorProject project) {
return project.getContextValue(
TargetPlatform.PRELIMINARY_TARGET_PLATFORM_KEY) instanceof PreliminaryTargetPlatformImpl preliminaryTargetPlatformImpl
? preliminaryTargetPlatformImpl
: null;
}

private List<ReactorProjectIdentities> getReferencedTychoProjects(ReactorProject reactorProject)
throws DependencyResolutionException {
List<ReactorProjectIdentities> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected Collection<IDependencyMetadata> 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);
Expand Down

0 comments on commit be79a86

Please sign in to comment.