// Note that some host libraries have the same module name as the target
// libraries. This is currently needed to build, for example, adb. But it's
// probably something that should be changed.

// Pull in the autogenerated sources modules
build = ["sources.bp"]

// Used by libcrypto, libssl, bssl tool, and native tests
cc_defaults {
    name: "boringssl_flags",
    vendor_available: true,

    cflags: [
        "-fvisibility=hidden",
        "-DBORINGSSL_SHARED_LIBRARY",
        "-DBORINGSSL_ANDROID_SYSTEM",
        "-DOPENSSL_SMALL",
        "-D_XOPEN_SOURCE=700",
        "-Werror",
        "-Wno-unused-parameter",
    ],

    cppflags: [
        "-Wall",
        "-Werror",
    ],

    conlyflags: ["-std=c99"],

    // Build BoringSSL and its tests against the same STL.
    sdk_version: "9",
    target: {
        android: {
            stl: "libc++_static",
        },
    },
}

// Used by libcrypto + libssl
cc_defaults {
    name: "boringssl_defaults",

    local_include_dirs: ["src/include"],
    export_include_dirs: ["src/include"],
    cflags: ["-DBORINGSSL_IMPLEMENTATION"],
}

//// libcrypto

// This should be removed when clang can compile everything.
libcrypto_sources_no_clang = [
    "linux-arm/crypto/fipsmodule/aes-armv4.S",
    "linux-arm/crypto/fipsmodule/bsaes-armv7.S",
]

cc_defaults {
    name: "libcrypto_defaults",
    host_supported: true,

    // Windows and Macs both have problems with assembly files
    target: {
        windows: {
            enabled: true,
            cflags: ["-DOPENSSL_NO_ASM"],
            host_ldlibs: ["-lws2_32"],
        },
        darwin: {
            cflags: ["-DOPENSSL_NO_ASM"],
        },
        host: {
            host_ldlibs: ["-lpthread"],
        },
    },

    local_include_dirs: ["src/crypto"],

    arch: {
        arm64: {
            clang_asflags: ["-march=armv8-a+crypto"],
        },
    },

    // This should be removed when clang can compile everything.
    exclude_srcs: libcrypto_sources_no_clang,
    whole_static_libs: ["libcrypto_no_clang"],
}

// Target and host library
cc_library {
    name: "libcrypto",
    vendor_available: true,
    vndk: {
        enabled: true,
    },
    double_loadable: true,
    recovery_available: true,
    defaults: ["libcrypto_sources", "libcrypto_defaults", "boringssl_defaults", "boringssl_flags"],
    unique_host_soname: true,
}

// Target and host library: files that don't compile with clang. This should
// go away when clang can compile everything with integrated assembler.
cc_library_static {
    name: "libcrypto_no_clang",
    defaults: ["boringssl_defaults", "boringssl_flags"],
    host_supported: true,
    recovery_available: true,

    target: {
        windows: {
            enabled: true,
        },
    },

    local_include_dirs: ["src/crypto"],

    arch: {
        arm: {
            clang_asflags: ["-no-integrated-as"],
            srcs: libcrypto_sources_no_clang,
        },
    },
}

// Static library
// This should only be used for host modules that will be in a JVM, all other
// modules should use the static variant of libcrypto.
cc_library_static {
    name: "libcrypto_static",
    defaults: ["libcrypto_sources", "libcrypto_defaults", "boringssl_defaults", "boringssl_flags"],

    target: {
        host: {
            // TODO: b/26160319. ASAN breaks use of this library in JVM.
            // Re-enable sanitization when the issue with making clients of this library
            // preload ASAN runtime is resolved. Without that, clients are getting runtime
            // errors due to unresolved ASAN symbols, such as
            // __asan_option_detect_stack_use_after_return.
            sanitize: {
                never: true,
            },
        },
    },
}

//// libssl

// Target static library
// Deprecated: all users should move to libssl
cc_library_static {
    name: "libssl_static",
    defaults: ["libssl_sources", "boringssl_defaults", "boringssl_flags"],
}

// Static and Shared library
cc_library {
    name: "libssl",
    recovery_available: true,
    vendor_available: true,
    vndk: {
        enabled: true,
    },
    host_supported: true,
    defaults: ["libssl_sources", "boringssl_defaults", "boringssl_flags"],
    unique_host_soname: true,

    shared_libs: ["libcrypto"],
}

// Tool
cc_binary {
    name: "bssl",
    host_supported: true,
    defaults: ["bssl_sources", "boringssl_flags"],

    shared_libs: [
        "libcrypto",
        "libssl",
    ],
    target: {
        darwin: {
            enabled: false,
        },
    },
}

cc_binary {
    name: "cavp",
    host_supported: true,
    srcs: [
        "src/fipstools/cavp_aes_gcm_test.cc",
        "src/fipstools/cavp_aes_test.cc",
        "src/fipstools/cavp_ctr_drbg_test.cc",
        "src/fipstools/cavp_ecdsa2_keypair_test.cc",
        "src/fipstools/cavp_ecdsa2_pkv_test.cc",
        "src/fipstools/cavp_ecdsa2_siggen_test.cc",
        "src/fipstools/cavp_ecdsa2_sigver_test.cc",
        "src/fipstools/cavp_hmac_test.cc",
        "src/fipstools/cavp_kas_test.cc",
        "src/fipstools/cavp_keywrap_test.cc",
        "src/fipstools/cavp_main.cc",
        "src/fipstools/cavp_rsa2_keygen_test.cc",
        "src/fipstools/cavp_rsa2_siggen_test.cc",
        "src/fipstools/cavp_rsa2_sigver_test.cc",
        "src/fipstools/cavp_sha_monte_test.cc",
        "src/fipstools/cavp_sha_test.cc",
        "src/fipstools/cavp_tdes_test.cc",
        "src/fipstools/cavp_test_util.cc",
        "src/fipstools/cavp_tlskdf_test.cc",
    ],

    shared_libs: [
        "libcrypto",
    ],

    defaults: ["boringssl_test_support_sources", "boringssl_flags"],
}

// Test support library
cc_library_static {
    name: "boringssl_test_support",
    host_supported: true,
    defaults: ["boringssl_test_support_sources", "boringssl_flags"],

    shared_libs: [
        "libcrypto",
        "libssl",
    ],
}

// Tests
cc_test {
  name: "boringssl_crypto_test",
  test_suites: ["device-tests"],
  host_supported: true,
  defaults: ["boringssl_crypto_test_sources", "boringssl_flags"],
  whole_static_libs: ["boringssl_test_support"],

  shared_libs: ["libcrypto"],
}

cc_test {
  name: "boringssl_ssl_test",
  test_suites: ["device-tests"],
  host_supported: true,
  defaults: ["boringssl_ssl_test_sources", "boringssl_flags"],
  whole_static_libs: ["boringssl_test_support"],

  shared_libs: ["libcrypto", "libssl"],
}
