Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 544b6f8f0b433459094805fdc9e399891027caf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import static org.apache.tools.ant.taskdefs.condition.Os.*

apply from: etGenerator
apply from: etUnitConverter

def outputDir = 'log'
def srcGenDir = 'src-gen'

clean.delete srcGenDir, outputDir

def getMainClass(String genDir) {
	// search for SubSystemRunner java file
	def mainFile = fileTree(genDir, { include '**/*Runner.java' }).getSingleFile()
	// relativize path to generation directory
	def relPath = file(genDir).toPath().relativize(mainFile.toPath()).normalize().toString()
	// remove '.java' at the end
	def path = relPath.substring(0, relPath.length() - 5)
	// replace all separators with '.'
	def mainClass = path.replace(File.separatorChar, '.' as char)
	return mainClass
}

/**
 * Creates all needed tasks for one test execution
 * @param lang: The eTrice generator type ('c', 'cpp', 'java')
 * @param name: The test name starting with a lower case letter
 * @param models: Models that are passed to the eTrice generator
 * @param options: Options that are passed to the eTrice generator
 */
ext.createTest = { lang, name, models, options = [] ->

	def nameCap = name.capitalize()
	def langCap = lang.capitalize()
	def testName = "$name$langCap"
	def testNameCap = testName.capitalize()
	def suiteName = "${project.name}.$testNameCap"
	def genDir = "$srcGenDir/$testName"
	def workingDir = "$outputDir/$testName"
	def etuFile = "$workingDir/log/${nameCap}.etu"
	
	def generateTask = createGeneratorTask("generate$testNameCap", lang, models, genDir, options)
	generateTask.environment 'etModellib', "org.eclipse.etrice.modellib.$lang"
		
	def runTask
	
	if(lang == 'c' || lang == 'cpp') {
		model {
			components {
				"$testName"(NativeExecutableSpec) {
					sources."$lang" {
						builtBy generateTask
						source {
							srcDirs = [genDir]
							include "**/*.$lang"
						}
						exportedHeaders {
							srcDirs = [genDir]
						}
						lib project: ":runtime:org.eclipse.etrice.modellib.$lang", library: "etrice_modellib_$lang", linkage: 'static'
						if(lang == 'cpp') lib project: ':runtime:org.eclipse.etrice.runtime.cpp', library: 'etrice_runtime_cpp', linkage: 'static'
						lib project: ':runtime:org.eclipse.etrice.runtime.c', library: 'etrice_runtime_c', linkage: 'static'
					}
					binaries.all { 
						cCompiler.args '-g3'
						cppCompiler.args '-g3'
						if(isFamily(FAMILY_UNIX)) { linker.args '-lpthread', '-lrt', '-lm' }
					}
				}
			}
		}
		
		def exeFile = "$buildDir/exe/$testName/$testName"
		runTask = tasks.create(name: "run$testNameCap", type: Exec) {
			dependsOn "${testName}Executable"
			commandLine exeFile, '-run_as_test'
			inputs.file exeFile
			outputs.file etuFile
			it.workingDir = workingDir
		}
	}
	
	else if(lang == 'java') {
		def testSourceSet = sourceSets.create(name) {
			java.srcDirs = [genDir]
		}
		
		dependencies {
			add "${name}Compile", project(':runtime:org.eclipse.etrice.modellib.java')
			add "${name}Compile", project(':runtime:org.eclipse.etrice.runtime.java')
		}
		
		tasks.getByName("compile${nameCap}Java").dependsOn generateTask
		
		runTask = tasks.create(name: "run$testNameCap", type: JavaExec) {
			dependsOn "${name}Classes"
			classpath = testSourceSet.runtimeClasspath
			args '-run_as_test'
			outputs.file etuFile
			it.workingDir = workingDir
			
			doFirst { main = getMainClass(genDir) }
		}
	}
	
	runTask.doFirst { file("$workingDir/log").mkdirs() }
	
	def convertTask = createEtUnitConverterTask("convert$testNameCap", [etuFile], ['-suite', suiteName])
	convertTask.dependsOn runTask
	
	check.dependsOn tasks.create(name: testName, dependsOn: convertTask, group: 'verification')
}

Back to the top