chromium/chrome/version.gni

# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This exposes the Chrome version as GN variables for use in build files.
# This also generates the various version codes used for builds of chrome for
# android.
#
# PREFER NOT TO USE THESE. The GYP build uses this kind of thing extensively.
# However, it is far better to write an action (or use
# //build/util/process_version.gni) to generate a file at build-time with the
# information you need. This allows better dependency checking and GN will
# run faster.
#
# These values should only be used if you REALLY need to depend on them at
# build-time, for example, in the computation of output file names.

# Give version.py a pattern that will expand to a GN scope consisting of
# all values we need at once.
_version_dictionary_template = "full = \"@MAJOR@.@MINOR@.@BUILD@.@PATCH@\" " +
                               "major = \"@MAJOR@\" minor = \"@MINOR@\" " +
                               "build = \"@BUILD@\" patch = \"@PATCH@\" "

# The file containing the Chrome version number.
chrome_version_file = "//chrome/VERSION"

_script_arguments = []

if (is_mac) {
  _version_dictionary_template += "patch_hi = @PATCH_HI@ patch_lo = @PATCH_LO@ "

  _script_arguments += [
    "-e",
    "PATCH_HI=int(PATCH)//256",
    "-e",
    "PATCH_LO=int(PATCH)%256",
  ]
} else if (target_os == "android") {
  import("//build/config/android/config.gni")
  import("//build/config/chrome_build.gni")

  _version_dictionary_template +=
      "chrome_version_code = " + "\"@CHROME_VERSION_CODE@\" " +
      "monochrome_version_code = \"@MONOCHROME_VERSION_CODE@\" " +
      "trichrome_version_code = \"@TRICHROME_VERSION_CODE@\" " +
      "trichrome_auto_version_code = \"@TRICHROME_AUTO_VERSION_CODE@\" " +
      "trichrome_beta_version_code = \"@TRICHROME_BETA_VERSION_CODE@\" " +
      "webview_stable_version_code = \"@WEBVIEW_STABLE_VERSION_CODE@\" " +
      "webview_beta_version_code = \"@WEBVIEW_BETA_VERSION_CODE@\" " +
      "webview_dev_version_code = \"@WEBVIEW_DEV_VERSION_CODE@\" "

  # Generate hybrid configurations for target_cpu's that can run in hybrid configurations.
  # i.e. "riscv64" does not show up in this list, as it's a 64-bit-only target_cpu.
  if (target_cpu == "arm64" || target_cpu == "x64") {
    _version_dictionary_template +=
        "chrome_32_version_code = \"@CHROME_32_VERSION_CODE@\" "
    _version_dictionary_template +=
        "monochrome_32_version_code = \"@MONOCHROME_32_VERSION_CODE@\" "
    _version_dictionary_template +=
        "monochrome_32_64_version_code = \"@MONOCHROME_32_64_VERSION_CODE@\" "
    _version_dictionary_template +=
        "monochrome_64_32_version_code = \"@MONOCHROME_64_32_VERSION_CODE@\" "
    _version_dictionary_template +=
        "monochrome_64_version_code = \"@MONOCHROME_64_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_32_version_code = \"@TRICHROME_32_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_auto_32_version_code = \"@TRICHROME_AUTO_32_VERSION_CODE@\" "
    _version_dictionary_template += "trichrome_auto_32_64_version_code = \"@TRICHROME_AUTO_32_64_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_32_64_version_code = \"@TRICHROME_32_64_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_64_32_version_code = \"@TRICHROME_64_32_VERSION_CODE@\" "
    _version_dictionary_template += "trichrome_auto_64_32_version_code = \"@TRICHROME_AUTO_64_32_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_64_version_code = \"@TRICHROME_64_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_auto_64_version_code = \"@TRICHROME_AUTO_64_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_desktop_64_version_code = \"@TRICHROME_DESKTOP_64_VERSION_CODE@\" "
    if (target_cpu == "arm64") {
      _version_dictionary_template += "trichrome_64_32_high_version_code = \"@TRICHROME_64_32_HIGH_VERSION_CODE@\" "
      _version_dictionary_template += "trichrome_auto_64_32_high_version_code = \"@TRICHROME_AUTO_64_32_HIGH_VERSION_CODE@\" "
    }
    _version_dictionary_template +=
        "trichrome_32_beta_version_code = \"@TRICHROME_32_BETA_VERSION_CODE@\" "
    _version_dictionary_template += "trichrome_32_64_beta_version_code = \"@TRICHROME_32_64_BETA_VERSION_CODE@\" "
    _version_dictionary_template += "trichrome_64_32_beta_version_code = \"@TRICHROME_64_32_BETA_VERSION_CODE@\" "
    _version_dictionary_template +=
        "trichrome_64_beta_version_code = \"@TRICHROME_64_BETA_VERSION_CODE@\" "
    if (target_cpu == "arm64") {
      _version_dictionary_template += "trichrome_64_32_high_beta_version_code = \"@TRICHROME_64_32_HIGH_BETA_VERSION_CODE@\" "
    }
    _version_dictionary_template +=
        "webview_32_stable_version_code = \"@WEBVIEW_32_STABLE_VERSION_CODE@\" "
    _version_dictionary_template +=
        "webview_32_beta_version_code = \"@WEBVIEW_32_BETA_VERSION_CODE@\" "
    _version_dictionary_template +=
        "webview_32_dev_version_code = \"@WEBVIEW_32_DEV_VERSION_CODE@\" "
    _version_dictionary_template +=
        "webview_64_stable_version_code = \"@WEBVIEW_64_STABLE_VERSION_CODE@\" "
    _version_dictionary_template +=
        "webview_64_beta_version_code = \"@WEBVIEW_64_BETA_VERSION_CODE@\" "
    _version_dictionary_template +=
        "webview_64_dev_version_code = \"@WEBVIEW_64_DEV_VERSION_CODE@\" "
  }

  _script_arguments += [
    "-a",
    target_cpu,
  ]

  if (defined(final_android_sdk) && !final_android_sdk) {
    _script_arguments += [ "--next" ]
  }
}

