Apply CFLAGS to user source code only

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
nicolas
Level 1
Level 1
First like given 5 replies posted 10 sign-ins

Hello, 

I am using ModusToolbox build system in the IDE mode. 

Is there a way in the Makefile to make the CFLAGS to be applied only to user (my) source code files, and not to all the framework  source files ?

Regards

Nicolas

0 Likes
1 Solution
Rakshith
Moderator
Moderator
Moderator
250 likes received 1000 replies posted 750 replies posted

Hi @nicolas

You could try following the steps mentioned in this thread - Compile option settings for one file or follow the steps mentioned below - 

Edit the application Makefile, by appending the following code snippet to the end of the Makefile (after the line with "include $(CY_TOOLS_DIR)/make/start.mk"):

define CY_MACRO_CUSTOM_CFLAGS
$(filter %$(1:%.c=%.o),$(CY_BUILD_ALL_OBJ_FILES)): $(filter %$(1),$(CY_SEARCH_APP_SOURCE) $(SOURCES))
    $$(info $$(CY_INDENT)Compiling file $$(CY_COMPILE_PRINT) with $(2))
    $$(CY_NOISE)$$(CY_BUILD_COMPILE_C) $$@ $$< $(2)
endef
 
ifneq ($(CY_SECONDSTAGE),)
  $(eval $(call CY_MACRO_CUSTOM_CFLAGS,main.c,-Wextra -Werror))
endif

The snippet above defines the additional C compiler flags: "-Wextra -Werror" for a single file main.c

Please let me know if this works.

Thanks and Regards,
Rakshith M B

View solution in original post

0 Likes
3 Replies
Rakshith
Moderator
Moderator
Moderator
250 likes received 1000 replies posted 750 replies posted

Hi @nicolas

You could try following the steps mentioned in this thread - Compile option settings for one file or follow the steps mentioned below - 

Edit the application Makefile, by appending the following code snippet to the end of the Makefile (after the line with "include $(CY_TOOLS_DIR)/make/start.mk"):

define CY_MACRO_CUSTOM_CFLAGS
$(filter %$(1:%.c=%.o),$(CY_BUILD_ALL_OBJ_FILES)): $(filter %$(1),$(CY_SEARCH_APP_SOURCE) $(SOURCES))
    $$(info $$(CY_INDENT)Compiling file $$(CY_COMPILE_PRINT) with $(2))
    $$(CY_NOISE)$$(CY_BUILD_COMPILE_C) $$@ $$< $(2)
endef
 
ifneq ($(CY_SECONDSTAGE),)
  $(eval $(call CY_MACRO_CUSTOM_CFLAGS,main.c,-Wextra -Werror))
endif

The snippet above defines the additional C compiler flags: "-Wextra -Werror" for a single file main.c

Please let me know if this works.

Thanks and Regards,
Rakshith M B
0 Likes

Hello Rakshith,

Thanks for your help.

Unfortunately this is not working for me. Some of the variables seems to be empty (CY_COMPILE_PRINT), log from the build is"Compiling file with -Wextra -Werror".
Also added an echo of the CY_BUILD_COMPILE_C after the eval but it does not include the additionals CFLAGS.  Checked also the main.c compilation line in the log but extra CFLAGS are not there.

I got a "sources" directory at the same level of the Makefile (I tried also "sources/main.c" instead of "main.c" in your proposal), I would need to be able to add extra flags to all the source files recursively in that folder.

Regards

Nicolas

 

 

0 Likes
AlenAn14
Moderator
Moderator
Moderator
500 replies posted 100 solutions authored 250 replies posted

Hi @nicolas ,

Can you please let us know the exact error you are receiving in modus Toolbox when you added the code snippet provided by @Rakshith in the Makefile.

NOTE: Make sure to use tabs, as the Makefile syntax supports tabs and not spaces

AlenAn14_0-1639565430763.png

 

Regards
Alen

0 Likes