Conditional Dependencies in Make

Make is the de facto build tool on Linux. I needed a way to conditionally require a dependency. Here is how I did it:

Before any of the executable code, I placed this if clause:

ifeq ($(findstring $(TARGET),$(wildcard *.s)), $(TARGET))
    conditional_file_exists = $(TARGET).o
else
    conditional_file_exists = $()
endif

Then, I placed $(conditional_file_exists) in the dependency list. If that .s file exists, it adds the object file to the dependency list, otherwise it doesn’t.

$(AXF_FILE): $(C_FILE).o $(conditional_file_exists)
...

Leave a Reply

Your email address will not be published. Required fields are marked *