//
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

//
// Definitions for building the Conscrypt Java library, native code,
// and associated tests.
//

// Conscrypt is divided into modules.
//
// The structure is:
//
//   constants/
//       src/gen             # Generates NativeConstants.java.
//   common/
//       src/main/java       # Common Java source for all platforms.
//       src/jni/
//            main           # Common C++ source for all platforms.
//            unbundled      # C++ source used for OpenJDK and unbundled Android.
//   android/
//       src/main/java       # Java source for unbundled Android.
//   openjdk/
//       src/main/java       # Java source for OpenJDK.
//       src/test
//            java/          # Java source for common tests.
//            resources/     # Support files for tests
//   platform/
//       src/main/java       # Java source for bundled Android.
//       src/test
//            java/          # Java source for bundled tests.
//

cc_defaults {
    name: "conscrypt_global",

    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
        "-Wunused",
    ],

    srcs: [
        "common/src/jni/main/cpp/conscrypt/compatibility_close_monitor.cc",
        "common/src/jni/main/cpp/conscrypt/jniload.cc",
        "common/src/jni/main/cpp/conscrypt/jniutil.cc",
        "common/src/jni/main/cpp/conscrypt/native_crypto.cc",
        "common/src/jni/main/cpp/conscrypt/netutil.cc",
        "common/src/jni/main/cpp/conscrypt/trace.cc",
    ],

    local_include_dirs: [
        "common/src/jni/main/include",
    ],

    compile_multilib: "both",
    stl: "c++_static",
}

cc_defaults {
    name: "conscrypt_unbundled-jni-defaults",

    local_include_dirs: [
        "common/src/jni/unbundled/include",
    ],

    shared_libs: [
        "liblog",
    ],

    static_libs: [
        "libssl",
        "libcrypto",
    ],

    sdk_version: "9",
}

cc_library {
    name: "libconscrypt_jni",
    defaults: [
        "conscrypt_global",
        "conscrypt_unbundled-jni-defaults",
    ],
}

cc_library_host_shared {
    name: "libconscrypt_openjdk_jni",
    defaults: ["conscrypt_global"],

    cflags: [
        "-DCONSCRYPT_OPENJDK",
    ],

    local_include_dirs: [
        "common/src/jni/unbundled/include",
    ],

    static_libs: [
        "libssl",
        "libcrypto",
    ],

    // TODO: b/26097626. 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,
    },

    stl: "libc++_static",
}

cc_binary_host {
    name: "conscrypt_generate_constants",
    srcs: ["constants/src/gen/cpp/generate_constants.cc"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libcrypto",
        "libssl",
    ],
}

genrule {
    name: "conscrypt_generated_constants",
    out: ["org/conscrypt/NativeConstants.java"],
    cmd: "$(location conscrypt_generate_constants) > $(out)",
    tools: ["conscrypt_generate_constants"],
}

// Create the conscrypt library without jarjar for tests
java_library_static {
    name: "conscrypt-nojarjar",
    host_supported: true,
    hostdex: true,

    srcs: [
        "common/src/main/java/**/*.java",
        ":conscrypt_generated_constants",
    ],

    target: {
        android: {
            srcs: ["platform/src/main/java/**/*.java"],
        },
        host: {
            srcs: ["openjdk/src/main/java/**/*.java"],
            javacflags: ["-XDignore.symbol.file"],
        },
    },

    no_framework_libs: true,

    required: ["libjavacrypto"],
    java_version: "1.7",
}

// Create the conscrypt library by running jarjar on conscrypt-nojarjar
java_library {
    name: "conscrypt",
    host_supported: true,
    hostdex: true,
    no_framework_libs: true,
    static_libs: ["conscrypt-nojarjar"],
    required: ["libjavacrypto"],
    target: {
        android: {
            jarjar_rules: "jarjar-rules.txt",
        },
    },
}

// A guaranteed unstripped version of conscrypt.
// The build system may or may not strip the conscrypt jar, but this one will
// not be stripped. See b/24535627.
java_library {
    name: "conscrypt-testdex",
    no_framework_libs: true,
    required: ["libjavacrypto"],
    static_libs: ["conscrypt-nojarjar"],
    jarjar_rules: "jarjar-rules.txt",
}

// Platform conscrypt crypto JNI library
cc_defaults {
    name: "libjavacrypto-defaults",

    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
        "-Wunused",
        "-fvisibility=hidden",
    ],

    srcs: ["common/src/jni/main/cpp/**/*.cc"],
    include_dirs: [
        "libcore/luni/src/main/native",
    ],
    local_include_dirs: ["common/src/jni/main/include"],
}

// Platform conscrypt crypto JNI library
cc_library_shared {
    name: "libjavacrypto",
    host_supported: true,
    defaults: ["libjavacrypto-defaults"],

    cflags: ["-DJNI_JARJAR_PREFIX=com/android/"],
    shared_libs: [
        "libcrypto",
        "libjavacore",
        "liblog",
        "libnativehelper",
        "libssl",
    ],

    target: {
        darwin: {
            enabled: false,
        },
    },
}

// Unbundled Conscrypt jar
java_library_static {
    name: "conscrypt_unbundled",

    srcs: [
        "common/src/main/java/**/*.java",
        "android/src/main/java/**/*.java",
        ":conscrypt_generated_constants",
    ],

    libs: ["conscrypt-stubs"],

    sdk_version: "current",
    java_version: "1.7",
}

// Stub library for unbundled builds
java_library_static {
    name: "conscrypt-stubs",

    srcs: ["android-stub/src/main/java/**/*.java"],

    sdk_version: "current",
    java_version: "1.7",
}

// Static unbundled Conscrypt crypto JNI library
cc_library_static {
    name: "libconscrypt_static",
    defaults: ["libjavacrypto-defaults"],

    cflags: [
        "-DJNI_JARJAR_PREFIX=com/google/android/gms/",
        "-DCONSCRYPT_UNBUNDLED",
        "-DSTATIC_LIB",
    ],

    local_include_dirs: ["common/src/jni/unbundled/include"],

    static_libs: [
        "libssl",
        "libcrypto",
    ],
    sdk_version: "9",
    stl: "c++_shared",
}