Skip to content

Commit

Permalink
A configurable zephyr board support.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-gorshenev committed Feb 12, 2018
1 parent d968faf commit 9789420
Show file tree
Hide file tree
Showing 63 changed files with 665 additions and 473 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class CInteropArguments : CommonInteropArguments() {
@Argument(value = "-def", valueDescription = "<file>", description = "the library definition file")
var def: String? = null

@Argument(value = "-properties", valueDescription = "<file>", description = "an alternative location of konan.properties file")
var properties: String? = null

// TODO: the short -h for -header conflicts with -h for -help.
// The -header currently wins, but need to make it a little more sound.
@Argument(value = "-header", shortName = "-h", valueDescription = "<file>", description = "header file to produce kotlin bindings for")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,20 @@

package org.jetbrains.kotlin.native.interop.tool

import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.properties.loadProperties
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.PlatformManager
import org.jetbrains.kotlin.konan.target.TargetManager
import org.jetbrains.kotlin.konan.util.DependencyProcessor
import org.jetbrains.kotlin.konan.target.*
import org.jetbrains.kotlin.konan.util.visibleName
import org.jetbrains.kotlin.native.interop.gen.jvm.KotlinPlatform

class ToolConfig(userProvidedTargetName: String?, userProvidedKonanProperties: String?,
runnerProvidedKonanHome: String, val flavor: KotlinPlatform) {
class ToolConfig(userProvidedTargetName: String?, flavor: KotlinPlatform) {

private val targetManager = TargetManager(userProvidedTargetName)
private val host = TargetManager.host
private val konanHome = System.getProperty("konan.home")
private val distribution = customerDistribution(konanHome)
private val platformManager = PlatformManager(distribution)
private val targetManager = platformManager.targetManager(userProvidedTargetName)
private val host = HostManager.host
private val target = targetManager.target

private val konanHome = File(runnerProvidedKonanHome).absolutePath
private val konanPropertiesFile = userProvidedKonanProperties?.File() ?: File(konanHome, "konan/konan.properties")
private val properties = konanPropertiesFile.loadProperties()

private val dependencies = DependencyProcessor.defaultDependenciesRoot

private val platform = PlatformManager(properties, dependencies.path).platform(target)
private val platform = platformManager.platform(target)

val substitutions = mapOf<String, String>(
"target" to target.detailedName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ private fun processCLib(args: Array<String>): Array<String>? {

val tool = ToolConfig(
arguments.target,
arguments.properties,
System.getProperty("konan.home"),
flavor
)
tool.downloadDependencies()
Expand Down
7 changes: 3 additions & 4 deletions backend.native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,19 @@ task start(dependsOn: "${hostName}Start")

// These files are built before the 'dist' is complete,
// so we provide custom values for
// -runtime, -properties, -library and -Djava.library.path
// konan.home, --runtime, -Djava.library.path etc

targetList.each { target ->
def konanJvmArgs = ["-ea",
"-Dkonan.home=$distDir",
"-Dkonan.home=${rootProject.projectDir}",
"-Djava.library.path=${project.buildDir}/nativelibs/$hostName",
"-Dfile.encoding=UTF-8"]

def defaultArgs = ['-nopack', '-nostdlib', '-nodefaultlibs', '-ea' ]
if (target != "wasm32") defaultArgs += '-g'
def konanArgs = [*defaultArgs,
'-target', target,
'-runtime', project(':runtime').file("build/${target}/runtime.bc"),
'-properties', rootProject.konanPropertiesFile.canonicalPath,
'--runtime', project(':runtime').file("build/${target}/runtime.bc"),
*project.globalBuildArgs]

task("${target}Stdlib", type: JavaExec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
arguments.mainPackage ?.let{ put(ENTRY, it) }
arguments.manifestFile ?.let{ put(MANIFEST_FILE, it) }
arguments.runtimeFile ?.let{ put(RUNTIME_FILE, it) }
arguments.propertyFile ?.let{ put(PROPERTY_FILE, it) }

put(LIST_TARGETS, arguments.listTargets)
put(OPTIMIZATION, arguments.optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-produce", shortName = "-p", valueDescription = "{program|dynamic|framework|library|bitcode}", description = "Specify output file kind")
var produce: String? = null

@Argument(value = "-properties", valueDescription = "<path>", description = "Override standard 'konan.properties' location")
var propertyFile: String? = null

@Argument(value = "-repo", shortName = "-r", valueDescription = "<path>", description = "Library search path")
var repositories: Array<String>? = null

@Argument(value = "-runtime", valueDescription = "<path>", description = "Override standard 'runtime.bc' location")
var runtimeFile: String? = null

@Argument(value = "-target", valueDescription = "<target>", description = "Set hardware target")
var target: String? = null

Expand Down Expand Up @@ -124,6 +118,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
@Argument(value = "--purge_user_libs", description = "Don't link unused libraries even explicitly specified")
var purgeUserLibs: Boolean = false

@Argument(value = "--runtime", valueDescription = "<path>", description = "Override standard 'runtime.bc' location")

var runtimeFile: String? = null
@Argument(value = "--time", description = "Report execution time for compiler phases")
var timePhases: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ internal class CAdapterGenerator(
output("RUNTIME_USED ${prefix}_ExportedSymbols* $exportedSymbol(void) { return &__konan_symbols;}")
outputStreamWriter.close()

if (context.config.targetManager.target.family == Family.WINDOWS) {
if (context.config.target.family == Family.WINDOWS) {
outputStreamWriter = context.config.tempFiles
.cAdapterDef
.printWriter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal fun produceOutput(context: Context) {
val neededLibraries
= context.llvm.librariesForLibraryManifest
val abiVersion = context.config.currentAbiVersion
val target = context.config.targetManager.target
val target = context.config.target
val nopack = config.getBoolean(KonanConfigKeys.NOPACK)
val manifest = config.get(KonanConfigKeys.MANIFEST_FILE)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,29 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration

val currentAbiVersion: Int = configuration.get(KonanConfigKeys.ABI_VERSION)!!

internal val targetManager = TargetManager(
configuration.get(KonanConfigKeys.TARGET))
internal val distribution = Distribution(
false,
null,
configuration.get(KonanConfigKeys.RUNTIME_FILE))

private val target = targetManager.target
internal val platformManager = PlatformManager(distribution)
internal val targetManager = platformManager.targetManager(configuration.get(KonanConfigKeys.TARGET))
internal val target = targetManager.target

init {
if (!target.enabled) {
error("Target $target is not available on the ${TargetManager.host} host")
if (!platformManager.isEnabled(target)) {
error("Target $target is not available on the ${HostManager.host} host")
}
}

val indirectBranchesAreAllowed = target != KonanTarget.WASM32

internal val distribution = Distribution(target,
configuration.get(KonanConfigKeys.PROPERTY_FILE),
configuration.get(KonanConfigKeys.RUNTIME_FILE))

internal val platform = PlatformManager(distribution.properties, distribution.dependenciesDir).platform(target).apply {
val platform = platformManager.platform(target).apply {
if (configuration.getBoolean(KonanConfigKeys.CHECK_DEPENDENCIES)) {
downloadDependencies()
}
}

internal val clang = platform.clang
val indirectBranchesAreAllowed = target != KonanTarget.WASM32

internal val produce get() = configuration.get(KonanConfigKeys.PRODUCE)!!
private val prefix = produce.prefix(target)
Expand All @@ -84,7 +83,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
get() = configuration.getList(KonanConfigKeys.LIBRARY_FILES)

private val repositories = configuration.getList(KonanConfigKeys.REPOSITORIES)
private val resolver = defaultResolver(repositories, distribution)
private val resolver = defaultResolver(repositories, target, distribution)

internal val immediateLibraries: List<LibraryReaderImpl> by lazy {
val result = resolver.resolveImmediateLibraries(
Expand All @@ -109,7 +108,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration

internal val defaultNativeLibraries =
if (produce == CompilerOutputKind.PROGRAM)
File(distribution.defaultNatives).listFiles.map { it.absolutePath }
File(distribution.defaultNatives(target)).listFiles.map { it.absolutePath }
else emptyList()

internal val nativeLibraries: List<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ class KonanConfigKeys {
= CompilerConfigurationKey.create("print locations")
val PRODUCE: CompilerConfigurationKey<CompilerOutputKind>
= CompilerConfigurationKey.create("compiler output kind")
val PROPERTY_FILE: CompilerConfigurationKey<String?>
= CompilerConfigurationKey.create("override default property file path")
val PURGE_USER_LIBS: CompilerConfigurationKey<Boolean>
= CompilerConfigurationKey.create("purge user-specified libs too")
val REPOSITORIES: CompilerConfigurationKey<List<String>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package org.jetbrains.kotlin.backend.konan

import org.jetbrains.kotlin.konan.KonanExternalToolFailure
import org.jetbrains.kotlin.konan.exec.Command
import org.jetbrains.kotlin.konan.file.*
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.file.createTempFile
import org.jetbrains.kotlin.konan.target.*

typealias BitcodeFile = String
Expand All @@ -28,7 +29,7 @@ typealias ExecutableFile = String
internal class LinkStage(val context: Context) {

private val config = context.config.configuration
private val target = context.config.targetManager.target
private val target = context.config.target
private val platform = context.config.platform
private val linker = platform.linker

Expand Down Expand Up @@ -117,6 +118,19 @@ internal class LinkStage(val context: Context) {
return combinedWasm
}

private fun llvmLinkAndLlc(bitcodeFiles: List<BitcodeFile>): String {
val combinedBc = temporary("combined", ".bc")
hostLlvmTool("llvm-link", "-o", combinedBc, *bitcodeFiles.toTypedArray())

val optimizedBc = temporary("optimized", ".bc")
hostLlvmTool("opt", combinedBc, "-o=$optimizedBc", "-O3")

val combinedO = temporary("combined", ".o")
hostLlvmTool("llc", combinedBc, "-filetype=obj", "-o", combinedO, "-function-sections", "-data-sections")

return combinedO
}

private fun asLinkerArgs(args: List<String>): List<String> {
if (linker.useCompilerDriverAsLinker) {
return args
Expand Down Expand Up @@ -201,15 +215,19 @@ internal class LinkStage(val context: Context) {
val libraryProvidedLinkerFlags =
libraries.map{it -> it.linkerOpts}.flatten()

var objectFiles: List<String> = listOf()
val objectFiles: MutableList<String> = mutableListOf()

val phaser = PhaseManager(context)
phaser.phase(KonanPhase.OBJECT_FILES) {
objectFiles = listOf(
if (target == KonanTarget.WASM32)
bitcodeToWasm(bitcodeFiles)
else
llvmLto(bitcodeFiles)
objectFiles.add(
when (platform.configurables) {
is WasmConfigurables
-> bitcodeToWasm(bitcodeFiles)
is ZephyrConfigurables
-> llvmLinkAndLlc(bitcodeFiles)
else
-> llvmLto(bitcodeFiles)
}
)
}
phaser.phase(KonanPhase.LINKER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@

package org.jetbrains.kotlin.backend.konan.library

import org.jetbrains.kotlin.backend.konan.Distribution
import org.jetbrains.kotlin.backend.konan.library.impl.LibraryReaderImpl
import org.jetbrains.kotlin.backend.konan.util.removeSuffixIfPresent
import org.jetbrains.kotlin.backend.konan.util.suffixIfNot
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.target.Distribution
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.TargetManager
import org.jetbrains.kotlin.konan.util.visibleName

interface SearchPathResolver {
val searchRoots: List<File>
Expand All @@ -32,12 +30,12 @@ interface SearchPathResolver {
}

fun defaultResolver(repositories: List<String>, target: KonanTarget): SearchPathResolver =
defaultResolver(repositories, Distribution(target))
defaultResolver(repositories, target, Distribution())

fun defaultResolver(repositories: List<String>, distribution: Distribution): SearchPathResolver =
fun defaultResolver(repositories: List<String>, target: KonanTarget, distribution: Distribution): SearchPathResolver =
KonanLibrarySearchPathResolver(
repositories,
distribution.target,
target,
distribution.klib,
distribution.localKonanDir
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {

val staticData = StaticData(context)

val runtimeFile = context.config.distribution.runtime
private val target = context.config.target

val runtimeFile = context.config.distribution.runtime(target)
val runtime = Runtime(runtimeFile) // TODO: dispose

init {
Expand Down Expand Up @@ -416,7 +418,17 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
null
}

private val personalityFunctionName = when (context.config.targetManager.target) {
val tlsMode by lazy {
when (target) {
KonanTarget.WASM32,
is KonanTarget.ZEPHYR
-> LLVMThreadLocalMode.LLVMNotThreadLocal
else
-> LLVMThreadLocalMode.LLVMGeneralDynamicTLSModel
}
}

private val personalityFunctionName = when (target) {
KonanTarget.MINGW -> "__gxx_personality_seh0"
else -> "__gxx_personality_v0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
// On x86_64 it is the case if the return value takes more than 16 bytes or is the structure with
// unaligned fields (there are some complicated exceptions currently ignored). The latter condition
// is "encoded" by stub generator by emitting either `getMessenger` or `getMessengerLU` intrinsic call.
val isStret = when (context.config.targetManager.target) {
val isStret = when (context.config.target) {
KonanTarget.MACBOOK, KonanTarget.IPHONE_SIM -> isLU // x86_64
KonanTarget.IPHONE -> false // aarch64
else -> TODO()
Expand Down
Loading

0 comments on commit 9789420

Please sign in to comment.