231 lines
6.0 KiB
Groovy
231 lines
6.0 KiB
Groovy
configurations {
|
|
cli_bc
|
|
}
|
|
|
|
dependencies {
|
|
cli_bc project(path: ':backend.native', configuration: 'cli_bc')
|
|
}
|
|
|
|
abstract class KonanTest extends DefaultTask {
|
|
protected String source
|
|
def backendNative = project.project(":backend.native")
|
|
def runtimeProject = project.project(":runtime")
|
|
def llvmLlc = llvmTool("llc")
|
|
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
|
def mainC = 'main.c'
|
|
String goldValue = null
|
|
String testData = null
|
|
|
|
public KonanTest(){
|
|
dependsOn([project.project(":runtime").tasks['build'],
|
|
project.parent.tasks['build']])
|
|
}
|
|
|
|
|
|
|
|
abstract void compileTest(File sourceS, File runtimeS, File out)
|
|
|
|
private File kt2bc(String ktSource) {
|
|
def sourceKt = project.file(ktSource)
|
|
def sourceBc = new File("${sourceKt.absolutePath}.bc")
|
|
println "${runtimeBc}"
|
|
|
|
project.javaexec {
|
|
main = 'org.jetbrains.kotlin.cli.bc.K2NativeKt'
|
|
classpath = project.configurations.cli_bc
|
|
jvmArgs "-ea"
|
|
args "-output", "${sourceBc.absolutePath}",
|
|
"-runtime", "${runtimeBc.absolutePath}",
|
|
"-headers", project.project(':runtime').file('src/main/kotlin'),
|
|
"${sourceKt.absolutePath}"
|
|
|
|
String libraryPath = "${project.llvmDir}/lib:${backendNative.buildDir.canonicalPath}/nativelibs"
|
|
environment 'LD_LIBRARY_PATH' : libraryPath
|
|
environment 'DYLD_LIBRARY_PATH' : libraryPath
|
|
}
|
|
return sourceBc
|
|
}
|
|
|
|
@TaskAction
|
|
void executeTest() {
|
|
def sourceS = bc2s(kt2bc(source))
|
|
def exe = new File(sourceS.absolutePath.replace(".kt.S", ""))
|
|
compileTest(sourceS, bc2s(runtimeBc), exe)
|
|
println "execution :${exe.absolutePath}"
|
|
|
|
def out = null
|
|
project.exec {
|
|
commandLine "${exe.absolutePath}"
|
|
if (testData != null) {
|
|
standardInput = new ByteArrayInputStream(testData.bytes)
|
|
}
|
|
if (goldValue != null) {
|
|
out = new ByteArrayOutputStream()
|
|
standardOutput = out
|
|
}
|
|
}
|
|
if (goldValue != null && goldValue != out.toString())
|
|
throw new RuntimeException("test failed")
|
|
}
|
|
|
|
private File bc2s(File bcFile) {
|
|
def outputFile = new File("${bcFile.absolutePath.replace(".bc",".S")}")
|
|
println "${bcFile.absolutePath} -> ${outputFile.absolutePath}"
|
|
println "tool: ${llvmLlc}"
|
|
project.exec {
|
|
commandLine "${llvmLlc}", "-o", "${outputFile.absolutePath}" , "${bcFile}"
|
|
}
|
|
return outputFile
|
|
}
|
|
|
|
private String llvmTool(String tool) {
|
|
return "${project.llvmDir}/bin/${tool}"
|
|
}
|
|
|
|
private linux() {
|
|
return System.properties['os.name'].startsWith('Linux')
|
|
}
|
|
|
|
protected String linkDl() {
|
|
if (linux()) {
|
|
return "-ldl"
|
|
}
|
|
else {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
protected String rdynamic() {
|
|
if (linux()) {
|
|
return "-rdynamic"
|
|
}
|
|
else {
|
|
return ""
|
|
}
|
|
}
|
|
}
|
|
|
|
class UnitKonanTest extends KonanTest {
|
|
void compileTest(File sourceS, File runtimeS, File exe) {
|
|
def testC = sourceS.absolutePath.replace(".kt.S", "-test.c")
|
|
project.execClang {
|
|
commandLine "clang", "${testC}", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}",
|
|
"-o", "${exe.absolutePath}", linkDl(), rdynamic()
|
|
}
|
|
}
|
|
}
|
|
|
|
class RunKonanTest extends KonanTest {
|
|
void compileTest(File sourceS, File runtimeS, File exe) {
|
|
project.execClang {
|
|
commandLine "clang", "-DRUN_TEST", "${runtimeS.absolutePath}", "${sourceS.absolutePath}", "${mainC}",
|
|
"-o", "${exe.absolutePath}", linkDl(), rdynamic()
|
|
}
|
|
}
|
|
}
|
|
|
|
task run() {
|
|
dependsOn(tasks.withType(KonanTest))
|
|
}
|
|
|
|
task sum (type:UnitKonanTest) {
|
|
source = "codegen/function/sum.kt"
|
|
}
|
|
|
|
/*task objectInitialization(type: UnitKonanTest) {
|
|
source = "codegen/object/initialization.kt"
|
|
}*/
|
|
|
|
/* task objectBasic(type: KonanTest) {
|
|
source = "$codegen/klass/basic.kt"
|
|
} */
|
|
|
|
task aritmetic(type: UnitKonanTest) {
|
|
source = "codegen/function/arithmetic.kt"
|
|
}
|
|
|
|
task sum1(type: UnitKonanTest) {
|
|
source = "codegen/function/sum_foo_bar.kt"
|
|
|
|
}
|
|
|
|
task sum2(type: UnitKonanTest) {
|
|
source = "codegen/function/sum_imm.kt"
|
|
}
|
|
|
|
task sum_3const(type: UnitKonanTest) {
|
|
source = "codegen/function/sum_3const.kt"
|
|
}
|
|
|
|
task local_variable(type: UnitKonanTest) {
|
|
source = "codegen/basics/local_variable.kt"
|
|
}
|
|
|
|
task hello0(type: RunKonanTest) {
|
|
goldValue = "Hello, world!\n"
|
|
source = "runtime/basic/hello0.kt"
|
|
}
|
|
|
|
task hello1(type: RunKonanTest) {
|
|
goldValue = "Hello World"
|
|
testData = "Hello World"
|
|
source = "runtime/basic/hello1.kt"
|
|
}
|
|
|
|
/* TODO: enable, once calling plus operator is implemented.
|
|
task hello2(type: RunKonanTest) {
|
|
goldValue = "Hello World"
|
|
testData = "Hello World"
|
|
source = "runtime/basic/hello2.kt"
|
|
} */
|
|
|
|
/* TODO: enable, once can call toString() on Int.
|
|
task hello3(type: RunKonanTest) {
|
|
goldValue = "239"
|
|
source = "runtime/basic/hello3.kt"
|
|
} */
|
|
|
|
task if_else(type: UnitKonanTest) {
|
|
source = "codegen/branching/if_else.kt"
|
|
}
|
|
|
|
task when2(type: UnitKonanTest) {
|
|
source = "codegen/branching/when2.kt"
|
|
}
|
|
|
|
task when5(type: UnitKonanTest) {
|
|
source = "codegen/branching/when5.kt"
|
|
}
|
|
|
|
task when_through(type: UnitKonanTest) {
|
|
source = "codegen/branching/when_through.kt"
|
|
}
|
|
|
|
task advanced_when2(type: UnitKonanTest) {
|
|
source = "codegen/branching/advanced_when2.kt"
|
|
}
|
|
|
|
task advanced_when5(type: UnitKonanTest) {
|
|
source = "codegen/branching/advanced_when5.kt"
|
|
}
|
|
|
|
task bool_yes(type: UnitKonanTest) {
|
|
source = "codegen/function/boolean.kt"
|
|
}
|
|
|
|
task plus_eq(type: UnitKonanTest) {
|
|
source = "codegen/function/plus_eq.kt"
|
|
}
|
|
|
|
task minus_eq(type: UnitKonanTest) {
|
|
source = "codegen/function/minus_eq.kt"
|
|
}
|
|
|
|
task cycle(type: UnitKonanTest) {
|
|
source = "codegen/cycles/cycle.kt"
|
|
}
|
|
|
|
task cycle_do(type: UnitKonanTest) {
|
|
source = "codegen/cycles/cycle_do.kt"
|
|
}
|