use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.
the class Stitcher method compileLanguageModule.
private static int compileLanguageModule(final String line, final JsOutput writer) throws IOException {
File clsrcTmpDir = Files.createTempDirectory(tmpDir, "clsrc").toFile();
final File tmpout = new File(clsrcTmpDir, Constants.DEFAULT_MODULE_DIR);
FileUtil.mkdirs(tmpout);
final Options opts = new Options().addRepo("build/runtime").comment(false).optimize(true).outRepo(tmpout.getAbsolutePath()).modulify(false).minify(true).suppressWarnings(EnumUtil.enumsFromStrings(Warning.class, Arrays.asList("unusedDeclaration", "ceylonNamespace", "unusedImport"))).addSrcDir(clSrcDir).addSrcDir(LANGMOD_JS_SRC);
// Typecheck the whole language module
if (langmodtc == null) {
langmodtc = new TypeCheckerBuilder().addSrcDirectory(clSrcDir).addSrcDirectory(LANGMOD_JS_SRC).encoding("UTF-8");
langmodtc.setRepositoryManager(CeylonUtils.repoManager().systemRepo(opts.getSystemRepo()).userRepos(opts.getRepos()).outRepo(opts.getOutRepo()).buildManager());
langmodtc.usageWarnings(false);
}
final TypeChecker tc = langmodtc.getTypeChecker();
tc.process(true);
if (tc.getErrors() > 0) {
return 1;
}
// Compile these files
final List<File> includes = new ArrayList<File>();
for (String filename : line.split(",")) {
filename = filename.trim();
final boolean isJsSrc = filename.endsWith(".js");
final boolean isDir = filename.endsWith("/");
final boolean exclude = filename.charAt(0) == '-';
if (exclude) {
filename = filename.substring(1);
}
final File src = ".".equals(filename) ? clSrcFileDir : new File(isJsSrc ? LANGMOD_JS_SRC : clSrcFileDir, isJsSrc || isDir ? filename : String.format("%s.ceylon", filename));
if (!addFiles(includes, src, exclude)) {
final File src2 = new File(clJsFileDir, isDir ? filename : String.format("%s.ceylon", filename));
if (!addFiles(includes, src2, exclude)) {
throw new IllegalArgumentException("Invalid Ceylon language module source " + src + " or " + src2);
}
}
}
if (includes.isEmpty()) {
return 0;
}
// Compile only the files specified in the line
// Set this before typechecking to share some decls that otherwise would be private
JsCompiler jsc = new JsCompiler(tc, opts, true).stopOnErrors(false).setSourceFiles(includes);
if (!jsc.generate()) {
jsc.printErrorsAndCount(new OutputStreamWriter(System.out));
return 1;
} else {
// We still call this here for any warning there might be
jsc.printErrorsAndCount(new OutputStreamWriter(System.out));
}
if (names == null) {
names = jsc.getNames();
}
File compsrc = new File(tmpout, "delete/me/delete-me.js");
if (compsrc.exists() && compsrc.isFile() && compsrc.canRead()) {
try {
writer.outputFile(compsrc);
} finally {
compsrc.delete();
}
} else {
System.out.println("Can't find generated js for language module in " + compsrc.getAbsolutePath());
return 1;
}
return 0;
}
use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.
the class Stitcher method encodeModel.
private static int encodeModel(final File moduleFile) throws IOException {
final String name = moduleFile.getName();
final File file = new File(moduleFile.getParentFile(), name.substring(0, name.length() - 3) + ArtifactContext.JS_MODEL);
System.out.println("Generating language module compile-time model in JSON...");
TypeCheckerBuilder tcb = new TypeCheckerBuilder().usageWarnings(false);
tcb.addSrcDirectory(clSrcDir);
TypeChecker tc = tcb.getTypeChecker();
tc.process(true);
MetamodelVisitor mmg = null;
final ErrorCollectingVisitor errVisitor = new ErrorCollectingVisitor(tc);
for (PhasedUnit pu : tc.getPhasedUnits().getPhasedUnits()) {
pu.getCompilationUnit().visit(errVisitor);
if (errVisitor.getErrorCount() > 0) {
errVisitor.printErrors(false, false);
System.out.println("errors in the language module " + pu.getCompilationUnit().getLocation());
return 1;
}
if (mmg == null) {
mmg = new MetamodelVisitor(pu.getPackage().getModule());
}
pu.getCompilationUnit().visit(mmg);
}
mod = tc.getPhasedUnits().getPhasedUnits().get(0).getPackage().getModule();
try (FileWriter writer = new FileWriter(file)) {
JsCompiler.beginWrapper(writer);
writer.write("ex$.$CCMM$=");
ModelEncoder.encodeModel(mmg.getModel(), writer);
writer.write(";\n");
final JsOutput jsout = new JsOutput(mod, true) {
@Override
public Writer getWriter() throws IOException {
return writer;
}
};
jsout.outputFile(new File(LANGMOD_JS_SRC, "MODEL.js"));
JsCompiler.endWrapper(writer);
} finally {
ShaSigner.sign(file, new JsJULLogger(), true);
}
final File npmFile = new File(moduleFile.getParentFile(), ArtifactContext.NPM_DESCRIPTOR);
try (FileWriter writer = new FileWriter(npmFile)) {
String npmdesc = new NpmDescriptorGenerator(mod, true, false).generateDescriptor();
writer.write(npmdesc);
}
return 0;
}
use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.
the class CeylonVersionTool method run.
@Override
public void run() throws IOException, RecognitionException {
// TODO if version is empty? Prompt? Or should --set have an optional argument?
TypeCheckerBuilder tcb = new TypeCheckerBuilder();
for (File path : this.sourceFolders) {
tcb.addSrcDirectory(applyCwd(path));
}
TypeChecker tc = tcb.getTypeChecker();
PhasedUnits pus = tc.getPhasedUnits();
pus.visitModules();
ArrayList<Module> moduleList = new ArrayList<Module>(pus.getModuleSourceMapper().getCompiledModules());
Collections.sort(moduleList, new Comparator<Module>() {
@Override
public int compare(Module m1, Module m2) {
if (match(m1) && !match(m2)) {
return -1;
} else if (!match(m1) && match(m2)) {
return +1;
}
int cmp = m1.getNameAsString().compareToIgnoreCase(m2.getNameAsString());
if (cmp == 0) {
cmp = m1.getVersion().compareTo(m2.getVersion());
}
return cmp;
}
});
// first update all module versions and remember which version we assigned to which module
// because the user can update every individual version
Map<String, String> updatedModuleVersions = new HashMap<String, String>();
for (Module module : moduleList) {
boolean isMatch = match(module);
if (newVersion == null) {
output(module, isMatch);
} else if (isMatch) {
if (!updateModuleVersion(module, updatedModuleVersions)) {
return;
}
}
}
if (updateDistribution) {
updatedModuleVersions.put("ceylon.bootstrap", newVersion);
updatedModuleVersions.put("ceylon.language", newVersion);
updatedModuleVersions.put("ceylon.runtime", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.compiler.java", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.compiler.js", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.typechecker", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.common", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.cli", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.model", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.module-loader", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.module-resolver", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-aether", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-webdav", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.module-resolver-javascript", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.langtools.classfile", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.tool.provider", newVersion);
updatedModuleVersions.put("org.eclipse.ceylon.tools", newVersion);
}
// now do dependencies because we know which modules we did update
if (newVersion != null && !noUpdateDependencies) {
for (Module module : moduleList) {
if (!updateModuleImports(module, updatedModuleVersions)) {
return;
}
}
}
}
use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.
the class MainForTest method main.
/**
* Files that are not under a proper module structure are
* placed under a <nomodule> module.
*/
public static void main(String[] args) throws Exception {
long start = System.nanoTime();
RepositoryManager repositoryManager = CeylonUtils.repoManager().systemRepo("../dist/dist/repo").outRepo("test/modules").logger(new LeakingLogger()).buildManager();
TypeChecker typeChecker = new TypeCheckerBuilder().statistics(true).verbose(false).addSrcDirectory(new File("test/main")).setRepositoryManager(repositoryManager).getTypeChecker();
typeChecker.process();
int errors = typeChecker.getErrors();
Tree.CompilationUnit compilationUnit = typeChecker.getPhasedUnitFromRelativePath("ceylon/language/Object.ceylon").getCompilationUnit();
if (compilationUnit == null) {
throw new RuntimeException("Failed to pass getCompilationUnitFromRelativePath for files in .src");
}
compilationUnit = typeChecker.getPhasedUnitFromRelativePath("capture/Capture.ceylon").getCompilationUnit();
if (compilationUnit == null) {
throw new RuntimeException("Failed to pass getCompilationUnitFromRelativePath for files in real src dir");
}
compilationUnit = typeChecker.getPhasedUnitFromRelativePath("org/eclipse/sample/multisource/Boo.ceylon").getCompilationUnit();
Module module = compilationUnit.getUnit().getPackage().getModule();
if (!"org.eclipse.sample.multisource".equals(module.getNameAsString())) {
throw new RuntimeException("Unable to extract module name");
}
if (!"0.2".equals(module.getVersion())) {
throw new RuntimeException("Unable to extract module version");
}
typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(new File("test/main/capture")).setRepositoryManager(repositoryManager).getTypeChecker();
typeChecker.process();
errors += typeChecker.getErrors();
compilationUnit = typeChecker.getPhasedUnitFromRelativePath("Capture.ceylon").getCompilationUnit();
if (compilationUnit == null) {
throw new RuntimeException("Failed to pass getCompilationUnitFromRelativePath for top level files (no package) in real src dir");
}
typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(new File("test/moduledep1")).addSrcDirectory(new File("test/moduledep2")).addSrcDirectory(new File("test/moduletest")).addSrcDirectory(new File("test/restricted")).setRepositoryManager(repositoryManager).getTypeChecker();
typeChecker.process();
errors += typeChecker.getErrors();
ClosableVirtualFile latestZippedLanguageSourceFile = MainHelper.getLatestZippedLanguageSourceFile();
typeChecker = new TypeCheckerBuilder().verbose(false).addSrcDirectory(latestZippedLanguageSourceFile).setRepositoryManager(repositoryManager).getTypeChecker();
typeChecker.process();
errors += typeChecker.getErrors();
latestZippedLanguageSourceFile.close();
System.out.println("Tests took " + ((System.nanoTime() - start) / 1000000) + " ms");
if (errors > 0) {
System.exit(1);
}
}
use of org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder in project ceylon by eclipse.
the class ModelLoaderTests method compareNativeRuntimeWithJavaRuntime.
@Test
public void compareNativeRuntimeWithJavaRuntime() {
// parse the ceylon sources from the language module and
// build a map of all the native declarations
final Map<String, Declaration> nativeFromSource = new HashMap<String, Declaration>();
ClosableVirtualFile latestZippedLanguageSourceFile = getLatestZippedLanguageSourceFile();
try {
TypeCheckerBuilder typeCheckerBuilder = new TypeCheckerBuilder().verbose(false).addSrcDirectory(latestZippedLanguageSourceFile);
TypeChecker typeChecker = typeCheckerBuilder.getTypeChecker();
typeChecker.process();
for (PhasedUnit pu : typeChecker.getPhasedUnits().getPhasedUnits()) {
for (Declaration d : pu.getDeclarations()) {
if (d.isNativeHeader() && d.isToplevel()) {
String qualifiedNameString = d.getQualifiedNameString();
String key = d.getDeclarationKind() + ":" + qualifiedNameString;
Declaration prev = nativeFromSource.put(key, d);
if (prev != null) {
Assert.fail("Two declarations with the same key " + key + ": " + d + " and: " + prev);
}
}
}
}
} finally {
latestZippedLanguageSourceFile.close();
}
System.out.println(nativeFromSource);
// now compile something (it doesn't matter what, we just need
// to get our hands on from-binary models for the language module)
RunnableTest tester = new RunnableTest() {
@Override
public void test(ModelLoader loader) {
OtherModelCompare comparer = new OtherModelCompare();
Module binaryLangMod = loader.getLoadedModule(AbstractModelLoader.CEYLON_LANGUAGE, Versions.CEYLON_VERSION_NUMBER);
for (Map.Entry<String, Declaration> entry : nativeFromSource.entrySet()) {
System.out.println(entry.getKey());
Declaration source = entry.getValue();
ModelLoader.DeclarationType dt = null;
switch(source.getDeclarationKind()) {
case TYPE:
case TYPE_PARAMETER:
dt = DeclarationType.TYPE;
break;
case MEMBER:
case SETTER:
dt = DeclarationType.VALUE;
break;
}
// Ensure the package is loaded
binaryLangMod.getDirectPackage(source.getQualifiedNameString().replaceAll("::.*", ""));
Declaration binary = loader.getDeclaration(binaryLangMod, source.getQualifiedNameString().replace("::", "."), dt);
comparer.compareDeclarations(source.getQualifiedNameString(), source, binary);
}
}
};
verifyCompilerClassLoading("Any.ceylon", tester, defaultOptions);
verifyRuntimeClassLoading(tester);
}
Aggregations