# 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
#              source_prereqs 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.

if (current_toolchain == host_toolchain) {

  # Various files referenced by multiple targets.
  yasm_gen_include_dir = "$target_gen_dir/include"
  config_makefile = "source/config/$os/Makefile"
  version_file = "version.mac"

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

  config("yasm_config") {
    include_dirs = [
      "source/config/$os",
      "source/patched-yasm",
    ]
    source_prereqs = [
      config_makefile,
      "source/config/$os/config.h",
      "source/config/$os/libyasm-stdint.h",
    ]
    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" ]
  }

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

  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" ]

    deps = [ ":yasm_utils" ]
  }

  # Used by both yasm and genperf binaries.
  source_set("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" ]
  }

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

  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" ]
  }

  executable("re2c") {
    sources = [
       "source/patched-yasm/tools/re2c/main.c",
       "source/patched-yasm/tools/re2c/code.c",
       "source/patched-yasm/tools/re2c/dfa.c",
       "source/patched-yasm/tools/re2c/parser.c",
       "source/patched-yasm/tools/re2c/actions.c",
       "source/patched-yasm/tools/re2c/scanner.c",
       "source/patched-yasm/tools/re2c/mbo_getopt.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" ]

    # re2c is missing CLOSEVOP from one switch.
    if (is_posix) {
      cflags = [ "-Wno-switch" ]
    }
  }

  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-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.c",
      "source/patched-yasm/modules/parsers/gas/gas-parse-intel.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/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" ]

    # Yasm generates a bunch of .c files which its source file #include.
    # Add the |target_gen_dir| into the include path so it can find them.
    # Ideally, these generated .c files would be placed into a separate
    # directory, but the gen_x86_insn.py script does not make this easy.
    include_dirs = [ yasm_gen_include_dir ]

    if (is_win) {
      cflags = [ "/wd4267" ]  # size_t to int conversion.
    } else {
      cflags = [ "-ansi", "-pedantic" ]
      if (is_clang) {
        cflags += [ "-Wno-incompatible-pointer-types" ]
      }
    }

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

  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, ".") + "/{{source_name_part}}.c",
    ]
    deps = [
      ":generate_x86_insn"
    ]
  }

  # 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
      "$yasm_gen_include_dir/x86insn_gas.gperf",
      "$yasm_gen_include_dir/x86insn_nasm.gperf",
    ]

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

  template("compile_macro") {
    compiled_action(target_name) {
      tool = ":genmacro"
      # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
      sources = invoker.sources
      outputs = invoker.outputs
      args = [
        rebase_path(outputs[0], root_build_dir),
        invoker.macro_varname,
        rebase_path(sources[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, ".") + "/{{source_name_part}}.c",
      "{{source}}",
    ]
  }

  # This call doesn't fit into the re2c template above.
  compiled_action("compile_re2c_lc3b") {
    tool = ":re2c"
    sources = [ "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(sources[0], root_build_dir),
    ]
  }

  compiled_action("generate_license") {
    tool = ":genstring"
    # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    sources = [ "source/patched-yasm/COPYING" ]
    outputs = [ "$yasm_gen_include_dir/license.c" ]
    args = [
      "license_msg",
      rebase_path(outputs[0], root_build_dir),
      rebase_path(sources[0], root_build_dir),
    ]
  }

  compiled_action("generate_module") {
    tool = ":genmodule"
    sources = [ "source/patched-yasm/libyasm/module.in" ]
    outputs = [ "$target_gen_dir/module.c" ]
    args = [
      rebase_path(sources[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)
    ]
  }

  action("generate_x86_insn") {
    script = "source/patched-yasm/modules/arch/x86/gen_x86_insn.py"
    # Output eventually #included by source/patched-yasm/frontends/yasm/x86id.c
    outputs = [
      "$yasm_gen_include_dir/x86insns.c",
      "$yasm_gen_include_dir/x86insn_gas.gperf",
      "$yasm_gen_include_dir/x86insn_nasm.gperf",
    ]
    args = [ rebase_path(yasm_gen_include_dir, root_build_dir) ]
  }
}