Search in sources :

Example 1 with SourceArgumentsResolver

use of org.eclipse.ceylon.common.tools.SourceArgumentsResolver in project ceylon by eclipse.

the class Main method addModuleFiles.

// Now add the files for each of the modules that were given on the command line
@SuppressWarnings("unchecked")
private ListBuffer<File> addModuleFiles(Collection<File> f) throws IOException {
    Iterable<File> srcdirs = (Iterable<File>) ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
    Iterable<File> resdirs = (Iterable<File>) ((JavacFileManager) fileManager).getLocation(CeylonLocation.RESOURCE_PATH);
    SourceArgumentsResolver resolver = new SourceArgumentsResolver(srcdirs, resdirs, Constants.CEYLON_SUFFIX, Constants.JAVA_SUFFIX);
    resolver.parse(classnames.toList());
    ListBuffer<File> filenames = new ListBuffer<File>();
    filenames.addAll(f);
    filenames.addAll(resolver.getSourceFiles());
    filenames.addAll(resolver.getResourceFiles());
    return filenames;
}
Also used : SourceArgumentsResolver(org.eclipse.ceylon.common.tools.SourceArgumentsResolver) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) File(java.io.File)

Example 2 with SourceArgumentsResolver

use of org.eclipse.ceylon.common.tools.SourceArgumentsResolver in project ceylon by eclipse.

the class CeylonCompileTool method initialize.

