This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Makefile syntax in SDK examples

Hi !

I've seen that the Makefile given with the examples changed a lot between SDK11 and SDK12, lots of improvements ! However there's still something I don't get. Why do you use this syntax :

SRC_FILES += \
$(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_serial.c \
$(SDK_ROOT)/components/libraries/log/src/nrf_log_frontend.c \
$(SDK_ROOT)/components/libraries/button/app_button.c \

Instead of this one ?

SRC_FILES += $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_serial.c 
SRC_FILES += $(SDK_ROOT)/components/libraries/log/src/nrf_log_frontend.c 
SRC_FILES += $(SDK_ROOT)/components/libraries/button/app_button.c 

In my opinion the second one is better, mainly because it allows to do that :

SRC_FILES += $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_serial.c 
SRC_FILES += $(SDK_ROOT)/components/libraries/log/src/nrf_log_frontend.c 
SRC_FILES += $(SDK_ROOT)/components/libraries/button/app_button.c 
# Conditionally adding some_file to the build
ifeq($(SOME_MAKEFILE_VAR),1)
SRC_FILES += $(SDK_ROOT)/components/libraries/button/some_file.c 
endif

To conditionally add a file to the build, a folder to the include paths, a parameter to the compiler, etc.

By example you could use Makefile variables to add RTT files to the build, as well as defining (or not) NRF_LOG_USES_RTT.

Is there a reason for using the first syntax ?

  • The first syntax is a bit cleaner since each row only requires one variable. Also if, for example, you forget the "+" SRC_FILES will miss some of the .c files. The example below won't compile nrf_log_frontend.c

    SRC_FILES += $(SDK_ROOT)/components/libraries/log/src/nrf_log_backend_serial.c 
    SRC_FILES = $(SDK_ROOT)/components/libraries/log/src/nrf_log_frontend.c 
    SRC_FILES += $(SDK_ROOT)/components/libraries/button/app_button.c
    

    It does not preclude you from adding additional files later like in your example:

    ifeq($(SOME_MAKEFILE_VAR),1)
    SRC_FILES += $(SDK_ROOT)/components/libraries/button/some_file.c 
    endif
    
  • That's right but then you mix two syntaxes. For the point about forgetting some characters, I think whatever mistake you do, it will not end as expected. Anyway I'd typically copy-paste this kind of line which prevents the risk of failing the left part of the line since it's always the same.

    Small precision but I just ask this by curiosity, I don't think there is an absolute best practice here (even though I have a preference).

  • I think the SDK team have generated the SRC_FILES and INC_FOLDERS using a tool (i am guessing this). I agree to your point that now this wont allow conditional additions of file. Do you want to raise this as a bug?

  • I don't think it's necessary since the syntax used is totally valid. It's probably arbitrary if I find the other one better. Since I write a lot of Makefiles I was curious if the SDK team had a good reason to prefer a syntax I personally dislike over the other one. Anyway as andxnor mentioned it's also possible to combine both syntaxes, but then we end up with a mix of syntax which, IMHO, would be the worst.

    What kind of tools could they use ? At some point you have to specify which files to build, don't you ? Aside of the standard Makefile template we find in all projects, this plus the compiler/linker flags are the main content of the Makefile. Maybe they are extracted from the Keil project ? Their workflow may be interesting for me since I also have to maintain both a Makefile and a Keil project file for the same project.

Related