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

Pass the profile properties so they can be considered in the resolve #3204

Closed
wants to merge 3 commits into from
Closed
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
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2maven;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IRequirement;

public class DirectDependenciesResult {

private Map<IRequirement, List<IInstallableUnit>> unitMap;

DirectDependenciesResult(Map<IRequirement, List<IInstallableUnit>> unitMap) {
this.unitMap = unitMap;
}

/**
* @return a all units contained in the results
*/
public List<IInstallableUnit> units() {
return unitMap.values().stream().flatMap(Collection::stream).distinct().toList();
}

public Collection<IRequirement> requirements() {
return unitMap.keySet();
}

public Collection<IInstallableUnit> getUnits(IRequirement requirement) {
return unitMap.getOrDefault(requirement, List.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*******************************************************************************/
package org.eclipse.tycho.p2maven;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand All @@ -27,7 +29,6 @@
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.equinox.p2.query.CollectionResult;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.IQueryable;
import org.eclipse.equinox.p2.query.QueryUtil;
Expand Down Expand Up @@ -76,32 +77,34 @@ public IQueryResult<IInstallableUnit> computeDependencies(Collection<IInstallabl
* @param rootIus the root {@link InstallableUnit}s to take into account
* @param avaiableIUs the {@link IQueryable} of all units that could be used for
* fulfilling a requirement
* @param contextIUs context IUs that represent the the profile properties to
* consider during resolution, can be empty in which case a
* filter is always considered a match
* @return the result of the slicing
* @throws CoreException if there is any error
*/
public IQueryResult<IInstallableUnit> computeDirectDependencies(Collection<IInstallableUnit> rootIus,
public DirectDependenciesResult computeDirectDependencies(Collection<IInstallableUnit> rootIus,
IQueryable<IInstallableUnit> avaiableIUs) throws CoreException {
Collection<IInstallableUnit> result = new LinkedHashSet<>();
List<IRequirement> collect = rootIus.stream().flatMap(iu -> iu.getRequirements().stream()).filter(req -> {
for (IInstallableUnit unit : rootIus) {
if (unit.satisfies(req)) {
// self full filled requirement
return false;
}
}
return true;
}).toList();
List<IRequirement> collect = rootIus.stream().flatMap(iu -> iu.getRequirements().stream())
.filter(req -> {
for (IInstallableUnit unit : rootIus) {
if (unit.satisfies(req)) {
// self full filled requirement
return false;
}
}
return true;
}).toList();
Map<IRequirement, List<IInstallableUnit>> unitMap = new LinkedHashMap<>();
for (IInstallableUnit iu : avaiableIUs.query(QueryUtil.ALL_UNITS, new NullProgressMonitor()).toSet()) {
for (IRequirement requirement : collect) {
if (iu.satisfies(requirement)) {
result.add(iu);
// TODO remove the requirement from the set so we only collect exactly one
// provider for a requirement?
break;
// need to add it to the book
unitMap.computeIfAbsent(requirement, nil -> new ArrayList<>()).add(iu);
}
}
}
return new CollectionResult<>(result);
return new DirectDependenciesResult(unitMap);
}

private final class TychoSlicer extends PermissiveSlicer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -44,6 +45,7 @@
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IProvidedCapability;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.query.CollectionResult;
import org.eclipse.equinox.p2.query.IQueryable;
Expand All @@ -57,7 +59,7 @@
public class MavenProjectDependencyProcessor {

private static final ProjectDependencies EMPTY_DEPENDENCIES = new ProjectDependencies(Collections.emptyList(),
Collections.emptyList());
Collections.emptyList(), Collections.emptyList());

private static final boolean DUMP_DATA = Boolean.getBoolean("tycho.p2.dump")
|| Boolean.getBoolean("tycho.p2.dump.dependencies");
Expand All @@ -72,19 +74,25 @@ public class MavenProjectDependencyProcessor {
* Computes the {@link ProjectDependencyClosure} of the given collection of
* projects.
*
* @param projects the projects to include in the closure
* @param session the maven session for this request
* @param projects the projects to include in the closure
* @param session the maven session for this request
* @param profilePropertiesSupplier supplier of context IUs for a project that
* represent the the profile properties to
* consider during resolution, can be empty in
* which case a filter is always considered a
* match
* @return the computed {@link ProjectDependencyClosure}
* @throws CoreException if computation failed
*/
public ProjectDependencyClosure computeProjectDependencyClosure(Collection<MavenProject> projects,
MavenSession session) throws CoreException {
MavenSession session, Function<MavenProject, Collection<IInstallableUnit>> profilePropertiesSupplier)
throws CoreException {
Objects.requireNonNull(session);
Map<MavenProject, Collection<IInstallableUnit>> projectIUMap = generator.getInstallableUnits(projects, session);
Collection<IInstallableUnit> availableIUs = projectIUMap.values().stream().flatMap(Collection::stream)
.collect(Collectors.toSet());
Map<MavenProject, ProjectDependencies> projectDependenciesMap = computeProjectDependencies(projects,
new CollectionResult<>(availableIUs), projectIUMap);
new CollectionResult<>(availableIUs), projectIUMap, profilePropertiesSupplier);
Map<IInstallableUnit, MavenProject> iuProjectMap = new HashMap<>();
for (var entry : projectIUMap.entrySet()) {
MavenProject mavenProject = entry.getKey();
Expand Down Expand Up @@ -138,14 +146,14 @@ public Collection<IInstallableUnit> getProjectUnits(MavenProject mavenProject) {
* @throws CoreException if computation failed
*/
private Map<MavenProject, ProjectDependencies> computeProjectDependencies(Collection<MavenProject> projects,
IQueryable<IInstallableUnit> avaiableIUs, Map<MavenProject, Collection<IInstallableUnit>> projectIUMap)
throws CoreException {
IQueryable<IInstallableUnit> avaiableIUs, Map<MavenProject, Collection<IInstallableUnit>> projectIUMap,
Function<MavenProject, Collection<IInstallableUnit>> profilePropertiesSupplier) throws CoreException {
List<CoreException> errors = new CopyOnWriteArrayList<>();
Map<MavenProject, ProjectDependencies> result = new ConcurrentHashMap<>();
projects.parallelStream().unordered().takeWhile(nil -> errors.isEmpty()).forEach(project -> {
try {
ProjectDependencies projectDependencies = computeProjectDependencies(projectIUMap.get(project),
avaiableIUs);
avaiableIUs, profilePropertiesSupplier.apply(project));
result.put(project, projectDependencies);
if (DUMP_DATA) {
File file = new File(project.getBasedir(), "project-dependencies.xml");
Expand Down Expand Up @@ -182,12 +190,25 @@ private Map<MavenProject, ProjectDependencies> computeProjectDependencies(Collec
* @throws CoreException if computation failed
*/
private ProjectDependencies computeProjectDependencies(Collection<IInstallableUnit> projectUnits,
IQueryable<IInstallableUnit> avaiableIUs) throws CoreException {
IQueryable<IInstallableUnit> avaiableIUs, Collection<IInstallableUnit> profileProperties)
throws CoreException {
if (projectUnits.isEmpty()) {
return EMPTY_DEPENDENCIES;
}
Set<IInstallableUnit> resolved = new LinkedHashSet<>(
slicer.computeDirectDependencies(projectUnits, avaiableIUs).toSet());
DirectDependenciesResult dependencies = slicer.computeDirectDependencies(projectUnits, avaiableIUs);
// first collect the maximum desired number
Set<IInstallableUnit> resolved = new LinkedHashSet<>();
Set<IInstallableUnit> unmatched = new LinkedHashSet<>();
Collection<IRequirement> requirements = dependencies.requirements();
for (IRequirement requirement : requirements) {
Collection<IInstallableUnit> units = dependencies.getUnits(requirement);
List<IInstallableUnit> limit = units.stream().limit(requirement.getMax()).toList();
resolved.addAll(limit);
if (isMatch(requirement, profileProperties)) {
unmatched.addAll(limit);
}
}
// remove everything that is our own units...
resolved.removeAll(projectUnits);
// now we need to filter all fragments that we are a host!
// for example SWT creates an explicit requirement to its fragments and we don't
Expand All @@ -200,7 +221,15 @@ private ProjectDependencies computeProjectDependencies(Collection<IInstallableUn
iterator.remove();
}
}
return new ProjectDependencies(resolved, projectFragments);
return new ProjectDependencies(resolved, projectFragments, unmatched);
}

private boolean isMatch(IRequirement requirement, Collection<IInstallableUnit> contextIUs) {
IMatchExpression<IInstallableUnit> filter = requirement.getFilter();
if (filter == null || contextIUs.isEmpty()) {
return true;
}
return contextIUs.stream().anyMatch(contextIU -> filter.isMatch(contextIU));
}

private static boolean hasAnyHost(IInstallableUnit unit, Iterable<IInstallableUnit> collection) {
Expand Down Expand Up @@ -242,10 +271,13 @@ public static final class ProjectDependencies {

private final Collection<IInstallableUnit> dependencies;
private final Collection<IInstallableUnit> fragments;
private final Collection<IInstallableUnit> unmatched;

private ProjectDependencies(Collection<IInstallableUnit> dependencies, Collection<IInstallableUnit> fragments) {
private ProjectDependencies(Collection<IInstallableUnit> dependencies, Collection<IInstallableUnit> fragments,
Collection<IInstallableUnit> unmatched) {
this.dependencies = dependencies;
this.fragments = fragments;
this.unmatched = unmatched;
}

public Collection<IInstallableUnit> getDependencies() {
Expand All @@ -256,6 +288,13 @@ public Collection<IInstallableUnit> getFragments() {
return fragments;
}

/**
* @return return all units that don'T match the current profile filters
*/
public Collection<IInstallableUnit> getUnmatched() {
return unmatched;
}

}

public static interface ProjectDependencyClosure {
Expand Down Expand Up @@ -296,6 +335,7 @@ default Collection<MavenProject> getDependencyProjects(MavenProject mavenProject
if (isFragment(mavenProject)) {
return list;
}
// TODO check if this is a negated/inactive dependecy...
return list.stream().flatMap(project -> {
ProjectDependencies dependecies = getProjectDependecies(project);
if (dependecies.getFragments().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.eclipse.tycho;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -88,7 +87,7 @@ public boolean isWindows() {
* Returns the target environment as map. The keys are "osgi.ws", "osgi.os", and "osgi.arch".
* This format is used by the p2 slicer to filter installable units by environments.
*
* @return a new instance of {@link HashMap} with the target environment set
* @return a new instance of {@link LinkedHashMap} with the target environment set
*/
public Map<String, String> toFilterProperties() {
//for nicer debug output, use an ordered map here
Expand Down
8 changes: 8 additions & 0 deletions tycho-baseline-plugin/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=17
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ public Result<ProjectDependencyGraph> build(MavenSession session) {
try {
ProjectDependencyClosure dependencyClosure;
try {
dependencyClosure = dependencyProcessor.computeProjectDependencyClosure(projects, session);
// here we don't use filters, the worst that can happen is that we get more
// projects to consider...
dependencyClosure = dependencyProcessor.computeProjectDependencyClosure(projects, session,
always -> List.of());
} catch (CoreException e) {
log.error("Cannot resolve projects", e);
return Result.error(graph, toProblems(e.getStatus(), new ArrayList<>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ClasspathEntry;
import org.eclipse.tycho.DefaultArtifactKey;
import org.eclipse.tycho.ExecutionEnvironmentConfiguration;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.ResolvedArtifactKey;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.TargetPlatformService;
import org.eclipse.tycho.TychoConstants;
Expand Down Expand Up @@ -143,6 +146,25 @@ public void readExecutionEnvironmentConfiguration(ReactorProject project, Execut
}
}

public List<IInstallableUnit> getContextIUs(MavenProject project) {
TargetPlatformConfiguration configuration = getTargetPlatformConfiguration(project);
return configuration.getEnvironments().stream().map(env -> getProfileProperties(env, configuration))
.map(InstallableUnit::contextIU).toList();
}

public Map<String, String> getProfileProperties(MavenProject project, TargetEnvironment environment) {
TargetPlatformConfiguration configuration = getTargetPlatformConfiguration(project);
return getProfileProperties(environment, configuration);
}

private Map<String, String> getProfileProperties(TargetEnvironment environment,
TargetPlatformConfiguration configuration) {
Map<String, String> properties = environment.toFilterProperties();
properties.put("org.eclipse.update.install.features", "true");
properties.putAll(configuration.getProfileProperties());
return properties;
}

public TargetPlatformConfiguration getTargetPlatformConfiguration(MavenProject project) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
return reactorProject.computeContextValue(CTX_TARGET_PLATFORM_CONFIGURATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
if (lazyProjects.size() > 0) {
try {
ProjectDependencyClosure closure = dependencyProcessor.computeProjectDependencyClosure(projects,
session);
session, projectManager::getContextIUs);
for (MavenProject project : lazyProjects) {
if (projectManager.getTychoProject(project).isEmpty()) {
//do not inject additional dependencies for non Tycho managed projects!
continue;
}
Collection<MavenProject> dependencyProjects = closure.getDependencyProjects(project);
System.out.println("=== " + project + " ===");
for (MavenProject mavenProject : projects) {
System.out.println(mavenProject);
}
MavenDependencyInjector.injectMavenProjectDependencies(project, dependencyProjects);
if (DUMP_DATA) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.justj.openjdk.hotspot.jre.base.stripped;singleton:=true
Bundle-Version: 17.0.9.qualifier
Bundle-ClassPath: .
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Automatic-Module-Name: org.eclipse.justj.openjdk.hotspot.jre.base.stripped
Loading