'gnu-make how to: Private or unique variable names on included makefiles
Our current main Makefile imports lots of sub makes. Along with auto dependency, the general format of each of these looks like this:
OBJS += \
$(OBJS_DIR)/<filename>.o \
...
C_DEPS += \
$(OBJS_DIR)/<filename>.d \
$(OBJS_DIR)/%.o: $(MODULES_PATH)/%.c $(OBJS_DIR)/%.d
$(GCC) ...
Where OBJS and C_DEPS are defined in the main makefile and accumulated across all included makefiles. BTW we are using -j in the call to make for parallel execution.
The first thing I tried was to change the fact that in each makefile each entry is listed twice, once in the OBJS and once again in the C_DEPS. The change I had in mind is the following:
OBJ := \
$(OBJS_DIR)/<filename>.o \
...
OBJS += $(OBJ)
C_DEPS += $(OBJ:.o=.d)
kind of making a local variable once and using it to add to the objects and dependency lists. But this doesn't work, there are at least two issues:
The first one is that each make include will overwrite the previous definition. The fact that the make files are included and not called results in all my attempts to use keywords like unexport and private prove useless and the variable OBJ is overwritten in each makefile.
I am concerned about make concurrency (
make -jis used) - should I be?
I was thinking of making the variable name dynamic but that also looks ugly and there is no guarantee that there won't be two makefiles with the same name from different paths included.
Is there any way around this?
Solution 1:[1]
Thanks to @Vroomfondel for the observations that lead to the simple answer. Saving the contents of the varibale in between include calls is actually what is performed by the concatination into OBJS and C_DEPS, the problem was that the variable was not expanded at the time it was read and after the accumelation was done it only held the value from the last makefile that set the value of OBJ. As it turns out it didnt realy matter what falver variable OBJ was, the problem lay in the fact that I had both OBJS and C_DEPS set as recursivly expanded variable. Once I set them to simply expanded varibles the issue was resolved, OBJ was expanded into them at reading time. That is from
OBJS =
C_DEPS =
To
OBJS :=
C_DEPS :=
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
