Kconfig flags generation

kconfig.bzl parses Zephyr's Kconfig tree and generates one Bazel config_setting for every boolean-type Kconfig symbol defined. These config_settings are written to the BUILD.bazel file of the generated @kconfig repository. They look like the following:

  bool_flag(
      name = "CONFIG_ARM",
      build_setting_default=False,
      visibility = ["//visibility:public"],
  )
  config_setting(
      name = "CONFIG_ARM=true",
      flag_values = {
          ":CONFIG_ARM": "true",
      },
  )

  bool_flag(
      name = "CONFIG_DEBUG",
      build_setting_default=False,
      visibility = ["//visibility:public"],
  )
  config_setting(
      name = "CONFIG_DEBUG=true",
      flag_values = {
          ":CONFIG_DEBUG": "true",
      },
  )

In addition, kconfig.bzl also generates Bazel flags for string-type, int-type and hex-type Kconfig symbols, for example:

  string_flag(
      name = "CONFIG_BOOT_BANNER_STRING",
      build_setting_default="",
      visibility = ["//visibility:public"],
  )

  int_flag(
      name = "CONFIG_FLASH_INIT_PRIORITY",
      build_setting_default=0,
      visibility = ["//visibility:public"],
  )

Kconfig flags usage

First, the boolean-type Kconfig symbols become config_settings that are used to conditionally compile source files in Zephyr. For example, to conditionally compile multithreading-related code in Zephyr kernel:

  cc_library(
      name = "kernel",
      srcs = [
          "banner.c",
          "busy_wait.c",
          "device.c",
          <other unconditional sources>
      ] + select({
          "//conditions:default": [],
          "@kconfig//:CONFIG_MULTITHREADING=true": [
              "idle.c",
              "mailbox.c",
              "msg_q.c",
              "mutex.c",
              "queue.c",
              "sem.c",
              "stack.c",
              "system_work_q.c",
              "work.c",
              "condvar.c",
              "priority_queues.c",
              "thread.c",
              "sched.c",
          ],
      }) + <other selects>
      ...
  )

Besides source file selection, all flags (including string_flags and int_flags) are converted to C macros like #define CONFIG_FLASH_INIT_PRIORITY 40 and #define CONFIG_BOOT_BANNER_STRING "Booting Zephyr OS build". These are used by Zephyr's source code. (To be implemented.)