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

# The yasm build process creates a slew of small C subprograms that
# dynamically generate files at various point in the build process.  This makes
# the build integration moderately complex.
#
# There are three classes of dynamically generated files:
#   1) C source files that should be included in the build (eg., lc3bid.c)
#   2) C source files that are #included by static C sources (eg., license.c)
#   3) Intermediate files that are used as input by other subprograms to
#      further generate files in category #1 or #2.  (eg., version.mac)
#
# This structure is represented with the following targets:
#   1) yasm -- Sources, flags for the main yasm executable. Also has most of
#              of the actions and rules that invoke the subprograms.
#   2) yasm_config -- General build configuration including setting a
#              inputs listing the checked in version of files
#              generated by manually running configure. These manually
#              generated files are used by all binaries.
#   3) yasm_utils -- Object files with memory management and hashing utilities
#              shared between yasm and the genperf subprogram.
#   4) genmacro, genmodule, etc. -- One executable target for each subprogram.
#   5) generate_license, generate_module, etc. -- Actions that invoke programs
#              built in #4 to generate .c files.
#   6) compile_gperf, compile_re2c, etc. -- Actions that invoke programs that
#              turn intermediate files into .c files.

import("//build/config/compiler/compiler.gni")

configs_to_delete = []
configs_to_add = []
if (is_debug) {
  configs_to_delete += [
    # Build with full optimizations even on debug configurations, because some
    # yasm build steps (highbd_sad4d_sse2.asm) can take ~33 seconds or more in
    # debug component builds on Windows. Enabling compiler optimizations saves
    #  ~5 seconds.
    "//build/config/compiler:default_optimization",

    # Don't define _DEBUG. Modest savings, but good for consistency.
    "//build/config:debug",
  ]
  configs_to_add += [
    "//build/config:release",
    "//build/config/compiler:optimize_max",
  ]
  if (is_win) {
    # This switches to using the release CRT. On debug component builds of
    # highbd_sad4d_sse2.asm on Windows this saves about 15 s.
    configs_to_delete += [ "//build/config/win:default_crt" ]
    configs_to_add += [ "//build/config/win:release_crt" ]
  }
}

