load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")

# This is the magic function which helps get the name of the native library
# from the native dependency. In general, the bazel cc_binary rules will
# output a binary based on the target name. This macro just infers the output
# so file name.
#
# The main functionality of this method is used for integration java/android
# testing. Bazel itself doesn't play well with different genrules outputting
# the same output. In this project there's 3 types of artifacts we end up
# using: envoy's aar, integration tests with just vanilla jvm, integration
# tests with android. Each of these require a different so file built which
# means that Bazel will have to output 3 types of so files with different names.
def native_lib_name(native_dep):
    lib_name = ""
    if ":" in native_dep:
        lib_name = native_dep.split(":")[1].split(".so")[0]
    else:
        lib_name = native_dep.split(".so")[0]
    return lib_name

def envoy_mobile_kt_library(name, visibility = None, srcs = [], deps = [], exports = []):
    # These source files must be re-exported to the kotlin custom library rule to ensure their
    # inclusion. This is used to work around testing visibility.
    native.filegroup(
        name = name + "_srcs",
        srcs = srcs,
        visibility = visibility,
    )

    kt_jvm_library(
        name = name,
        srcs = srcs,
        deps = deps,
        exports = exports,
        visibility = visibility,
    )

# Basic macro which uses a genrule to generate a jnilib file from an so file
#
# The native dep passed in should be the cc_binary target of the name
# lib{}.so since linux platforms expect the .so file to be named in that form.
# The resulting output will be lib{}.jnilib for the OS X platforms.
#
# Example usage:
# envoy_mobile_so_to_jni_lib(
#     name = "java_jni_lib.jnilib",
#     native_dep = "libjava_jni_lib.so",
# )
def envoy_mobile_so_to_jni_lib(name, native_dep, testonly = False):
    lib_name = native_lib_name(native_dep)
    output = "{}.jnilib".format(lib_name)

    return native.genrule(
        name = name,
        testonly = testonly,
        outs = [output],
        srcs = [native_dep],
        cmd = """
        so_file="{}.so"
        if [ ! -f $$so_file ]; then
            dir=$$(dirname $@)
            cp $< $$dir/{}.so 2>/dev/null || :
            chmod 755 $$dir/{}.so
        fi

        cp $< $@
        chmod 755 $@
        """.replace("{}", lib_name),
    )

@@ -60,16 +60,72 @@
 ibrary%22)
+%0Aload(%22@bazel_skylib//rules:copy_file.bzl%22, %22copy_file%22)
 %0A%0A# This
@@ -2096,147 +2096,38 @@
     
-output = %22%7B%7D.jnilib%22.format(lib_name)%0A%0A    return native.genrule(%0A        name = name,%0A        testonly = testonly,%0A        outs = %5Boutput%5D
+copy_file(%0A        name = name
 ,%0A  
@@ -2135,21 +2135,19 @@
      src
-s
  = 
-%5B
 native_d
@@ -2148,17 +2148,16 @@
 tive_dep
-%5D
 ,%0A      
@@ -2162,275 +2162,71 @@
     
-cmd
+out
  = %22
-%22%22%0A        so_file=%22%7B%7D.so%22%0A        if %5B ! -f $$so_file %5D; then%0A            dir=$$(dirname $@)%0A            cp $%3C $$dir/%7B%7D.so 2%3E/dev/null %7C%7C :%0A            chmod 755 $$dir/%7B%7D.so%0A        fi%0A%0A        cp $%3C $@%0A        chmod 755 $@%0A        %22%22%22.replace(%22%7B%7D%22, lib_name)
+%7B%7D.jnilib%22.format(lib_name),%0A        testonly = testonly
 ,%0A  

simplify jnilib copy (#2214)