Browse Source

enabled compilation of c source fmus under mac and linux

Kenneth Lausdahl 6 years ago
parent
commit
8a14d64fb4

+ 36 - 41
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppBasicTest.xtend

@@ -25,17 +25,17 @@ import org.junit.Test
 import org.junit.runner.RunWith
 import be.uantwerpen.ansymo.semanticadaptation.testframework.StaticGenerators
 import org.junit.Ignore
-
+import org.junit.Assert
 
 @RunWith(XtextRunner)
 @InjectWith(SemanticAdaptationInjectorProvider)
 class CgCppBasicTest extends AbstractSemanticAdaptationTest {
-	
+
 	val testOutputFolder = "c-gen-tests"
-	
-	def getOutputDirectory(String directoryName){
-		
-		return new File(("target/"+testOutputFolder+"/"+directoryName).replace("/",File.separatorChar));
+
+	def getOutputDirectory(String directoryName) {
+
+		return new File(("target/" + testOutputFolder + "/" + directoryName).replace("/", File.separatorChar));
 	}
 
 // @Inject CppGenerator underTest
@@ -48,15 +48,17 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 //		__parseNoErrorsWithValidation('test_input/single_folder_spec/window',
 //			'test_input/single_folder_spec/window/window_sa_canonical.BASE.sa');
 	}
-	
+
 	@Test def window_sa_canonical_new() {
-		__parseNoErrors('test_input/single_folder_spec/window/window_sa_canonical_new.BASE.sa', 'generated', "powerwindow");
+		__parseNoErrors('test_input/single_folder_spec/window/window_sa_canonical_new.BASE.sa', 'generated',
+			"powerwindow");
 	}
-	
+
 	@Test def window_sa_canonical_generated() {
-		__parseNoErrors('test_input/single_folder_spec/window/window_sa.BASE_canonical.sa', 'generated', "powerwindow_generated");
+		__parseNoErrors('test_input/single_folder_spec/window/window_sa.BASE_canonical.sa', 'generated',
+			"powerwindow_generated");
 	}
-	
+
 	@Ignore
 	@Test def lazy_sa_canonical() {
 		__parseNoErrors('test_input/single_folder_spec/lazy/lazy_canonical.sa', 'generated', "lazy");
@@ -78,7 +80,7 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 			val file = new File(correctFileDirectory, filename2);
 			val correctFileContent = Files.readAllLines(file.toPath);
 
-			var path =  getOutputDirectory(directory+filename);
+			var path = getOutputDirectory(directory + filename);
 			if (path.exists)
 				path.delete
 			else
@@ -111,7 +113,6 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 
 	}
 
-
 	def __parseNoErrors(String filename, String directory, String projectName) {
 		val saRootDir = getOutputDirectory(directory + File.separatorChar + projectName);
 		val srcGenPath = new File(saRootDir, "sources")
@@ -127,11 +128,11 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 		val IGeneratorContext ctxt = null;
 		val cppGen = new CppGenerator();
 		cppGen.doGenerate(model.eResource, fsa, ctxt);
-		
+
 		if (saRootDir.exists) {
-			BuildUtilities.deleteFolder(saRootDir);
+			BuildUtilities.deleteFolder(srcGenPath);
 		}
-				
+
 		saRootDir.mkdirs();
 		srcGenPath.mkdirs();
 		resourcesPath.mkdirs();
@@ -139,41 +140,35 @@ class CgCppBasicTest extends AbstractSemanticAdaptationTest {
 
 		for (files : fsa.allFiles.entrySet) {
 			val fName = files.key.substring(14);
-			
+
 			var File fp;
-			if(fName.equals("modelDescription.xml"))
-			{
+			if (fName.equals("modelDescription.xml")) {
 				fp = new File(saRootDir, fName);
-			}
-			else
-			{
+			} else {
 				fp = new File(srcGenPath, fName);
 			}
-			
-			BuildUtilities.writeToFile(fp, files.value.toString);	
+
+			BuildUtilities.writeToFile(fp, files.value.toString);
 		}
-		
-		val mainCpp = StaticGenerators.generateMainCppFile(resourcesPath.absolutePath.replace("\\","\\\\"));
-		BuildUtilities.writeToFile(new File(srcGenPath,"main.cpp"), mainCpp);
-		
-		
-		for(rf : cppGen.resourcePaths)
-		{
+
+		val mainCpp = StaticGenerators.generateMainCppFile(resourcesPath.absolutePath.replace("\\", "\\\\"));
+		BuildUtilities.writeToFile(new File(srcGenPath, "main.cpp"), mainCpp);
+
+		for (rf : cppGen.resourcePaths) {
 			val sinkFile = new File(resourcesPath, rf.name);
 			System.out.println("Copied file to: " + sinkFile);
-			BuildUtilities.copyFile(rf, sinkFile);	
+			BuildUtilities.copyFile(rf, sinkFile);
 		}
-		
-		
-		BuildUtilities.writeToFile(new File(saRootDir,"CMakeLists.txt"), StaticGenerators.generateCMakeLists(projectName, "framework"));
-		
+
+		BuildUtilities.writeToFile(new File(saRootDir, "CMakeLists.txt"),
+			StaticGenerators.generateCMakeLists(projectName, "framework"));
+
 		val cMakeToolChain = StaticGenerators.generateToolChainCmake();
 		BuildUtilities.writeToFile(new File(saRootDir, "msys-toolchain.cmake"), cMakeToolChain);
-		
-//		(new BuildUtilities()).copyNativeLibFiles(saFrameworkPath);
-//		System.out.println("Stored framework at: " + saFrameworkPath);
-		
-				
+
+		if (TestCompileUtils.mac || TestCompileUtils.unix) {
+			Assert.assertTrue("Could not compile FMU.", new TestCompileUtils().checkCompile(saRootDir));
+		}
 	}
 
 	def __parseNoErrorsPrint(String filename) {

+ 83 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/TestCompileUtils.java

@@ -0,0 +1,83 @@
+package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Scanner;
+import java.util.concurrent.TimeUnit;
+
+
+public class TestCompileUtils {
+
+	private static String OS = System.getProperty("os.name").toLowerCase();
+
+	public static boolean isMac() {
+		return OS.indexOf("mac") >= 0;
+	}
+	
+	 public static boolean isUnix() {
+	        return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );
+	    }
+
+	protected int runExternalProcess(ProcessBuilder pb) throws IOException, InterruptedException {
+
+		Process p = pb.start();
+
+		final InputStream inStream = p.getInputStream();
+		new Thread(new Runnable() {
+			public void run() {
+				InputStreamReader reader = new InputStreamReader(inStream);
+				Scanner scan = new Scanner(reader);
+				while (scan.hasNextLine()) {
+					System.out.println(scan.nextLine());
+				}
+				scan.close();
+			}
+		}).start();
+
+		final InputStream inErrStream = p.getErrorStream();
+		new Thread(new Runnable() {
+			public void run() {
+				InputStreamReader reader = new InputStreamReader(inErrStream);
+				Scanner scan = new Scanner(reader);
+				while (scan.hasNextLine()) {
+					System.out.println(scan.nextLine());
+				}
+				scan.close();
+			}
+		}).start();
+
+		if (!p.waitFor(5, TimeUnit.MINUTES)) {
+			// timeout - kill the process.
+			p.destroy(); // consider using destroyForcibly instead
+		}
+		return p.exitValue();
+	}
+
+	public boolean checkCompile(File destination) throws InterruptedException, IOException {
+		System.out.println(destination.getAbsolutePath());
+		ProcessBuilder pb = new ProcessBuilder();
+		pb.directory(destination);
+		String cmake = "cmake";
+
+		if (isMac()) {
+			cmake = "/usr/local/bin/cmake";
+		}
+
+		pb.command(cmake, ".");
+
+		int exitCode = runExternalProcess(pb);
+		if (exitCode != 0)
+			return false;
+
+		pb = new ProcessBuilder();
+		pb.directory(destination);
+		pb.command("make", "-j4");
+
+		exitCode = runExternalProcess(pb);
+		if (exitCode != 0)
+			return false;
+		return true;
+	}
+}