@Override
public void initialize(CeylonTool mainTool) throws Exception {
    super.initialize(mainTool);
    compiler = new Main("ceylon compile");
    helper.options.clear();
    Options.instance(new Context());
    includeDependencies = processCompileFlags(includeDependencies, DefaultToolOptions.getCompilerIncludeDependencies());
    if (modulesOrFiles.isEmpty() && !javac.contains("-help") && !javac.contains("-X") && !javac.contains("-version")) {
        throw new IllegalStateException("Argument moduleOrFile should appear at least 1 time(s)");
    }
    arguments = new ArrayList<>();
    if (cwd != null) {
        arguments.add("-cwd");
        arguments.add(cwd.getPath());
        validateWithJavac(org.eclipse.ceylon.langtools.tools.javac.main.Option.CEYLONCWD, "-cwd", cwd.getPath());
    }
    if (jdkProvider != null) {
        arguments.add("-jdk-provider");
        arguments.add(jdkProvider.toString());
    }
    if (aptModules != null) {
        for (ModuleSpec mod : aptModules) {
            arguments.add("-apt");
            arguments.add(mod.toString());
        }
    }
    for (File source : applyCwd(this.sources)) {
        arguments.add("-src");
        arguments.add(source.getPath());
        validateWithJavac(org.eclipse.ceylon.langtools.tools.javac.main.Option.CEYLONSOURCEPATH, "-sourcepath", source.getPath());
    }
    for (File resource : applyCwd(this.resources)) {
        arguments.add("-res");
        arguments.add(resource.getPath());
    }
    if (resourceRoot != null) {
        arguments.add("-resroot");
        arguments.add(resourceRoot);
    }
    if (continueOnErrors) {
        arguments.add("-continue");
    }
    if (progress) {
        arguments.add("-progress");
    }
    if (offline) {
        arguments.add("-offline");
    }
    if (timeout != -1) {
        arguments.add("-timeout");
        arguments.add(String.valueOf(timeout));
    }
    if (flatClasspath) {
        arguments.add("-flat-classpath");
    }
    if (autoExportMavenDependencies) {
        arguments.add("-auto-export-maven-dependencies");
    }
    if (fullyExportMavenDependencies) {
        arguments.add("-fully-export-maven-dependencies");
    }
    if (overrides != null) {
        arguments.add("-overrides");
        if (overrides.startsWith("classpath:")) {
            arguments.add(overrides);
        } else {
            arguments.add(applyCwd(new File(overrides)).getPath());
        }
    }
    if (jigsaw) {
        arguments.add("-module-info");
    }
    if (noOsgi) {
        arguments.add("-noosgi");
    }
    if (osgiProvidedBundles != null && !osgiProvidedBundles.isEmpty()) {
        arguments.add("-osgi-provided-bundles");
        arguments.add(osgiProvidedBundles);
    }
    if (noPom) {
        arguments.add("-nopom");
    }
    if (pack200) {
        arguments.add("-pack200");
    }
    if (verbose != null) {
        if (verbose.isEmpty()) {
            arguments.add("-verbose");
        } else {
            arguments.add("-verbose:" + verbose);
        }
    }
    if (out != null) {
        arguments.add("-out");
        arguments.add(out);
    }
    if (user != null) {
        arguments.add("-user");
        arguments.add(user);
    }
    if (pass != null) {
        arguments.add("-pass");
        arguments.add(pass);
    }
    String fileEncoding = encoding;
    if (fileEncoding == null) {
        fileEncoding = DefaultToolOptions.getDefaultEncoding();
    }
    if (fileEncoding != null) {
        try {
            Charset.forName(fileEncoding);
        } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
            throw new IllegalArgumentException("Unsupported encoding: " + fileEncoding);
        }
        arguments.add(org.eclipse.ceylon.langtools.tools.javac.main.Option.ENCODING.text);
        arguments.add(fileEncoding);
    }
    if (systemRepo != null) {
        arguments.add("-sysrep");
        arguments.add(systemRepo);
    }
    if (cacheRepo != null) {
        arguments.add("-cacherep");
        arguments.add(cacheRepo);
    }
    if (noDefRepos) {
        arguments.add("-nodefreps");
    }
    if (repos != null) {
        for (URI uri : this.repos) {
            arguments.add("-rep");
            arguments.add(uri.toString());
        }
    }
    if (suppressWarnings != null) {
        arguments.add("-suppress-warnings");
        arguments.add(EnumUtil.enumsToString(suppressWarnings));
    }
    if (targetVersion != null) {
        arguments.add("-source");
        arguments.add(targetVersion.toString());
        arguments.add("-target");
        arguments.add(targetVersion.toString());
    }
    if (ee) {
        arguments.add("-ee");
    }
    if (eeImport != null) {
        for (String eeImport : this.eeImport) {
            arguments.add("-ee-import");
            arguments.add(eeImport);
        }
    }
    if (eeAnnotation != null) {
        for (String eeAnnotation : this.eeAnnotation) {
            arguments.add("-ee-annotation");
            arguments.add(eeAnnotation);
        }
    }
    addJavacArguments(arguments, javac);
    List<File> srcs = applyCwd(this.sources);
    Collection<String> expandedModulesOrFiles = ModuleWildcardsHelper.expandWildcards(srcs, this.modulesOrFiles, Backend.Java);
    expandedModulesOrFiles = normalizeFileNames(expandedModulesOrFiles);
    if (expandedModulesOrFiles.isEmpty()) {
        String msg = CeylonCompileMessages.msg("error.no.sources");
        if (ModuleWildcardsHelper.onlyGlobArgs(this.modulesOrFiles)) {
            throw new NonFatalToolMessage(msg);
        } else {
            throw new ToolUsageError(msg);
        }
    }
    for (String moduleOrFile : expandedModulesOrFiles) {
        if (!org.eclipse.ceylon.langtools.tools.javac.main.Option.SOURCEFILE.matches(moduleOrFile)) {
            throw new IllegalArgumentException(CeylonCompileMessages.msg("argument.error", moduleOrFile));
        }
        validateWithJavac(org.eclipse.ceylon.langtools.tools.javac.main.Option.SOURCEFILE, moduleOrFile, moduleOrFile);
    }
    // We validate that all source arguments are correct
    SourceArgumentsResolver sar = new SourceArgumentsResolver(this.sources, this.resources, Constants.CEYLON_SUFFIX, Constants.JAVA_SUFFIX);
    sar.cwd(cwd).parse(expandedModulesOrFiles);
    if (includeDependencies != null && !COMPILE_NEVER.equals(includeDependencies)) {
        // Determine any dependencies that might need compiling as well
        SourceDependencyResolver sdr = new SourceDependencyResolver(getModuleVersionReader(), this.sources, Backends.JAVA);
        if (sdr.traverseDependencies(sar.getSourceFiles())) {
            for (ModuleVersionDetails mvd : sdr.getAdditionalModules()) {
                if (COMPILE_FORCE.equals(includeDependencies) || (COMPILE_CHECK.equals(includeDependencies) && shouldRecompile(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JVM, true)) || (COMPILE_ONCE.equals(includeDependencies) && shouldRecompile(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JVM, false))) {
                    expandedModulesOrFiles.add(mvd.getModule());
                    if (incremental) {
                        sar.parse(expandedModulesOrFiles);
                    }
                }
            }
        }
    }
    if (incremental) {
        next_module: for (String module : sar.getModules()) {
            // Determine module version from source
            ModuleVersionDetails mvd = getModuleVersionReader().fromSource(module);
            if (mvd != null) {
                // Find the module's CAR file
                ArtifactResult artifact = getModuleArtifact(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JVM);
                if (artifact != null) {
                    // Check the language module version
                    for (ArtifactResult dep : artifact.dependencies()) {
                        if ("ceylon.language".equals(dep.name())) {
                            if (!Versions.CEYLON_VERSION_NUMBER.equals(dep.version())) {
                                // Skip handling of --incremental on this module
                                continue next_module;
                            }
                            break;
                        }
                    }
                    File carFile = artifact.artifact();
                    // Check if it has META-INF/errors.txt
                    Properties errors = getMetaInfErrors(carFile);
                    if (errors != null && !errors.isEmpty()) {
                        // TODO handle this incrementally
                        continue;
                    }
                    // Check if it has META-INF/hashes.txt
                    Properties oldHashes = getMetaInfHashes(carFile);
                    if (oldHashes != null) {
                        // Get the hashes for the new files
                        List<File> files = sar.getFilesByModule().get(module);
                        Properties newHashes = getFileHashes(module, files);
                        // Compare the two and make list of changed files
                        Collection<String> changedFiles = determineChangedFiles(module, oldHashes, newHashes, carFile);
                        if (changedFiles == null) {
                        // This shouldn't happen, but if it does we just skip any
                        // special treatment and compile this module normally
                        } else if (changedFiles.isEmpty()) {
                            // No files were changed, we shouldn't compile the module
                            expandedModulesOrFiles.remove(module);
                            // And we remove its files too if any were mentioned
                            Collection<String> remove = filesToStrings(module, files);
                            expandedModulesOrFiles.removeAll(remove);
                        } else {
                            if (expandedModulesOrFiles.contains(module)) {
                                // The module itself was mentioned on the command line
                                if (changedFiles.size() < files.size()) {
                                    // There were fewer changed files than the total number
                                    // So we remove the module
                                    expandedModulesOrFiles.remove(module);
                                    // And we remove its files too if any were mentioned
                                    Collection<String> remove = filesToStrings(module, files);
                                    expandedModulesOrFiles.removeAll(remove);
                                    // And then we add only those files that were changed
                                    expandedModulesOrFiles.addAll(changedFiles);
                                }
                            } else {
                                // Separate source files were mentioned on the command line
                                // So we remove the unchanged files
                                Collection<String> unchanged = filesToStrings(module, files);
                                unchanged.removeAll(changedFiles);
                                expandedModulesOrFiles.removeAll(unchanged);
                            }
                        }
                    }
                }
            }
        }
        if (expandedModulesOrFiles.isEmpty()) {
            String msg = CeylonCompileMessages.msg("error.no.need");
            throw new NonFatalToolMessage(msg);
        }
    }
    arguments.addAll(expandedModulesOrFiles);
    if (verbose != null) {
        System.out.println(arguments);
        System.out.flush();
    }
}
Also used : Context(org.eclipse.ceylon.langtools.tools.javac.util.Context) SourceArgumentsResolver(org.eclipse.ceylon.common.tools.SourceArgumentsResolver) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) SourceDependencyResolver(org.eclipse.ceylon.common.tools.SourceDependencyResolver) Properties(java.util.Properties) URI(java.net.URI) NonFatalToolMessage(org.eclipse.ceylon.common.tool.NonFatalToolMessage) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Main(org.eclipse.ceylon.compiler.java.launcher.Main) File(java.io.File)

