packages feed

cabal-buck2-0.6.0.0: buck2/prelude/decls/kotlin_rules.bzl

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.

# TODO(cjhopman): This was generated by scripts/hacks/rules_shim_with_docs.py,
# but should be manually edited going forward. There may be some errors in
# the generated docs, and so those should be verified to be accurate and
# well-formatted (and then delete this TODO)

load("@prelude//:attrs_validators.bzl", "validation_common")
load("@prelude//:validation_deps.bzl", "VALIDATION_DEPS_ATTR_NAME")
load("@prelude//android:build_only_native_code.bzl", "is_build_only_native_code")
load("@prelude//android:configuration.bzl", "is_building_android_binary_attr")
load("@prelude//decls:test_common.bzl", "test_common")
load(":common.bzl", "TestType", "buck", "prelude_rule")
load(":java_rules.bzl", "dex_min_sdk_version")
load(":jvm_common.bzl", "jvm_common")
load(":re_test_common.bzl", "re_test_common")
load(":toolchains_common.bzl", "toolchains_common")

_no_x_jdk_release_doc = """
        By default, classic kotlin adds -Xjdk-release=java_version to the kotlinc arguments.
        Set this to True to not add -Xjdk-release=java_version to the kotlinc arguments.
        This is helpful if the -Xjdk-release= brings trouble since it is not 100% guaranteed to work as expected.
        See the [doc of kotlinc](https://kotlinlang.org/docs/compiler-reference.html#xjdk-release-version) for more detail.
"""