_script_arguments += [
  "-f",
  rebase_path(chrome_version_file, root_build_dir),
  "-t",
  _version_dictionary_template,
  "--os",
  target_os,
]

_result = exec_script("//build/util/version.py",
                      _script_arguments,
                      "scope",
                      [
                        chrome_version_file,
                        "//build/util/android_chrome_version.py",
                      ])

# Full version. For example "45.0.12321.0"
chrome_version_full = _result.full

# The constituent parts of the full version.
chrome_version_major = _result.major
chrome_version_minor = _result.minor
chrome_version_build = _result.build
chrome_version_patch = _result.patch

if (is_mac) {
  chrome_version_patch_hi = _result.patch_hi
  chrome_version_patch_lo = _result.patch_lo

  chrome_dylib_version = "$chrome_version_build.$chrome_version_patch_hi" +
                         ".$chrome_version_patch_lo"
} else if (target_os == "android") {
  forward_variables_from(_result,
                         [
                           "chrome_version_code",
                           "chrome_32_version_code",
                           "monochrome_version_code",
                           "monochrome_32_version_code",
                           "monochrome_32_64_version_code",
                           "monochrome_64_32_version_code",
                           "monochrome_64_version_code",
                           "trichrome_version_code",
                           "trichrome_auto_version_code",
                           "trichrome_32_version_code",
                           "trichrome_auto_32_version_code",
                           "trichrome_32_64_version_code",
                           "trichrome_64_32_version_code",
                           "trichrome_64_32_high_version_code",
                           "trichrome_auto_32_64_version_code",
                           "trichrome_auto_64_version_code",
                           "trichrome_auto_64_32_version_code",
                           "trichrome_auto_64_32_high_version_code",
                           "trichrome_desktop_64_version_code",
                           "trichrome_64_version_code",
                           "trichrome_beta_version_code",
                           "trichrome_32_beta_version_code",
                           "trichrome_32_64_beta_version_code",
                           "trichrome_64_32_beta_version_code",
                           "trichrome_64_32_high_beta_version_code",
                           "trichrome_64_beta_version_code",
                           "webview_beta_version_code",
                           "webview_dev_version_code",
                           "webview_stable_version_code",
                           "webview_32_beta_version_code",
                           "webview_32_dev_version_code",
                           "webview_32_stable_version_code",
                           "webview_64_beta_version_code",
                           "webview_64_dev_version_code",
                           "webview_64_stable_version_code",
                         ])

  chrome_version_name = chrome_version_full

  # There is a different version code only for arm64.
  if (is_high_end_android && target_cpu != "arm64") {
    trichrome_64_32_high_beta_version_code = trichrome_64_32_beta_version_code
    trichrome_64_32_high_version_code = trichrome_64_32_version_code
    trichrome_auto_64_32_high_version_code = trichrome_auto_64_32_version_code
  }
}

