1
+ package eu.iamgio.pikt.properties
2
+
3
+ import java.io.File
4
+ import kotlin.system.exitProcess
5
+
6
+ /* *
7
+ * Storage for properties passed from command line.
8
+ *
9
+ * @param source source image file ("-Dsource")
10
+ * @param output output executable file without extension ("-Doutput")
11
+ * @param target compilation target ("-Dtarget")
12
+ * @param jvmCompilerPath optional path to the Kotlin/JVM compiler (required if [target] is [CompilationTarget.JVM]) ("-Djvmcompiler")
13
+ * @param nativeCompilerPath optional path to the Kotlin/Native compiler (required if [target] is [CompilationTarget.NATIVE]) ("-Dnativecompiler")
14
+ * // TODO colors properties
15
+ * @author Giorgio Garofalo
16
+ */
17
+ data class PiktProperties (
18
+ val source : File ,
19
+ val output : File ,
20
+ val target : CompilationTarget ,
21
+ val jvmCompilerPath : String? ,
22
+ val nativeCompilerPath : String? ,
23
+ )
24
+
25
+ /* *
26
+ * Single-instance object that parses JVM properties into a [PiktProperties] instance
27
+ *
28
+ * @author Giorgio Garofalo
29
+ */
30
+ object PiktPropertiesRetriever {
31
+
32
+ /* *
33
+ * Converts raw JVM properties to parsed [PiktProperties].
34
+ * If any required field is missing or invalid it prints an error and exits.
35
+ * @return parsed properties
36
+ */
37
+ fun retrieve (): PiktProperties {
38
+ val sourceProperty = System .getProperty(" source" )
39
+ val outputProperty = System .getProperty(" source" )
40
+ val targetProperty = System .getProperty(" target" )
41
+ val jvmCompilerPathProperty = System .getProperty(" jvmcompiler" )
42
+ val nativeCompilerPathProperty = System .getProperty(" nativecompiler" )
43
+
44
+ var isError = false
45
+
46
+ fun error (message : String ) {
47
+ System .err.println (message)
48
+ isError = true
49
+ }
50
+
51
+ // Source image file
52
+ val source: File ? = if (sourceProperty == null ) {
53
+ error(" Source file (-Dsource) is not set." )
54
+ null
55
+ } else File (sourceProperty).also {
56
+ if (! it.exists()) {
57
+ error(" Source file $it does not exist." )
58
+ }
59
+ }
60
+
61
+ // Output file without extension
62
+ val output: File ? = when {
63
+ outputProperty != null -> File (outputProperty)
64
+ source != null -> File (source.parent, source.nameWithoutExtension)
65
+ else -> null
66
+ }
67
+
68
+ // Compilation target type
69
+ val target = CompilationTarget .values().firstOrNull { it.argName == targetProperty }
70
+ if (target == null ) {
71
+ error(" Compilation target $target is invalid. 'jvm' and 'native' are supported targets." )
72
+ }
73
+
74
+ // JVM compiler
75
+ if (jvmCompilerPathProperty == null && CompilationTarget .JVM == target) {
76
+ error(" JVM compiler (-Djvmcompiler) is not set but target is JVM." )
77
+ } else if (! File (jvmCompilerPathProperty).exists()) {
78
+ error(" JVM compiler $jvmCompilerPathProperty does not exist." )
79
+ }
80
+
81
+ // Native compiler
82
+ if (nativeCompilerPathProperty == null && CompilationTarget .NATIVE == target) {
83
+ error(" Native compiler (-Dnativecompiler) is not set but target is native." )
84
+ } else if (! File (nativeCompilerPathProperty).exists()) {
85
+ error(" Native compiler $nativeCompilerPathProperty does not exist." )
86
+ }
87
+
88
+ if (isError) {
89
+ System .err.println (" Could not initialize Pikt properties. Exiting." )
90
+ exitProcess(- 1 )
91
+ }
92
+
93
+ return PiktProperties (
94
+ source = source!! ,
95
+ output = output!! ,
96
+ target = target!! ,
97
+ jvmCompilerPath = jvmCompilerPathProperty,
98
+ nativeCompilerPath = nativeCompilerPathProperty,
99
+ )
100
+ }
101
+ }
0 commit comments