kotlin_library = prelude_rule(
    name = "kotlin_library",
    docs = """
        A `kotlin_library()` rule is used to define a set of Kotlin
        files that can be compiled together. The main output of a
         `kotlin_library()` rule is a single JAR file containing all
        of the compiled class files, as well as the static files specified in
        the `resources` argument.
    """,
    examples = """
        ```
        # A rule that compiles a single .kt file.
        kotlin_library(
          name = 'JsonUtil',
          srcs = ['JsonUtil.kt'],
          deps = [
            '//third_party/guava:guava',
            '//third_party/jackson:jackson',
          ],
        )

        # A rule that compiles all of the .kt files under the directory in
        # which the rule is defined using glob(). It also excludes an
        # individual file that may have additional dependencies, so it is
        # compiled by a separate rule.
        kotlin_library(
          name = 'messenger',
          srcs = glob(['**/*.kt'], excludes = ['MessengerModule.kt']),
          deps = [
            '//src/com/facebook/base:base',
            '//third_party/guava:guava',
          ],
        )

        kotlin_library(
          name = 'MessengerModule',
          srcs = ['MessengerModule.kt'],
          deps = [
            '//src/com/facebook/base:base',
            '//src/com/google/inject:inject',
            '//third_party/guava:guava',
            '//third_party/jsr-330:jsr-330',
          ],
        )

        # A rule that builds a library with both relative and
        # fully-qualified deps.
        kotlin_library(
          name = 'testutil',
          srcs = glob(['tests/**/*.kt'], excludes = 'tests/**/*Test.kt'),
          deps = [
            ':lib-fb4a',
            '//java/com/facebook/base:base',
          ],
        )
        ```
    """,
    further = None,
    attrs = (
        # @unsorted-dict-items
        {
            "srcs": attrs.list(
                attrs.source(),
                default = [],
                doc = """
                The set of `.kt`, `.java` or `.kts` files to compile for this rule.
                 If any of the files in this list end in `.src.zip`,
                 then the entries in the ZIP file that end in `.java` or `.kt` will be
                 included as ordinary inputs to compilation.
            """,
            ),
        }
        | jvm_common.resources_arg()
        | {
            "deps": attrs.list(
                attrs.dep(),
                default = [],
                doc = """
                Rules (usually other `kotlin_library` rules) that are used to
                 generate the classpath required to compile this `kotlin_library`.
            """,
            ),
            "extra_kotlinc_arguments": attrs.list(
                attrs.arg(anon_target_compatible = True),
                default = [],
                doc = """
                List of additional arguments to pass into the Kotlin compiler.
            """,
            ),
            "friend_paths": jvm_common.friend_paths_attr(),
        }
        | jvm_common.annotation_processors()
        | jvm_common.remove_classes_arg()
        | jvm_common.exported_deps()
        | jvm_common.provided_deps()
        | jvm_common.exported_provided_deps()
        | jvm_common.k2()
        | jvm_common.kotlin_compiler_plugins()
        | jvm_common.incremental()
        | jvm_common.kotlincd_content_based_paths()
        | jvm_common.plugins()
        | jvm_common.javac()
        | jvm_common.enable_used_classes()
        | jvm_common.classic_java_content_based_paths()
        | buck.labels_arg()
        | jvm_common.abi_generation_mode()
        | {
            "extra_arguments": attrs.list(attrs.string(), default = []),
            "jar_postprocessor": attrs.option(attrs.exec_dep(), default = None),
            "java_version": attrs.option(attrs.string(), default = None),
            "jdk_release": attrs.option(attrs.string(), default = None),
            "keep_synthetics_in_class_abi": attrs.option(attrs.bool(), default = None),
            "manifest_file": attrs.option(attrs.source(), default = None),
            "maven_coords": attrs.option(attrs.string(), default = None),
            "no_x_jdk_release": attrs.bool(default = False, doc = _no_x_jdk_release_doc),
            "proguard_config": attrs.option(attrs.source(), default = None),
            "required_for_source_only_abi": attrs.bool(default = False),
            "resources_root": attrs.option(attrs.string(), default = None),
            "runtime_deps": attrs.list(attrs.dep(), default = []),
            "source": attrs.option(attrs.string(), default = None),
            "source_only_abi_deps": attrs.list(attrs.dep(), default = []),
            "target": attrs.option(attrs.string(), default = None),
            VALIDATION_DEPS_ATTR_NAME: attrs.set(attrs.dep(), sorted = True, default = []),
            "_build_only_native_code": attrs.default_only(attrs.bool(default = is_build_only_native_code())),
            "_dex_min_sdk_version": attrs.option(attrs.int(), default = dex_min_sdk_version()),
            "_dex_toolchain": toolchains_common.dex(),
            "_exec_os_type": buck.exec_os_type_arg(),
            "_is_building_android_binary": is_building_android_binary_attr(),
            "_java_toolchain": toolchains_common.java(),
            "_kotlin_toolchain": toolchains_common.kotlin(),
        }
        | buck.licenses_arg()
        | buck.contacts_arg()
        | jvm_common.plugins()
        | validation_common.attrs_validators_arg()
    ),
)