Example 3 with SourceArgumentsResolver

use of org.eclipse.ceylon.common.tools.SourceArgumentsResolver in project ceylon by eclipse.

the class CeylonCompileJsTool method run.

@Override
public void run() throws Exception {
    AppendableWriter writer = new AppendableWriter(getOutAppendable());
    final Options opts = new Options().cwd(cwd).repos(getRepositoryAsStrings()).sourceDirs(roots).resourceDirs(resources).resourceRootName(resourceRootName).systemRepo(systemRepo).outRepo(getOut()).user(user).pass(pass).optimize(optimize).modulify(modulify).comment(comments).verbose(getVerbose()).profile(profile).stdin(false).generateSourceArchive(!skipSrc).encoding(encoding).includeDependencies(includeDependencies).diagnosticListener(diagnosticListener).outWriter(writer).suppressWarnings(suppwarns);
    final TypeChecker typeChecker;
    if (opts.hasVerboseFlag("cmr")) {
        append("Using repositories: " + getRepositoryAsStrings());
        newline();
    }
    final RepositoryManager repoman = getRepositoryManager();
    long t0, t1, t2, t3, t4;
    final TypeCheckerBuilder tcb;
    List<File> onlySources = null;
    List<File> onlyResources = null;
    if (opts.isStdin()) {
        VirtualFile src = new VirtualFile() {

            @Override
            public boolean exists() {
                return true;
            }

            @Override
            public boolean isFolder() {
                return false;
            }

            @Override
            public String getName() {
                return "SCRIPT.ceylon";
            }

            @Override
            public String getPath() {
                return getName();
            }

            @Override
            public String getRelativePath(VirtualFile file) {
                return "";
            }

            @Override
            public InputStream getInputStream() {
                return System.in;
            }

            @Override
            public List<VirtualFile> getChildren() {
                return Collections.emptyList();
            }

            @Override
            public int hashCode() {
                return getPath().hashCode();
            }

            @Override
            public boolean equals(Object obj) {
                if (obj instanceof VirtualFile) {
                    return ((VirtualFile) obj).getPath().equals(getPath());
                } else {
                    return super.equals(obj);
                }
            }

            @Override
            public int compareTo(VirtualFile o) {
                return getPath().compareTo(o.getPath());
            }
        };
        t0 = System.nanoTime();
        tcb = new TypeCheckerBuilder().addSrcDirectory(src);
    } else {
        t0 = System.nanoTime();
        tcb = new TypeCheckerBuilder();
        SourceArgumentsResolver resolver = new SourceArgumentsResolver(roots, resources, Constants.CEYLON_SUFFIX, Constants.JS_SUFFIX);
        resolver.cwd(cwd).expandAndParse(files, Backend.JavaScript);
        if (includeDependencies != null && !COMPILE_NEVER.equals(includeDependencies)) {
            // Determine any dependencies that might need compiling as well
            SourceDependencyResolver sdr = new SourceDependencyResolver(getModuleVersionReader(), roots, Backends.JS);
            if (sdr.traverseDependencies(resolver.getSourceFiles())) {
                for (ModuleVersionDetails mvd : sdr.getAdditionalModules()) {
                    if (COMPILE_FORCE.equals(includeDependencies) || (COMPILE_CHECK.equals(includeDependencies) && shouldRecompile(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JS, true)) || (COMPILE_ONCE.equals(includeDependencies) && shouldRecompile(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JS, false))) {
                        files.add(mvd.getModule());
                        resolver.expandAndParse(files, Backend.JavaScript);
                    }
                }
            }
        }
        onlySources = resolver.getSourceFiles();
        onlyResources = resolver.getResourceFiles();
        if (onlySources.isEmpty()) {
            String msg = CeylonCompileJsMessages.msg("error.no.sources");
            if (ModuleWildcardsHelper.onlyGlobArgs(files)) {
                throw new NonFatalToolMessage(msg);
            } else {
                throw new ToolUsageError(msg);
            }
        }
        if (opts.isVerbose()) {
            append("Adding source directories to typechecker:" + roots).newline();
        }
        for (File root : roots) {
            File cwdRoot = applyCwd(root);
            if (cwdRoot.exists() && cwdRoot.isDirectory()) {
                tcb.addSrcDirectory(cwdRoot);
            }
        }
        tcb.setSourceFiles(onlySources);
        if (!resolver.getSourceModules().isEmpty()) {
            tcb.setModuleFilters(resolver.getSourceModules());
        }
        tcb.statistics(opts.isProfile());
        JsModuleManagerFactory.setVerbose(opts.hasVerboseFlag("loader"));
        tcb.moduleManagerFactory(new JsModuleManagerFactory(encoding));
    }
    // getting the type checker does process all types in the source directory
    tcb.verbose(opts.hasVerboseFlag("ast")).setRepositoryManager(repoman);
    tcb.usageWarnings(false).encoding(encoding);
    typeChecker = tcb.getTypeChecker();
    t1 = System.nanoTime();
    TypeCache.doWithoutCaching(new Runnable() {

        @Override
        public void run() {
            typeChecker.process(true);
        }
    });
    t2 = System.nanoTime();
    JsCompiler jsc = new JsCompiler(typeChecker, opts);
    if (onlySources != null) {
        if (opts.isVerbose()) {
            append("Only these files will be compiled: " + onlySources).newline();
        }
        jsc.setSourceFiles(onlySources);
    }
    if (onlyResources != null) {
        jsc.setResourceFiles(onlyResources);
    }
    t3 = System.nanoTime();
    if (!jsc.generate()) {
        if (jsc.getExitCode() != 0) {
            if (throwOnError)
                throw new RuntimeException("Compiler exited with non-zero exit code: " + jsc.getExitCode());
            else {
                jsc.printErrorsAndCount(writer);
                System.exit(jsc.getExitCode());
            }
        }
        int count = jsc.printErrorsAndCount(writer);
        String msg = (count > 1) ? "There were %d errors." : "There was %d error.";
        flush();
        throw new CompilerErrorException(String.format(msg, count));
    } else {
        // We still call this here for any warning there might be
        jsc.printErrorsAndCount(writer);
    }
    t4 = System.nanoTime();
    if (opts.isProfile() || opts.hasVerboseFlag("benchmark")) {
        System.err.println("PROFILING INFORMATION");
        System.err.printf("TypeChecker creation:   %6d nanos%n", t1 - t0);
        System.err.printf("TypeChecker processing: %6d nanos%n", t2 - t1);
        System.err.printf("JS compiler creation:   %6d nanos%n", t3 - t2);
        System.err.printf("JS compilation:         %6d nanos%n", t4 - t3);
        System.out.println("Compilation finished.");
    }
}
Also used : VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) Options(org.eclipse.ceylon.compiler.js.util.Options) DefaultToolOptions(org.eclipse.ceylon.common.config.DefaultToolOptions) JsModuleManagerFactory(org.eclipse.ceylon.compiler.js.loader.JsModuleManagerFactory) SourceArgumentsResolver(org.eclipse.ceylon.common.tools.SourceArgumentsResolver) TypeChecker(org.eclipse.ceylon.compiler.typechecker.TypeChecker) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) SourceDependencyResolver(org.eclipse.ceylon.common.tools.SourceDependencyResolver) TypeCheckerBuilder(org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder) NonFatalToolMessage(org.eclipse.ceylon.common.tool.NonFatalToolMessage) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) VirtualFile(org.eclipse.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Aggregations

File (java.io.File)3 SourceArgumentsResolver (org.eclipse.ceylon.common.tools.SourceArgumentsResolver)3 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)2 NonFatalToolMessage (org.eclipse.ceylon.common.tool.NonFatalToolMessage)2 ToolUsageError (org.eclipse.ceylon.common.tool.ToolUsageError)2 SourceDependencyResolver (org.eclipse.ceylon.common.tools.SourceDependencyResolver)2 URI (java.net.URI)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Properties (java.util.Properties)1 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)1 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)1 DefaultToolOptions (org.eclipse.ceylon.common.config.DefaultToolOptions)1 Main (org.eclipse.ceylon.compiler.java.launcher.Main)1 JsModuleManagerFactory (org.eclipse.ceylon.compiler.js.loader.JsModuleManagerFactory)1 Options (org.eclipse.ceylon.compiler.js.util.Options)1 TypeChecker (org.eclipse.ceylon.compiler.typechecker.TypeChecker)1