Home | History | Annotate | Download | only in yasm
      1 # Copyright 2014 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 # The yasm build process creates a slew of small C subprograms that
      6 # dynamically generate files at various point in the build process.  This makes
      7 # the build integration moderately complex.
      8 #
      9 # There are three classes of dynamically generated files:
     10 #   1) C source files that should be included in the build (eg., lc3bid.c)
     11 #   2) C source files that are #included by static C sources (eg., license.c)
     12 #   3) Intermediate files that are used as input by other subprograms to
     13 #      further generate files in category #1 or #2.  (eg., version.mac)
     14 #
     15 # This structure is represented with the following targets:
     16 #   1) yasm -- Sources, flags for the main yasm executable. Also has most of
     17 #              of the actions and rules that invoke the subprograms.
     18 #   2) yasm_config -- General build configuration including setting a
     19 #              inputs listing the checked in version of files
     20 #              generated by manually running configure. These manually
     21 #              generated files are used by all binaries.
     22 #   3) yasm_utils -- Object files with memory management and hashing utilities
     23 #              shared between yasm and the genperf subprogram.
     24 #   4) genmacro, genmodule, etc. -- One executable target for each subprogram.
     25 #   5) generate_license, generate_module, etc. -- Actions that invoke programs
     26 #              built in #4 to generate .c files.
     27 #   6) compile_gperf, compile_re2c, etc. -- Actions that invoke programs that
     28 #              turn intermediate files into .c files.
     29 
     30 import("//build/config/compiler/compiler.gni")
     31 
     32 configs_to_delete = []
     33 configs_to_add = []
     34 if (is_debug) {
     35   configs_to_delete += [
     36     # Build with full optimizations even on debug configurations, because some
     37     # yasm build steps (highbd_sad4d_sse2.asm) can take ~33 seconds or more in
     38     # debug component builds on Windows. Enabling compiler optimizations saves
     39     #  ~5 seconds.
     40     "//build/config/compiler:default_optimization",
     41 
     42     # Don't define _DEBUG. Modest savings, but good for consistency.
     43     "//build/config:debug",
     44   ]
     45   configs_to_add += [
     46     "//build/config:release",
     47     "//build/config/compiler:optimize_max",
     48   ]
     49   if (is_win) {
     50     # This switches to using the release CRT. On debug component builds of
     51     # highbd_sad4d_sse2.asm on Windows this saves about 15 s.
     52     configs_to_delete += [ "//build/config/win:default_crt" ]
     53     configs_to_add += [ "//build/config/win:release_crt" ]
     54   }
     55 }
     56 
     57 if (current_toolchain == host_toolchain) {
     58   # Various files referenced by multiple targets. yasm_gen_include_dir was moved
     59   # from $target_gen_dir/include to avoid conflicts with x86insn_gas.c and
     60   # x86insn_nasm.c. These files were previously generated during the build but
     61   # are now shipped pre-generated by yasm.
     62   yasm_gen_include_dir = "$target_gen_dir/gen_include"
     63   config_makefile = "source/config/Makefile"
     64   version_file = "version.mac"
     65 
     66   import("//build/compiled_action.gni")
     67 
     68   config("yasm_config") {
     69     include_dirs = [
     70       "source/config/$host_os",
     71       "source/patched-yasm",
     72     ]
     73     defines = [ "HAVE_CONFIG_H" ]
     74     if (is_posix) {
     75       cflags = [ "-std=gnu99" ]
     76     }
     77   }
     78 
     79   executable("genmacro") {
     80     sources = [
     81       "source/patched-yasm/tools/genmacro/genmacro.c",
     82     ]
     83     configs -= [ "//build/config/compiler:chromium_code" ]
     84     configs += [
     85       ":yasm_config",
     86       "//build/config/compiler:no_chromium_code",
     87     ]
     88     deps = [
     89       "//build/config:exe_and_shlib_deps",
     90 
     91       # Default manifest on Windows (a no-op elsewhere).
     92       "//build/win:default_exe_manifest",
     93     ]
     94   }
     95 
     96   executable("genmodule") {
     97     sources = [
     98       "source/patched-yasm/libyasm/genmodule.c",
     99     ]
    100     configs -= [ "//build/config/compiler:chromium_code" ]
    101     configs += [
    102       ":yasm_config",
    103       "//build/config/compiler:no_chromium_code",
    104     ]
    105     deps = [
    106       "//build/config:exe_and_shlib_deps",
    107 
    108       # Default manifest on Windows (a no-op elsewhere).
    109       "//build/win:default_exe_manifest",
    110     ]
    111   }
    112 
    113   executable("genperf") {
    114     sources = [
    115       "source/patched-yasm/tools/genperf/genperf.c",
    116       "source/patched-yasm/tools/genperf/perfect.c",
    117     ]
    118 
    119     configs -= [ "//build/config/compiler:chromium_code" ]
    120     configs += [
    121       ":yasm_config",
    122       "//build/config/compiler:no_chromium_code",
    123     ]
    124 
    125     # Must be compatible with yasm_utils/yasm
    126     configs -= configs_to_delete
    127     configs += configs_to_add
    128 
    129     deps = [
    130       ":yasm_utils",
    131       "//build/config:exe_and_shlib_deps",
    132 
    133       # Default manifest on Windows (a no-op elsewhere).
    134       "//build/win:default_exe_manifest",
    135     ]
    136   }
    137 
    138   # Used by both yasm and genperf binaries.
    139   static_library("yasm_utils") {
    140     sources = [
    141       "source/patched-yasm/libyasm/phash.c",
    142       "source/patched-yasm/libyasm/xmalloc.c",
    143       "source/patched-yasm/libyasm/xstrdup.c",
    144     ]
    145 
    146     configs -= [ "//build/config/compiler:chromium_code" ]
    147     configs += [
    148       ":yasm_config",
    149       "//build/config/compiler:no_chromium_code",
    150     ]
    151 
    152     # Must be compatible with yasm
    153     configs -= configs_to_delete
    154     configs += configs_to_add
    155   }
    156 
    157   executable("genstring") {
    158     sources = [
    159       "source/patched-yasm/genstring.c",
    160     ]
    161     configs -= [ "//build/config/compiler:chromium_code" ]
    162     configs += [
    163       ":yasm_config",
    164       "//build/config/compiler:no_chromium_code",
    165     ]
    166     deps = [
    167       "//build/config:exe_and_shlib_deps",
    168 
    169       # Default manifest on Windows (a no-op elsewhere).
    170       "//build/win:default_exe_manifest",
    171     ]
    172   }
    173 
    174   executable("genversion") {
    175     sources = [
    176       "source/patched-yasm/modules/preprocs/nasm/genversion.c",
    177     ]
    178     configs -= [ "//build/config/compiler:chromium_code" ]
    179     configs += [
    180       ":yasm_config",
    181       "//build/config/compiler:no_chromium_code",
    182     ]
    183     deps = [
    184       "//build/config:exe_and_shlib_deps",
    185 
    186       # Default manifest on Windows (a no-op elsewhere).
    187       "//build/win:default_exe_manifest",
    188     ]
    189   }
    190 
    191   config("re2c_warnings") {
    192     # re2c is missing CLOSEVOP from one switch.
    193     if (is_clang) {
    194       cflags = [
    195         # re2c is missing CLOSEVOP from one switch.
    196         "-Wno-switch",
    197 
    198         # re2c contains many static functions in headers (because it's
    199         # a C library predating C99.)
    200         "-Wno-unused-function",
    201       ]
    202     }
    203   }
    204 
    205   executable("re2c") {
    206     sources = [
    207       "source/patched-yasm/tools/re2c/actions.c",
    208       "source/patched-yasm/tools/re2c/code.c",
    209       "source/patched-yasm/tools/re2c/dfa.c",
    210       "source/patched-yasm/tools/re2c/main.c",
    211       "source/patched-yasm/tools/re2c/mbo_getopt.c",
    212       "source/patched-yasm/tools/re2c/parser.c",
    213       "source/patched-yasm/tools/re2c/scanner.c",
    214       "source/patched-yasm/tools/re2c/substr.c",
    215       "source/patched-yasm/tools/re2c/translate.c",
    216     ]
    217 
    218     configs -= [ "//build/config/compiler:chromium_code" ]
    219     configs += [
    220       ":yasm_config",
    221       "//build/config/compiler:no_chromium_code",
    222 
    223       # Must be after no_chromium_code for warning flags to be ordered
    224       # correctly.
    225       ":re2c_warnings",
    226     ]
    227     deps = [
    228       "//build/config:exe_and_shlib_deps",
    229 
    230       # Default manifest on Windows (a no-op elsewhere).
    231       "//build/win:default_exe_manifest",
    232     ]
    233   }
    234 
    235   config("yasm_warnings") {
    236     if (is_clang) {
    237       cflags = [
    238         # reg3264type in x86expr.c is unused.
    239         "-Wno-unused-local-typedef",
    240       ]
    241     } else if (is_linux) {
    242       cflags = [
    243         # dosexe_objfmt_output ignores the return value of ftruncate.
    244         "-Wno-unused-result",
    245       ]
    246     }
    247   }
    248 
    249   executable("yasm") {
    250     sources = [
    251       "source/patched-yasm/frontends/yasm/yasm-options.c",
    252       "source/patched-yasm/frontends/yasm/yasm.c",
    253       "source/patched-yasm/libyasm/assocdat.c",
    254       "source/patched-yasm/libyasm/bc-align.c",
    255       "source/patched-yasm/libyasm/bc-data.c",
    256       "source/patched-yasm/libyasm/bc-incbin.c",
    257       "source/patched-yasm/libyasm/bc-org.c",
    258       "source/patched-yasm/libyasm/bc-reserve.c",
    259       "source/patched-yasm/libyasm/bitvect.c",
    260       "source/patched-yasm/libyasm/bytecode.c",
    261       "source/patched-yasm/libyasm/errwarn.c",
    262       "source/patched-yasm/libyasm/expr.c",
    263       "source/patched-yasm/libyasm/file.c",
    264       "source/patched-yasm/libyasm/floatnum.c",
    265       "source/patched-yasm/libyasm/hamt.c",
    266       "source/patched-yasm/libyasm/insn.c",
    267       "source/patched-yasm/libyasm/intnum.c",
    268       "source/patched-yasm/libyasm/inttree.c",
    269       "source/patched-yasm/libyasm/linemap.c",
    270       "source/patched-yasm/libyasm/md5.c",
    271       "source/patched-yasm/libyasm/mergesort.c",
    272       "source/patched-yasm/libyasm/section.c",
    273       "source/patched-yasm/libyasm/strcasecmp.c",
    274       "source/patched-yasm/libyasm/strsep.c",
    275       "source/patched-yasm/libyasm/symrec.c",
    276       "source/patched-yasm/libyasm/valparam.c",
    277       "source/patched-yasm/libyasm/value.c",
    278       "source/patched-yasm/modules/arch/lc3b/lc3barch.c",
    279       "source/patched-yasm/modules/arch/lc3b/lc3bbc.c",
    280       "source/patched-yasm/modules/arch/x86/x86arch.c",
    281       "source/patched-yasm/modules/arch/x86/x86bc.c",
    282       "source/patched-yasm/modules/arch/x86/x86expr.c",
    283       "source/patched-yasm/modules/arch/x86/x86id.c",
    284       "source/patched-yasm/modules/dbgfmts/codeview/cv-dbgfmt.c",
    285       "source/patched-yasm/modules/dbgfmts/codeview/cv-symline.c",
    286       "source/patched-yasm/modules/dbgfmts/codeview/cv-type.c",
    287       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-aranges.c",
    288       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c",
    289       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-info.c",
    290       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-line.c",
    291       "source/patched-yasm/modules/dbgfmts/null/null-dbgfmt.c",
    292       "source/patched-yasm/modules/dbgfmts/stabs/stabs-dbgfmt.c",
    293       "source/patched-yasm/modules/listfmts/nasm/nasm-listfmt.c",
    294       "source/patched-yasm/modules/objfmts/bin/bin-objfmt.c",
    295       "source/patched-yasm/modules/objfmts/coff/coff-objfmt.c",
    296       "source/patched-yasm/modules/objfmts/coff/win64-except.c",
    297       "source/patched-yasm/modules/objfmts/dbg/dbg-objfmt.c",
    298       "source/patched-yasm/modules/objfmts/elf/elf-objfmt.c",
    299       "source/patched-yasm/modules/objfmts/elf/elf-x86-amd64.c",
    300       "source/patched-yasm/modules/objfmts/elf/elf-x86-x32.c",
    301       "source/patched-yasm/modules/objfmts/elf/elf-x86-x86.c",
    302       "source/patched-yasm/modules/objfmts/elf/elf.c",
    303       "source/patched-yasm/modules/objfmts/macho/macho-objfmt.c",
    304       "source/patched-yasm/modules/objfmts/rdf/rdf-objfmt.c",
    305       "source/patched-yasm/modules/objfmts/xdf/xdf-objfmt.c",
    306       "source/patched-yasm/modules/parsers/gas/gas-parse-intel.c",
    307       "source/patched-yasm/modules/parsers/gas/gas-parse.c",
    308       "source/patched-yasm/modules/parsers/gas/gas-parser.c",
    309       "source/patched-yasm/modules/parsers/nasm/nasm-parse.c",
    310       "source/patched-yasm/modules/parsers/nasm/nasm-parser.c",
    311       "source/patched-yasm/modules/preprocs/cpp/cpp-preproc.c",
    312       "source/patched-yasm/modules/preprocs/gas/gas-eval.c",
    313       "source/patched-yasm/modules/preprocs/gas/gas-preproc.c",
    314       "source/patched-yasm/modules/preprocs/nasm/nasm-eval.c",
    315       "source/patched-yasm/modules/preprocs/nasm/nasm-pp.c",
    316       "source/patched-yasm/modules/preprocs/nasm/nasm-preproc.c",
    317       "source/patched-yasm/modules/preprocs/nasm/nasmlib.c",
    318       "source/patched-yasm/modules/preprocs/raw/raw-preproc.c",
    319 
    320       # Files generated by compile_gperf
    321       "$target_gen_dir/x86cpu.c",
    322       "$target_gen_dir/x86regtmod.c",
    323 
    324       # Files generated by compile_re2c
    325       "$target_gen_dir/gas-token.c",
    326       "$target_gen_dir/nasm-token.c",
    327 
    328       # File generated by compile_re2c_lc3b
    329       "$target_gen_dir/lc3bid.c",
    330 
    331       # File generated by generate_module
    332       "$target_gen_dir/module.c",
    333     ]
    334 
    335     configs -= [ "//build/config/compiler:chromium_code" ]
    336     configs += [
    337       ":yasm_config",
    338       "//build/config/compiler:no_chromium_code",
    339       "//build/config/compiler:no_incompatible_pointer_warnings",
    340 
    341       # Must be after no_chromium_code for warning flags to be ordered
    342       # correctly.
    343       ":yasm_warnings",
    344     ]
    345 
    346     # Disable WPO for yasm: crbug.com/604808
    347     if (is_official_build && full_wpo_on_official) {
    348       configs -= [ "//build/config/compiler:default_optimization" ]
    349       configs += [ "//build/config/compiler:optimize_no_wpo" ]
    350     } else {
    351       configs -= configs_to_delete
    352       configs += configs_to_add
    353     }
    354 
    355     # Yasm generates a bunch of .c files which its source file #include. These
    356     # are placed in |yasm_gen_include_dir|.
    357     include_dirs = [ yasm_gen_include_dir ]
    358 
    359     if (!is_win) {
    360       cflags = [
    361         "-std=c89",
    362         "-pedantic",
    363       ]
    364     }
    365 
    366     # TODO(ajwong): This should take most of the generated output as
    367     # inputs.
    368     deps = [
    369       ":compile_gperf",
    370       ":compile_gperf_for_include",
    371       ":compile_nasm_macros",
    372       ":compile_nasm_version",
    373       ":compile_re2c",
    374       ":compile_re2c_lc3b",
    375       ":compile_win64_gas",
    376       ":compile_win64_nasm",
    377       ":generate_license",
    378       ":generate_module",
    379       ":generate_version",
    380       ":yasm_utils",
    381       "//build/config:exe_and_shlib_deps",
    382 
    383       # Default manifest on Windows (a no-op elsewhere).
    384       "//build/win:default_exe_manifest",
    385     ]
    386   }
    387 
    388   compiled_action_foreach("compile_gperf") {
    389     tool = ":genperf"
    390     sources = [
    391       "source/patched-yasm/modules/arch/x86/x86cpu.gperf",
    392       "source/patched-yasm/modules/arch/x86/x86regtmod.gperf",
    393     ]
    394 
    395     outputs = [
    396       "$target_gen_dir/{{source_name_part}}.c",
    397     ]
    398     args = [
    399       "{{source}}",
    400       rebase_path(target_gen_dir, root_build_dir) + "/{{source_name_part}}.c",
    401     ]
    402   }
    403 
    404   # This differs from |compile_gperf| in where it places it output files.
    405   compiled_action_foreach("compile_gperf_for_include") {
    406     tool = ":genperf"
    407     sources = [
    408       # Make sure the generated gperf files in $target_gen_dir are synced with
    409       # the outputs for the related generate_*_insn actions in the
    410       # generate_files target below.
    411       #
    412       # The output for these two are #included by
    413       #   source/patched-yasm/modules/arch/x86/x86id.c
    414       "source/patched-yasm/x86insn_gas.gperf",
    415       "source/patched-yasm/x86insn_nasm.gperf",
    416     ]
    417 
    418     outputs = [
    419       "$yasm_gen_include_dir/{{source_name_part}}.c",
    420     ]
    421     args = [
    422       "{{source}}",
    423       rebase_path(yasm_gen_include_dir, root_build_dir) +
    424           "/{{source_name_part}}.c",
    425     ]
    426   }
    427 
    428   template("compile_macro") {
    429     compiled_action(target_name) {
    430       tool = ":genmacro"
    431 
    432       # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    433       inputs = invoker.sources
    434       outputs = invoker.outputs
    435       args = [
    436         rebase_path(outputs[0], root_build_dir),
    437         invoker.macro_varname,
    438         rebase_path(inputs[0], root_build_dir),
    439       ]
    440       if (defined(invoker.deps)) {
    441         deps = invoker.deps
    442       }
    443     }
    444   }
    445 
    446   compile_macro("compile_nasm_macros") {
    447     # Output #included by
    448     #   source/patched-yasm/modules/preprocs/nasm/nasm-parser.c
    449     sources = [
    450       "source/patched-yasm/modules/parsers/nasm/nasm-std.mac",
    451     ]
    452     outputs = [
    453       "$yasm_gen_include_dir/nasm-macros.c",
    454     ]
    455     macro_varname = "nasm_standard_mac"
    456   }
    457 
    458   compile_macro("compile_nasm_version") {
    459     # Output #included by
    460     #   source/patched-yasm/modules/preprocs/nasm/nasm-preproc.c
    461     sources = [
    462       "$target_gen_dir/$version_file",
    463     ]
    464     outputs = [
    465       "$yasm_gen_include_dir/nasm-version.c",
    466     ]
    467     macro_varname = "nasm_version_mac"
    468     deps = [
    469       ":generate_version",
    470     ]
    471   }
    472 
    473   compile_macro("compile_win64_gas") {
    474     # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    475     sources = [
    476       "source/patched-yasm/modules/objfmts/coff/win64-gas.mac",
    477     ]
    478     outputs = [
    479       "$yasm_gen_include_dir/win64-gas.c",
    480     ]
    481     macro_varname = "win64_gas_stdmac"
    482   }
    483 
    484   compile_macro("compile_win64_nasm") {
    485     # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    486     sources = [
    487       "source/patched-yasm/modules/objfmts/coff/win64-nasm.mac",
    488     ]
    489     outputs = [
    490       "$yasm_gen_include_dir/win64-nasm.c",
    491     ]
    492     macro_varname = "win64_nasm_stdmac"
    493   }
    494 
    495   compiled_action_foreach("compile_re2c") {
    496     tool = ":re2c"
    497     sources = [
    498       "source/patched-yasm/modules/parsers/gas/gas-token.re",
    499       "source/patched-yasm/modules/parsers/nasm/nasm-token.re",
    500     ]
    501     outputs = [
    502       "$target_gen_dir/{{source_name_part}}.c",
    503     ]
    504     args = [
    505       "-b",
    506       "-o",
    507       rebase_path(target_gen_dir, root_build_dir) + "/{{source_name_part}}.c",
    508       "{{source}}",
    509     ]
    510   }
    511 
    512   # This call doesn't fit into the re2c template above.
    513   compiled_action("compile_re2c_lc3b") {
    514     tool = ":re2c"
    515     inputs = [
    516       "source/patched-yasm/modules/arch/lc3b/lc3bid.re",
    517     ]
    518     outputs = [
    519       "$target_gen_dir/lc3bid.c",
    520     ]
    521     args = [
    522       "-s",
    523       "-o",
    524       rebase_path(outputs[0], root_build_dir),
    525       rebase_path(inputs[0], root_build_dir),
    526     ]
    527   }
    528 
    529   compiled_action("generate_license") {
    530     tool = ":genstring"
    531 
    532     # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    533     inputs = [
    534       "source/patched-yasm/COPYING",
    535     ]
    536     outputs = [
    537       "$yasm_gen_include_dir/license.c",
    538     ]
    539     args = [
    540       "license_msg",
    541       rebase_path(outputs[0], root_build_dir),
    542       rebase_path(inputs[0], root_build_dir),
    543     ]
    544   }
    545 
    546   compiled_action("generate_module") {
    547     tool = ":genmodule"
    548     inputs = [
    549       "source/patched-yasm/libyasm/module.in",
    550       config_makefile,
    551     ]
    552     outputs = [
    553       "$target_gen_dir/module.c",
    554     ]
    555     args = [
    556       rebase_path(inputs[0], root_build_dir),
    557       rebase_path(config_makefile, root_build_dir),
    558       rebase_path(outputs[0], root_build_dir),
    559     ]
    560   }
    561 
    562   compiled_action("generate_version") {
    563     tool = ":genversion"
    564     outputs = [
    565       "$target_gen_dir/$version_file",
    566     ]
    567     args = [ rebase_path(outputs[0], root_build_dir) ]
    568   }
    569 }
    570