Detect compilation for ChibiOS

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

Detect compilation for ChibiOS

Post 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....?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Detect compilation for ChibiOS

Post by Giovanni »

Hi,

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

Giovanni
mikeprotts
Posts: 166
Joined: Wed Jan 09, 2019 12:37 pm
Has thanked: 19 times
Been thanked: 31 times

Re: Detect compilation for ChibiOS

Post 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
Post Reply