// Copyright (C) 2017 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.

// This is what we want to do:
//  event_logtags = $(shell
//    sed -n
//        "s/^\([0-9]*\)[ \t]*$1[ \t].*/-D`echo $1 | tr a-z A-Z`_LOG_TAG=\1/p"
//        $(LOCAL_PATH)/$2/event.logtags)
//  event_flag := $(call event_logtags,auditd)
//  event_flag += $(call event_logtags,logd)
//  event_flag += $(call event_logtags,tag_def)
// so make sure we do not regret hard-coding it as follows:
package {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

event_flag = [
    "-DAUDITD_LOG_TAG=1003",
    "-DCHATTY_LOG_TAG=1004",
    "-DTAG_DEF_LOG_TAG=1005",
    "-DLIBLOG_LOG_TAG=1006",
]

cc_defaults {
    name: "logd_defaults",

    shared_libs: [
        "libbase",
        "libz",
    ],
    static_libs: ["libzstd"],
    header_libs: ["libcutils_headers"],
    cflags: [
        "-Wextra",
        "-Wthread-safety",
    ] + event_flag,

    lto: {
        thin: true,
    },
    sanitize: {
        cfi: true,
    },
}

cc_library_static {
    name: "liblogd",
    defaults: ["logd_defaults"],
    host_supported: true,
    srcs: [
        "ChattyLogBuffer.cpp",
        "CompressionEngine.cpp",
        "LogBufferElement.cpp",
        "LogReaderList.cpp",
        "LogReaderThread.cpp",
        "LogSize.cpp",
        "LogStatistics.cpp",
        "LogTags.cpp",
        "LogdLock.cpp",
        "PruneList.cpp",
        "SerializedFlushToState.cpp",
        "SerializedLogBuffer.cpp",
        "SerializedLogChunk.cpp",
        "SimpleLogBuffer.cpp",
    ],
    static_libs: ["liblog"],
    logtags: ["event.logtags"],

    export_include_dirs: ["."],
}

cc_binary {
    name: "logd",
    defaults: ["logd_defaults"],
    init_rc: ["logd.rc"],

    srcs: [
        "main.cpp",
        "LogPermissions.cpp",
        "CommandListener.cpp",
        "LogListener.cpp",
        "LogReader.cpp",
        "LogAudit.cpp",
        "LogKlog.cpp",
        "libaudit.cpp",
        "PkgIds.cpp",
    ],

    static_libs: [
        "liblog",
        "liblogd",
    ],

    shared_libs: [
        "libsysutils",
        "libcutils",
        "libpackagelistparser",
        "libprocessgroup",
        "libcap",
    ],
}

cc_binary {
    name: "auditctl",

    srcs: [
        "auditctl.cpp",
        "libaudit.cpp",
    ],

    shared_libs: ["libbase"],

    cflags: [
        "-Wextra",
    ],
}

prebuilt_etc {
    name: "logtagd.rc",
    src: "logtagd.rc",
    sub_dir: "init",
}

// -----------------------------------------------------------------------------
// Unit tests.
// -----------------------------------------------------------------------------

cc_defaults {
    name: "logd-unit-test-defaults",

    cflags: [
        "-fstack-protector-all",
        "-g",
        "-Wall",
        "-Wthread-safety",
        "-Wextra",
        "-Werror",
        "-fno-builtin",
    ] + event_flag,

    srcs: [
        "ChattyLogBufferTest.cpp",
        "logd_test.cpp",
        "LogBufferTest.cpp",
        "SerializedLogBufferTest.cpp",
        "SerializedLogChunkTest.cpp",
        "SerializedFlushToStateTest.cpp",
    ],
    sanitize: {
        cfi: true,
    },
    static_libs: [
        "libbase",
        "libcutils",
        "liblog",
        "liblogd",
        "libselinux",
        "libz",
        "libzstd",
    ],
}

// Build tests for the logger. Run with:
//   adb shell /data/nativetest/logd-unit-tests/logd-unit-tests
cc_test {
    name: "logd-unit-tests",
    host_supported: true,
    defaults: ["logd-unit-test-defaults"],
    test_suites: [
        "general-tests",
    ],
}

cc_test {
    name: "CtsLogdTestCases",
    defaults: ["logd-unit-test-defaults"],
    multilib: {
        lib32: {
            suffix: "32",
        },
        lib64: {
            suffix: "64",
        },
    },
    test_suites: [
        "cts",
        "device-tests",
    ],
    test_config: "device_test_config.xml",
}

cc_binary {
    name: "replay_messages",
    defaults: ["logd_defaults"],
    host_supported: true,

    srcs: [
        "ReplayMessages.cpp",
    ],

    static_libs: [
        "libbase",
        "libcutils",
        "liblog",
        "liblogd",
        "libselinux",
        "libz",
        "libzstd",
    ],
}