Adding set operations to GNU Make

[article]
Summary:

The new GMSL functions include ways of testing the relationship between sets and determining whether an individual element is in a set.

usa := $(call set_create,blue white red white red white red white red white red white red white red)

Printing the value of usa would reveal blue red white.

Equality, subsets and elements
The new GMSL functions include ways of testing the relationship between sets and determining whether an individual element is in a set.

The set_is_subset, set_equal and set_is_member functions use the GMSL $(true) and $(false) variables to return values. These special variables are in fact the string T (for $(true)) and an empty string (for $(false)) so that the result of a GMSL truth function can be used with built in GNU Make functions like $(if). To determine if the US and French flags contain the same colors the set_equal function can be used (this example prints Colors are the same and illustrates the use of set_equal inside the GNU make $(if) function):

all:
@echo Colors are $(if $(call set_equal,$(france),$(usa)),the same,different)

To determine if oneset is a subset of another (i.e. all the elements of the first set are included in the second), the GMSL has the set_is_subset function. The Japanese flag consists of a red circle on a white background and its colors can be defined using set_create:

japan := $(call set_create,white red)

To determine if the set of colors in the Japanese flag is a subset of the French flag use set_is_subset (once again the GMSL function is used in conjunction with GNU Make's $(if) and causes Japanese colors are a subset of French colors to be echoed):

all:
@echo Japanese colors are $(if $(call set_is_subset,$(japan),$(france)),,not) a subset of French colors

The German flag is colored black, red, and yellow and hence the following test shows that the German colors are not a subset of the French colors:

germany := $(call set_create,black red yellow)
all:
@echo German colors are $(if $(call set_is_subset,$(germany),$(france)),,not) a subset of French colors

Finally, to check if an individual element is in a set use set_is_member. For example, red is present in the US flag:

all:
@echo Red is $(if $(call set_is_member,red,$(usa)),,not) a color in the US flag

Union and intersection
The last two set related functions are set_union and set_intersection. They implement the standard union and intersection operations on a set.

So to create the set of colors that appear in either the French or German flags take the union of the two sets:

all:
@echo The French and German flags have the following colors: $(call set_union,$(france),$(germany))


which prints The French and German flags have the following colors: black blue red white yellow. To find out the colors that are common between Japan and the USA use the intersection of the two sets:

all:
@echo The Japanese and US flags have the following colors in common: $(call set_intersection,$(japan),$(usa))

which will display The Japanese and US flags have the following colors in common: red white.

Conclusion
Next time I'll use these set functions to maintain a set of breakpoints (which will be the names of targets to break on) as I enhance the GNU Make Debugger to allow the user to enter breakpoints at the debugger prompt.

About the author

CMCrossroads is a TechWell community.

Through conferences, training, consulting, and online resources, TechWell helps you develop and deliver great software every day.