What's New in GMSL 1.0.2


The GMSLThe GMSL is a library of functions to enhance GNU Make. It provides string and list functions, arithmetic operators, tracing facilities and even associative arrays.

The GMSLThe GMSL is a library of functions to enhance GNU Make. It provides string and list functions, arithmetic operators, tracing facilities and even associative arrays.

Check out the main GMSL site: gmsl.sf.net.

GMSL Background

To use the GMSL in your Makefiles first download it from SourceForge (the latest version is available here).

There are two files to save with your own Makefile: gmsl and __gmsl. To start using the GMSL simply place include gmsl in your own Makefile (the gmsl file decides whether it should include __gmsl to prevent multiple inclusion of the same file).

GMSL functions are called using the standard GNU Make $(call ...) built-in function. For example, to get the last element of the list $(MY_LIST) using the GMSL last function do $(call last,$(MY_LIST)).

New in v1.0.2: Logical Operators

GMSL v1.0.2 introduces the logical operators and, or, xor, nand, nor, xnor and not.

GNU Make's concept of truth values is simple: any non-empty string is considered to be 'true' and an empty string is 'false'. To make this explicit the GMSL define variables $(true) and $(false). $(true) is simply the string T and $(false) is an empty string.

GNU Make's (and the GMSL) boolean values can be used with both GMSL functions and GNU Make's $(if ...) built-in. The GMSL logical operators were designed for use with $(if ...) and the GNU Make preprocessor ifeq directive.

Example: Is DEBUG set to Y?

Imagine that a Makefile has a debug option which is enabled by setting the DEBUG environment variable to Y. Using the GMSL function seq (string equal) and the new or operator it's easy to determine whether debugging is desired or not.

include gmsl

debug_needed := $(call or,$(call seq,$(DEBUG),Y),$(call seq,$(DEBUG),y))

Since the GMSL has a 'lower case' function (lc)it's possible to write this example without the need for the or:

include gmsl

debug_needed := $(call seq,$(call lc,$(DEBUG)),y)

But the logical operator or allows us to be even more generous and accept YES as well as Y for the debug option:

include gmsl

debug_needed := $(call or,$(call seq,$(call lc,$(DEBUG)),y),$(call seq,$(call lc,$(DEBUG)),yes))

And that's case insensitive too.

Example: Is DEBUG set to Y or N?

Another possible use of the logical operators is to force the user of the Makefile to set DEBUG to either Y or N, thus avoiding problems if the debug option is forgotten about.

The GMSL has an assertion function (assert) that will output a fatal error if its argument is not true. So we can use it to assert that DEBUG must be Y or N:

include gmsl

$(call assert,$(call or,$(call seq,$(DEBUG),Y),$(call seq,$(DEBUG),N)),DEBUG must be Y or N)

If the user makes an error and sets DEBUG to Oui then the following error is generated by the assertion:

Make DEBUG=Oui

Makefile:1: *** GNU Make Standard Library: Assertion failure: DEBUG must be Y or N. Stop.

That should keep the French office in check!

Example: Using Logical Operators in the Preprocessor

Since GNU Make's preprocessor (which has ifeq, ifneq and ifdef directives) doesn't have any logical operations it's hard to write a complex statement. For example, to define a section of a Makefile if DEBUG is set to Y or Yes in GNU Make requires either duplicating a section of code (yuck!) or writing the following hard to understand statement:

ifeq ($(DEBUG),$(filter $(DEBUG),Y Yes))

This works by filtering the list Y Yes with the value of $(DEBUG) which returns an empty list if $(DEBUG) is not Y or Yes, and the value of $(DEBUG) if it is. The ifeq then compares the resulting value with $(DEBUG). That's pretty ugly,

About the author

AgileConnection is a TechWell community.

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