Skip to content

Commit 9b58757

Browse files
Amarasleios
andauthored
Added and adapted Kotlin tool from scons-contrib (#984)
Co-authored-by: James Schloss <jrs.schloss@gmail.com>
1 parent 0d3866f commit 9b58757

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

SConstruct

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ languages_to_import = {
4444
'coconut': ['coconut'],
4545
'go': ['go'],
4646
'rust': ['rustc', 'cargo'],
47+
'kotlin': ['kotlin'],
4748
}
4849

4950
for language, tools in languages_to_import.items():
@@ -78,6 +79,7 @@ languages = {
7879
'java': 'java',
7980
'javascript': 'js',
8081
'julia': 'jl',
82+
'kotlin': 'kt',
8183
'lolcode': 'lol',
8284
'lua': 'lua',
8385
'php': 'php',

builders/kotlin.py

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# MIT License
2+
#
3+
# Copyright The SCons Foundation
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining
6+
# a copy of this software and associated documentation files (the
7+
# "Software"), to deal in the Software without restriction, including
8+
# without limitation the rights to use, copy, modify, merge, publish,
9+
# distribute, sublicense, and/or sell copies of the Software, and to
10+
# permit persons to whom the Software is furnished to do so, subject to
11+
# the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included
14+
# in all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17+
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18+
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
"""SCons.Tool.kotlin
25+
Tool-specific initialization for Kotlin.
26+
"""
27+
28+
import SCons.Action
29+
import SCons.Builder
30+
import SCons.Util
31+
32+
33+
class ToolKotlinWarning(SCons.Warnings.SConsWarning):
34+
pass
35+
36+
37+
class KotlinNotFound(ToolKotlinWarning):
38+
pass
39+
40+
41+
SCons.Warnings.enableWarningClass(ToolKotlinWarning)
42+
43+
44+
def _detect(env):
45+
""" Try to detect the kotlinc binary """
46+
try:
47+
return env["kotlinc"]
48+
except KeyError:
49+
pass
50+
51+
kotlin = env.Detect("kotlinc")
52+
if kotlin:
53+
return kotlin
54+
55+
SCons.Warnings.warn(KotlinNotFound, "Could not find kotlinc executable")
56+
57+
58+
#
59+
# Builders
60+
#
61+
kotlinc_builder = SCons.Builder.Builder(
62+
action=SCons.Action.Action("$KOTLINCCOM", "$KOTLINCCOMSTR"),
63+
suffix="$KOTLINCLASSSUFFIX",
64+
src_suffix="$KOTLINSUFFIX",
65+
single_source=True,
66+
) # file by file
67+
68+
kotlin_jar_builder = SCons.Builder.Builder(
69+
action=SCons.Action.Action("$KOTLINJARCOM", "$KOTLINJARCOMSTR"),
70+
suffix="$KOTLINJARSUFFIX",
71+
src_suffix="$KOTLINSUFFIX",
72+
single_source=True,
73+
) # file by file
74+
75+
kotlin_rtjar_builder = SCons.Builder.Builder(
76+
action=SCons.Action.Action("$KOTLINRTJARCOM", "$KOTLINRTJARCOMSTR"),
77+
suffix="$KOTLINJARSUFFIX",
78+
src_suffix="$KOTLINSUFFIX",
79+
single_source=True,
80+
) # file by file
81+
82+
83+
def Kotlin(env, target, source=None, *args, **kw):
84+
"""
85+
A pseudo-Builder wrapper for the kotlinc executable.
86+
kotlinc [options] file
87+
"""
88+
if not SCons.Util.is_List(target):
89+
target = [target]
90+
if not source:
91+
source = target[:]
92+
if not SCons.Util.is_List(source):
93+
source = [source]
94+
95+
result = []
96+
kotlinc_suffix = env.subst("$KOTLINCLASSSUFFIX")
97+
kotlinc_extension = env.subst("$KOTLINEXTENSION")
98+
for t, s in zip(target, source):
99+
t_ext = t
100+
if not t.endswith(kotlinc_suffix):
101+
if not t.endswith(kotlinc_extension):
102+
t_ext += kotlinc_extension
103+
104+
t_ext += kotlinc_suffix
105+
# Ensure that the case of first letter is upper-case
106+
t_ext = t_ext[:1].upper() + t_ext[1:]
107+
# Call builder
108+
kotlin_class = kotlinc_builder.__call__(env, t_ext, s, **kw)
109+
result.extend(kotlin_class)
110+
111+
return result
112+
113+
114+
def KotlinJar(env, target, source=None, *args, **kw):
115+
"""
116+
A pseudo-Builder wrapper for creating JAR files with the kotlinc executable.
117+
kotlinc [options] file -d target
118+
"""
119+
if not SCons.Util.is_List(target):
120+
target = [target]
121+
if not source:
122+
source = target[:]
123+
if not SCons.Util.is_List(source):
124+
source = [source]
125+
126+
result = []
127+
for t, s in zip(target, source):
128+
# Call builder
129+
kotlin_jar = kotlin_jar_builder.__call__(env, t, s, **kw)
130+
result.extend(kotlin_jar)
131+
132+
return result
133+
134+
135+
def KotlinRuntimeJar(env, target, source=None, *args, **kw):
136+
"""
137+
A pseudo-Builder wrapper for creating standalone JAR files with the kotlinc executable.
138+
kotlinc [options] file -d target -include-runtime
139+
"""
140+
if not SCons.Util.is_List(target):
141+
target = [target]
142+
if not source:
143+
source = target[:]
144+
if not SCons.Util.is_List(source):
145+
source = [source]
146+
147+
result = []
148+
for t, s in zip(target, source):
149+
# Call builder
150+
kotlin_jar = kotlin_rtjar_builder.__call__(env, t, s, **kw)
151+
result.extend(kotlin_jar)
152+
153+
return result
154+
155+
156+
def generate(env):
157+
"""Add Builders and construction variables for kotlinc to an Environment."""
158+
159+
env["KOTLINC"] = _detect(env)
160+
161+
env.SetDefault(
162+
KOTLINC="kotlinc",
163+
KOTLINSUFFIX=".kt",
164+
KOTLINEXTENSION="Kt",
165+
KOTLINCLASSSUFFIX=".class",
166+
KOTLINJARSUFFIX=".jar",
167+
KOTLINCFLAGS=SCons.Util.CLVar(),
168+
KOTLINJARFLAGS=SCons.Util.CLVar(),
169+
KOTLINRTJARFLAGS=SCons.Util.CLVar(["-include-runtime"]),
170+
KOTLINCCOM="$KOTLINC $KOTLINCFLAGS $SOURCE",
171+
KOTLINCCOMSTR="",
172+
KOTLINJARCOM="$KOTLINC $KOTLINJARFLAGS -d $TARGET $SOURCE",
173+
KOTLINJARCOMSTR="",
174+
KOTLINRTJARCOM="$KOTLINC $KOTLINRTJARFLAGS -d $TARGET $SOURCE",
175+
KOTLINRTJARCOMSTR="",
176+
)
177+
178+
env.AddMethod(Kotlin, "Kotlin")
179+
env.AddMethod(KotlinJar, "KotlinJar")
180+
env.AddMethod(KotlinRuntimeJar, "KotlinRuntimeJar")
181+
182+
183+
def exists(env):
184+
return _detect(env)

sconscripts/kotlin_SConscript

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Import('files_to_compile env')
2+
3+
for file_info in files_to_compile:
4+
build_target = f'#/build/{file_info.language}/{file_info.chapter}/{file_info.path.stem}'
5+
build_result = env.KotlinJar(build_target, str(file_info.path))
6+
env.Alias(str(file_info.chapter), build_result)

0 commit comments

Comments
 (0)