[K/N] Rework defines passing to runtime compilation

This commit is contained in:
Pavel Kunyavskiy
2022-08-09 19:12:15 +02:00
committed by Space
parent 339868305e
commit de1dd47ab1
7 changed files with 184 additions and 100 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed -> in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.konan.target
@@ -45,8 +34,41 @@ sealed class ClangArgs(
// TODO: Should be dropped in favor of real MSVC target.
private val argsForWindowsJni = forJni && target == KonanTarget.MINGW_X64
private val clangArgsSpecificForKonanSources
get() = configurables.runtimeDefinitions.map { "-D$it" }
private val clangArgsSpecificForKonanSources : List<String>
get() {
val konanOptions = listOfNotNull(
target.architecture.name.takeIf { target != KonanTarget.WATCHOS_ARM64 },
"ARM32".takeIf { target == KonanTarget.WATCHOS_ARM64 },
target.family.name.takeIf { target.family != Family.MINGW },
"WINDOWS".takeIf { target.family == Family.MINGW },
"MACOSX".takeIf { target.family == Family.OSX },
"NO_THREADS".takeUnless { target.supportsThreads() },
"NO_EXCEPTIONS".takeUnless { target.supportsExceptions() },
"NO_MEMMEM".takeUnless { target.suportsMemMem() },
"NO_64BIT_ATOMIC".takeUnless { target.supports64BitAtomics() },
"NO_UNALIGNED_ACCESS".takeUnless { target.supportsUnalignedAccess() },
"FORBID_BUILTIN_MUL_OVERFLOW".takeUnless { target.supports64BitMulOverflow() },
"OBJC_INTEROP".takeIf { target.supportsObjcInterop() },
"HAS_FOUNDATION_FRAMEWORK".takeIf { target.hasFoundationFramework() },
"HAS_UIKIT_FRAMEWORK".takeIf { target.hasUIKitFramework() },
"REPORT_BACKTRACE_TO_IOS_CRASH_LOG".takeIf { target.supportsIosCrashLog() },
"NEAD_SMALL_BINARY".takeIf { target.needSmallBinary() },
"TARGET_HAS_ADDRESS_DEPENDENCY".takeIf { target.hasAddressDependencyInMemoryModel() },
).map { "KONAN_$it=1" }
val otherOptions = listOfNotNull(
"USE_ELF_SYMBOLS=1".takeIf { target.binaryFormat() == BinaryFormat.ELF },
"ELFSIZE=${target.pointerBits()}".takeIf { target.binaryFormat() == BinaryFormat.ELF },
"MACHSIZE=${target.pointerBits()}".takeIf { target.binaryFormat() == BinaryFormat.MACH_O },
"__ANDROID__".takeIf { target.family == Family.ANDROID },
"USE_PE_COFF_SYMBOLS=1".takeIf { target.binaryFormat() == BinaryFormat.PE_COFF },
"UNICODE".takeIf { target.family == Family.MINGW },
"USE_GCC_UNWIND=1".takeIf { target.supportsGccUnwind() }
)
val customOptions = target.customArgsForKonanSources()
return (konanOptions + otherOptions + customOptions).map { "-D$it" }
}
private val binDir = when (HostManager.host) {
KonanTarget.LINUX_X64 -> "$absoluteTargetToolchain/bin"
@@ -5,7 +5,33 @@
package org.jetbrains.kotlin.konan.target
// TODO: This all needs to go to konan.properties
enum class BinaryFormat {
ELF,
PE_COFF,
MACH_O
}
fun KonanTarget.binaryFormat() = when (family) {
Family.WATCHOS -> BinaryFormat.MACH_O
Family.IOS -> BinaryFormat.MACH_O
Family.TVOS -> BinaryFormat.MACH_O
Family.OSX -> BinaryFormat.MACH_O
Family.ANDROID -> BinaryFormat.ELF
Family.LINUX -> BinaryFormat.ELF
Family.MINGW -> BinaryFormat.PE_COFF
Family.WASM, Family.ZEPHYR -> null
}
fun KonanTarget.pointerBits() = when (architecture) {
Architecture.X64 -> 64
Architecture.X86 -> 32
Architecture.ARM64 -> if (this == KonanTarget.WATCHOS_ARM64) 32 else 64
Architecture.ARM32 -> 32
Architecture.MIPS32 -> 32
Architecture.MIPSEL32 -> 32
Architecture.WASM32 -> 32
}
fun KonanTarget.supportsCodeCoverage(): Boolean =
// TODO: Disabled for now, because we don't support
@@ -46,13 +72,71 @@ fun KonanTarget.supportsCoreSymbolication(): Boolean =
KonanTarget.WATCHOS_X86, KonanTarget.WATCHOS_X64, KonanTarget.WATCHOS_SIMULATOR_ARM64
)
fun KonanTarget.supportsGccUnwind(): Boolean = family == Family.ANDROID || family == Family.LINUX || family == Family.MINGW
fun KonanTarget.supportsThreads(): Boolean =
when(this) {
is KonanTarget.WASM32 -> false
is KonanTarget.ZEPHYR -> false
else -> true
}
fun KonanTarget.supportsThreads(): Boolean = when(this) {
is KonanTarget.WASM32 -> false
is KonanTarget.ZEPHYR -> false
else -> true
}
fun KonanTarget.supportsExceptions(): Boolean = when(this) {
is KonanTarget.WASM32 -> false
is KonanTarget.ZEPHYR -> false
else -> true
}
fun KonanTarget.suportsMemMem(): Boolean = when (this) {
is KonanTarget.WASM32 -> false
is KonanTarget.MINGW_X86 -> false
is KonanTarget.MINGW_X64 -> false
is KonanTarget.ZEPHYR -> false
else -> true
}
fun KonanTarget.supportsObjcInterop(): Boolean = family.isAppleFamily
fun KonanTarget.hasFoundationFramework(): Boolean = family.isAppleFamily
fun KonanTarget.hasUIKitFramework(): Boolean = family == Family.IOS || family == Family.TVOS
fun KonanTarget.supports64BitMulOverflow(): Boolean = when (this) {
is KonanTarget.MINGW_X86 -> false
is KonanTarget.LINUX_ARM32_HFP -> false
is KonanTarget.LINUX_MIPS32 -> false
is KonanTarget.LINUX_MIPSEL32 -> false
is KonanTarget.WASM32 -> false
is KonanTarget.ZEPHYR -> false
is KonanTarget.ANDROID_ARM32 -> false
is KonanTarget.ANDROID_X86 -> false
else -> true
}
fun KonanTarget.supportsIosCrashLog(): Boolean = when (this) {
KonanTarget.IOS_ARM32 -> true
KonanTarget.IOS_ARM64 -> true
KonanTarget.WATCHOS_ARM32 -> true
KonanTarget.WATCHOS_ARM64 -> true
KonanTarget.TVOS_ARM64 -> true
else -> false
}
/*
* While not 100% correct here, using atomic ops on iOS armv7 requires 8 byte alignment,
* and general ABI requires 4-byte alignment on 64-bit long fields as mentioned in
* https://developer.apple.com/library/archive/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html#//apple_ref/doc/uid/TP40009021-SW1
* See https://github.com/ktorio/ktor/issues/941 for the context.
* TODO: reconsider once target MIPS can do proper 64-bit load/store/CAS.
*/
fun KonanTarget.supports64BitAtomics(): Boolean = when (architecture) {
Architecture.ARM32, Architecture.WASM32, Architecture.MIPS32, Architecture.MIPSEL32 -> false
Architecture.X86, Architecture.ARM64, Architecture.X64 -> true
} && this != KonanTarget.WATCHOS_ARM64 && this != KonanTarget.WATCHOS_X86
fun KonanTarget.supportsUnalignedAccess(): Boolean = when (architecture) {
Architecture.ARM32, Architecture.WASM32, Architecture.MIPS32, Architecture.MIPSEL32 -> false
Architecture.X86, Architecture.ARM64, Architecture.X64 -> true
} && this != KonanTarget.WATCHOS_ARM64
fun KonanTarget.needSmallBinary() = (architecture == Architecture.ARM32 && family.isAppleFamily) || this == KonanTarget.WATCHOS_ARM64
fun KonanTarget.supportedSanitizers(): List<SanitizerKind> =
when(this) {
@@ -65,9 +149,32 @@ fun KonanTarget.supportedSanitizers(): List<SanitizerKind> =
else -> listOf()
}
// should be consistent with KONAN_TARGET_HAS_ADDRESS_DEPENDENCY macro
fun KonanTarget.hasAddressDependencyInMemoryModel(): Boolean =
when (this.architecture) {
Architecture.X86, Architecture.X64, Architecture.ARM32, Architecture.ARM64 -> true
Architecture.MIPS32, Architecture.MIPSEL32, Architecture.WASM32 -> false
}
// TODO: this is bad function. It should be replaced by capabilities functions like above
// but two affected targets are too strange, so we postpone it
fun KonanTarget.customArgsForKonanSources() = when (this) {
KonanTarget.WASM32 -> listOf(
"KONAN_NO_FFI=1",
"KONAN_INTERNAL_DLMALLOC=1",
"KONAN_INTERNAL_SNPRINTF=1",
"KONAN_INTERNAL_NOW=1",
"KONAN_NO_CTORS_SECTION=1",
"KONAN_NO_BACKTRACE=1",
"KONAN_NO_EXTERNAL_CALLS_CHECKER=1",
)
is KonanTarget.ZEPHYR -> listOf(
"KONAN_NO_FFI=1",
"KONAN_NO_MATH=1",
"KONAN_INTERNAL_SNPRINTF=1",
"KONAN_INTERNAL_NOW=1",
"KONAN_NO_CTORS_SECTION=1",
"KONAN_NO_BACKTRACE=1"
)
else -> emptyList()
}