FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

Google-gflagsでフラグを別のファイルから参照したい場合

例えば、swimmer_main.cppでは、Google-gflagsを使って、いくつかのオプションフラグを宣言している。

// FLAGs definition
DEFINE_string (hexfile, "", "Hex file to simulate.");
DEFINE_string (binfile, "", "Binary file to simulate.");
DEFINE_string (imgfile, "", "Image file to simulate.");
DEFINE_bool (debug, false, "Generate debug log");
DEFINE_bool (debug_func, false, "Generate funciton debug log");
DEFINE_bool (debug_gvar, false, "Generate global variable debug log");
DEFINE_string (out, "", "Output log filename");
DEFINE_int64 (max, 65535, "Max instructions to simulate");
#ifdef ARCH_MIPS
DEFINE_string (init_pc, "0xbfc00000", "Initial program counter value");
#else  // ARCH_MIPS
#ifdef ARCH_RISCV
DEFINE_string (init_pc, "0x00000200", "Initial program counter value");
#endif // ARCH_RISCV
#endif // ARCH_MIPS
DEFINE_bool (stop_host, true, "Stop simulation by accessing MTOHOST");
DEFINE_string (script, "", "Initial script to simulate");
DEFINE_bool (load_dump, false, "Dump Loaded Bin file (only enable with --binfile)");
DEFINE_bool (img_dump, false, "Dump Loaded Bin file (only enable with --imgfile)");
DEFINE_string (uart_out, "", "Filename of UART output (default: console)");
DEFINE_string (reg_abi, "hard", "Register Calling Convention Mode in trace log");

これを使って、別のファイルからこのオプションを参照したいとき、いままでは外部変数に一度渡してから、別ファイルで読み込んでいた。

g_debug_enable = FLAGS_debug;

(別ファイルにて)

extern bool g_debug_enable;   // declare from swimmer_main.cpp

しかし、実際にはこの必要は無く、Google-GflagsのDECLAREマクロを使えば、外部参照を実現できる。

(別ファイルにて)

DECLARE_bool (debug);

これで、別ファイル中でもFLAGS_debugスイッチに参照できるようになった。これに気がつかなかったため、だいぶ面倒なことをしていたが、ふとGflagsのドキュメントを見直していて発見。即座に自分のISSをこのスイッチを使って修正。

github.com