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 if (current_toolchain == host_toolchain) {
     31 
     32   # Various files referenced by multiple targets.
     33   yasm_gen_include_dir = "$target_gen_dir/include"
     34   config_makefile = "source/config/$os/Makefile"
     35   version_file = "version.mac"
     36 
     37   import("//build/compiled_action.gni")
     38 
     39   config("yasm_config") {
     40     include_dirs = [
     41       "source/config/$os",
     42       "source/patched-yasm",
     43     ]
     44     defines = [ "HAVE_CONFIG_H" ]
     45     if (is_posix) {
     46       cflags = [ "-std=gnu99" ]
     47     }
     48   }
     49 
     50   executable("genmacro") {
     51     sources = [ "source/patched-yasm/tools/genmacro/genmacro.c" ]
     52     configs -= [ "//build/config/compiler:chromium_code" ]
     53     configs += [ ":yasm_config",
     54                  "//build/config/compiler:no_chromium_code" ]
     55   }
     56 
     57   executable("genmodule") {
     58     sources = [ "source/patched-yasm/libyasm/genmodule.c" ]
     59     configs -= [ "//build/config/compiler:chromium_code" ]
     60     configs += [ ":yasm_config",
     61                  "//build/config/compiler:no_chromium_code" ]
     62   }
     63 
     64   executable("genperf") {
     65     sources = [
     66       "source/patched-yasm/tools/genperf/genperf.c",
     67       "source/patched-yasm/tools/genperf/perfect.c",
     68     ]
     69 
     70     configs -= [ "//build/config/compiler:chromium_code" ]
     71     configs += [ ":yasm_config",
     72                  "//build/config/compiler:no_chromium_code" ]
     73 
     74     deps = [ ":yasm_utils" ]
     75   }
     76 
     77   # Used by both yasm and genperf binaries.
     78   source_set("yasm_utils") {
     79     sources = [
     80       "source/patched-yasm/libyasm/phash.c",
     81       "source/patched-yasm/libyasm/xmalloc.c",
     82       "source/patched-yasm/libyasm/xstrdup.c",
     83     ]
     84 
     85     configs -= [ "//build/config/compiler:chromium_code" ]
     86     configs += [ ":yasm_config",
     87                  "//build/config/compiler:no_chromium_code" ]
     88   }
     89 
     90   executable("genstring") {
     91     sources = [ "source/patched-yasm/genstring.c", ]
     92     configs -= [ "//build/config/compiler:chromium_code" ]
     93     configs += [ ":yasm_config",
     94                  "//build/config/compiler:no_chromium_code" ]
     95   }
     96 
     97   executable("genversion") {
     98     sources = [ "source/patched-yasm/modules/preprocs/nasm/genversion.c" ]
     99     configs -= [ "//build/config/compiler:chromium_code" ]
    100     configs += [ ":yasm_config",
    101                  "//build/config/compiler:no_chromium_code" ]
    102   }
    103 
    104   executable("re2c") {
    105     sources = [
    106        "source/patched-yasm/tools/re2c/main.c",
    107        "source/patched-yasm/tools/re2c/code.c",
    108        "source/patched-yasm/tools/re2c/dfa.c",
    109        "source/patched-yasm/tools/re2c/parser.c",
    110        "source/patched-yasm/tools/re2c/actions.c",
    111        "source/patched-yasm/tools/re2c/scanner.c",
    112        "source/patched-yasm/tools/re2c/mbo_getopt.c",
    113        "source/patched-yasm/tools/re2c/substr.c",
    114        "source/patched-yasm/tools/re2c/translate.c",
    115     ]
    116 
    117     configs -= [ "//build/config/compiler:chromium_code" ]
    118     configs += [ ":yasm_config",
    119                  "//build/config/compiler:no_chromium_code" ]
    120 
    121     # re2c is missing CLOSEVOP from one switch.
    122     if (is_posix) {
    123       cflags = [ "-Wno-switch" ]
    124     } else if (is_win) {
    125       cflags = [ "/wd4267" ]  # size_t to int conversion.
    126     }
    127   }
    128 
    129   executable("yasm") {
    130     sources = [
    131       "source/patched-yasm/frontends/yasm/yasm-options.c",
    132       "source/patched-yasm/frontends/yasm/yasm.c",
    133       "source/patched-yasm/libyasm/assocdat.c",
    134       "source/patched-yasm/libyasm/bc-align.c",
    135       "source/patched-yasm/libyasm/bc-data.c",
    136       "source/patched-yasm/libyasm/bc-incbin.c",
    137       "source/patched-yasm/libyasm/bc-org.c",
    138       "source/patched-yasm/libyasm/bc-reserve.c",
    139       "source/patched-yasm/libyasm/bitvect.c",
    140       "source/patched-yasm/libyasm/bytecode.c",
    141       "source/patched-yasm/libyasm/errwarn.c",
    142       "source/patched-yasm/libyasm/expr.c",
    143       "source/patched-yasm/libyasm/file.c",
    144       "source/patched-yasm/libyasm/floatnum.c",
    145       "source/patched-yasm/libyasm/hamt.c",
    146       "source/patched-yasm/libyasm/insn.c",
    147       "source/patched-yasm/libyasm/intnum.c",
    148       "source/patched-yasm/libyasm/inttree.c",
    149       "source/patched-yasm/libyasm/linemap.c",
    150       "source/patched-yasm/libyasm/md5.c",
    151       "source/patched-yasm/libyasm/mergesort.c",
    152       "source/patched-yasm/libyasm/section.c",
    153       "source/patched-yasm/libyasm/strcasecmp.c",
    154       "source/patched-yasm/libyasm/strsep.c",
    155       "source/patched-yasm/libyasm/symrec.c",
    156       "source/patched-yasm/libyasm/valparam.c",
    157       "source/patched-yasm/libyasm/value.c",
    158       "source/patched-yasm/modules/arch/lc3b/lc3barch.c",
    159       "source/patched-yasm/modules/arch/lc3b/lc3bbc.c",
    160       "source/patched-yasm/modules/arch/x86/x86arch.c",
    161       "source/patched-yasm/modules/arch/x86/x86bc.c",
    162       "source/patched-yasm/modules/arch/x86/x86expr.c",
    163       "source/patched-yasm/modules/arch/x86/x86id.c",
    164       "source/patched-yasm/modules/dbgfmts/codeview/cv-dbgfmt.c",
    165       "source/patched-yasm/modules/dbgfmts/codeview/cv-symline.c",
    166       "source/patched-yasm/modules/dbgfmts/codeview/cv-type.c",
    167       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-aranges.c",
    168       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c",
    169       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-info.c",
    170       "source/patched-yasm/modules/dbgfmts/dwarf2/dwarf2-line.c",
    171       "source/patched-yasm/modules/dbgfmts/null/null-dbgfmt.c",
    172       "source/patched-yasm/modules/dbgfmts/stabs/stabs-dbgfmt.c",
    173       "source/patched-yasm/modules/listfmts/nasm/nasm-listfmt.c",
    174       "source/patched-yasm/modules/objfmts/bin/bin-objfmt.c",
    175       "source/patched-yasm/modules/objfmts/coff/coff-objfmt.c",
    176       "source/patched-yasm/modules/objfmts/coff/win64-except.c",
    177       "source/patched-yasm/modules/objfmts/dbg/dbg-objfmt.c",
    178       "source/patched-yasm/modules/objfmts/elf/elf-objfmt.c",
    179       "source/patched-yasm/modules/objfmts/elf/elf-x86-amd64.c",
    180       "source/patched-yasm/modules/objfmts/elf/elf-x86-x86.c",
    181       "source/patched-yasm/modules/objfmts/elf/elf.c",
    182       "source/patched-yasm/modules/objfmts/macho/macho-objfmt.c",
    183       "source/patched-yasm/modules/objfmts/rdf/rdf-objfmt.c",
    184       "source/patched-yasm/modules/objfmts/xdf/xdf-objfmt.c",
    185       "source/patched-yasm/modules/parsers/gas/gas-parse.c",
    186       "source/patched-yasm/modules/parsers/gas/gas-parse-intel.c",
    187       "source/patched-yasm/modules/parsers/gas/gas-parser.c",
    188       "source/patched-yasm/modules/parsers/nasm/nasm-parse.c",
    189       "source/patched-yasm/modules/parsers/nasm/nasm-parser.c",
    190       "source/patched-yasm/modules/preprocs/cpp/cpp-preproc.c",
    191       "source/patched-yasm/modules/preprocs/nasm/nasm-eval.c",
    192       "source/patched-yasm/modules/preprocs/nasm/nasm-pp.c",
    193       "source/patched-yasm/modules/preprocs/nasm/nasm-preproc.c",
    194       "source/patched-yasm/modules/preprocs/nasm/nasmlib.c",
    195       "source/patched-yasm/modules/preprocs/raw/raw-preproc.c",
    196 
    197       # Files generated by compile_gperf
    198       "$target_gen_dir/x86cpu.c",
    199       "$target_gen_dir/x86regtmod.c",
    200 
    201       # Files generated by compile_re2c
    202       "$target_gen_dir/gas-token.c",
    203       "$target_gen_dir/nasm-token.c",
    204 
    205       # File generated by compile_re2c_lc3b
    206       "$target_gen_dir/lc3bid.c",
    207 
    208       # File generated by generate_module
    209       "$target_gen_dir/module.c"
    210     ]
    211 
    212     configs -= [ "//build/config/compiler:chromium_code" ]
    213     configs += [ ":yasm_config",
    214                  "//build/config/compiler:no_chromium_code" ]
    215 
    216     # Yasm generates a bunch of .c files which its source file #include.
    217     # Add the |target_gen_dir| into the include path so it can find them.
    218     # Ideally, these generated .c files would be placed into a separate
    219     # directory, but the gen_x86_insn.py script does not make this easy.
    220     include_dirs = [ yasm_gen_include_dir ]
    221 
    222     if (is_win) {
    223       cflags = [ "/wd4267" ]  # size_t to int conversion.
    224     } else {
    225       cflags = [ "-ansi", "-pedantic" ]
    226       if (is_clang) {
    227         cflags += [ "-Wno-incompatible-pointer-types" ]
    228       }
    229     }
    230 
    231     # TODO(ajwong): This should take most of the generated output as
    232     # inputs.
    233     deps = [
    234       ":compile_gperf",
    235       ":compile_gperf_for_include",
    236       ":compile_nasm_macros",
    237       ":compile_nasm_version",
    238       ":compile_re2c_lc3b",
    239       ":compile_win64_gas",
    240       ":compile_win64_nasm",
    241       ":compile_re2c",
    242       ":generate_license",
    243       ":generate_module",
    244       ":generate_version",
    245       ":yasm_utils",
    246     ]
    247   }
    248 
    249   compiled_action_foreach("compile_gperf") {
    250     tool = ":genperf"
    251     sources = [
    252       "source/patched-yasm/modules/arch/x86/x86cpu.gperf",
    253       "source/patched-yasm/modules/arch/x86/x86regtmod.gperf",
    254     ]
    255 
    256     outputs = [ "$target_gen_dir/{{source_name_part}}.c" ]
    257     args = [
    258       "{{source}}",
    259       rebase_path(target_gen_dir, root_build_dir) + "/{{source_name_part}}.c",
    260     ]
    261     deps = [
    262       ":generate_x86_insn"
    263     ]
    264   }
    265 
    266   # This differs from |compile_gperf| in where it places it output files.
    267   compiled_action_foreach("compile_gperf_for_include") {
    268     tool = ":genperf"
    269     sources = [
    270       # Make sure the generated gperf files in $target_gen_dir are synced with
    271       # the outputs for the related generate_*_insn actions in the
    272       # generate_files target below.
    273       #
    274       # The output for these two are #included by
    275       #   source/patched-yasm/modules/arch/x86/x86id.c
    276       "$yasm_gen_include_dir/x86insn_gas.gperf",
    277       "$yasm_gen_include_dir/x86insn_nasm.gperf",
    278     ]
    279 
    280     outputs = [ "$yasm_gen_include_dir/{{source_name_part}}.c" ]
    281     args = [
    282       "{{source}}",
    283       rebase_path(yasm_gen_include_dir, root_build_dir) +
    284           "/{{source_name_part}}.c",
    285     ]
    286     deps = [
    287       ":generate_x86_insn"
    288     ]
    289   }
    290 
    291   template("compile_macro") {
    292     compiled_action(target_name) {
    293       tool = ":genmacro"
    294       # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    295       inputs = invoker.sources
    296       outputs = invoker.outputs
    297       args = [
    298         rebase_path(outputs[0], root_build_dir),
    299         invoker.macro_varname,
    300         rebase_path(inputs[0], root_build_dir),
    301       ]
    302       if (defined(invoker.deps)) {
    303         deps = invoker.deps
    304       }
    305     }
    306   }
    307 
    308   compile_macro("compile_nasm_macros") {
    309     # Output #included by
    310     #   source/patched-yasm/modules/preprocs/nasm/nasm-parser.c
    311     sources = [ "source/patched-yasm/modules/parsers/nasm/nasm-std.mac" ]
    312     outputs = [ "$yasm_gen_include_dir/nasm-macros.c" ]
    313     macro_varname = "nasm_standard_mac"
    314   }
    315 
    316   compile_macro("compile_nasm_version") {
    317     # Output #included by
    318     #   source/patched-yasm/modules/preprocs/nasm/nasm-preproc.c
    319     sources = [ "$target_gen_dir/$version_file" ]
    320     outputs = [ "$yasm_gen_include_dir/nasm-version.c" ]
    321     macro_varname = "nasm_version_mac"
    322     deps = [ ":generate_version" ]
    323   }
    324 
    325   compile_macro("compile_win64_gas") {
    326     # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    327     sources = [ "source/patched-yasm/modules/objfmts/coff/win64-gas.mac" ]
    328     outputs = [ "$yasm_gen_include_dir/win64-gas.c" ]
    329     macro_varname = "win64_gas_stdmac"
    330   }
    331 
    332   compile_macro("compile_win64_nasm") {
    333     # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    334     sources = [ "source/patched-yasm/modules/objfmts/coff/win64-nasm.mac" ]
    335     outputs = [ "$yasm_gen_include_dir/win64-nasm.c" ]
    336     macro_varname = "win64_nasm_stdmac"
    337   }
    338 
    339   compiled_action_foreach("compile_re2c") {
    340     tool = ":re2c"
    341     sources = [
    342       "source/patched-yasm/modules/parsers/gas/gas-token.re",
    343       "source/patched-yasm/modules/parsers/nasm/nasm-token.re",
    344     ]
    345     outputs = [ "$target_gen_dir/{{source_name_part}}.c" ]
    346     args = [
    347       "-b",
    348       "-o",
    349       rebase_path(target_gen_dir, root_build_dir) + "/{{source_name_part}}.c",
    350       "{{source}}",
    351     ]
    352   }
    353 
    354   # This call doesn't fit into the re2c template above.
    355   compiled_action("compile_re2c_lc3b") {
    356     tool = ":re2c"
    357     inputs = [ "source/patched-yasm/modules/arch/lc3b/lc3bid.re" ]
    358     outputs = [ "$target_gen_dir/lc3bid.c" ]
    359     args = [
    360       "-s",
    361       "-o",
    362       rebase_path(outputs[0], root_build_dir),
    363       rebase_path(inputs[0], root_build_dir),
    364     ]
    365   }
    366 
    367   compiled_action("generate_license") {
    368     tool = ":genstring"
    369     # Output #included by source/patched-yasm/frontends/yasm/yasm.c.
    370     inputs = [ "source/patched-yasm/COPYING" ]
    371     outputs = [ "$yasm_gen_include_dir/license.c" ]
    372     args = [
    373       "license_msg",
    374       rebase_path(outputs[0], root_build_dir),
    375       rebase_path(inputs[0], root_build_dir),
    376     ]
    377   }
    378 
    379   compiled_action("generate_module") {
    380     tool = ":genmodule"
    381     inputs = [
    382       "source/patched-yasm/libyasm/module.in",
    383       config_makefile,
    384     ]
    385     outputs = [ "$target_gen_dir/module.c" ]
    386     args = [
    387       rebase_path(inputs[0], root_build_dir),
    388       rebase_path(config_makefile, root_build_dir),
    389       rebase_path(outputs[0], root_build_dir),
    390     ]
    391   }
    392 
    393   compiled_action("generate_version") {
    394     tool = ":genversion"
    395     outputs = [ "$target_gen_dir/$version_file" ]
    396     args = [
    397       rebase_path(outputs[0],
    398       root_build_dir)
    399     ]
    400   }
    401 
    402   action("generate_x86_insn") {
    403     script = "source/patched-yasm/modules/arch/x86/gen_x86_insn.py"
    404     # Output eventually #included by source/patched-yasm/frontends/yasm/x86id.c
    405     outputs = [
    406       "$yasm_gen_include_dir/x86insns.c",
    407       "$yasm_gen_include_dir/x86insn_gas.gperf",
    408       "$yasm_gen_include_dir/x86insn_nasm.gperf",
    409     ]
    410     args = [ rebase_path(yasm_gen_include_dir, root_build_dir) ]
    411   }
    412 }
    413