xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/toolchains/kotlin_jvm/kotlinc_flags.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1*3a22c0a3SAlix# Copyright 2022 Google LLC. All rights reserved.
2*3a22c0a3SAlix#
3*3a22c0a3SAlix# Licensed under the Apache License, Version 2.0 (the License);
4*3a22c0a3SAlix# you may not use this file except in compliance with the License.
5*3a22c0a3SAlix# You may obtain a copy of the License at
6*3a22c0a3SAlix#
7*3a22c0a3SAlix#     http://www.apache.org/licenses/LICENSE-2.0
8*3a22c0a3SAlix#
9*3a22c0a3SAlix# Unless required by applicable law or agreed to in writing, software
10*3a22c0a3SAlix# distributed under the License is distributed on an "AS IS" BASIS,
11*3a22c0a3SAlix# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*3a22c0a3SAlix# See the License for the specific language governing permissions and
13*3a22c0a3SAlix# limitations under the License.
14*3a22c0a3SAlix
15*3a22c0a3SAlix"""Static flags for kotlinc."""
16*3a22c0a3SAlix
17*3a22c0a3SAlixload("//:visibility.bzl", "RULES_KOTLIN")
18*3a22c0a3SAlix
19*3a22c0a3SAlix_KT_LANG_VERSION = "1.9"
20*3a22c0a3SAlix
21*3a22c0a3SAlix_SHARED_FLAGS = [
22*3a22c0a3SAlix    # We're supplying JDK in bootclasspath explicitly instead
23*3a22c0a3SAlix    "-no-jdk",
24*3a22c0a3SAlix
25*3a22c0a3SAlix    # stdlib included in merged_deps
26*3a22c0a3SAlix    "-no-stdlib",
27*3a22c0a3SAlix
28*3a22c0a3SAlix    # The bytecode format to emit
29*3a22c0a3SAlix    "-jvm-target",
30*3a22c0a3SAlix    "11",
31*3a22c0a3SAlix
32*3a22c0a3SAlix    # Emit bytecode with parameter names
33*3a22c0a3SAlix    "-java-parameters",
34*3a22c0a3SAlix
35*3a22c0a3SAlix    # Allow default method declarations, akin to what javac emits (b/110378321).
36*3a22c0a3SAlix    "-Xjvm-default=all",
37*3a22c0a3SAlix
38*3a22c0a3SAlix    # Trust JSR305 nullness type qualifier nicknames the same as @Nonnull/@Nullable
39*3a22c0a3SAlix    # (see https://kotlinlang.org/docs/reference/java-interop.html#jsr-305-support)
40*3a22c0a3SAlix    "-Xjsr305=strict",
41*3a22c0a3SAlix
42*3a22c0a3SAlix    # Trust annotations on type arguments, etc.
43*3a22c0a3SAlix    # (see https://kotlinlang.org/docs/java-interop.html#annotating-type-arguments-and-type-parameters)
44*3a22c0a3SAlix    "-Xtype-enhancement-improvements-strict-mode",
45*3a22c0a3SAlix
46*3a22c0a3SAlix    # TODO: Remove this as the default setting (probably Kotlin 1.7)
47*3a22c0a3SAlix    "-Xenhance-type-parameter-types-to-def-not-null=true",
48*3a22c0a3SAlix
49*3a22c0a3SAlix    # Explicitly set language version so we can update compiler separate from language version
50*3a22c0a3SAlix    "-language-version",
51*3a22c0a3SAlix    _KT_LANG_VERSION,
52*3a22c0a3SAlix
53*3a22c0a3SAlix    # Enable type annotations in the JVM bytecode (b/170647926)
54*3a22c0a3SAlix    "-Xemit-jvm-type-annotations",
55*3a22c0a3SAlix
56*3a22c0a3SAlix    # TODO: Temporarily disable 1.5's sam wrapper conversion
57*3a22c0a3SAlix    "-Xsam-conversions=class",
58*3a22c0a3SAlix
59*3a22c0a3SAlix    # We don't want people to use experimental APIs, but if they do, we want them to use @OptIn
60*3a22c0a3SAlix    "-opt-in=kotlin.RequiresOptIn",
61*3a22c0a3SAlix
62*3a22c0a3SAlix    # Don't complain when using old builds or release candidate builds
63*3a22c0a3SAlix    "-Xskip-prerelease-check",
64*3a22c0a3SAlix
65*3a22c0a3SAlix    # Allows a no source files to create an empty jar.
66*3a22c0a3SAlix    "-Xallow-no-source-files",
67*3a22c0a3SAlix
68*3a22c0a3SAlix    # TODO: Remove this flag
69*3a22c0a3SAlix    "-Xuse-old-innerclasses-logic",
70*3a22c0a3SAlix
71*3a22c0a3SAlix    # TODO: Remove this flag
72*3a22c0a3SAlix    "-Xno-source-debug-extension",
73*3a22c0a3SAlix]
74*3a22c0a3SAlix
75*3a22c0a3SAlix_CLI_ADDITIONAL_FLAGS = [
76*3a22c0a3SAlix    # Silence all warning-level diagnostics
77*3a22c0a3SAlix    "-nowarn",
78*3a22c0a3SAlix]
79*3a22c0a3SAlix
80*3a22c0a3SAlixdef _read_one_define_flags(ctx, name):
81*3a22c0a3SAlix    define = ctx.var.get(name, default = None)
82*3a22c0a3SAlix    return [f for f in define.split(" ") if f] if define else []
83*3a22c0a3SAlix
84*3a22c0a3SAlixdef _read_define_flags(ctx):
85*3a22c0a3SAlix    return (
86*3a22c0a3SAlix        _read_one_define_flags(ctx, "extra_kt_jvm_opts")
87*3a22c0a3SAlix    )
88*3a22c0a3SAlix
89*3a22c0a3SAlixkotlinc_flags = struct(
90*3a22c0a3SAlix    # go/keep-sorted start
91*3a22c0a3SAlix    CLI_FLAGS = _SHARED_FLAGS + _CLI_ADDITIONAL_FLAGS,
92*3a22c0a3SAlix    IDE_FLAGS = _SHARED_FLAGS,
93*3a22c0a3SAlix    KT_LANG_VERSION = _KT_LANG_VERSION,
94*3a22c0a3SAlix    read_define_flags = _read_define_flags,
95*3a22c0a3SAlix    # go/keep-sorted end
96*3a22c0a3SAlix)
97