// Copyright 2024 The Pigweed Authors
//
// 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
//
//     https://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.

package {
    default_applicable_licenses: ["external_pigweed_license"],
}

filegroup {
    name: "pw_bluetooth_emboss_definitions",
    srcs: [
        "public/pw_bluetooth/att.emb",
        "public/pw_bluetooth/hci_android.emb",
        "public/pw_bluetooth/hci_commands.emb",
        "public/pw_bluetooth/hci_common.emb",
        "public/pw_bluetooth/hci_data.emb",
        "public/pw_bluetooth/hci_events.emb",
        "public/pw_bluetooth/hci_h4.emb",
        "public/pw_bluetooth/l2cap_frames.emb",
    ],
}

// Generates an emboss header file for a pw_bluetooth emboss definition. All
// files imported by the emboss definition must have the pw_bluetooth prefix:
//
// import "pw_bluetooth/<import_file_name>.emb"
//
// Since the rule does not know the path of the import files, it copies all
// input files into a temp $(genDir)/pw_bluetooth directory and uses $(genDir)
// as the import-dir.
//
// $(in) Emboss definition source file for which to generate the header file
// followed by any .emb imports of the source file.
// $(out) The generated header file.
genrule_defaults {
    name: "pw_bluetooth_generate_emboss_header",
    cmd: "in_files=($(in)) && prefix=pw_bluetooth && " +
        "mkdir -p $${prefix} && cp -t $${prefix} $(in) && " +
        "python3 $(location embossc_runner_py) " +
        "$(location :embossc_script) " +
        "--generate cc " +
        "--no-cc-enum-traits " +
        "--import-dir=$(genDir) " +
        "--output-file=$(out) " +
        "$${in_files[0]}",
    tools: [
        "embossc_runner_py",
    ],
    tool_files: [
        ":embossc_script",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_att",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/att.emb",
        "public/pw_bluetooth/hci_data.emb",
        "public/pw_bluetooth/l2cap_frames.emb",
    ],
    out: [
        "pw_bluetooth/att.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_hci_android",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/hci_android.emb",
        "public/pw_bluetooth/hci_common.emb",
    ],
    out: [
        "pw_bluetooth/hci_android.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_hci_commands",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/hci_commands.emb",
        "public/pw_bluetooth/hci_common.emb",
    ],
    out: [
        "pw_bluetooth/hci_commands.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_hci_common",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/hci_common.emb",
    ],
    out: [
        "pw_bluetooth/hci_common.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_hci_data",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/hci_data.emb",
    ],
    out: [
        "pw_bluetooth/hci_data.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_hci_events",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        // Separate out here to ensure order - hci_events.emb needs to be
        // generated first or the build will fail.
        "public/pw_bluetooth/hci_events.emb",

        "public/pw_bluetooth/hci_common.emb",
    ],
    out: [
        "pw_bluetooth/hci_events.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_hci_h4",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/hci_h4.emb",
    ],
    out: [
        "pw_bluetooth/hci_h4.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_l2cap_frames",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/l2cap_frames.emb",
    ],
    out: [
        "pw_bluetooth/l2cap_frames.emb.h",
    ],
}

genrule {
    name: "pw_bluetooth_generate_emboss_rfcomm_frames",
    defaults: [
        "pw_bluetooth_generate_emboss_header",
    ],
    srcs: [
        "public/pw_bluetooth/rfcomm_frames.emb",
    ],
    out: [
        "pw_bluetooth/rfcomm_frames.emb.h",
    ],
}

cc_library_headers {
    name: "pw_bluetooth_emboss_group",
    defaults: [
        "pw_android_common_target_support",
    ],
    export_generated_headers: [
        "pw_bluetooth_generate_emboss_att",
        "pw_bluetooth_generate_emboss_hci_android",
        "pw_bluetooth_generate_emboss_hci_commands",
        "pw_bluetooth_generate_emboss_hci_common",
        "pw_bluetooth_generate_emboss_hci_data",
        "pw_bluetooth_generate_emboss_hci_events",
        "pw_bluetooth_generate_emboss_hci_h4",
        "pw_bluetooth_generate_emboss_l2cap_frames",
        "pw_bluetooth_generate_emboss_rfcomm_frames",
    ],
    generated_headers: [
        "pw_bluetooth_generate_emboss_att",
        "pw_bluetooth_generate_emboss_hci_android",
        "pw_bluetooth_generate_emboss_hci_commands",
        "pw_bluetooth_generate_emboss_hci_common",
        "pw_bluetooth_generate_emboss_hci_data",
        "pw_bluetooth_generate_emboss_hci_events",
        "pw_bluetooth_generate_emboss_hci_h4",
        "pw_bluetooth_generate_emboss_l2cap_frames",
        "pw_bluetooth_generate_emboss_rfcomm_frames",
    ],
}

cc_library_headers {
    name: "pw_bluetooth_emboss_util_include_dirs",
    defaults: [
        "pw_android_common_target_support",
    ],
    export_include_dirs: ["public"],
}

cc_library_static {
    name: "pw_bluetooth_hci_util",
    defaults: [
        "pw_android_common_backends",
        "pw_android_common_target_support",
    ],
    export_include_dirs: ["public"],
    header_libs: [
        "emboss_runtime_headers",
        "pw_bluetooth_emboss_group",
    ],
    static_libs: [
        "pw_result",
        "pw_span",
    ],
    srcs: [
        "hci_util.cc",
    ],
}
