Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e2009c7d authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

Host tools support

This adds support for the dumpsys bundler tool (which combines multiple
flatbuffer schemas) and the packetgen tool (which converts pdls to
a language parser).

Bug: 176847256
Tag: #floss
Test: atest --host bluetooth_test_gd
Change-Id: I1315979093668f60a476b85f41bb1d707995b2a7
parent a41cd707
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
#
#  Copyright 2021 Google, Inc.
#
#  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.

import("//bt/gd/dumpsys/bundler/bundler.gni")

source_set("BluetoothDumpsysSources") {
  sources = [
    "filter.cc",
    "init_flags.cc",
    "internal/filter_internal.cc",
    "reflection_schema.cc",
  ]

  cflags_cc = [ "-Wno-enum-compare-switch" ]

  configs += [ "//bt/gd:gd_defaults" ]
  deps = [ "//bt/gd:gd_default_deps" ]
}

bt_flatc_bundler("BluetoothGeneratedDumpsysBundledSchema_h") {
  root_name = "bluetooth.DumpsysData"
  filename = "generated_dumpsys_bundled_schema"
  namespace = "bluetooth::dumpsys"
  deps = [ "//bt/gd:BluetoothGeneratedDumpsysBinarySchema_bfbs" ]
}
+30 −5
Original line number Diff line number Diff line
@@ -13,14 +13,39 @@
#  See the License for the specific language governing permissions and
#  limitations under the License.

import("//bt/gd/dumpsys/bundler/bundler.gni")
import("//common-mk/flatbuffer.gni")

