FPGA開発日記

FPGAというより、コンピュータアーキテクチャかもね! カテゴリ別記事インデックス https://sites.google.com/site/fpgadevelopindex/

google-gflagsを試す(実際に利用してcmakeからビルドする)

次に、実際にビルドしたgoogle-gflagsをビルドに利用してみよう。

github.com

基本的に、cmakeで使う分には簡単だ。

github.com

# include_directories(../vendor/gflags/include/)
set (gflags_SHARED    TRUE)
set (gflags_NOTHREADS TRUE)
include_directories ("../vendor/gflags/build/include/")
set (gflags_DIR "../vendor/gflags/build/")
link_directories("../vendor/gflags/build/lib/")
find_package(gflags REQUIRED)

find_package (Threads)

threadsパッケージを探しているのは、どうやらgflagsがpthreadを利用しているらしく、pthreadのパッケージが存在しないとビルドに失敗するらしい。gflags-nothreadsを利用するにはどうしたら良いかは調査中。

以下のように、add_executableの下に必要なライブラリを書いてやる。

add_executable (../swimmer_riscv
               swimmer_main.cpp env.cpp simulation.cpp trace.cpp inst_print.cpp inst_mnemonic.cpp inst_riscv_init.cpp
               inst_decoder.cpp inst_riscv.cpp inst_operand.cpp dec_utils.cpp
               sysreg_rw.cpp sysreg_func.cpp sysreg_str.cpp memory_block.cpp
               )
target_link_libraries (../swimmer_riscv gflags)
target_link_libraries (../swimmer_riscv ${CMAKE_THREAD_LIBS_INIT})

とりあえずこれでビルドが通るようになる。

次に、ソースコード上でどのようにgflagsをソースコード中で使うかだが、こっちの方が情報としては多い。

// FLAGs definition
DEFINE_string (hexfile, "", "hex file to simulate.");
DEFINE_bool (debug, false, "generate debug log");
DEFINE_string (debugfile, "", "output log filename");
DEFINE_int64 (max, 65535, "max instructions to simulate");
DEFINE_string (init_pc, "0x00000000", "initial program counter value");
DEFINE_bool (stop_host, true, "Stop simulation by accessing MTOHOST");

...

int main (int argc, char *argv[])
{
    FILE *hexfp;
    FILE *debugfp = stdout;

    char debug_out = false;
    std::string debug_filename,
        input_filename;

...
    gflags::SetUsageMessage("Swimmer-RISCV: RISC-V instruction set simulator");
    gflags::ParseCommandLineFlags (&argc, &argv, true); //% parse command line by gflags

SetUsageMassageを設定しておけばとりあえず--helpとかをうまく表示してくれるらしいが、まだかゆいところに手が届かない。

swimmer_riscv: Swimmer-RISCV: RISC-V instruction set simulator

  Flags from /home/masayuki/work/swimmer_riscv/src_cpp/swimmer_main.cpp:
    -debug (generate debug log) type: bool default: false
    -debugfile (output log filename) type: string default: ""
    -hexfile (hex file to simulate.) type: string default: ""
    -init_pc (initial program counter value) type: string default: "0x00000000"
    -max (max instructions to simulate) type: int64 default: 65535
    -stop_host (Stop simulation by accessing MTOHOST) type: bool default: true



  Flags from /home/masayuki/work/swimmer_riscv/vendor/gflags/src/gflags.cc:
    -flagfile (load flags from file) type: string default: ""
    -fromenv (set flags from the environment [use 'export FLAGS_flag1=value'])
      type: string default: ""
    -tryfromenv (set flags from the environment if present) type: string
      default: ""
    -undefok (comma-separated list of flag names that it is okay to specify on
      the command line even if the program does not define a flag with that
      name.  IMPORTANT: flags in this list that have arguments MUST use the
      flag=value format) type: string default: ""

  Flags from /home/masayuki/work/swimmer_riscv/vendor/gflags/src/gflags_completions.cc:
    -tab_completion_columns (Number of columns to use in output for tab
      completion) type: int32 default: 80
    -tab_completion_word (If non-empty, HandleCommandLineCompletions() will
      hijack the process and attempt to do bash-style command line flag
      completion on this value.) type: string default: ""

  Flags from /home/masayuki/work/swimmer_riscv/vendor/gflags/src/gflags_reporting.cc:
    -help (show help on all flags [tip: all flags can have two dashes])
      type: bool default: false currently: true
    -helpfull (show help on all flags -- same as -help) type: bool
      default: false
    -helpmatch (show help on modules whose name contains the specified substr)
      type: string default: ""
    -helpon (show help on the modules named by this flag value) type: string
      default: ""
    -helppackage (show help on all modules in the main package) type: bool
      default: false
    -helpshort (show help on only the main module for this program) type: bool
      default: false
    -helpxml (produce an xml version of help) type: bool default: false
    -version (show version and build info and exit) type: bool default: false

長すぎ!あとは、省略形(--outputを-oとしたり)ができないかなあ。