if (is_android && current_toolchain == default_toolchain) {
  lines_to_write = [
    "VersionName: $chrome_version_name",
    "Chrome: $chrome_version_code",
    "Monochrome: $monochrome_version_code",
    "TrichromeChrome: $trichrome_version_code",
    "TrichromeChromeOpenBeta: $trichrome_beta_version_code",
    "TrichromeChromeAuto: $trichrome_auto_version_code",
    "AndroidWebviewStable: $webview_stable_version_code",
    "AndroidWebviewBeta: $webview_beta_version_code",
    "AndroidWebviewDev: $webview_dev_version_code",
  ]

  if (android_64bit_target_cpu && defined(android_app_secondary_abi)) {
    lines_to_write += [
      "Monochrome32: $monochrome_32_version_code",
      "Monochrome3264: $monochrome_32_64_version_code",
      "Monochrome6432: $monochrome_64_32_version_code",
      "Monochrome64: $monochrome_64_version_code",
      "TrichromeChrome32: $trichrome_32_version_code",
      "TrichromeChrome3264: $trichrome_32_64_version_code",
      "TrichromeChrome6432: $trichrome_64_32_version_code",
      "TrichromeChrome64: $trichrome_64_version_code",
      "TrichromeChromeAuto32: $trichrome_auto_32_version_code",
      "TrichromeChromeAuto3264: $trichrome_auto_32_64_version_code",
      "TrichromeChromeAuto64: $trichrome_auto_64_version_code",
      "TrichromeChromeAuto6432: $trichrome_auto_64_32_version_code",
      "TrichromeChromeDesktop64: $trichrome_desktop_64_version_code",
    ]
    if (target_cpu == "arm64") {
      lines_to_write += [
        "TrichromeChrome6432High: $trichrome_64_32_high_version_code",
        "TrichromeChromeAuto6432High: $trichrome_auto_64_32_high_version_code",
      ]
    }
    lines_to_write += [
      "TrichromeChrome32OpenBeta: $trichrome_32_beta_version_code",
      "TrichromeChrome3264OpenBeta: $trichrome_32_64_beta_version_code",
      "TrichromeChrome6432OpenBeta: $trichrome_64_32_beta_version_code",
      "TrichromeChrome64OpenBeta: $trichrome_64_beta_version_code",
    ]
    if (target_cpu == "arm64") {
      lines_to_write += [ "TrichromeChrome6432HighOpenBeta: $trichrome_64_32_high_beta_version_code" ]
    }
    lines_to_write += [
      "AndroidWebview32Stable: $webview_32_stable_version_code",
      "AndroidWebview32Beta: $webview_32_beta_version_code",
      "AndroidWebview32Dev: $webview_32_dev_version_code",
      "AndroidWebview64Stable: $webview_64_stable_version_code",
      "AndroidWebview64Beta: $webview_64_beta_version_code",
      "AndroidWebview64Dev: $webview_64_dev_version_code",
    ]
  }

  write_file("$root_out_dir/android_chrome_versions.txt", lines_to_write)
}