config("bundler_generated_config") {
  include_dirs = [ "$target_gen_dir" ]
bt_flatc_binary_schema("BluetoothGeneratedBundlerSchema_h_bfbs") {
  sources = [ "bundler.fbs" ]
  include_dir = "bt/gd"
  gen_header = true
}

flatbuffer("bundler_generated") {
  sources = [ "bundler.fbs" ]
#
# The remaining rules are for building on the host
#

config("bundler_defaults") {
  cflags = [ "-fPIC" ]

  cflags_cc = [
    "-std=c++17",
    "-Wno-unused-parameter",
    "-Wno-unused-variable",
    "-Wno-poison-system-directories",
  ]
}

executable("bluetooth_flatbuffer_bundler") {
  sources = [
    "bundler.cc",
    "main.cc",
  ]

  libs = [ "flatbuffers" ]

  deps = [ ":BluetoothGeneratedBundlerSchema_h_bfbs" ]

  all_dependent_configs = [ ":bundler_generated_config" ]
  configs += [ ":bundler_defaults" ]
}
+180 −0
Original line number Diff line number Diff line
#
#  Copyright 2021 Google, Inc.
#
#  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.

# Generate bundled flat buffers
#
# Args:
#   include_dir: Path to include directory
#   sources: Flatbuffer source files
#   gen_header [optional]: Whether to generate headers.
template("bt_flatc_binary_schema") {
  action_name = "${target_name}_gen"
  action(action_name) {
    forward_variables_from(invoker,
                           [
                             "include_dir",
                             "sources",
                           ])
    assert(defined(include_dir), "include_dir must be set")
    assert(defined(sources), "sources must be set")

    gen_header = defined(invoker.gen_header) && invoker.gen_header

    script = "//common-mk/file_generator_wrapper.py"
    binfile = "flatc"
    args = [
      binfile,
      "-I",
      "${include_dir}",
      "-b",
      "--schema",
      "-o",
      "${target_gen_dir}",
    ]

    bfbs_names = []
    srclist = []
    outputs = []

    foreach(s, sources) {
      srclist += [ rebase_path(s) ]

      # bfbs get generated into ${target_gen_root} directory
      name = string_replace(get_path_info(s, "file"), ".fbs", ".bfbs")
      bfbs_names += [ "${target_gen_dir}/${name}" ]
      outputs += [ "${target_gen_dir}/${name}" ]

      # headers get generated ito subdirectories based on relative path
      if (gen_header) {
        header_name = string_replace(s, ".fbs", ".h")
        outputs += [ "${target_gen_dir}/${header_name}" ]
      }
    }

    # Generate header file as well
    if (gen_header) {
      args += [ "--cpp" ]
    }

    # Actual input files at the end
    args += srclist

    metadata = {
      bfbs_outputs = bfbs_names
    }
  }

  all_dependent_config_name = "_${target_name}_all_dependent_config"
  config(all_dependent_config_name) {
    # Since each header will be generated into a subdirectory, add them to the
    # include dirs as well
    gen_dirs = []
    foreach(s, invoker.sources) {
      gen_dirs += [ get_path_info(s, "gen_dir") ]
    }

    include_dirs = [ "${target_gen_dir}" ] + gen_dirs
  }

  generated_file(target_name) {
    outputs = [ "${target_gen_dir}/${target_name}.files" ]
    output_conversion = "list lines"
    data_keys = [ "bfbs_outputs" ]

    all_dependent_configs = [ ":${all_dependent_config_name}" ]
    if (defined(invoker.all_dependent_configs)) {
      all_dependent_configs += invoker.all_dependent_configs
    }

    deps = [ ":${action_name}" ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    if (defined(invoker.configs)) {
      configs += invoker.configs
    }
  }
}

# Generate bundled header
template("bt_flatc_bundler") {
  action_name = "${target_name}_gen"
  action(action_name) {
    forward_variables_from(invoker, [ "deps" ])
    assert(defined(deps), "deps must be set")
    assert(defined(invoker.root_name), "root_name must be set")
    assert(defined(invoker.filename), "filename must be set")
    assert(defined(invoker.namespace), "namespace must be set")

    files_list = []
    foreach(s, deps) {
      name = get_label_info(s, "name")
      gen_dir = get_label_info(s, "target_gen_dir")
      files_list += [ "${gen_dir}/${name}.files" ]
    }

    script = "//bt/gd/dumpsys/bundler/extract_files_and_call.py"
    binfile = "${root_out_dir}/bluetooth_flatbuffer_bundler"
    args = files_list
    args += [
      "--",
      binfile,
      "-w",
      "-m",
      "${invoker.root_name}",
      "-f",
      "${invoker.filename}",
      "-n",
      "${invoker.namespace}",
      "-g",
      "${target_gen_dir}",
    ]

    outputs = [
      "${target_gen_dir}/${invoker.filename}.h",
      "${target_gen_dir}/${invoker.filename}",
    ]

    metadata = {
      all_outputs = outputs
    }
  }

  all_dependent_config_name = "_${target_name}_all_dependent_config"
  config(all_dependent_config_name) {
    include_dirs = [ "${target_gen_dir}" ]
  }

  generated_file(target_name) {
    outputs = [ "${target_gen_dir}/${target_name}.files" ]
    output_conversion = "list lines"
    data_keys = [ "all_outputs" ]

    all_dependent_configs = [ ":${all_dependent_config_name}" ]
    if (defined(invoker.all_dependent_configs)) {
      all_dependent_configs += invoker.all_dependent_configs
    }

    deps = [ ":${action_name}" ]
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }

    if (defined(invoker.configs)) {
      configs += invoker.configs
    }
  }
}
+58 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  Copyright 2021 Google, Inc.
#
#  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.
""" Get contents of given files and pass as args to remaining params.

Example:
  a.files = [ "foo", "bar" ]
  b.files = [ "fizz", "buzz" ]

  extract_files_and_call.py a.files b.files -- somebin -a --set foo -c -o

  will result in this call:

  somebin -a --set foo -c -o foo bar fizz buzz

"""

from __future__ import print_function

import subprocess
import sys


def file_to_args(filename):
    """ Read file and return lines with empties removed.
    """
    with open(filename, 'r') as f:
        return [x.strip() for x in f.readlines() if x.strip()]


def main():
    file_contents = []
    args = []
    for i in range(1, len(sys.argv) - 1):
        if sys.argv[i] == '--':
            args = sys.argv[i + 1:] + file_contents
            break
        else:
            file_contents.extend(file_to_args(sys.argv[i]))

    subprocess.check_call(args)


if __name__ == "__main__":
    main()
+87 −0
Original line number Diff line number Diff line
#
#  Copyright 2021 Google, Inc.
#
#  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.
#

import("//common-mk/bison.gni")
import("//common-mk/flex.gni")

config("pktgen_configs") {
  include_dirs = [ "//bt/gd/packet/parser" ]

  cflags = [ "-fPIC" ]

  cflags_cc = [
    "-std=c++17",
    "-Wno-inconsistent-missing-override",
    "-Wno-implicit-fallthrough",
    "-Wno-poison-system-directories",
    "-Wno-unknown-warning-option",
  ]
}

executable("bluetooth_packetgen") {
  sources = [
    "checksum_def.cc",
    "custom_field_def.cc",
    "enum_def.cc",
    "enum_gen.cc",
    "fields/array_field.cc",
    "fields/body_field.cc",
    "fields/checksum_field.cc",
    "fields/checksum_start_field.cc",
    "fields/count_field.cc",
    "fields/custom_field.cc",
    "fields/custom_field_fixed_size.cc",
    "fields/enum_field.cc",
    "fields/fixed_enum_field.cc",
    "fields/fixed_field.cc",
    "fields/fixed_scalar_field.cc",
    "fields/group_field.cc",
    "fields/packet_field.cc",
    "fields/padding_field.cc",
    "fields/payload_field.cc",
    "fields/reserved_field.cc",
    "fields/scalar_field.cc",
    "fields/size_field.cc",
    "fields/struct_field.cc",
    "fields/variable_length_struct_field.cc",
    "fields/vector_field.cc",
    "gen_cpp.cc",
    "gen_rust.cc",
    "main.cc",
    "packet_def.cc",
    "parent_def.cc",
    "struct_def.cc",
    "struct_parser_generator.cc",
  ]

  include_dirs = [ "//bt/gd/packet/parser" ]

  deps = [
    ":pktlexer",
    ":pktparser",
  ]
  configs += [ ":pktgen_configs" ]
}

flex_source("pktlexer") {
  sources = [ "language_l.ll" ]
  configs = [ ":pktgen_configs" ]
}

bison_source("pktparser") {
  sources = [ "language_y.yy" ]
  configs = [ ":pktgen_configs" ]
}
Loading