Add gcc preprocesor macro with git msg

Hi,
preprocessor of gcc has many useful standard predefined macros doc, of which I usually use two:

__DATE__
__TIME__

that describes the date and time on which the preprocessor is being run.

I also notice that Workbench (toolchains) defines some useful macros, e.g:
SYSTEM_VERSION_STRING

I only miss the macro associated with the application git repository, so I modified file .particle/toolchains/deviceOS/1.4.2/firmware-1.4.2/build/version.mk and add new macro GIT_MSG

ifneq "$(wildcard $(APPDIR)/.git )" "" #check if local repo exist
#Create variable GIT_MSG with:
#1) git sha and additionally dirty flag
GIT_MSG := sha:$(shell git --git-dir=${APPDIR}/.git --work-tree=${APPDIR} --no-pager describe --tags --always --dirty)
#2) last commit msg
GIT_MSG += msg:$(shell git --git-dir=${APPDIR}/.git --work-tree=${APPDIR} --no-pager log -1 --pretty=%B)
else
GIT_MSG := Can't find git repo
endif
#add user define macro (-D) to gcc
CFLAGS += -DSYSTEM_VERSION_STRING=$(VERSION_STRING) -DGIT_MSG=\"$(strip "$(GIT_MSG)")\"

Now I can use it to print full info about source (user app) and compilation, e.g:

#ifdef __INTELLISENSE__
#define GIT_MSG "This macro will be evaluated after compile (gcc macro -DGIT_MSG)"
#endif
void setup() {
Serial.printf("Compile date/time: %s %s\n", __DATE__, __TIME__);  
Serial.printf("Git: %s\n", GIT_MSG);
Serial.printf("System ver: %s", PP_STR(SYSTEM_VERSION_STRING));
}

After that I get this output on console:

Serial connection closed.  Attempting to reconnect...
Serial monitor opened successfully:
Compile date/time: Nov 12 2019 11:55:47
Git: sha:17d3722-dirty msg:Init 
System ver: 1.4.2

or if repo does not exist:

Serial monitor opened successfully:
Compile date/time: Nov 12 2019 12:27:21
Git: Can't find git repo 
System ver: 1.4.2
4 Likes

neat! i think it’s worth proposing the change over here:

(opening a pull request is usually the best approach)

:pray::+1:

1 Like

Hi m_m,
in v1.4.2.tar.gz I found dir .workbench with json files where are defined preprocessor macros for intellisense, but I have problem with finding this files in official repo device-os/tree/v1.4.2
I would like add new macro in this files (in “defines” section) for my a pull request.

the .workbench/intellisense/<platform>.json files are generated when we release a new version of the Device OS source bundle that Workbench uses so your new macro / define should be picked up automatically.

maybe call out in your PR description that you expect the new macro to be recognized by intellisense and we can confirm that it will be :+1:

Thanks for the suggestion.

My take is that since this is specific functionality for your application and your workflow, it’s best implemented in a custom user makefile. Add a file named build.mk in your application src directory and it will get picked up by Workbench when doing local compilation.

Note that since this is advanced customized functionality you may need to update this file in a future Device OS release due to build system changes.

# src/build.mk
ifneq "$(wildcard $(APPDIR)/.git )" "" #check if local repo exist
#Create variable GIT_MSG with:
#1) git sha and additionally dirty flag
GIT_MSG := sha:$(shell git --git-dir=${APPDIR}/.git --work-tree=${APPDIR} --no-pager describe --tags --always --dirty)
#2) last commit msg
GIT_MSG += msg:$(shell git --git-dir=${APPDIR}/.git --work-tree=${APPDIR} --no-pager log -1 --pretty=%B)
else
GIT_MSG := Can't find git repo
endif
#add user define macro (-D) for gcc preprocessor
CFLAGS += -DGIT_MSG=\"$(strip "$(GIT_MSG)")\"

# default functionality from https://github.com/particle-iot/device-os/blob/e9b7157c04298f976819af77b4d88c0c915571a3/user/build.mk#L57
INCLUDE_DIRS += $(SOURCE_PATH)/$(USRSRC)  # add user sources to include path
# add C and CPP files - if USRSRC is not empty, then add a slash
CPPSRC += $(call target_files,$(USRSRC_SLASH),*.cpp)
CSRC += $(call target_files,$(USRSRC_SLASH),*.c)

APPSOURCES=$(call target_files,$(USRSRC_SLASH),*.cpp)
APPSOURCES+=$(call target_files,$(USRSRC_SLASH),*.c)

Thanks for your solution.

It is what I was looking for, now I can customize (add/modified) preprocessor define/macro for my application.

1 Like