[GN] 生成bin和hex

gn對於工具鏈的配置只到可執行文件就結束了,在我們之前配置的armcc中,最終生成axf就算結束了,但對於一些應用場景來說,bin和hex是必須的,因此還需要執行額外的步驟來實現bin和hex的生成,對於bin和hex,arm工具鏈中提供了fromelf來做這件事情。

構建後動作

gn提供了目標action來做工具鏈之外的動作,action可以接受python腳本,因此我們的選擇就是在python腳本中調用fromelf來生成bin和axf。首先看gn中的配置

action("build_target") {
  deps = [
    ":gn_demo",
  ]
  script = "//build/py_script/axf_format.py"
  sources = [
    "$root_out_dir/bin/gn_demo.axf",
  ]
  outputs = [
    "$root_out_dir/bin/gn_demo.bin",
  ]
  args = [
    "--format_tool",
    rebase_path("$MDK_DIR/ARM/ARMCC/bin/fromelf.exe"),
    "--axf",
    rebase_path("$root_out_dir/bin/gn_demo.axf"),
    "--dir",
    rebase_path("$root_out_dir/bin"),
    "--flag",
    "b",
  ]
}

gn指定了一個action來執行生成動作,生成動作將會調用axf_format.py文件,該部分依賴於axf的生成。即當構建完成後,將會調用該腳本來將axf轉換成bin和hex。此處調用了一個腳本,腳本如下:

import os
import sys
import getopt

format_tool = ""
axf_path = ""
out_dir = ""
flag = ""

#獲取命令行參數
try:
    opts, args = getopt.getopt(sys.argv[1:],"",["format_tool=","axf=","dir=","flag="])
except getopt.GetoptError:
    print("axf_format.py --format_tool <tool_path> --axf <axf_path> --dir <out_dir> --flag <b or h>")
    sys.exit(2)
for opt, val in opts:
    if opt in ("--format_tool"):
        format_tool = val
    if opt in ("--axf"):
        axf_path = val
    if opt in ("--dir"):
        out_dir = val
    if opt in ("--flag"):
        flag = val

#獲取axf的文件名,根據此文件名指定輸出的文件名
(axf_name, axf_ext) = os.path.splitext(os.path.basename(axf_path))

bin_path = "%(path)s/%(name)s.bin"%{'path':out_dir,'name':axf_name}
hex_path = "%(path)s/%(name)s.hex"%{'path':out_dir,'name':axf_name}

#生成bin文件
if "b" in flag:
    os.system("%(tool)s --bin --output %(bin)s %(axf)s"%{'tool':format_tool,'bin':bin_path,'axf':axf_path})
#生成hex文件
if "h" in flag:
    os.system("%(tool)s --i32 --output %(hex)s %(axf)s"%{'tool':format_tool,'hex':hex_path,'axf':axf_path})

腳本主要的作用是獲取傳入的命令行參數,根據參數執行轉換工作。

結果測試

可以看到在執行結束後看到了bin和hex文件,這樣就完成了預期的所有工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章