GNU Make user-defined functions, part 2


string into the output buffer o of a GNU Make function.   The first argument should be the output buffer, the second the string to copy and the last the amount of the string to copy.

func_findstring either copies all of its first argument (if the strstr was successful) or leaves [tt ]o[/tt] untouched (and, hence, empty since it is initialized to an empty string before func_findstring is called).

With that we have enough information to start making our own GNU Make function.

Reverse a string

There's no easy wasy to reverse a string in GNU Make, but it's easy to write a C function that does that and insert it into GNU Make.

First, we'll add the definition of reverse to the list of functions that GNU Make knows about.  reverse will have a single argument that must be expanded and will call a C function
named func_reverse .

Here's the entry to add to the function_table_init[]:

~  { STRING_SIZE_TUPLE("reverse"),   
1,  1,  1,  func_reverse},


And now we can define
[tt]func_reverse[/tt] which reverses the string in

[tt]argv[0][/tt] by
swapping characters and then updates the output



static char*

func_reverse (char *o, char **argv,
const char *funcname UNUSED)


~  int len = strlen(argv[0]);

~  if (
len > 0 ) {

~    char * p = argv[0];

~    int left = 0;

~    int
right = len - 1;

~    while ( left < right ) {

~      char temp = *(p +

~      *(p + left) = *(p + right);

~      *(p + right) =

~      left++;

~      right--;

~    }

~    o =
variable_buffer_output (o, p, len);

~  }

~  return


This function works by walking from the start of the string and from the end of the string at the same time and swapping characters as they go until they meet in the middle.

To test it out we can write a little Makefile that tries three possibilities: an empty string, a string with even length and a string with odd length calling the new built-in function reverse:


$(info Empty string:
[$(reverse $(EMPTY))]);

EVEN := 1234

$(info Even length string:
[$(reverse $(EVEN))]);


$(info Odd length string:
[$(reverse $(ODD))]);


And the output shows that it works


$ ./make

Empty string: []

Even length string:

Odd length string: [EDCBA]

Since writing in C gives you access to the full range of C library functions, the GNU Make built-in functions you create are only limited by your imagination and your needs.


Creating GNU Make built-in functions is easy, but it does create a maintenance problem: the next time GNU Make is updated we'll need to port our changes to the new version.

If we can do what we need with GNU Make built-ins without resorting to modiying the source then Makefiles will be more portable.   One helping hand is the GNU Make Standard Library project that provides lots of additional functionality without modifying the GNU Make source.

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.