U-BOOT打印信息里的编译日期和时间的自动生成

时间:2023-02-08 06:02:08

U-BOOT的打印信息,可以清楚的显示该镜像的编译日期和时间,例如:

U-Boot 2013.01 (Nov 19 2013 - 14:12:53)

CPU0: P2020E, Version: 2.1, (0x80ea0021)
Core: E500, Version: 5.1, (0x80211051)

首先,查看u-boot的makefie

depend dep:     $(TIMESTAMP_FILE) $(VERSION_FILE) \
$(obj)include/autoconf.mk \
$(obj)include/generated/generic-asm-offsets.h \
$(obj)include/generated/asm-offsets.h
for dir in $(SUBDIRS) $(CPUDIR) $(LDSCRIPT_MAKEFILE_DIR) ; do \
$(MAKE) -C $$dir _depend ; done

编译要依赖 $(TIMESTAMP_FILE)

$(TIMESTAMP_FILE):
@mkdir -p $(dir $(TIMESTAMP_FILE))
@LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > $@.tmp
@LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> $@.tmp
@cmp -s $@ $@.tmp && rm -f $@.tmp || mv -f $@.tmp $@

在编译的时候,可以动态生成$(TIMESTAMP_FILE)文件,里面动态包含了当前日期和时间

TIMESTAMP_FILE = $(obj)include/generated/timestamp_autogenerated.h

查看u-boot代码,在一阶段初始化序列中会执行display_options

int display_options (void)
{
#if defined(BUILD_TAG)
printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
#else
printf ("\n\n%s\n\n", version_string);
#endif
return 0;
}
version_string字符串common/version.h定义

U_BOOT_VERSION_STRING在inclue/version.h定义

U_BOOT_DATE和U_BOOT_TIME这是在timestamp_autogenerated.h定义

#define U_BOOT_VERSION_STRING U_BOOT_VERSION " (" U_BOOT_DATE " - " \
U_BOOT_TIME ")" CONFIG_IDENT_STRING

const char __weak version_string[] = U_BOOT_VERSION_STRING;

借鉴这个套路,可以轻松在C源码中获得日期和时间字符串来应用到需要的地方,下面是一个简单的测试

目录结构

auto_date.c  Makefile  tmp

C源码

#include<stdio.h>
#include"tmp/auto.h"

#define MY_STR "("MY_STR_DATE"---"MY_STR_TIME")"
int main()
{
printf("%s\n",MY_STR);
return 0;


}

Makefile

all:auto
@gcc auto_date.c
@echo compile completed
auto:
@date +'#define MY_STR_DATE "%a %b %d %Y"' > tmp/auto.h
@date +'#define MY_STR_TIME "%T"' >> tmp/auto.h
clean:
rm tmp/auto.h
rm a.out