if (is_android) {
  import("//build/config/android/channel.gni")
  import("//build/config/android/config.gni")
  import("//build/config/chrome_build.gni")

  # Lookup tables for version codes.
  # For the list of configurations that exist, see:
  #     https://chromium.googlesource.com/chromium/src/+/main/docs/android_native_libraries.md#build-variants-eg_monochrome_64_32_apk
  #
  # For how version codes are computed, see:
  #     //build/util/android_chrome_version.py

  # Key: {android_64bit_target_cpu}_{is_64_bit_browser}_{include_64_bit_webview}_{include_32_bit_webview}
  TRICHROME_VERSION_MAP = {
    _use_stable_package_name_for_trichrome =
        use_stable_package_name_for_trichrome && android_channel == "beta"

    # 64-bit constants are not generated by version.py on non-64-bit target_cpu.
    if (android_64bit_target_cpu) {
      # hybrid constants are not generated by version.py on non-64-bit-only target_cpu.
      if (defined(android_app_secondary_abi)) {
        if (_use_stable_package_name_for_trichrome) {
          if (is_high_end_android) {
            true_true_true_true = trichrome_64_32_high_beta_version_code
          } else {
            true_true_true_true = trichrome_64_32_beta_version_code
          }
          true_true_true_false = trichrome_64_beta_version_code
          true_false_false_true = trichrome_32_beta_version_code
          true_false_true_true = trichrome_32_64_beta_version_code
        } else {
          if (is_high_end_android) {
            true_true_true_true = trichrome_64_32_high_version_code
          } else {
            true_true_true_true = trichrome_64_32_version_code
          }
          if (is_desktop_android) {
            true_true_true_false = trichrome_desktop_64_version_code
          } else {
            true_true_true_false = trichrome_64_version_code
          }
          true_false_false_true = trichrome_32_version_code
          true_false_true_true = trichrome_32_64_version_code
        }
      } else {
        if (_use_stable_package_name_for_trichrome) {
          true_true_true_false = trichrome_beta_version_code
        } else {
          true_true_true_false = trichrome_version_code
        }
      }
    } else {
      # !android_64bit_target_cpu
      if (_use_stable_package_name_for_trichrome) {
        false_false_false_true = trichrome_beta_version_code
      } else {
        false_false_false_true = trichrome_version_code
      }
    }
  }

  # Key: {android_64bit_target_cpu}_{is_64_bit_browser}_{include_64_bit_webview}_{include_32_bit_webview}
  MONOCHROME_VERSION_MAP = {
    if (android_64bit_target_cpu) {
      if (defined(android_app_secondary_abi)) {
        true_true_true_true = monochrome_64_32_version_code
        true_false_true_true = monochrome_32_64_version_code
        true_true_true_false = monochrome_64_version_code
        true_false_false_true = monochrome_32_version_code
      } else {
        true_true_true_false = monochrome_version_code
      }
    } else {
      false_false_false_true = monochrome_version_code
    }
  }

  # Key: {android_64bit_target_cpu}_{include_64_bit_webview}_{include_32_bit_webview}
  WEBVIEW_VERSION_MAP = {
    # 64-bit constants are not generated by version.py on non-64-bit target_cpu.
    if (android_64bit_target_cpu) {
      if (defined(android_app_secondary_abi)) {
        if (android_channel == "dev") {
          true_false_true = webview_32_dev_version_code
          true_true_false = webview_64_dev_version_code
          true_true_true = webview_dev_version_code
        } else if (android_channel == "beta") {
          true_false_true = webview_32_beta_version_code
          true_true_false = webview_64_beta_version_code
          true_true_true = webview_beta_version_code
        } else {
          true_false_true = webview_32_stable_version_code
          true_true_false = webview_64_stable_version_code
          true_true_true = webview_stable_version_code
        }
      } else {
        if (android_channel == "dev") {
          true_true_false = webview_dev_version_code
        } else if (android_channel == "beta") {
          true_true_false = webview_beta_version_code
        } else {
          true_true_false = webview_stable_version_code
        }
      }
    } else {
      # !android_64bit_target_cpu
      if (android_channel == "dev") {
        false_false_true = webview_dev_version_code
      } else if (android_channel == "beta") {
        false_false_true = webview_beta_version_code
      } else {
        false_false_true = webview_stable_version_code
      }
    }

    # For system_webview_no_weblayer_apk.
    true_false_false = "1"
    false_false_false = "1"
  }
}