开发者

Can makefile variables be assigned with values read from source files?

开发者 https://www.devze.com 2023-01-07 20:34 出处:网络
Suppose there is a C program, which stores its version in a global char* in main.c. Can the buildsystem (gnu make) somehow extract the value of this variable on build time, so that the executable buil

Suppose there is a C program, which stores its version in a global char* in main.c. Can the buildsystem (gnu make) somehow extract the value of this variable on build time, so that the executable built could have the exact version name as it appears in the program?

What I'd like to achieve is that given the source:

char g_version[] = "Superprogram 1.32 build 1142";

the buildsystem would generate an executable named Superprogram 1.32 bu开发者_如何学运维ild 1142.exe


The shell function allows you to call external applications such as sed that can be used to parse the source for the details required.


Define your version variable from a Macro:

char g_version[] = VERSION;

then make your makefile put a -D argument on the command line when compiling

gcc hack.c -DVERSION=\"Superprogram\ 1.99\"

And of course you should in your example use sed/grep/awk etc to generate your version string.


You can use any combination of unix text tools (grep, sed, awk, perl, tail, ...) in your Makefile in order to extract that information from source file.


Usually the version is defined as a composition of several #define values (like in the arv library for example).

So let's go for a simple and working example:

// myversion.h
#define  __MY_MAJOR__ 1
#define  __MY_MINOR__ 8

Then in your Makefile:

# Makefile
source_file := myversion.h
MAJOR_Identifier := __MY_MAJOR__
MINOR_Identifier := __MY_MINOR__

MAJOR := `cat $(source_file) | tr -s ' ' | grep "\#define $(MAJOR_Identifier)" | cut -d" " -f 3`
MINOR := `cat $(source_file) | tr -s ' ' | grep '\#define $(MINOR_Identifier)' | cut -d" " -f 3`

all:
  @echo "From the Makefile we extract: MAJOR=$(MAJOR) MINOR=$(MINOR)"

Explanation

Here I have used several tools so it is more robust:

  • tr -s ' ': to remove extra space between elements,
  • grep: to select the unique line matching our purpose,
  • cut -d" " -f 3: to extract the third element of the selected line which is the target value!

Note that the define values can be anything (not only numeric).

Beware to use := (not =) see: https://stackoverflow.com/a/10081105/4716013

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号