You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2020/12/16 08:35:38 UTC

[GitHub] [netbeans] gilles-duboscq commented on a change in pull request #2575: Open GraalVM sources out of the box

gilles-duboscq commented on a change in pull request #2575:
URL: https://github.com/apache/netbeans/pull/2575#discussion_r544107561



##########
File path: java/java.mx.project/src/org/netbeans/modules/java/mx/project/SuiteSources.java
##########
@@ -0,0 +1,1195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.mx.project;
+
+import java.beans.PropertyChangeListener;
+import java.beans.PropertyChangeSupport;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.Icon;
+import javax.swing.event.ChangeListener;
+import org.netbeans.modules.java.mx.project.suitepy.MxDistribution;
+import org.netbeans.modules.java.mx.project.suitepy.MxImports;
+import org.netbeans.modules.java.mx.project.suitepy.MxLibrary;
+import org.netbeans.modules.java.mx.project.suitepy.MxProject;
+import org.netbeans.modules.java.mx.project.suitepy.MxSuite;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.queries.AnnotationProcessingQuery;
+import org.netbeans.api.java.queries.SourceForBinaryQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectManager;
+import org.netbeans.api.project.SourceGroup;
+import org.netbeans.api.project.Sources;
+import org.netbeans.spi.java.classpath.ClassPathFactory;
+import org.netbeans.spi.java.classpath.ClassPathImplementation;
+import org.netbeans.spi.java.classpath.FlaggedClassPathImplementation;
+import org.netbeans.spi.java.classpath.PathResourceImplementation;
+import org.netbeans.spi.java.classpath.support.ClassPathSupport;
+import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation2;
+import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.filesystems.URLMapper;
+import org.openide.util.Exceptions;
+import org.openide.util.Utilities;
+import java.util.stream.Collectors;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation;
+import org.netbeans.spi.java.queries.SourceLevelQueryImplementation2;
+import org.netbeans.spi.project.SubprojectProvider;
+
+final class SuiteSources implements Sources,
+                BinaryForSourceQueryImplementation2<SuiteSources.Group>, SourceForBinaryQueryImplementation2,
+                SourceLevelQueryImplementation2, SubprojectProvider, MultipleRootsUnitTestForSourceQueryImplementation {
+    private static final Logger LOG = Logger.getLogger(SuiteSources.class.getName());
+    private static final SuiteSources CORE;
+
+    static {
+        MxSuite coreSuite = CoreSuite.CORE_5_279_0;
+        CORE = new SuiteSources(null, null, coreSuite);
+    }
+
+    private final MxSuite suite;
+    private final List<Group> groups;
+    private final List<Library> libraries;
+    private final List<Dist> distributions;
+    private final FileObject dir;
+    /**
+     * non-null if the dependencies haven't yet been properly initialized
+     */
+    private Map<String, Dep> transitiveDeps;
+    /**
+     * avoid GC of imported projects
+     */
+    private final SuiteProject prj;
+    private final Map<String, SuiteSources> imported;
+
+    SuiteSources(SuiteProject owner, FileObject dir, MxSuite suite) {
+        final Map<String, Dep> fillDeps = new HashMap<>();
+        this.prj = owner;
+        this.dir = dir;
+        this.groups = findGroups(fillDeps, suite, dir);
+        this.libraries = findLibraries(fillDeps, suite);
+        this.imported = findImportedSuites(dir, suite, fillDeps);
+        this.distributions = findDistributions(suite, this.libraries, this.groups, fillDeps);
+        this.suite = suite;
+        this.transitiveDeps = fillDeps;
+    }
+
+    @Override
+    public String toString() {
+        return "MxSources[" + (dir == null ? "mx" : dir.toURI()) + "]";
+    }
+
+    private List<Group> findGroups(Map<String, Dep> fillDeps, MxSuite s, FileObject dir) {
+        List<Group> arr = new ArrayList<>();
+        for (Map.Entry<String, MxProject> entry : s.projects().entrySet()) {
+            String name = entry.getKey();
+            MxProject mxPrj = entry.getValue();
+            FileObject prjDir = findPrjDir(dir, name, mxPrj);
+            if (prjDir == null) {
+                fillDeps.put(name, new Group(name, mxPrj, null, null, null, name, name));
+                continue;
+            }
+            String prevName = null;
+            Group firstGroup = null;
+            String binPrefix;
+            if (mxPrj.subDir() == null) {
+                binPrefix = "mxbuild/";
+            } else {
+                binPrefix = "mxbuild/" + mxPrj.subDir() + "/";
+            }
+            for (String rel : mxPrj.sourceDirs()) {
+                FileObject srcDir = prjDir.getFileObject(rel);
+                FileObject binDir = getSubDir(dir, binPrefix + name + "/bin");
+                FileObject srcGenDir = getSubDir(dir, binPrefix + name + "/src_gen");
+                if (srcDir != null && binDir != null) {
+                    String prgName = name + "-" + rel;
+                    String displayName;
+                    if (prevName == null) {
+                        displayName = name;
+                    } else {
+                        displayName = name + "[" + rel + "]";
+                    }
+                    Group g = new Group(name, mxPrj, srcDir, srcGenDir, binDir, prgName, displayName);
+                    arr.add(g);
+                    if (firstGroup == null) {
+                        firstGroup = g;
+                    }
+                    prevName = displayName;
+                }
+            }
+            if (firstGroup != null) {
+                fillDeps.put(name, firstGroup);
+            }
+        }
+        return arr;
+    }
+
+    private static FileObject getSubDir(FileObject dir, String relPath) {
+        FileObject subDir = dir.getFileObject(relPath);
+        if (subDir == null) {
+            try {
+                subDir = FileUtil.createFolder(dir, relPath);
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+        return subDir;
+    }
+
+    private List<Library> findLibraries(Map<String, Dep> fillDeps, MxSuite suite) {
+        final Map<String, MxLibrary> allLibraries = new HashMap<>();
+        registerLibs(allLibraries, null, suite.libraries());
+
+        List<Library> arr = new ArrayList<>();
+        for (Map.Entry<String, MxLibrary> entry : allLibraries.entrySet()) {
+            final Library library = new Library(entry.getKey(), entry.getValue());
+            arr.add(library);
+            fillDeps.put(library.getName(), library);
+        }
+        for (Map.Entry<String, MxLibrary> entry : suite.jdklibraries().entrySet()) {
+            final JdkLibrary library = new JdkLibrary(entry.getKey(), entry.getValue());
+            arr.add(library);
+            fillDeps.put(library.getName(), library);
+        }
+        return arr;
+    }
+
+    private static Map<String, SuiteSources> findImportedSuites(FileObject dir, MxSuite s, Map<String, Dep> fillDeps) {
+        if (dir == null) {
+            return Collections.emptyMap();
+        }
+        CORE.registerDeps("mx", fillDeps);
+        final MxImports imports = s.imports();
+        if (imports != null) {
+            Map<String, SuiteSources> imported = new LinkedHashMap<>();
+            for (MxImports.Suite imp : imports.suites()) {
+                SuiteSources impSources = findSuiteSources(dir, imp);
+                final String suiteName = imp.name();
+                if (impSources == null) {
+                    LOG.log(Level.INFO, "cannot find imported suite: {0}", suiteName);
+                    continue;
+                }
+                imported.put(suiteName, impSources);
+                impSources.registerDeps(suiteName, fillDeps);
+            }
+            return imported;
+        }
+        return Collections.emptyMap();
+    }
+
+    private List<Dist> findDistributions(MxSuite s, List<Library> libraries, List<Group> groups, Map<String, Dep> fillDeps) {
+        List<Dist> dists = new ArrayList<>();
+        for (Map.Entry<String, MxDistribution> entry : s.distributions().entrySet()) {
+            Dist d = new Dist(entry.getKey(), entry.getValue());
+            dists.add(d);
+            fillDeps.put(d.getName(), d);
+        }
+        return dists;
+    }
+
+    final synchronized void computeTransitiveDeps() {
+        Map<String, Dep> collectedDeps = this.transitiveDeps;
+        if (collectedDeps == null) {
+            return;
+        }
+        this.transitiveDeps = null;
+        for (Library l : this.libraries) {
+            transitiveDeps(l, collectedDeps);
+        }
+        for (Group g : this.groups) {
+            transitiveDeps(g, collectedDeps);
+        }
+        for (Dist d : this.distributions) {
+            transitiveDeps(d, collectedDeps);
+        }
+        for (Group g : groups) {
+            g.computeClassPath(collectedDeps);
+        }
+        for (Dist d : this.distributions) {
+            d.computeSourceRoots(collectedDeps);
+        }
+    }
+
+    private static SuiteSources findSuiteSources(FileObject dir, MxImports.Suite imp) throws IllegalArgumentException {
+        SuiteSources sources = findSuiteSources(dir.getParent(), imp.name());
+        if (sources != null) {
+            return sources;
+        }
+        if (imp.subdir()) {
+            for (FileObject subDir : dir.getParent().getChildren()) {
+                sources = findSuiteSources(subDir, imp.name());
+                if (sources != null) {
+                    return sources;
+                }
+            }
+            for (FileObject subDir : dir.getParent().getParent().getChildren()) {

Review comment:
       There is indeed a 2-level limit in mx: starting from the directory in which the various repositories are checked out, the "importer" specifies if the imported suite is found at depth 1 or 2, no other depth can be specified (default is depth 1 and a `"subdir" : True` attribute switches the depth to 2).




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists