The GNU Make Standard Library

[article]
Summary:

The GMSL contains functions for list and string manipulation, has a complete integer arithmetic library, and functions for data structures: there are GNU Make implementations of associative arrays and stacks. There are also built-in debugging facilities.

The GMSL contains functions for list and string manipulation, has a complete integer arithmetic library, and functions for data structures: there are GNU Make implementations of associative arrays and stacks. There are also built-in debugging facilities.

In this article I'll show how to use the functions of the GMSL in a realistic Makefile.Importing the GMSLThe GMSL is implemented as a pair of Makefiles named gmsl and __gmsl. __gmsl is imported by gmsl, so to include the GMSL in your Makefile simply add the line

    include gmsl 

You can do this in as many files as you want, the GMSL automatically detects if it has already been included to prevent multiple definitions and unintended error messages.

Of course, GNU Make must be able to find gmsl and __gmsl. GNU Make looks in a number of places by default for Makefiles: /usr/local/include, /usr/gnu/include/, /usr/include, the current directory and any directories specified by the GNU Make -I (or -include-dir) command-line option.

A good place to put gmsl and __gmsl is /usr/local/include : then they'll be available to all your Makefiles.

If GNU Make can't find gmsl or __gmsl you'll get the regular GNU Make error message such as:

    Makefile:1: gmsl: No such file or directory 

Calling a GMSL FunctionThe functions in the GMSL are implemented as variable declarations. For example the function last (which returns the last element of a list) is declared like this:

    last = $(if $1,$(word $(words $1),$1)) 

And the function is called by using GNU Make's $(call) built-in function. For example, to return the last element of the list 1 2 3 do

    $(call last,1 2 3) 

which will return 3. $(call) expands the variable named in its first argument (in this case last ) setting special local variables, $1, $2, $3, ... to the argument given to $(call) after the function name. So $1 is 1 2 3 in this case.

The GMSL defines the boolean values true and false, which are just variables and can be accessed using $() or ${} : for example, $(true) or ${false}. false is an empty string and true is the letter T ; these definitions correspond to GNU Make's notion of true (a non-empty string) and false (an empty string).true and false can be used in GNU Make's $(if) function or within a preprocessor ifeq:

    $(if $(true),It's true!,Totally false)
ifeq ($(true),$(true))
...do this...
endif
 

Obviously, those examples are contrived and you'd expect the $(true) in the $(if) and the first $(true) in the ifeq to be the return values from a function call, and not a constant value. Checking the GMSL VersionThe GMSL includes a function that can be used to check that the version included is compatible with your use of the GMSL. The function gmsl_compatible checks that the version number of the included GMSL is greater than, or equal to, the version number passed as an argument.

The current GMSL is v1.0.0. To check that the included GMSL is at least version, say, v0.9.0 call gmsl_compatible with a list argument containing three elements: 0 9 0.

    $(call gmsl_compatible,0 9 0) 

It will return $(true) because the current GMSL is v1.0.0 and hence greater than v1.0.0. If we asked for v2.0.0 we'd get the response $(false): $(call gmsl_compatible,2 0 0).

A simple way to make sure that you are using the right version of GMSL is to wrap the call to gmsl_compatible in an assertion (of which, more below):

    $(call assert,$(call gmsl_compatible,1 0 0),Wrong GMSL version)
 

Example: Case insensitive comparisonGMSL contains two functions that make it possible to define a simple function to do a case insensitive comparison of two strings:

    ifcase = 

About the author

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!