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