how to parse parameter value passed to make file - makefile

I have a makefile which takes a TYPE parameter and runs a test file using that name. Otherwise it takes all the test files.
ifneq (,$(TYPE))
TEST_SRCS := $(wildcard test/$(TYPE)Test.cpp)
else
TEST_SRCS := $(wildcard test/*Test.cpp)
endif
print:
echo $(TEST_SRCS)
This works fine If I execute like,
make TYPE=Add
But Now I want to give more than one value to TYPE So that it should take both AddTest.cpp and SubtractTest.cpp
e.g.
make TYPE=Add,Subtract
I have tried $(foreach var,$(TYPE), TEST_FILES += var)

Make isn't good at this kind of manipulation, but you can break a comma-separated list into a space-separated list like this:
COMMA = ,
TYPE_LIST = $(subst $(COMMA), ,$(TYPE))
Then you can apply your wildcard logic like this:
FILE_LIST = $(patsubst %,test/%Test.cpp,$(TYPE_LIST))
TEST_SRCS := $(wildcard $(FILE_LIST))

Related

GNU Make: Remove string from array with subdirectories if string contains "dirToExclude"

I have a directory with a lot of subdirectories which are containing source-files. Therefore I am looking in every subdirectoy for .c-files: cSources = $(shell dir /s /b *.c)
However there are subdirectories with source-files I do not want to compile. If I have a variable dirToExclude = dirName how can I remove every string in cSources that contains 'dirName'?
I know that there is the findstring-function. But I can't figure out how to use findstring on every string in the array. I am looking for some way to get a nested loop like (pseudo code):
foreach (var in cSources)
if (var.contains(dirToExclude))
cSources -= var
endif
If you know dirToExclude is always at the beginning of the string you can just write:
cSources := $(filter-out $(dirToExclude)%,$(cSources))
If you want to remove any filename where the string appears anywhere, you can use:
cSources := $(foreach S,$(cSources),$(if $(findstring $(dirToExclude),$(S)),,$(S)))
See https://www.gnu.org/software/make/manual/html_node/Functions.html

Makefile issue with multiple variable assignment

I have issues assigning the same variable multiple times in Makefile.
I am making a kind of modular makefile where I only deal with components instead of every source file and include directory.
This first solution works fine
MainRootDir = Components/Main
include $(RootDir)/$(MainRootDir)/Main.mak
Debug_Includes += $(addprefix $(MainRootDir)/,$(Main_Includes))
Debug_ASM_Sources += $(addprefix $(MainRootDir)/,$(Main_ASM_Static))
Debug_CC_Sources += $(addprefix $(MainRootDir)/,$(Main_CC_Static))
Debug_ASM_Sources += $(addprefix $(CfgDir)/, $(Main_ASM_Config))
Debug_CC_Sources += $(addprefix $(CfgDir)/, $(Main_CC_Config))
AdderRootDir = Components/Adder
include $(RootDir)/$(AdderRootDir)/Adder.mak
Debug_Includes += $(addprefix $(AdderRootDir)/,$(Adder_Includes))
Debug_ASM_Sources += $(addprefix $(AdderRootDir)/,$(Adder_ASM_Static))
Debug_CC_Sources += $(addprefix $(AdderRootDir)/,$(Adder_CC_Static))
Debug_ASM_Sources += $(addprefix $(CfgDir)/, $(Adder_ASM_Config))
Debug_CC_Sources += $(addprefix $(CfgDir)/, $(Adder_CC_Config))
However, if I try to standardize the block by defining variables and Re-Assigning new value before adding the software component each time, it stops working:
SwcName = Main
SwcRootDir = Components/$(SwcName)
include $(RootDir)/$(SwcRootDir)/$(SwcName).mak
Debug_Includes += $(addprefix $(SwcRootDir)/,$($(SwcName)_Includes))
Debug_ASM_Sources += $(addprefix $(SwcRootDir)/,$($(SwcName)_ASM_Static))
Debug_CC_Sources += $(addprefix $(SwcRootDir)/,$($(SwcName)_CC_Static))
Debug_ASM_Sources += $(addprefix $(CfgDir)/, $($(SwcName)_ASM_Config))
Debug_CC_Sources += $(addprefix $(CfgDir)/, $($(SwcName)_CC_Config))
SwcName = Adder
SwcRootDir = Components/$(SwcName)
include $(RootDir)/$(SwcRootDir)/$(SwcName).mak
Debug_Includes += $(addprefix $(SwcRootDir)/,$($(SwcName)_Includes))
Debug_ASM_Sources += $(addprefix $(SwcRootDir)/,$($(SwcName)_ASM_Static))
Debug_CC_Sources += $(addprefix $(SwcRootDir)/,$($(SwcName)_CC_Static))
Debug_ASM_Sources += $(addprefix $(CfgDir)/, $($(SwcName)_ASM_Config))
Debug_CC_Sources += $(addprefix $(CfgDir)/, $($(SwcName)_CC_Config))
The issue is that the source files of component Main are defined multiple times.
As if the Adder block is also expanding SwcName with the value Main.
Any idea how to prevent this?
You are using recursively expanded variables (var = ...), which is the default. So make defers the evaluation of the right hand side of your assignments until the variable's value is really needed. Example:
a = 1
b += $(a)
a = 2
b += $(a)
.PHONY: all
all:
#echo $(b)
will print 2 2 instead of the 1 2 you expect because what make actually stored in variable b was $(a) $(a), which it evaluated only before passing the recipe of all to the shell. And at that time a has only one single value: the last one it has been assigned, that is, 2.
You could use simply expanded variables (var := ...), instead:
b :=
a := 1
b += $(a)
a := 2
b += $(a)
.PHONY: all
all:
#echo $(b)
which will print 1 2. Do not forget the apparently useless b :=: it tells make that b is not a recursively expanded variable but a simply expanded one. Different from the default form, the value assigned to a simply expanded variable is evaluated when it is defined, its expansion is not deferred until the variable's value is needed.
But repeating large portions of code as in what you show is not optimal. Indeed, repeating things is frequently needed with humans but almost never optimal with computers. If you are using GNU make, as suggested by tripleee, you could use a kind of user-defined function:
Debug_Includes :=
Debug_ASM_Sources :=
...
# $(1) is the current SwcName
define MY_FUNC
SwcRootDir := Components/$(1)
include $$(RootDir)/$$(SwcRootDir)/$$(1).mak
Debug_Includes += $$(addprefix $$(SwcRootDir)/,$$($(1)_Includes))
Debug_ASM_Sources += $$(addprefix $$(SwcRootDir)/,$$($$(1)_ASM_Static))
...
endef
and then:
$(foreach SwcName,Main Adder,$(eval $(call MY_FUNC,$(SwcName))))
Do not forget the $$ in the macro definition. They are needed. See the GNU make manual for the full explanation.
This is a matter of recursive vs simply expanded variables. I'm going to guess that Debug_Includes is not initialized with a := earlier on, which means it defaults to be a recursively expanded variable. Therefore it is set to $(RootDir)/$(SwcRootDir)/$(SwcName).mak, and the internal variables are only expanded when it is referenced (at which point SwcRootDir will be Components/$(SwcName)).
Try initializing your variables with := at the top of your file to make them simple. Then they will be assigned values right away, and the internal variables will be set to what they were at the time of the definition.

Makefile: If string ends with other string

I'm trying to have rule that if VAR ends with ENDS_WITH it does X else it does Y. Now, I was able to kinda achieve this with the following:
VAR := Hello, World
ENDS_WITH := rld
endswith:
ifeq ($(findstring $(ENDS_WITH)potato,$(VAR)potato),)
#echo "$(VAR) doesn't end with $(ENDS_WITH)"
# Do X
else
#echo "$(VAR) ends with $(ENDS_WITH)"
# Do Y
endif
However, this assumes neither string contains potato otherwise it will have strange behavior. (Also it's kinda a hack)
What would be the correct way to go about achieving this?
You can use this:
$(patsubst %$(ENDS_WITH),,$(lastword $(VAR)))
This will expand to an empty string if VAR ends with the value of the variable ENDS_WITH, else the non-empty string. We have to use lastword here because patsubst works on each word individually but we only care about the last one.
So:
endswith:
ifeq ($(patsubst %$(ENDS_WITH),,$(lastword $(VAR))),)
#echo "$(VAR) ends with $(ENDS_WITH)"
# Do Y
else
#echo "$(VAR) doesn't end with $(ENDS_WITH)"
# Do X
endif

Why does an escaped variable $$ work and not $

This is a follow up question to the previous one I had asked in the thread:
search subdirectories and update path variables in makefile
In the following:
There is a rules makefile which adds various standard defines,
Include.mak. It has a define:
RESOLVED_PATH=$(subst $(abspath .)/,,$(abspath $(1)))
It is being used in another makefile:
include Include.mak
ROOT:=.
define UPDATE_GUI
d:= $$(strip $(1))
GUI_FOLDER:= $(ROOT)/$(d)
GUI_ALL:=$(call RESOLVED_PATH,$(GUI_FOLDER)/$(d)_generated.txt)
$$(warning GUI_ALL is set to $$(GUI_ALL))
endef
TARGET_FILE:= ProjectParms.rb
ACTIVE_GUIS = $(wildcard GUI*/$(TARGET_FILE))
GUIS_OF_INTEREST = $(dir $(ACTIVE_GUIS))
GUIS= $(patsubst %/,%,$(GUIS_OF_INTEREST))
$(foreach GUI, $(GUIS), $(eval $(call UPDATE_GUI, $(GUI))))
This does not give the output as desired, but if I expand the same in the line:
GUI_ALL:=$$(subst $$(abspath .)/,,$$(abspath $$(GUI_FOLDER)/$$(d)_generated.txt)
$$(warning GUI_ALL is set to $$(GUI_ALL))
It works fine, can someone tell me why? The $(call RESOLVED_PATH, Param ) is being used throughout the make project in different places (though no $(warning '') is being used anywhere else, I am just using that for a quick sanity check.
You have to escape everything in the function except the actual call arguments. Sometimes you don't want this but almost all the time you do. So it should be:
define UPDATE_GUI
d := $$(strip $(1))
GUI_FOLDER := $$(ROOT)/$$(d)
GUI_ALL := $$(call RESOLVED_PATH,$$(GUI_FOLDER)/$$(d)_generated.txt)
$$(warning GUI_ALL is set to $$(GUI_ALL))
endef
For debugging, you can replace the eval function with the info function; this will print out for you what the makefile syntax is that eval would work on. If you do that, you'll see that by the time eval gets the text to parse it's already wrong, because call expanded it. E.g.,
$(foreach GUI,$(GUIS),$(info $(call UPDATE_GUI,$(GUI))))
ETA
I will point out that you can greatly simplify this by removing the call function altogether. In order to do this you'll have to be OK with the UPDATE_GUI define being tightly bound with the foreach. Since you're just passing one argument to call anyway, you can just not do that and put the GUI variable (the loop variable in foreach) directly into the UPDATE_GUI define; then you don't need call. Like this:
define UPDATE_GUI
d := $(strip $(GUI))
GUI_FOLDER := $(ROOT)/$(d)
GUI_ALL := $(call RESOLVED_PATH,$(GUI_FOLDER)/$(d)_generated.txt)
$(warning GUI_ALL is set to $(GUI_ALL))
endef
$(foreach GUI,$(GUIS),$(eval $(UPDATE_GUI)))

Variable based on active makefile target (outside rule)

I've got a build configuration that looks something like this:
Makefile:
build-for-x: X := yes
build-for-x: all
build-for-y: Y := yes
build-for-y: all
SRC :=
include foo.mk
include bar.mk
all: # ... some targets which use SRC eventually
foo.mk:
ifeq ($(X),yes)
SRC += foo_x.c
endif
ifeq ($(Y),yes)
SRC += foo_y.c
endif
bar.mk:
ifeq ($(X),yes)
SRC += bar_x.c
endif
ifeq ($(Y),yes)
SRC += bar_y.c
endif
This is obviously highly simplified. What I'm actually doing is building the same codebase for two different embedded platforms, with many different modularized sections (foo and bar here) and several variables for source, include, tests, etc.
The above doesn't work, because X or Y are only set within the targets which build-for-x/build-for-y have as prerequisites. The code within foo.mk/bar.mk is evaluated beforehand, when neither variable is set.
Is there a good way to handle situations like this? What's a sane way I might restructure my build configuration to:
Preserve the modularity (foo.mk/bar.mk are separate files)
Allow one to run make build-for-x/make build-for-y to build the separate implementations
I want to avoid making foo.mk look something like:
build-for-x: SRC += foo_x.c
build-for-y: SRC += foo_y.c
unless I absolutely have to, since this forces me to have a dependency on a particular target. I would much rather keep all the target-dependent assignments at the top level.
How about this:
Makefile:
build-for-x: SRC := SRC_X
build-for-x: all
build-for-y: SRC := SRC_Y
build-for-y: all
foo.mk:
SRC_X += foo.x_c
SRC_Y += foo.y_c
bar.mk:
SRC_X += bar.x_c
SRC_Y += bar.y_c

Resources