if (current_toolchain == host_toolchain) {
  # Various files referenced by multiple targets. yasm_gen_include_dir was moved
  # from $target_gen_dir/include to avoid conflicts with x86insn_gas.c and
  # x86insn_nasm.c. These files were previously generated during the build but
  # are now shipped pre-generated by yasm.
  yasm_gen_include_dir = "$target_gen_dir/gen_include"
  config_makefile = "source/config/Makefile"
  version_file = "version.mac"

  import("//build/compiled_action.gni")

  config("yasm_config") {
    include_dirs = [
      "source/config/$host_os",
      "source/patched-yasm",
    ]
    defines = [ "HAVE_CONFIG_H" ]
    if (is_posix) {
      cflags = [ "-std=gnu99" ]
    }
  }

  executable("genmacro") {
    sources = [
      "source/patched-yasm/tools/genmacro/genmacro.c",
    ]
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
    ]
    deps = [
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  executable("genmodule") {
    sources = [
      "source/patched-yasm/libyasm/genmodule.c",
    ]
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
    ]
    deps = [
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  executable("genperf") {
    sources = [
      "source/patched-yasm/tools/genperf/genperf.c",
      "source/patched-yasm/tools/genperf/perfect.c",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
    ]

    # Must be compatible with yasm_utils/yasm
    configs -= configs_to_delete
    configs += configs_to_add

    deps = [
      ":yasm_utils",
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  # Used by both yasm and genperf binaries.
  static_library("yasm_utils") {
    sources = [
      "source/patched-yasm/libyasm/phash.c",
      "source/patched-yasm/libyasm/xmalloc.c",
      "source/patched-yasm/libyasm/xstrdup.c",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
    ]

    # Must be compatible with yasm
    configs -= configs_to_delete
    configs += configs_to_add
  }

  executable("genstring") {
    sources = [
      "source/patched-yasm/genstring.c",
    ]
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
    ]
    deps = [
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  executable("genversion") {
    sources = [
      "source/patched-yasm/modules/preprocs/nasm/genversion.c",
    ]
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
    ]
    deps = [
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  config("re2c_warnings") {
    # re2c is missing CLOSEVOP from one switch.
    if (is_clang) {
      cflags = [
        # re2c is missing CLOSEVOP from one switch.
        "-Wno-switch",

        # re2c contains many static functions in headers (because it's
        # a C library predating C99.)
        "-Wno-unused-function",
      ]
    }
  }

  executable("re2c") {
    sources = [
      "source/patched-yasm/tools/re2c/actions.c",
      "source/patched-yasm/tools/re2c/code.c",
      "source/patched-yasm/tools/re2c/dfa.c",
      "source/patched-yasm/tools/re2c/main.c",
      "source/patched-yasm/tools/re2c/mbo_getopt.c",
      "source/patched-yasm/tools/re2c/parser.c",
      "source/patched-yasm/tools/re2c/scanner.c",
      "source/patched-yasm/tools/re2c/substr.c",
      "source/patched-yasm/tools/re2c/translate.c",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",

      # Must be after no_chromium_code for warning flags to be ordered
      # correctly.
      ":re2c_warnings",
    ]
    deps = [
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  config("yasm_warnings") {
    if (is_clang) {
      cflags = [
        # reg3264type in x86expr.c is unused.
        "-Wno-unused-local-typedef",
      ]
    } else if (is_linux) {
      cflags = [
        # dosexe_objfmt_output ignores the return value of ftruncate.
        "-Wno-unused-result",
      ]
    }
  }

  executable("yasm") {
    sources = [
      "source/patched-yasm/frontends/yasm/yasm-options.c",
      "source/patched-yasm/frontends/yasm/yasm.c",
      "source/patched-yasm/libyasm/assocdat.c",
      "source/patched-yasm/libyasm/bc-align.c",
      "source/patched-yasm/libyasm/bc-data.c",
      "source/patched-yasm/libyasm/bc-incbin.c",
      "source/patched-yasm/libyasm/bc-org.c",
      "source/patched-yasm/libyasm/bc-reserve.c",
      "source/patched-yasm/libyasm/bitvect.c",
      "source/patched-yasm/libyasm/bytecode.c",
      "source/patched-yasm/libyasm/errwarn.c",
      "source/patched-yasm/libyasm/expr.c",
      "source/patched-yasm/libyasm/file.c",
      "source/patched-yasm/libyasm/floatnum.c",
      "source/patched-yasm/libyasm/hamt.c",
      "source/patched-yasm/libyasm/insn.c",
      "source/patched-yasm/libyasm/intnum.c",
      "source/patched-yasm/libyasm/inttree.c",
      "source/patched-yasm/libyasm/linemap.c",
      "source/patched-yasm/libyasm/md5.c",
      "source/patched-yasm/libyasm/mergesort.c",
      "source/patched-yasm/libyasm/section.c",
      "source/patched-yasm/libyasm/strcasecmp.c",
      "source/patched-yasm/libyasm/strsep.c",
      "source/patched-yasm/libyasm/symrec.c",
      "source/patched-yasm/libyasm/valparam.c",
      "source/patched-yasm/libyasm/value.c",
      "source/patched-yasm/modules/arch/lc3b/lc3barch.c",
      "source/patched-yasm/modules/arch/lc3b/lc3bbc.c",
      "source/patched-yasm/modules/arch/x86/x86arch.c",
      "source/patched-yasm/modules/arch/x86/x86bc.c",
      "source/patched-yasm/modules/arch/x86/x86expr.c",
      "source/patched-yasm/modules/arch/x86/x86id.c",
      "source/patched-yasm/modules/dbgfmts/codeview/cv-dbgfmt.c",
      "source/patched-yasm/modules/dbgfmts/codeview/cv-symline.c",
      "source/patched-yasm/modules/dbgfmts/codeview/cv-type.c",
      "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-aranges.c",
      "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c",
      "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-info.c",
      "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-line.c",
      "source/patched-yasm/modules/dbgfmts/null/null-dbgfmt.c",
      "source/patched-yasm/modules/dbgfmts/stabs/stabs-dbgfmt.c",
      "source/patched-yasm/modules/listfmts/nasm/nasm-listfmt.c",
      "source/patched-yasm/modules/objfmts/bin/bin-objfmt.c",
      "source/patched-yasm/modules/objfmts/coff/coff-objfmt.c",
      "source/patched-yasm/modules/objfmts/coff/win64-except.c",
      "source/patched-yasm/modules/objfmts/dbg/dbg-objfmt.c",
      "source/patched-yasm/modules/objfmts/elf/elf-objfmt.c",
      "source/patched-yasm/modules/objfmts/elf/elf-x86-amd64.c",
      "source/patched-yasm/modules/objfmts/elf/elf-x86-x32.c",
      "source/patched-yasm/modules/objfmts/elf/elf-x86-x86.c",
      "source/patched-yasm/modules/objfmts/elf/elf.c",
      "source/patched-yasm/modules/objfmts/macho/macho-objfmt.c",
      "source/patched-yasm/modules/objfmts/rdf/rdf-objfmt.c",
      "source/patched-yasm/modules/objfmts/xdf/xdf-objfmt.c",
      "source/patched-yasm/modules/parsers/gas/gas-parse-intel.c",
      "source/patched-yasm/modules/parsers/gas/gas-parse.c",
      "source/patched-yasm/modules/parsers/gas/gas-parser.c",
      "source/patched-yasm/modules/parsers/nasm/nasm-parse.c",
      "source/patched-yasm/modules/parsers/nasm/nasm-parser.c",
      "source/patched-yasm/modules/preprocs/cpp/cpp-preproc.c",
      "source/patched-yasm/modules/preprocs/gas/gas-eval.c",
      "source/patched-yasm/modules/preprocs/gas/gas-preproc.c",
      "source/patched-yasm/modules/preprocs/nasm/nasm-eval.c",
      "source/patched-yasm/modules/preprocs/nasm/nasm-pp.c",
      "source/patched-yasm/modules/preprocs/nasm/nasm-preproc.c",
      "source/patched-yasm/modules/preprocs/nasm/nasmlib.c",
      "source/patched-yasm/modules/preprocs/raw/raw-preproc.c",

      # Files generated by compile_gperf
      "$target_gen_dir/x86cpu.c",
      "$target_gen_dir/x86regtmod.c",

      # Files generated by compile_re2c
      "$target_gen_dir/gas-token.c",
      "$target_gen_dir/nasm-token.c",

      # File generated by compile_re2c_lc3b
      "$target_gen_dir/lc3bid.c",

      # File generated by generate_module
      "$target_gen_dir/module.c",
    ]

    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [
      ":yasm_config",
      "//build/config/compiler:no_chromium_code",
      "//build/config/compiler:no_incompatible_pointer_warnings",

      # Must be after no_chromium_code for warning flags to be ordered
      # correctly.
      ":yasm_warnings",
    ]

    # Disable WPO for yasm: crbug.com/604808
    if (is_official_build && full_wpo_on_official) {
      configs -= [ "//build/config/compiler:default_optimization" ]
      configs += [ "//build/config/compiler:optimize_no_wpo" ]
    } else {
      configs -= configs_to_delete
      configs += configs_to_add
    }

    # Yasm generates a bunch of .c files which its source file #include. These
    # are placed in |yasm_gen_include_dir|.
    include_dirs = [ yasm_gen_include_dir ]

    if (!is_win) {
      cflags = [
        "-std=c89",
        "-pedantic",
      ]
    }

    # TODO(ajwong): This should take most of the generated output as
    # inputs.
    deps = [
      ":compile_gperf",
      ":compile_gperf_for_include",
      ":compile_nasm_macros",
      ":compile_nasm_version",
      ":compile_re2c",
      ":compile_re2c_lc3b",
      ":compile_win64_gas",
      ":compile_win64_nasm",
      ":generate_license",
      ":generate_module",
      ":generate_version",
      ":yasm_utils",
      "//build/config:exe_and_shlib_deps",

      # Default manifest on Windows (a no-op elsewhere).
      "//build/win:default_exe_manifest",
    ]
  }

  compiled_action_foreach("compile_gperf") {
    tool = ":genperf"
    sources = [
      "source/patched-yasm/modules/arch/x86/x86cpu.gperf",
      "source/patched-yasm/modules/arch/x86/x86regtmod.gperf",
    ]

    outputs = [
      "$target_gen_dir/{{source_name_part}}.c",
    ]
    args = [
      "{{source}}",
      rebase_path(target_gen_dir, root_build_dir) + "/{{source_name_part}}.c",
    ]
  }

  # This differs from |compile_gperf| in where it places it output files.
  compiled_action_foreach("compile_gperf_for_include") {
    tool = ":genperf"
    sources = [
      # Make sure the generated gperf files in $target_gen_dir are synced with
      # the outputs for the related generate_*_insn actions in the
      # generate_files target below.
      #
      # The output for these two are #included by
      #   source/patched-yasm/modules/arch/x86/x86id.c
      "source/patched-yasm/x86insn_gas.gperf",
      "source/patched-yasm/x86insn_nasm.gperf",
    ]

    outputs = [
      "$yasm_gen_include_dir/{{source_name_part}}.c",
    ]
    args = [
      "{{source}}",
      rebase_path(yasm_gen_include_dir, root_build_dir) +
          "/{{source_name_part}}.c",
    ]
  }

  template("compile_macro") {
    compiled_action(target_name) {
      tool = ":genmacro"

      # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
      inputs = invoker.sources
      outputs = invoker.outputs
      args = [
        rebase_path(outputs[0], root_build_dir),
        invoker.macro_varname,
        rebase_path(inputs[0], root_build_dir),
      ]
      if (defined(invoker.deps)) {
        deps = invoker.deps
      }
    }
  }

  compile_macro("compile_nasm_macros") {
    # Output #included by
    #   source/patched-yasm/modules/preprocs/nasm/nasm-parser.c
    sources = [
      "source/patched-yasm/modules/parsers/nasm/nasm-std.mac",
    ]
    outputs = [
      "$yasm_gen_include_dir/nasm-macros.c",
    ]
    macro_varname = "nasm_standard_mac"
  }

  compile_macro("compile_nasm_version") {
    # Output #included by
    #   source/patched-yasm/modules/preprocs/nasm/nasm-preproc.c
    sources = [
      "$target_gen_dir/$version_file",
    ]
    outputs = [
      "$yasm_gen_include_dir/nasm-version.c",
    ]
    macro_varname = "nasm_version_mac"
    deps = [
      ":generate_version",
    ]
  }

  compile_macro("compile_win64_gas") {
    # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    sources = [
      "source/patched-yasm/modules/objfmts/coff/win64-gas.mac",
    ]
    outputs = [
      "$yasm_gen_include_dir/win64-gas.c",
    ]
    macro_varname = "win64_gas_stdmac"
  }

  compile_macro("compile_win64_nasm") {
    # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    sources = [
      "source/patched-yasm/modules/objfmts/coff/win64-nasm.mac",
    ]
    outputs = [
      "$yasm_gen_include_dir/win64-nasm.c",
    ]
    macro_varname = "win64_nasm_stdmac"
  }

  compiled_action_foreach("compile_re2c") {
    tool = ":re2c"
    sources = [
      "source/patched-yasm/modules/parsers/gas/gas-token.re",
      "source/patched-yasm/modules/parsers/nasm/nasm-token.re",
    ]
    outputs = [
      "$target_gen_dir/{{source_name_part}}.c",
    ]
    args = [
      "-b",
      "-o",
      rebase_path(target_gen_dir, root_build_dir) + "/{{source_name_part}}.c",
      "{{source}}",
    ]
  }

  # This call doesn't fit into the re2c template above.
  compiled_action("compile_re2c_lc3b") {
    tool = ":re2c"
    inputs = [
      "source/patched-yasm/modules/arch/lc3b/lc3bid.re",
    ]
    outputs = [
      "$target_gen_dir/lc3bid.c",
    ]
    args = [
      "-s",
      "-o",
      rebase_path(outputs[0], root_build_dir),
      rebase_path(inputs[0], root_build_dir),
    ]
  }

  compiled_action("generate_license") {
    tool = ":genstring"

    # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    inputs = [
      "source/patched-yasm/COPYING",
    ]
    outputs = [
      "$yasm_gen_include_dir/license.c",
    ]
    args = [
      "license_msg",
      rebase_path(outputs[0], root_build_dir),
      rebase_path(inputs[0], root_build_dir),
    ]
  }

  compiled_action("generate_module") {
    tool = ":genmodule"
    inputs = [
      "source/patched-yasm/libyasm/module.in",
      config_makefile,
    ]
    outputs = [
      "$target_gen_dir/module.c",
    ]
    args = [
      rebase_path(inputs[0], root_build_dir),
      rebase_path(config_makefile, root_build_dir),
      rebase_path(outputs[0], root_build_dir),
    ]
  }

  compiled_action("generate_version") {
    tool = ":genversion"
    outputs = [
      "$target_gen_dir/$version_file",
    ]
    args = [ rebase_path(outputs[0], root_build_dir) ]
  }
}