Buck: kotlin_library()
Support Ukraine. Help Provide Humanitarian Aid to Ukraine.

kotlin_library()

This is liable to change in the future.

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.

Arguments

  • name (required) #

    The short name for this build target.

  • srcs (defaults to []) #

    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.

  • resources (defaults to []) #

    Static files to include with the compiled .class files. These files can be loaded via Class.getResource().

    Note: If resources_root isn't set, Buck uses the [java].src_rootsproperty in .buckconfig to determine where resources should be placed within the generated JAR file.

  • resources_root (defaults to None) #

    The path that resources are resolved against. For example, if resources_root is "res" and resources contains the file "res/com/example/foo.txt", that file will end up as "com/example/foo.txt" in the output JAR. This parameter overrides the [java].src_roots property in .buckconfig.

  • deps (defaults to []) #

    Rules (usually other kotlin_library rules) that are used to generate the classpath required to compile this kotlin_library.

  • kotlinc_plugins (defaults to []) #

    Use this to specify Kotlin compiler plugins to use when compiling this library. This takes a list of source paths, each of which will be prefixed with -Xplugin= and passed as extra arguments to the compiler. Unlike free_compiler_args, these can be source paths, not just strings (though free_compiler_args should be used to specify plugin compiler options with -P).

    For example, if you want to use kotlinx.serialization with kotlin_library(), you need to specify kotlinx-serialization-compiler-plugin.jar under kotlinc_plugins and kotlinx-serialization-runtime.jar (which you may have to fetch from Maven) in your deps:

    kotlin_library(
        name = "example",
        srcs = glob(["*.kt"]),
        deps = [
            ":kotlinx-serialization-runtime",
        ],
        kotlinc_plugins = [
            # Likely copied from your $KOTLIN_HOME directory.
            "kotlinx-serialization-compiler-plugin.jar",
        ],
    )
    
    prebuilt_jar(
        name = "kotlinx-serialization-runtime",
        binary_jar = ":kotlinx-serialization-runtime-0.10.0",
    )
    
    # Note you probably want to set
    # maven_repo=http://jcenter.bintray.com/ in your .buckconfig until
    # https://github.com/Kotlin/kotlinx.serialization/issues/64
    # is closed.
    remote_file(
        name = "kotlinx-serialization-runtime-0.10.0",
        out = "kotlinx-serialization-runtime-0.10.0.jar",
        url = "mvn:org.jetbrains.kotlinx:kotlinx-serialization-runtime:jar:0.10.0",
        sha1 = "23d777a5282c1957c7ce35946374fff0adab114c"
    )
    

  • free_compiler_args (defaults to []) #

    A list of additional compiler arguments.

  • all_warnings_as_errors (defaults to false) #

    Report an error if there are any warnings.

  • suppress_warnings (defaults to false) #

    Generate no warnings.

  • verbose (defaults to false) #

    Enable verbose logging output.

  • include_runtime (defaults to false) #

    Include Kotlin runtime in to resulting .jar

  • jvm_target (defaults to 1.6) #

    Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6 Possible values: "1.6", "1.8"

  • jdk_home (defaults to None) #

    Path to JDK home directory to include into classpath, if differs from default JAVA_HOME

  • no_jdk (defaults to false) #

    Don't include Java runtime into classpath.

  • no_stdlib (defaults to true) #

    Don't include kotlin-stdlib.jar or kotlin-reflect.jar into classpath.

  • no_reflect (defaults to true) #

    Don't include kotlin-reflect.jar into classpath.

  • java_parameters (defaults to false) #

    Generate metadata for Java 1.8 reflection on method parameters.

  • api_version (defaults to None) #

    Allow to use declarations only from the specified version of bundled libraries. Possible values: "1.0", "1.1", "1.2", "1.3", "1.4 (EXPERIMENTAL)".

  • language_version (defaults to None) #

    Provide source compatibility with specified language version. Possible values: "1.0", "1.1", "1.2", "1.3", "1.4 (EXPERIMENTAL)".

  • friend_paths (defaults to []) #

    List of source paths to pass into the Kotlin compiler as friend-paths, that is, modules you can have access to internal methods.

  • annotation_processing_tool (defaults to kapt) #

    Specifies the tool to use for annotation processing. Possible values: "kapt" or "javac". "kapt" allows running Java annotation processors against Kotlin sources while backporting it for Java sources too. "javac" works only against Java sources, Kotlin sources won't have access to generated classes at compile time.

  • kapt_ap_options (required) #

    Map of annotation processor options to pass into kapt via the apoptions plugin option. Each entry should be a key value pair of the processor option and its value. Default is an empty map. E.g. kapt_ap_options = { 'someAnnotationOption': 'someValue' } More information here: https://kotlinlang.org/docs/reference/kapt.html

  • remove_classes (defaults to []) #

    Specifies a list of Patterns that are used to excludeclasses from the JAR. The pattern matching is based on the name of the class. This can be used to exclude a member class or delete a local view of a class that will be replaced during a later stage of the build.

  • exported_deps (defaults to []) #

    Other kotlin_library rules that depend on this rule will also include its exported_deps in their classpaths. This is useful when the public API of a rule has return types or checked exceptions that are defined in another rule, which would otherwise require callers to add an extra dependency. It's also useful for exposing e.g. a collection of prebuilt_jar rules as a single target for callers to depend on. Targets in exported_deps are implicitly included in the deps of this rule, so they don't need to be repeated there.

  • provided_deps (defaults to []) #

    These represent dependencies that are known to be provided at run time, but are required in order for the code to compile. Examples of provided_deps include the JEE servlet APIs. When this rule is included in a kotlin_library, the provided_deps will not be packaged into the output.

  • exported_provided_deps (defaults to []) #

    This is a combination of provided_deps and exported_deps. Rules listed in this parameter will be added to classpath of rules that depend on this rule, but they will not be included in a binary if binary depends on a such target.

  • labels (defaults to []) #

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query attrfilter().

  • tests (defaults to []) #

    List of build targets that identify tests that exercise this target.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their deps or exported_deps attributes. For more information, see visibility.

  • licenses (defaults to []) #

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query.

  • labels (defaults to []) #

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query attrfilter().

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',
  ],
)