Add optional exitProcess after extraction

This commit is contained in:
Tamas Vajk
2021-11-09 17:02:28 +01:00
committed by Ian Lynagh
parent 69e8db06cb
commit 356639dadd
3 changed files with 32 additions and 11 deletions

View File

@@ -30,6 +30,13 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
description = "The start time of the compilation as a Unix timestamp",
required = false,
allowMultipleOccurrences = false
),
CliOption(
optionName = OPTION_EXIT_AFTER_EXTRACTION,
valueDescription = "Specify whether to call exitProcess after the extraction has completed",
description = "Specify whether to call exitProcess after the extraction has completed",
required = false,
allowMultipleOccurrences = false
)
)
@@ -39,12 +46,8 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
configuration: CompilerConfiguration
) = when (option.optionName) {
OPTION_INVOCATION_TRAP_FILE -> configuration.put(KEY_INVOCATION_TRAP_FILE, value)
OPTION_CHECK_TRAP_IDENTICAL ->
when (value) {
"true" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, true)
"false" -> configuration.put(KEY_CHECK_TRAP_IDENTICAL, false)
else -> error("kotlin extractor: Bad argument $value for $OPTION_CHECK_TRAP_IDENTICAL")
}
OPTION_CHECK_TRAP_IDENTICAL -> processBooleanOption(value, OPTION_CHECK_TRAP_IDENTICAL, KEY_CHECK_TRAP_IDENTICAL, configuration)
OPTION_EXIT_AFTER_EXTRACTION -> processBooleanOption(value, OPTION_EXIT_AFTER_EXTRACTION, KEY_EXIT_AFTER_EXTRACTION, configuration)
OPTION_COMPILATION_STARTTIME ->
when (val v = value.toLongOrNull()) {
is Long -> configuration.put(KEY_COMPILATION_STARTTIME, v)
@@ -52,6 +55,13 @@ class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
}
else -> error("kotlin extractor: Bad option: ${option.optionName}")
}
private fun processBooleanOption(value: String, optionName: String, configKey: CompilerConfigurationKey<Boolean>, configuration: CompilerConfiguration) =
when (value) {
"true" -> configuration.put(configKey, true)
"false" -> configuration.put(configKey, false)
else -> error("kotlin extractor: Bad argument $value for $optionName")
}
}
private val OPTION_INVOCATION_TRAP_FILE = "invocationTrapFile"
@@ -60,3 +70,5 @@ private val OPTION_CHECK_TRAP_IDENTICAL = "checkTrapIdentical"
val KEY_CHECK_TRAP_IDENTICAL= CompilerConfigurationKey<Boolean>(OPTION_CHECK_TRAP_IDENTICAL)
private val OPTION_COMPILATION_STARTTIME = "compilationStartTime"
val KEY_COMPILATION_STARTTIME= CompilerConfigurationKey<Long>(OPTION_COMPILATION_STARTTIME)
private val OPTION_EXIT_AFTER_EXTRACTION = "exitAfterExtraction"
val KEY_EXIT_AFTER_EXTRACTION= CompilerConfigurationKey<Boolean>(OPTION_EXIT_AFTER_EXTRACTION)

View File

@@ -14,11 +14,10 @@ class KotlinExtractorComponentRegistrar : ComponentRegistrar {
if (invocationTrapFile == null) {
throw Exception("Required argument for TRAP invocation file not given")
}
val checkTrapIdentical = configuration[KEY_CHECK_TRAP_IDENTICAL]
val compilationStartTime = configuration[KEY_COMPILATION_STARTTIME]
IrGenerationExtension.registerExtension(project, KotlinExtractorExtension(
invocationTrapFile,
checkTrapIdentical ?: false,
compilationStartTime))
configuration[KEY_CHECK_TRAP_IDENTICAL] ?: false,
configuration[KEY_COMPILATION_STARTTIME],
configuration[KEY_EXIT_AFTER_EXTRACTION] ?: false))
}
}

View File

@@ -27,8 +27,15 @@ import com.semmle.extractor.java.OdasaOutput.TrapFileManager
import com.semmle.util.files.FileUtil
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.util.*
import kotlin.system.exitProcess
class KotlinExtractorExtension(
private val invocationTrapFile: String,
private val checkTrapIdentical: Boolean,
private val compilationStartTime: Long?,
private val exitAfterExtraction: Boolean)
: IrGenerationExtension {
class KotlinExtractorExtension(private val invocationTrapFile: String, private val checkTrapIdentical: Boolean, private val compilationStartTime: Long?) : IrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
val startTimeMs = System.currentTimeMillis()
// This default should be kept in sync with com.semmle.extractor.java.interceptors.KotlinInterceptor.initializeExtractionContext
@@ -66,6 +73,9 @@ class KotlinExtractorExtension(private val invocationTrapFile: String, private v
tw.writeCompilation_finished(compilation, -1.0, compilationTimeMs.toDouble() / 1000)
tw.flush()
}
if (exitAfterExtraction) {
exitProcess(0)
}
}
}