From afd8981c003bb9cc39d17fff98e310f52dc643f2 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Mon, 28 Nov 2011 14:51:12 +0200 Subject: [PATCH] do not write compiled stdlib in to output dir --- .../jet/compiler/CompileEnvironment.java | 35 +++++++++++-------- .../jet/compiler/CompileEnvironmentTest.java | 27 +++++++++++++- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java b/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java index 63ac39601d1..669bda88f70 100644 --- a/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java +++ b/compiler/backend/src/org/jetbrains/jet/compiler/CompileEnvironment.java @@ -230,6 +230,17 @@ public class CompileEnvironment { return new File(PathManager.getResourceRoot(CompileEnvironment.class, "/org/jetbrains/jet/compiler/CompileEnvironment.class")).getParentFile().getParentFile().getParent(); } + private static final List sanitized = Arrays.asList("kotlin/", "std/"); + public static boolean skipFile(String name) { + boolean skip = false; + for (String prefix : sanitized) { + if(name.startsWith(prefix)) { + return true; + } + } + return false; + } + public static void writeToJar(ClassFileFactory factory, final OutputStream fos, @Nullable String mainClass, boolean includeRuntime) { try { Manifest manifest = new Manifest(); @@ -240,18 +251,9 @@ public class CompileEnvironment { mainAttributes.putValue("Main-Class", mainClass); } JarOutputStream stream = new JarOutputStream(fos, manifest); - List sanitized = Arrays.asList("kotlin/", "std/"); try { for (String file : factory.files()) { - boolean skip = false; - for (String prefix : sanitized) { - if(file.startsWith(prefix)) { - skip = true; - break; - } - } - - if(!skip) { + if(!skipFile(file)) { stream.putNextEntry(new JarEntry(file)); stream.write(factory.asBytes(file)); } @@ -321,6 +323,7 @@ public class CompileEnvironment { public void compileBunchOfSources(String sourceFileOrDir, String jar, String outputDir, boolean includeRuntime) { CompileSession session = new CompileSession(myEnvironment); session.addSources(sourceFileOrDir); + session.addStdLibSources(); String mainClass = null; for (JetNamespace namespace : session.getSourceFileNamespaces()) { @@ -352,11 +355,13 @@ public class CompileEnvironment { private static void writeToOutputDirectory(ClassFileFactory factory, final String outputDir) { List files = factory.files(); for (String file : files) { - File target = new File(outputDir, file); - try { - FileUtil.writeToFile(target, factory.asBytes(file)); - } catch (IOException e) { - throw new CompileEnvironmentException(e); + if(!skipFile(file)) { + File target = new File(outputDir, file); + try { + FileUtil.writeToFile(target, factory.asBytes(file)); + } catch (IOException e) { + throw new CompileEnvironmentException(e); + } } } } diff --git a/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java b/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java index bb96b43f203..41ec6729100 100644 --- a/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java +++ b/compiler/tests/org/jetbrains/jet/compiler/CompileEnvironmentTest.java @@ -51,7 +51,7 @@ public class CompileEnvironmentTest extends TestCase { assertTrue(entries.contains("Smoke/namespace.class")); } - public void testSmokeWithCompiler() throws IOException { + public void testSmokeWithCompilerJar() throws IOException { File tempFile = File.createTempFile("compilerTest", "compilerTest"); try { KotlinCompiler.main(Arrays.asList("-module", JetParsingTest.getTestDataDir() + "/compiler/smoke/Smoke.kts", "-jar", tempFile.getAbsolutePath()).toArray(new String[0])); @@ -76,6 +76,31 @@ public class CompileEnvironmentTest extends TestCase { } } + private static void delete(File file) { + if(file.isDirectory()) { + for (File child : file.listFiles()) { + delete(child); + } + } + + file.delete(); + } + + public void testSmokeWithCompilerOutput() throws IOException { + File tempFile = File.createTempFile("compilerTest", "compilerTest"); + tempFile.delete(); + tempFile = new File(tempFile.getAbsolutePath()); + tempFile.mkdir(); + try { + KotlinCompiler.main(Arrays.asList("-src", JetParsingTest.getTestDataDir() + "/compiler/smoke/Smoke.kt", "-output", tempFile.getAbsolutePath()).toArray(new String[0])); + assertEquals(1, tempFile.listFiles().length); + assertEquals(1, tempFile.listFiles()[0].listFiles().length); + } + finally { + delete(tempFile); + } + } + private List listEntries(JarInputStream is) throws IOException { List entries = new ArrayList(); while (true) {