use of sharpen.xobotos.generator.CompilationUnitBuilder in project XobotOS by xamarin.
the class XobotBuilder method stage4_save_output.
private boolean stage4_save_output(IProgressMonitor monitor, List<CompilationUnitBuilder> builders) {
final int count = builders.size();
final int totalWork = OUTPUT_PRICE * count;
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, totalWork);
subMonitor.beginTask("Writing output", totalWork);
Sharpen.Log(Level.INFO, "Writing output ...");
int i = 0;
boolean foundErrors = false;
for (final CompilationUnitBuilder builder : builders) {
final ICompilationUnit source = builder.getPair().source;
final CSCompilationUnit unit = builder.getCompilationUnit();
final String pathName = builder.getName();
final String message = String.format("Saving (%d/%d): %s", ++i, count, pathName);
subMonitor.subTask(message);
try {
if (!builder.writeOutput()) {
Sharpen.Log(Level.SEVERE, "Failed to save output file: %s", pathName);
foundErrors = true;
continue;
}
if (unit.ignore() || unit.types().isEmpty())
continue;
final StringWriter writer = new StringWriter();
writer.write(_config.header());
CSharpPrinter printer = new CSharpPrinter();
printer.setWriter(writer);
printer.print(unit);
if (writer.getBuffer().length() > 0)
saveConvertedFile(source, unit, writer);
} catch (CoreException e) {
Sharpen.Log(e, "Cannot save output file: %s", pathName);
foundErrors = true;
} finally {
subMonitor.worked(OUTPUT_PRICE);
}
}
if (foundErrors) {
Sharpen.Log(Level.SEVERE, "Errors while writing output!");
_foundErrors = true;
}
subMonitor.done();
return !foundErrors;
}
use of sharpen.xobotos.generator.CompilationUnitBuilder in project XobotOS by xamarin.
the class XobotBuilder method run.
protected boolean run(final IProgressMonitor monitor) {
Sharpen.Log(Level.INFO, "Starting build");
try {
checkFileList();
} catch (Exception e) {
Sharpen.Log(e, "Cannot compute file list");
return false;
}
int countModified = 0;
for (final Entry<ICompilationUnit, Boolean> entry : _sources.entrySet()) {
if (!entry.getValue())
continue;
++countModified;
_mustParse.put(entry.getKey(), true);
}
if (countModified < 1) {
Sharpen.Log(Level.INFO, "Nothing to do.");
return true;
}
List<ICompilationUnit> parsingList = new ArrayList<ICompilationUnit>();
for (final Entry<ICompilationUnit, Boolean> entry : _mustParse.entrySet()) {
if (!entry.getValue())
continue;
parsingList.add(entry.getKey());
}
final int totalWork = parsingList.size() * PARSING_PRICE + countModified * (GENERATING_PRICE + OUTPUT_PRICE);
Sharpen.Log(Level.INFO, "Converting %d files (must parse %d).", countModified, parsingList.size());
monitor.beginTask("Converting", totalWork);
final List<CompilationUnitPair> pairs = stage1_parse(parsingList, monitor);
if (monitor.isCanceled() || (pairs == null) || (pairs.size() == 0))
return false;
if (!stage1b_checkForErrors(pairs)) {
Sharpen.Log(Level.SEVERE, "Found errors while parsing compilation units; aborting!");
return false;
}
AST ast = pairs.get(0).ast.getAST();
final BindingManager bindings = new BindingManager(ast, _configFile.getNativeConfig());
final ByRef<Map<ICompilationUnit, CompilationUnitBuilder>> builders = new ByRef<Map<ICompilationUnit, CompilationUnitBuilder>>();
Environments.runWith(Environments.newClosedEnvironment(_api, bindings, _config), new Runnable() {
@Override
public void run() {
builders.value = stage2_preprocess(bindings, pairs);
}
});
if (monitor.isCanceled())
return false;
if (builders.value == null) {
Sharpen.Log(Level.SEVERE, "Found errors while pre-processing; aborting!");
return false;
}
final List<CompilationUnitPair> modifiedPairs = new ArrayList<CompilationUnitPair>();
final List<CompilationUnitBuilder> modified = new ArrayList<CompilationUnitBuilder>();
for (final CompilationUnitBuilder builder : builders.value.values()) {
if (_sources.get(builder.getPair().source)) {
modifiedPairs.add(builder.getPair());
modified.add(builder);
}
}
final ASTResolver resolver = new DefaultASTResolver(modifiedPairs);
final ByRef<Boolean> ok = new ByRef<Boolean>();
Environments.runWith(Environments.newClosedEnvironment(_api, bindings, _config, _configFile, resolver), new Runnable() {
@Override
public void run() {
ok.value = stage3_generate(monitor, modified);
}
});
if (monitor.isCanceled() || !ok.value)
return false;
Environments.runWith(Environments.newClosedEnvironment(_api, bindings, _config, _configFile, resolver), new Runnable() {
@Override
public void run() {
// ok.value =
// stage4_post_processs(monitor,
// modified);
ok.value = bindings.postProcess();
}
});
if (monitor.isCanceled() || !ok.value)
return false;
Environments.runWith(Environments.newClosedEnvironment(_api, bindings, _config, _configFile, resolver), new Runnable() {
@Override
public void run() {
ok.value = stage4_save_output(monitor, modified);
}
});
if (monitor.isCanceled() || !ok.value)
return false;
return stage5_generate_csproj();
}
use of sharpen.xobotos.generator.CompilationUnitBuilder in project XobotOS by xamarin.
the class XobotBuilder method stage2_preprocess.
private Map<ICompilationUnit, CompilationUnitBuilder> stage2_preprocess(BindingManager bindings, List<CompilationUnitPair> sources) {
Map<ICompilationUnit, CompilationUnitBuilder> builders = new HashMap<ICompilationUnit, CompilationUnitBuilder>();
boolean foundErrors = false;
for (final CompilationUnitPair pair : sources) {
CompilationUnitBuilder builder = _api.preprocess(pair);
if (builder == null) {
Sharpen.Log(Level.SEVERE, "Preprocessor failed on %s", getUnitName(pair.source));
continue;
}
builders.put(pair.source, builder);
}
if (foundErrors)
return null;
if (!bindings.resolveTypes()) {
Sharpen.Log(Level.SEVERE, "Failed to resolve some types!");
}
return builders;
}
use of sharpen.xobotos.generator.CompilationUnitBuilder in project XobotOS by xamarin.
the class XobotBuilder method stage3_generate.
private Boolean stage3_generate(IProgressMonitor monitor, List<CompilationUnitBuilder> builders) {
final int totalWork = GENERATING_PRICE * builders.size();
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, totalWork);
subMonitor.beginTask("Converting", totalWork);
Sharpen.Log(Level.INFO, "Converting %d files", builders.size());
int i = 0;
int errorCount = 0;
for (final CompilationUnitBuilder builder : builders) {
if (monitor.isCanceled())
return null;
final String message = String.format("Generating (%d/%d): %s", ++i, builders.size(), builder.getName());
subMonitor.subTask(message);
try {
if (!builder.build()) {
Sharpen.Log(Level.SEVERE, "Generator did not produce any output for %s", builder.getName());
++errorCount;
}
} catch (Exception e) {
Sharpen.Log(e, "Exception while generating %s", builder.getName());
++errorCount;
} finally {
subMonitor.worked(GENERATING_PRICE);
}
}
if (errorCount > 0) {
Sharpen.Log(Level.SEVERE, "Converting finished, found %d errors.", errorCount);
_foundErrors = true;
}
subMonitor.done();
return !_foundErrors;
}
Aggregations