[MinGW][Coverage] Workaround for missing lprofGetHostName.

This commit is contained in:
Sergey Bogolepov
2019-10-01 13:17:05 +03:00
committed by Sergey Bogolepov
parent 8622eb3be4
commit 4282dc5821
4 changed files with 44 additions and 5 deletions
@@ -150,9 +150,13 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
return resolvedLibraries.filterRoots { (!it.isDefault && !this.purgeUserLibs) || it.isNeededForLink }.getFullList(TopologicalLibraryOrder) as List<KonanLibrary> return resolvedLibraries.filterRoots { (!it.isDefault && !this.purgeUserLibs) || it.isNeededForLink }.getFullList(TopologicalLibraryOrder) as List<KonanLibrary>
} }
val shouldCoverSources = configuration.getBoolean(KonanConfigKeys.COVERAGE)
val shouldCoverLibraries = !configuration.getList(KonanConfigKeys.LIBRARIES_TO_COVER).isNullOrEmpty()
internal val runtimeNativeLibraries: List<String> = mutableListOf<String>().apply { internal val runtimeNativeLibraries: List<String> = mutableListOf<String>().apply {
add(if (debug) "debug.bc" else "release.bc") add(if (debug) "debug.bc" else "release.bc")
add(if (memoryModel == MemoryModel.STRICT) "strict.bc" else "relaxed.bc") add(if (memoryModel == MemoryModel.STRICT) "strict.bc" else "relaxed.bc")
if (shouldCoverLibraries || shouldCoverSources) add("profileRuntime.bc")
}.map { }.map {
File(distribution.defaultNatives(target)).child(it).absolutePath File(distribution.defaultNatives(target)).child(it).absolutePath
} }
@@ -21,8 +21,8 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.module
*/ */
internal class CoverageManager(val context: Context) { internal class CoverageManager(val context: Context) {
private val shouldCoverProgram: Boolean = private val shouldCoverSources: Boolean =
context.config.configuration.getBoolean(KonanConfigKeys.COVERAGE) context.config.shouldCoverSources
private val librariesToCover: Set<String> = private val librariesToCover: Set<String> =
context.config.coveredLibraries.map { it.libraryName }.toSet() context.config.coveredLibraries.map { it.libraryName }.toSet()
@@ -39,7 +39,7 @@ internal class CoverageManager(val context: Context) {
?: defaultOutputFilePath ?: defaultOutputFilePath
val enabled: Boolean = val enabled: Boolean =
shouldCoverProgram || librariesToCover.isNotEmpty() shouldCoverSources || librariesToCover.isNotEmpty()
init { init {
if (enabled && !checkRestrictions()) { if (enabled && !checkRestrictions()) {
@@ -60,10 +60,10 @@ internal class CoverageManager(val context: Context) {
filesRegionsInfo.flatMap { it.functions }.firstOrNull { it.function == irFunction } filesRegionsInfo.flatMap { it.functions }.firstOrNull { it.function == irFunction }
private val coveredModules: Set<ModuleDescriptor> by lazy { private val coveredModules: Set<ModuleDescriptor> by lazy {
val coveredUserCode = if (shouldCoverProgram) setOf(context.moduleDescriptor) else emptySet() val coveredUserCode = if (shouldCoverSources) setOf(context.moduleDescriptor) else emptySet()
val coveredLibs = context.irModules.filter { it.key in librariesToCover }.values val coveredLibs = context.irModules.filter { it.key in librariesToCover }.values
.map { it.descriptor }.toSet() .map { it.descriptor }.toSet()
val coveredIncludedLibs = if (shouldCoverProgram) context.getIncludedLibraryDescriptors().toSet() else emptySet() val coveredIncludedLibs = if (shouldCoverSources) context.getIncludedLibraryDescriptors().toSet() else emptySet()
coveredLibs + coveredUserCode + coveredIncludedLibs coveredLibs + coveredUserCode + coveredIncludedLibs
} }
+7
View File
@@ -21,6 +21,7 @@ targetList.each { targetName ->
dependsOn "${targetName}Release" dependsOn "${targetName}Release"
dependsOn "${targetName}Strict" dependsOn "${targetName}Strict"
dependsOn "${targetName}Relaxed" dependsOn "${targetName}Relaxed"
dependsOn "${targetName}ProfileRuntime"
target targetName target targetName
includeRuntime(delegate) includeRuntime(delegate)
linkerArgs project.file("../common/build/$targetName/hash.bc").path linkerArgs project.file("../common/build/$targetName/hash.bc").path
@@ -60,6 +61,12 @@ targetList.each { targetName ->
target targetName target targetName
includeRuntime(delegate) includeRuntime(delegate)
} }
task ("${targetName}ProfileRuntime", type: CompileCppToBitcode) {
name "profileRuntime"
srcRoot file('src/profile_runtime')
target targetName
}
} }
task hostRuntime(dependsOn: "${hostName}Runtime") task hostRuntime(dependsOn: "${hostName}Runtime")
@@ -0,0 +1,28 @@
// Define symbols that are required for code coverage but missing in compiler-RT for MinGW.
// See https://reviews.llvm.org/D58106/ for details.
#ifdef KONAN_WINDOWS
#include <windows.h>
extern "C" {
__attribute__((used))
int lprofGetHostName(char *hostName, int length) {
const int maxHostNameLength = 128;
WCHAR buffer[maxHostNameLength];
DWORD bufferSize = sizeof(buffer);
COMPUTER_NAME_FORMAT nameType = ComputerNameDnsFullyQualified;
if (!GetComputerNameExW(nameType, buffer, &bufferSize)) {
return -1;
}
int bytesWritten = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, hostName, length, nullptr, nullptr);
if (bytesWritten == 0) {
return -1;
} else {
return 0;
}
}
}
#endif