BUILD.gn中编写如下将编译信息传入TESTTIME、TESTAUTHOR、TESTVERSION,将上述定义用到代码中:
BUILD_TIME = exec_script("./test.py", [ "-t" ], "trim string")
BUILD_AUTHOR = exec_script("./test.py", [ "-a" ], "trim string")
BUILD_VERSION = exec_script("./test.py", [ "-v" ], "trim string")
static_library("testlib") {
sources = [
"test/source.c",
]
include_dirs = [
"test/include",
]
defines = [
"TESTTIME=\"${BUILD_TIME}\"",
"TESTAUTHOR=\"${BUILD_AUTHOR}\"",
"TESTVERSION=\"${BUILD_VERSION}\"",
]
}
用于获取编译信息的test.py脚本编写如下:
import argparse
from datetime import datetime
import time
from git import Repo
import os
def get_build_time():
build_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S,%f")