Page 1 of 1

Detect compilation for ChibiOS

Posted: Thu Oct 15, 2020 10:42 am
by avrhack
Sure this is a simple one but struggling to figure it out from the various makefiles.....

I have a library routine (.h and .c) that needs to use e.g. chsnprintf instead of sprintf when compiled for ChibiOS. I can obviously add my own macro and conditional compilation, but I note that _CHIBIOS_HAL_ is defined when using the HAL, and streams that contain chsnprintf is part of the HAL.

Problem is that this macro seems to be defined in the hal.h file so you can't use _CHIBIOS_HAL_ macro to conditionally compile the hal.h and associated headers that don't exist for non-ChibiOS builds.

I had expected there to be some macro defined in the makefiles specific to ChibiOS, which would then allow me to conditionally pull in the relevant ChibiIOS-specifics header files like hal.h and also pick up chsnprintf instead of snprintf but I can't see how to do that.

Thoughts anyone on the elegant way to do this, before I just go adding my own specific PREPROC define in the makefile and look for that in my code....?

Re: Detect compilation for ChibiOS

Posted: Thu Oct 15, 2020 10:46 am
by Giovanni
Hi,

There are no macros defined into the makefile, not all implementations use makefiles, you may add it yourself using -D

Giovanni

Re: Detect compilation for ChibiOS

Posted: Thu Oct 15, 2020 2:21 pm
by mikeprotts
Not entirely elegant, but would this work, coding everything as chsnprintf etc and:

#if !defined(chsnprintf)
#define chsnprintf snprintf
#endif

or maybe use your own mysnprintf that maps as:

#if defined(chsnprintf)
#define myprintf chsnprintf
#else
#define myprintf snprintf
#endif

or else use a prefix (e.g. MYPREFIXsnprintf) with:
#if defined(chsnprintf)
#define MYPREFIX ch
#else
#define MYPREFIX
#endif

Mike