mirror of
https://gitlab.com/camelot/kickc.git
synced 2024-11-25 05:33:29 +00:00
Fixed problem where temporary folders not being deleted causes errors on every compile. Closes #493
This commit is contained in:
parent
9a98476d6e
commit
5a99c43efb
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,9 +3,11 @@
|
|||||||
*/*.brk
|
*/*.brk
|
||||||
*/*.prg
|
*/*.prg
|
||||||
*/*.sym
|
*/*.sym
|
||||||
|
*/.tmpdirs
|
||||||
*/bin/
|
*/bin/
|
||||||
*/workspace.xml
|
*/workspace.xml
|
||||||
./target/
|
./target/
|
||||||
target/
|
target/
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
.project
|
.project
|
||||||
|
.tmpdirs
|
||||||
|
@ -220,6 +220,9 @@ public class KickC implements Callable<Integer> {
|
|||||||
|
|
||||||
Program program = compiler.getProgram();
|
Program program = compiler.getProgram();
|
||||||
|
|
||||||
|
// Initialize tmp dir manager
|
||||||
|
TmpDirManager.init(program.getAsmFragmentBaseFolder());
|
||||||
|
|
||||||
// Initialize the master ASM fragment synthesizer
|
// Initialize the master ASM fragment synthesizer
|
||||||
program.initAsmFragmentMasterSynthesizer(!optimizeNoFragmentCache);
|
program.initAsmFragmentMasterSynthesizer(!optimizeNoFragmentCache);
|
||||||
|
|
||||||
@ -486,6 +489,9 @@ public class KickC implements Callable<Integer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(TmpDirManager.MANAGER!=null)
|
||||||
|
TmpDirManager.MANAGER.cleanup();
|
||||||
|
|
||||||
return CommandLine.ExitCode.OK;
|
return CommandLine.ExitCode.OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
137
src/main/java/dk/camelot64/kickc/TmpDirManager.java
Normal file
137
src/main/java/dk/camelot64/kickc/TmpDirManager.java
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
package dk.camelot64.kickc;
|
||||||
|
|
||||||
|
import dk.camelot64.kickc.model.CompileError;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages temporary folders with files.
|
||||||
|
* KickAssembler holds open handles to compiled ASM files. Therefore they cannot be deleted immediately after compilation.
|
||||||
|
* This manager
|
||||||
|
* - supports creation of new temporary folders
|
||||||
|
* - attempts deletion on exit
|
||||||
|
* - if deletion is not successful the folder paths are saved to a file. The folders in this file is attempted deleted on the next invocation.
|
||||||
|
*/
|
||||||
|
public class TmpDirManager {
|
||||||
|
|
||||||
|
/** Singleton manager. */
|
||||||
|
public static TmpDirManager MANAGER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the singleton manager
|
||||||
|
*
|
||||||
|
* @param baseFolder The base folder where the TXT file containing dirs to delete will be loaded/saved from.
|
||||||
|
*/
|
||||||
|
public static void init(Path baseFolder) {
|
||||||
|
MANAGER = new TmpDirManager(baseFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The base folder where the TXT file containing dirs to delete will be loaded/saved from. */
|
||||||
|
private Path baseFolder;
|
||||||
|
|
||||||
|
/** Tmp folders that have been created and not deleted. */
|
||||||
|
private List<Path> tmpDirs;
|
||||||
|
|
||||||
|
private TmpDirManager(Path baseFolder) {
|
||||||
|
this.baseFolder = baseFolder;
|
||||||
|
this.tmpDirs = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new temporary directory
|
||||||
|
*
|
||||||
|
* @return The new temporary folder
|
||||||
|
*/
|
||||||
|
public Path newTmpDir() {
|
||||||
|
try {
|
||||||
|
Path tmpDir = Files.createTempDirectory("kickc");
|
||||||
|
this.tmpDirs.add(tmpDir);
|
||||||
|
return tmpDir;
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new CompileError("Error creating temporary directory. ", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes all temporary folders - including any files in the folders.
|
||||||
|
* If deletion is not successful the absolute paths are saved to a txt-file to tried again later.
|
||||||
|
* This also loads the txt-file with previous failed deletions and retries them
|
||||||
|
*/
|
||||||
|
public void cleanup() {
|
||||||
|
try {
|
||||||
|
// Attempt deletion of all temporary folders - including all files in the folders
|
||||||
|
List<Path> failedDirs = new ArrayList<>();
|
||||||
|
for(Path tmpDir : tmpDirs) {
|
||||||
|
boolean success = deleteTmpDir(tmpDir);
|
||||||
|
if(!success) {
|
||||||
|
failedDirs.add(tmpDir);
|
||||||
|
//System.out.println("Cannot delete temporary folder, postponing " + tmpDir);
|
||||||
|
} else {
|
||||||
|
//System.out.println("Successfully deleted temporary folder " + tmpDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Read postponed file and delete any paths
|
||||||
|
File todoFile = baseFolder.resolve(".tmpdirs").toFile();
|
||||||
|
if(todoFile.exists()) {
|
||||||
|
FileReader todoFileReader = new FileReader(todoFile);
|
||||||
|
BufferedReader todoBufferedReader = new BufferedReader(todoFileReader);
|
||||||
|
String todoPathAbs = todoBufferedReader.readLine();
|
||||||
|
while(todoPathAbs != null) {
|
||||||
|
Path todoPath = new File(todoPathAbs).toPath();
|
||||||
|
boolean success = deleteTmpDir(todoPath);
|
||||||
|
if(!success) {
|
||||||
|
failedDirs.add(todoPath);
|
||||||
|
//System.out.println("Cannot delete postponed temporary folder - postponing again " + todoPathAbs);
|
||||||
|
} else {
|
||||||
|
//System.out.println("Successfully deleted postponed temporary folder " + todoPathAbs);
|
||||||
|
}
|
||||||
|
todoPathAbs = todoBufferedReader.readLine();
|
||||||
|
}
|
||||||
|
todoBufferedReader.close();
|
||||||
|
todoFileReader.close();
|
||||||
|
}
|
||||||
|
// Delete the old postponed file
|
||||||
|
if(todoFile.exists()) {
|
||||||
|
if(!todoFile.delete())
|
||||||
|
System.err.println("Warning! Cannot delete .tmpdir file " + todoFile.getAbsolutePath());
|
||||||
|
//System.out.println("Deleted old .tmpdir file " + todoFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
// Save any failed paths to new postponed file
|
||||||
|
if(failedDirs.size() > 0) {
|
||||||
|
PrintStream todoPrintStream = new PrintStream(todoFile);
|
||||||
|
for(Path failedDir : failedDirs) {
|
||||||
|
todoPrintStream.println(failedDir.toAbsolutePath());
|
||||||
|
}
|
||||||
|
todoPrintStream.close();
|
||||||
|
//System.out.println("Saved .tmpdir file with " + failedDirs.size() + " postponed temporary folders " + todoFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new CompileError("Error cleaning up temporary files", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean deleteTmpDir(Path tmpDir) {
|
||||||
|
// Delete the temporary directory with folders
|
||||||
|
boolean success = true;
|
||||||
|
String[] entries = tmpDir.toFile().list();
|
||||||
|
if(entries != null)
|
||||||
|
for(String s : entries) {
|
||||||
|
File currentFile = new File(tmpDir.toFile(), s);
|
||||||
|
if(!currentFile.delete()) {
|
||||||
|
//System.err.println("Warning! Cannot delete temporary file " + currentFile.getAbsolutePath());
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!tmpDir.toFile().delete()) {
|
||||||
|
//System.err.println("Warning! Cannot delete temporary folder " + tmpDir.toAbsolutePath());
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,6 +2,7 @@ package dk.camelot64.kickc.passes;
|
|||||||
|
|
||||||
import dk.camelot64.cpufamily6502.CpuAddressingMode;
|
import dk.camelot64.cpufamily6502.CpuAddressingMode;
|
||||||
import dk.camelot64.cpufamily6502.CpuOpcode;
|
import dk.camelot64.cpufamily6502.CpuOpcode;
|
||||||
|
import dk.camelot64.kickc.TmpDirManager;
|
||||||
import dk.camelot64.kickc.asm.*;
|
import dk.camelot64.kickc.asm.*;
|
||||||
import dk.camelot64.kickc.model.CompileError;
|
import dk.camelot64.kickc.model.CompileError;
|
||||||
import dk.camelot64.kickc.model.Program;
|
import dk.camelot64.kickc.model.Program;
|
||||||
@ -21,8 +22,6 @@ import java.util.regex.Pattern;
|
|||||||
*/
|
*/
|
||||||
public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
||||||
|
|
||||||
private Path tmpDir;
|
|
||||||
|
|
||||||
public Pass5FixLongBranches(Program program) {
|
public Pass5FixLongBranches(Program program) {
|
||||||
super(program);
|
super(program);
|
||||||
}
|
}
|
||||||
@ -54,35 +53,25 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
|||||||
private boolean step() {
|
private boolean step() {
|
||||||
// Reindex ASM lines
|
// Reindex ASM lines
|
||||||
new Pass5ReindexAsmLines(getProgram()).optimize();
|
new Pass5ReindexAsmLines(getProgram()).optimize();
|
||||||
|
Path tmpDir = TmpDirManager.MANAGER.newTmpDir();
|
||||||
// Create a temporary directory for the ASM file
|
|
||||||
try {
|
|
||||||
tmpDir = Files.createTempDirectory("kickc");
|
|
||||||
} catch(IOException e) {
|
|
||||||
throw new CompileError("Error creating temp file.", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the ASM file
|
// Generate the ASM file
|
||||||
String outputFileName = getProgram().getPrimaryFileName();
|
String outputFileName = getProgram().getPrimaryFileName();
|
||||||
try {
|
try {
|
||||||
//getLog().append("ASM");
|
//getLog().append("ASM");
|
||||||
//getLog().append(getProgram().getAsm().toString(false, true));
|
//getLog().append(getProgram().getAsm().toString(false, true));
|
||||||
|
writeOutputFile(tmpDir, outputFileName, ".asm", getProgram().getAsm().toString(new AsmProgram.AsmPrintState(false), null));
|
||||||
writeOutputFile(outputFileName, ".asm", getProgram().getAsm().toString(new AsmProgram.AsmPrintState(false), null));
|
|
||||||
|
|
||||||
// Copy Resource Files
|
// Copy Resource Files
|
||||||
for(Path asmResourceFile : getProgram().getAsmResourceFiles()) {
|
for(Path asmResourceFile : getProgram().getAsmResourceFiles()) {
|
||||||
File binFile = getTmpFile(asmResourceFile.getFileName().toString());
|
File binFile = getTmpFile(tmpDir, asmResourceFile.getFileName().toString());
|
||||||
Files.copy(asmResourceFile, binFile.toPath());
|
Files.copy(asmResourceFile, binFile.toPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
throw new CompileError("Error writing ASM temp file.", e);
|
throw new CompileError("Error writing ASM temp file.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile using KickAssembler - catch the output in a String
|
// Compile using KickAssembler - catch the output in a String
|
||||||
File asmFile = getTmpFile(outputFileName, ".asm");
|
File asmFile = getTmpFile(tmpDir, outputFileName, ".asm");
|
||||||
File binaryFile = getTmpFile(outputFileName, "."+getProgram().getTargetPlatform().getOutFileExtension());
|
File binaryFile = getTmpFile(tmpDir, outputFileName, "."+getProgram().getTargetPlatform().getOutFileExtension());
|
||||||
ByteArrayOutputStream kickAssOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream kickAssOut = new ByteArrayOutputStream();
|
||||||
System.setOut(new PrintStream(kickAssOut));
|
System.setOut(new PrintStream(kickAssOut));
|
||||||
int asmRes = -1;
|
int asmRes = -1;
|
||||||
@ -117,7 +106,6 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
|||||||
// Found line number
|
// Found line number
|
||||||
//getLog().append("Found long branch line number "+contextLineIdx);
|
//getLog().append("Found long branch line number "+contextLineIdx);
|
||||||
if(fixLongBranch(contextLineIdx - 1)) {
|
if(fixLongBranch(contextLineIdx - 1)) {
|
||||||
removeTmpDir();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,25 +113,9 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeTmpDir();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeTmpDir() {
|
|
||||||
// Delete the temporary directory with folders
|
|
||||||
String[]entries = tmpDir.toFile().list();
|
|
||||||
for(String s: entries){
|
|
||||||
File currentFile = new File(tmpDir.toFile(),s);
|
|
||||||
if(!currentFile.delete()) {
|
|
||||||
System.err.println("Warning! Cannot delete temporary file "+currentFile.getAbsolutePath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!tmpDir.toFile().delete()) {
|
|
||||||
System.err.println("Warning! Cannot delete temporary folder "+tmpDir.toAbsolutePath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fix a long branch detected at a specific ASM index
|
* Fix a long branch detected at a specific ASM index
|
||||||
*
|
*
|
||||||
@ -202,9 +174,9 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public File writeOutputFile(String fileName, String extension, String outputString) throws IOException {
|
private File writeOutputFile(Path tmpDir, String fileName, String extension, String outputString) throws IOException {
|
||||||
// Write output file
|
// Write output file
|
||||||
File file = getTmpFile(fileName, extension);
|
File file = getTmpFile(tmpDir, fileName, extension);
|
||||||
FileOutputStream outputStream = new FileOutputStream(file);
|
FileOutputStream outputStream = new FileOutputStream(file);
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
|
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
|
||||||
writer.write(outputString);
|
writer.write(outputString);
|
||||||
@ -214,12 +186,12 @@ public class Pass5FixLongBranches extends Pass5AsmOptimization {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getTmpFile(String fileName, String extension) {
|
private static File getTmpFile(Path tmpDir, String fileName, String extension) {
|
||||||
Path kcPath = FileSystems.getDefault().getPath(fileName);
|
Path kcPath = FileSystems.getDefault().getPath(fileName);
|
||||||
return new File(tmpDir.toFile(), kcPath.getFileName().toString() + extension);
|
return new File(tmpDir.toFile(), kcPath.getFileName().toString() + extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getTmpFile(String fileName) {
|
private static File getTmpFile(Path tmpDir, String fileName) {
|
||||||
return new File(tmpDir.toFile(), fileName );
|
return new File(tmpDir.toFile(), fileName );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dk.camelot64.kickc.test;
|
package dk.camelot64.kickc.test;
|
||||||
|
|
||||||
|
import dk.camelot64.kickc.TmpDirManager;
|
||||||
import kickass.KickAssembler65CE02;
|
import kickass.KickAssembler65CE02;
|
||||||
import kickass.nonasm.c64.CharToPetsciiConverter;
|
import kickass.nonasm.c64.CharToPetsciiConverter;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -19,15 +20,21 @@ public class TestKickAssRun {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testKickAssRun() throws IOException, URISyntaxException {
|
public void testKickAssRun() throws IOException, URISyntaxException {
|
||||||
|
TmpDirManager.init(new File("").toPath());
|
||||||
|
|
||||||
ReferenceHelper asmHelper = new ReferenceHelperFolder("src/test/java/dk/camelot64/kickc/test/");
|
ReferenceHelper asmHelper = new ReferenceHelperFolder("src/test/java/dk/camelot64/kickc/test/");
|
||||||
URI asmUri = asmHelper.loadReferenceFile("kickasstest", ".asm");
|
URI asmUri = asmHelper.loadReferenceFile("kickasstest", ".asm");
|
||||||
Path asmPath = Paths.get(asmUri);
|
Path asmPath = Paths.get(asmUri);
|
||||||
File asmPrgFile = getTmpFile("kickasstest", ".prg");
|
|
||||||
|
Path tmpDir = TmpDirManager.MANAGER.newTmpDir();
|
||||||
|
File asmFile = getTmpFile(tmpDir, "kickasstest", ".asm");
|
||||||
|
File asmPrgFile = getTmpFile(tmpDir, "kickasstest", ".prg");
|
||||||
|
Files.copy(asmPath, asmFile.toPath());
|
||||||
ByteArrayOutputStream kickAssOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream kickAssOut = new ByteArrayOutputStream();
|
||||||
System.setOut(new PrintStream(kickAssOut));
|
System.setOut(new PrintStream(kickAssOut));
|
||||||
try {
|
try {
|
||||||
CharToPetsciiConverter.setCurrentEncoding("screencode_mixed");
|
CharToPetsciiConverter.setCurrentEncoding("screencode_mixed");
|
||||||
KickAssembler65CE02.main2(new String[]{asmPath.toAbsolutePath().toString(), "-o", asmPrgFile.getAbsolutePath()});
|
KickAssembler65CE02.main2(new String[]{asmFile.getAbsolutePath(), "-o", asmPrgFile.getAbsolutePath()});
|
||||||
} catch(AssertionError e) {
|
} catch(AssertionError e) {
|
||||||
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
|
||||||
String output = kickAssOut.toString();
|
String output = kickAssOut.toString();
|
||||||
@ -38,11 +45,11 @@ public class TestKickAssRun {
|
|||||||
}
|
}
|
||||||
String output = kickAssOut.toString();
|
String output = kickAssOut.toString();
|
||||||
System.out.println(output);
|
System.out.println(output);
|
||||||
|
|
||||||
|
TmpDirManager.MANAGER.cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static File getTmpFile(Path tmpDir, String fileName, String extension) throws IOException {
|
||||||
public File getTmpFile(String fileName, String extension) throws IOException {
|
|
||||||
Path tmpDir = Files.createTempDirectory("kickc");
|
|
||||||
Path kcPath = FileSystems.getDefault().getPath(fileName);
|
Path kcPath = FileSystems.getDefault().getPath(fileName);
|
||||||
return new File(tmpDir.toFile(), kcPath.getFileName().toString() + extension);
|
return new File(tmpDir.toFile(), kcPath.getFileName().toString() + extension);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package dk.camelot64.kickc.test;
|
|||||||
import dk.camelot64.kickc.CompileLog;
|
import dk.camelot64.kickc.CompileLog;
|
||||||
import dk.camelot64.kickc.Compiler;
|
import dk.camelot64.kickc.Compiler;
|
||||||
import dk.camelot64.kickc.SourceLoader;
|
import dk.camelot64.kickc.SourceLoader;
|
||||||
|
import dk.camelot64.kickc.TmpDirManager;
|
||||||
import dk.camelot64.kickc.asm.AsmProgram;
|
import dk.camelot64.kickc.asm.AsmProgram;
|
||||||
import dk.camelot64.kickc.model.CompileError;
|
import dk.camelot64.kickc.model.CompileError;
|
||||||
import dk.camelot64.kickc.model.Program;
|
import dk.camelot64.kickc.model.Program;
|
||||||
@ -68,6 +69,7 @@ public class TestPrograms {
|
|||||||
public void testAtariXlMd5b() throws IOException, URISyntaxException {
|
public void testAtariXlMd5b() throws IOException, URISyntaxException {
|
||||||
compileAndCompare("atarixl-md5b.c");
|
compileAndCompare("atarixl-md5b.c");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAtariXlMd5() throws IOException, URISyntaxException {
|
public void testAtariXlMd5() throws IOException, URISyntaxException {
|
||||||
compileAndCompare("atarixl-md5.c");
|
compileAndCompare("atarixl-md5.c");
|
||||||
@ -4822,10 +4824,13 @@ public class TestPrograms {
|
|||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
TmpDirManager.init(new File("").toPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterAll
|
@AfterAll
|
||||||
public static void tearDown() {
|
public static void tearDown() {
|
||||||
|
if(TmpDirManager.MANAGER != null)
|
||||||
|
TmpDirManager.MANAGER.cleanup();
|
||||||
//AsmFragmentTemplateUsages.logUsages(log, false, false, false, false, false, false);
|
//AsmFragmentTemplateUsages.logUsages(log, false, false, false, false, false, false);
|
||||||
//printGCStats();
|
//printGCStats();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user