Transfer C argv to Kotlin's args.
This commit is contained in:
committed by
alexander-gorshenev
parent
e38ac775ad
commit
3f480eb846
@@ -12,10 +12,12 @@ abstract class KonanTest extends DefaultTask {
|
|||||||
def runtimeProject = project.project(":runtime")
|
def runtimeProject = project.project(":runtime")
|
||||||
def llvmLlc = llvmTool("llc")
|
def llvmLlc = llvmTool("llc")
|
||||||
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
|
||||||
|
def launcherBc = new File("${runtimeProject.buildDir.canonicalPath}/launcher.bc")
|
||||||
def stdlibKtBc = new File("${runtimeProject.buildDir.canonicalPath}/stdlib.kt.bc")
|
def stdlibKtBc = new File("${runtimeProject.buildDir.canonicalPath}/stdlib.kt.bc")
|
||||||
def mainC = 'main.c'
|
def mainC = 'main.c'
|
||||||
String goldValue = null
|
String goldValue = null
|
||||||
String testData = null
|
String testData = null
|
||||||
|
List<String> arguments = null
|
||||||
|
|
||||||
public KonanTest(){
|
public KonanTest(){
|
||||||
dependsOn([project.project(":runtime").tasks['build'],
|
dependsOn([project.project(":runtime").tasks['build'],
|
||||||
@@ -59,6 +61,9 @@ abstract class KonanTest extends DefaultTask {
|
|||||||
def out = null
|
def out = null
|
||||||
project.exec {
|
project.exec {
|
||||||
commandLine "${exe.absolutePath}"
|
commandLine "${exe.absolutePath}"
|
||||||
|
if (arguments != null) {
|
||||||
|
args arguments
|
||||||
|
}
|
||||||
if (testData != null) {
|
if (testData != null) {
|
||||||
standardInput = new ByteArrayInputStream(testData.bytes)
|
standardInput = new ByteArrayInputStream(testData.bytes)
|
||||||
}
|
}
|
||||||
@@ -131,8 +136,8 @@ class UnitKonanTest extends KonanTest {
|
|||||||
class RunKonanTest extends KonanTest {
|
class RunKonanTest extends KonanTest {
|
||||||
void compileTest(File sourceS, File runtimeS, File libraryPath, File exe) {
|
void compileTest(File sourceS, File runtimeS, File libraryPath, File exe) {
|
||||||
project.execClang {
|
project.execClang {
|
||||||
commandLine "clang++", "-DRUN_TEST", runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath,
|
commandLine "clang++", launcherBc.absolutePath, runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath,
|
||||||
"-o", exe.absolutePath, linkDl(), linkM(), rdynamic(), '-xc', mainC
|
"-o", exe.absolutePath, linkDl(), linkM(), rdynamic(), '-xc'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -599,3 +604,10 @@ task range0(type: RunKonanTest) {
|
|||||||
goldValue = "123\nabcd\n"
|
goldValue = "123\nabcd\n"
|
||||||
source = "runtime/collections/range0.kt"
|
source = "runtime/collections/range0.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task args0(type: RunKonanTest) {
|
||||||
|
arguments = ["AAA", "BB", "C"]
|
||||||
|
goldValue = "AAA\nBB\nC\n"
|
||||||
|
source = "runtime/basic/args0.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,6 @@
|
|||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
/**
|
|
||||||
* > llc-mp-3.8 b.out -o b.S
|
|
||||||
* > /opt/local/libexec/llvm-3.8/bin/clang main.c b.S -o sum-test
|
|
||||||
*/
|
|
||||||
|
|
||||||
extern int run_test();
|
extern int run_test();
|
||||||
|
|
||||||
@@ -24,13 +20,6 @@ void * resolve_symbol(char *name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
kotlinNativeMain() {
|
main() {
|
||||||
#ifdef RUN_TEST
|
|
||||||
void (*main)(void *) = resolve_symbol("kfun:main(Array<String>)");
|
|
||||||
main((void *)0);
|
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
exit(run_test());
|
exit(run_test());
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
for (s in args) {
|
||||||
|
println(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,10 +8,18 @@ task build(type: CompileCppToBitcode) {
|
|||||||
srcRoot file('src/main')
|
srcRoot file('src/main')
|
||||||
|
|
||||||
dependsOn ':common:compileHash'
|
dependsOn ':common:compileHash'
|
||||||
|
dependsOn 'launcher'
|
||||||
compilerArgs '-I' + project.file('../common/src/hash/headers') // TODO: copy-pasted from 'common'
|
compilerArgs '-I' + project.file('../common/src/hash/headers') // TODO: copy-pasted from 'common'
|
||||||
linkerArgs project.file('../common/build/hash.bc').path
|
linkerArgs project.file('../common/build/hash.bc').path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task launcher(type: CompileCppToBitcode) {
|
||||||
|
name 'launcher'
|
||||||
|
srcRoot file('src/launcher')
|
||||||
|
compilerArgs '-I' + project.file('../common/src/hash/headers') // TODO: copy-pasted from 'common'
|
||||||
|
compilerArgs '-I' + project.file('src/main/cpp')
|
||||||
|
}
|
||||||
|
|
||||||
task clean << {
|
task clean << {
|
||||||
delete buildDir
|
delete buildDir
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include "Memory.h"
|
||||||
|
#include "Natives.h"
|
||||||
|
#include "Types.h"
|
||||||
|
|
||||||
|
extern "C" ArrayHeader* konanArrayInit(ArrayHeader*, int)
|
||||||
|
asm("_kfun:kotlin.Array.<init>(Int)");
|
||||||
|
|
||||||
|
extern "C" const TypeInfo konanArrayType
|
||||||
|
asm("_ktype:kotlin.Array");
|
||||||
|
|
||||||
|
ArrayHeader* setupArgs(int argc, char** argv) {
|
||||||
|
|
||||||
|
// The count is one less, because we skip argv[0] which is the binary name.
|
||||||
|
ArrayHeader* array = AllocArrayInstance(&konanArrayType, SCOPE_GLOBAL, argc-1);
|
||||||
|
ArrayHeader* args = konanArrayInit(array, argc-1);
|
||||||
|
|
||||||
|
for (int i = 0; i < argc-1; i++) {
|
||||||
|
Kotlin_Array_set(args, i, (ArrayHeader*) makeString( argv[i+1] ));
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void konanMain(void*) asm("_kfun:main(Array<String>)");
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
|
InitMemory();
|
||||||
|
|
||||||
|
ArrayHeader* args = setupArgs(argc, argv);
|
||||||
|
konanMain(args);
|
||||||
|
|
||||||
|
// Yes, we have to follow Java convention and return zero.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -180,4 +180,15 @@ KString Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KString makeString(const char* cstring) {
|
||||||
|
uint32_t length = strlen(cstring);
|
||||||
|
ArrayHeader* result = ArrayContainer(
|
||||||
|
theStringTypeInfo, length).GetPlace();
|
||||||
|
memcpy(
|
||||||
|
ByteArrayAddressOfElementAt(result, 0),
|
||||||
|
cstring,
|
||||||
|
length);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ extern "C" {
|
|||||||
|
|
||||||
KString TheEmptyString();
|
KString TheEmptyString();
|
||||||
|
|
||||||
|
KString makeString(const char* cstring);
|
||||||
|
|
||||||
// Any.kt
|
// Any.kt
|
||||||
KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other);
|
KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other);
|
||||||
KInt Kotlin_Any_hashCode(KConstRef thiz);
|
KInt Kotlin_Any_hashCode(KConstRef thiz);
|
||||||
|
|||||||
@@ -7,21 +7,6 @@
|
|||||||
#include "Natives.h"
|
#include "Natives.h"
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
KString makeString(const char* cstring) {
|
|
||||||
uint32_t length = strlen(cstring);
|
|
||||||
ArrayHeader* result = ArrayContainer(
|
|
||||||
theStringTypeInfo, length).GetPlace();
|
|
||||||
memcpy(
|
|
||||||
ByteArrayAddressOfElementAt(result, 0),
|
|
||||||
cstring,
|
|
||||||
length);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
KString Kotlin_Byte_toString(KByte value) {
|
KString Kotlin_Byte_toString(KByte value) {
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
#include "Memory.h"
|
|
||||||
|
|
||||||
extern "C" void kotlinNativeMain();
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
InitMemory();
|
|
||||||
kotlinNativeMain();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user