kotlin_test = prelude_rule(
    name = "kotlin_test",
    docs = """
        A `kotlin_test()` rule is used to define a set of
         `.kt` files that contain tests to run via JUnit.
    """,
    examples = None,
    further = None,
    attrs = (
        # @unsorted-dict-items
        buck.inject_test_env_arg()
        | {
            "resources": attrs.list(
                attrs.source(),
                default = [],
                doc = """
                Same as `kotlin_library()`.
            """,
            ),
            "srcs": attrs.list(
                attrs.source(),
                default = [],
                doc = """
                Like ``kotlin_library()``,
                 all of the `.kt` files specified by the
                 `srcs` argument will be compiled when this rule is
                 built. In addition, all of the corresponding `.class` files that are built by this rule will be passed as arguments to
                 JUnit when this rule is run as a test. `.class` files
                 that are passed to JUnit that do not have any methods annotated with
                 `@Test` are considered failed tests, so make sure that
                 only test case classes are specified as `srcs`. This is
                 frequently done by specifying `srcs` as
                 `glob(['**/*Test.kt'])`.
            """,
            ),
        }
        | buck.test_label_arg()
        | {
            "deps": attrs.list(
                attrs.dep(),
                default = [],
                doc = """
                Same as `kotlin_library()`.
                 // org.junit.rules.Timeout was not introduced until 4.7.
                 Must include JUnit (version 4.7 or later) as a dependency for JUnit tests.
                 Must include TestNG (version 6.2 or later) and hamcrest as a dependencies for TestNG tests.
            """,
            ),
            "test_type": attrs.option(
                attrs.enum(TestType),
                default = None,
                doc = """
                Specifies which test framework to use.
                 The currently supported options are 'junit' and 'testng'.
            """,
            ),
        }
        | buck.run_test_separately_arg(run_test_separately_type = attrs.bool(default = False))
        | re_test_common.test_args()
        | buck.test_rule_timeout_ms()
        | {
            "vm_args": attrs.list(
                attrs.arg(),
                default = [],
                doc = """
                Runtime arguments to the JVM running the tests.
            """,
            ),
        }
        | jvm_common.annotation_processors()
        | jvm_common.enable_used_classes()
        | jvm_common.incremental()
        | jvm_common.classic_java_content_based_paths()
        | jvm_common.kotlincd_content_based_paths()
        | jvm_common.javac()
        | jvm_common.k2()
        | jvm_common.kotlin_compiler_plugins()
        | jvm_common.plugins()
        | jvm_common.test_env()
        | jvm_common.abi_generation_mode()
        | {
            "cxx_library_allowlist": attrs.list(
                attrs.dep(),
                default = [],
                doc = """
                List of cxx_library targets to build, if use_cxx_libraries is true.
                This can be useful if some dependencies are Android-only and won't build for the test host platform.
            """,
            ),
            "deps_query": attrs.option(attrs.query(), default = None),
            "discover_all_test_classes": attrs.bool(default = False),
            "exported_deps": attrs.list(attrs.dep(), default = []),
            "exported_provided_deps": attrs.list(attrs.dep(), default = []),
            "extra_arguments": attrs.list(attrs.string(), default = []),
            "extra_kotlinc_arguments": attrs.list(attrs.arg(anon_target_compatible = True), default = []),
            "friend_paths": jvm_common.friend_paths_attr(),
            "java": attrs.option(attrs.dep(), default = None),
            "java_agents": attrs.list(attrs.source(), default = []),
            "java_version": attrs.option(attrs.string(), default = None),
            "jdk_release": attrs.option(attrs.string(), default = None),
            "manifest_file": attrs.option(attrs.source(), default = None),
            "maven_coords": attrs.option(attrs.string(), default = None),
            "no_x_jdk_release": attrs.bool(default = False, doc = _no_x_jdk_release_doc),
            "proguard_config": attrs.option(attrs.source(), default = None),
            "provided_deps": attrs.list(attrs.dep(), default = []),
            "remove_classes": attrs.list(attrs.regex(), default = []),
            "required_for_source_only_abi": attrs.bool(default = False),
            "resources_root": attrs.option(attrs.string(), default = None),
            "runtime_deps": attrs.list(attrs.dep(), default = []),
            "source": attrs.option(attrs.string(), default = None),
            "source_only_abi_deps": attrs.list(attrs.dep(), default = []),
            "supports_test_execution_caching": attrs.bool(default = False),
            "target": attrs.option(attrs.string(), default = None),
            "test_case_timeout_ms": attrs.option(attrs.int(), default = None),
            "test_class_names_file": attrs.option(attrs.source(), default = None),
            "test_info_type": attrs.option(attrs.string(), default = None),
            "unbundled_resources_root": attrs.option(attrs.source(allow_directory = True), default = None),
            "use_cxx_libraries": attrs.option(attrs.bool(), default = None),
            "_build_only_native_code": attrs.default_only(attrs.bool(default = is_build_only_native_code())),
            "_exec_os_type": buck.exec_os_type_arg(),
            "_is_building_android_binary": attrs.default_only(attrs.bool(default = False)),
            "_java_test_toolchain": toolchains_common.java_test(),
            "_java_toolchain": toolchains_common.java(),
            "_kotlin_toolchain": toolchains_common.kotlin(),
        }
        | buck.licenses_arg()
        | buck.contacts_arg()
        | test_common.attributes()
    ),
)

kotlin_rules = struct(
    kotlin_library = kotlin_library,
    kotlin_test = kotlin_test,
)