use of org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache in project legend-pure by finos.
the class JavaClassLoaderSourceCodeGenerator method gen.
public static void gen(long start, Path filePath, String name, MutableList<CompiledExtension> extensions) {
RichIterable<CodeRepository> repositoriesForCompilation = Lists.fixedSize.of(CodeRepository.newPlatformCodeRepository());
PureCodeStorage codeStorage = new PureCodeStorage(null, new ClassLoaderCodeStorage(repositoriesForCompilation));
ClassLoaderPureGraphCache graphCache = new ClassLoaderPureGraphCache();
PureRuntime runtime = new PureRuntimeBuilder(codeStorage).withCache(graphCache).setTransactionalByDefault(false).buildAndTryToInitializeFromCache();
if (!runtime.isInitialized()) {
CacheState cacheState = graphCache.getCacheState();
if (cacheState != null) {
String lastStackTrace = cacheState.getLastStackTrace();
if (lastStackTrace != null) {
System.out.println("Cache initialization failure: " + lastStackTrace);
}
}
System.out.println("Initialization from caches failed - compiling from scratch");
runtime.reset();
runtime.loadAndCompileCore();
runtime.loadAndCompileSystem();
}
System.out.format("Finished Pure initialization (%.6fs)%n", (System.nanoTime() - start) / 1_000_000_000.0);
JavaSourceCodeGenerator javaSourceCodeGenerator = new JavaSourceCodeGenerator(runtime.getProcessorSupport(), runtime.getCodeStorage(), true, filePath, false, extensions, name, JavaPackageAndImportBuilder.externalizablePackage());
javaSourceCodeGenerator.generateCode();
javaSourceCodeGenerator.generatePureCoreHelperClasses(new ProcessorContext(runtime.getProcessorSupport(), false));
}
use of org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache in project legend-pure by finos.
the class PureCompiledJarMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
getLog().info("Generating Java Compiled JAR");
getLog().info(" Requested repositories: " + Arrays.toString(this.repositories));
getLog().info(" Excluded repositories: " + Arrays.toString(this.excludedRepositories));
getLog().info(" Extra repositories: " + Arrays.toString(this.extraRepositories));
RichIterable<CodeRepository> resolvedRepositories = resolveRepositories(repositories, excludedRepositories, extraRepositories);
getLog().info(" Repositories with resolved dependencies: " + resolvedRepositories);
Path distributedMetadataDirectory = null;
Path codegenDirectory = null;
if (!this.generateMetadata) {
getLog().info(" Classes output directory: " + this.classesDirectory);
getLog().info(" No metadata output");
} else if (this.useSingleDir) {
distributedMetadataDirectory = this.classesDirectory.toPath();
getLog().info(" All in output directory: " + this.classesDirectory);
} else {
distributedMetadataDirectory = this.targetDirectory.toPath().resolve("metadata-distributed");
getLog().info(" Classes output directory: " + this.classesDirectory);
getLog().info(" Distributed metadata output directory: " + distributedMetadataDirectory);
}
if (this.generateSources) {
codegenDirectory = this.targetDirectory.toPath().resolve("generated");
getLog().info(" Codegen output directory: " + codegenDirectory);
}
long start = System.nanoTime();
// Initialize runtime
PureRuntime runtime;
try {
getLog().info(" Beginning Pure initialization");
SetIterable<CodeRepository> repositoriesForCompilation = PureCodeStorage.getRepositoryDependencies(PureRepositoriesExternal.repositories(), resolvedRepositories);
// Add the project output to the plugin classloader
URL[] urlsForClassLoader = ListAdapter.adapt(project.getCompileClasspathElements()).collect(mavenCompilePath -> {
try {
return new File(mavenCompilePath).toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}).toArray(new URL[0]);
getLog().info(" Project classLoader URLs " + Arrays.toString(urlsForClassLoader));
ClassLoader classLoader = new URLClassLoader(urlsForClassLoader, PureCompiledJarMojo.class.getClassLoader());
// Initialize from PAR files cache
PureCodeStorage codeStorage = new PureCodeStorage(null, new ClassLoaderCodeStorage(classLoader, repositoriesForCompilation));
ClassLoaderPureGraphCache graphCache = new ClassLoaderPureGraphCache(classLoader);
runtime = new PureRuntimeBuilder(codeStorage).withCache(graphCache).setTransactionalByDefault(false).buildAndTryToInitializeFromCache();
if (!runtime.isInitialized()) {
CacheState cacheState = graphCache.getCacheState();
if (cacheState != null) {
String lastStackTrace = cacheState.getLastStackTrace();
if (lastStackTrace != null) {
getLog().warn(" Cache initialization failure: " + lastStackTrace);
}
}
getLog().info(" Initialization from caches failed - compiling from scratch");
runtime.reset();
runtime.loadAndCompileCore();
runtime.loadAndCompileSystem();
}
getLog().info(String.format(" Finished Pure initialization (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0));
} catch (PureException e) {
getLog().error(String.format(" Error initializing Pure (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0), e);
throw new MojoFailureException(e.getInfo(), e);
} catch (Exception e) {
getLog().error(String.format(" Error initializing Pure (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0), e);
throw new MojoExecutionException(" Error initializing Pure", e);
}
if (generateMetadata) {
// Write distributed metadata
String writeMetadataStep = "writing distributed Pure metadata";
long writeMetadataStart = startStep(writeMetadataStep);
try {
DistributedBinaryGraphSerializer.serialize(runtime, distributedMetadataDirectory);
completeStep(writeMetadataStep, writeMetadataStart);
} catch (Exception e) {
throw mojoException(e, writeMetadataStep, writeMetadataStart, start);
}
}
JavaStandaloneLibraryGenerator generator = JavaStandaloneLibraryGenerator.newGenerator(runtime, CompiledExtensionLoader.extensions(), false, JavaPackageAndImportBuilder.externalizablePackage());
// Generate Java sources
Generate generate;
String generateStep = "Pure compiled mode Java code generation";
long generateStart = startStep(generateStep);
try {
generate = generator.generateOnly(this.generateSources, codegenDirectory);
completeStep(generateStep, generateStart);
} catch (Exception e) {
throw mojoException(e, generateStep, generateStart, start);
}
// Set generator and runtime to null so the memory can be cleaned up
generator = null;
runtime = null;
// Compile Java sources
PureJavaCompiler compiler;
String compilationStep = "Pure compiled mode Java code compilation";
long compilationStart = startStep(compilationStep);
try {
compiler = JavaStandaloneLibraryGenerator.compileOnly(generate.getJavaSources(), generate.getExternalizableSources(), false);
completeStep(compilationStep, compilationStart);
} catch (Exception e) {
throw mojoException(e, compilationStep, compilationStart, start);
}
// Write class files
String writeClassFilesStep = "writing Pure compiled mode Java classes";
long writeClassFilesStart = startStep(writeClassFilesStep);
try {
compiler.writeClassJavaSources(this.classesDirectory.toPath());
completeStep(writeClassFilesStep, writeClassFilesStart);
} catch (Exception e) {
throw mojoException(e, writeClassFilesStep, writeClassFilesStart, start);
}
// Set compiler to null so the memory can be cleaned up
compiler = null;
getLog().info(String.format(" Finished building Pure compiled mode jar (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0));
} catch (Exception e) {
throw new MojoExecutionException("error", e);
}
}
Aggregations