4
votes

The latest version of GNU-Make http://www.gnu.org/software/make/ provides many advanced capabilities, including many useful functions. (...) On systems which support dynamically loadable objects, you can write your own extension in any language (which can be compiled into such an object) and load it to provide extended capabilities... http://www.gnu.org/software/make/manual/make.html#Loading-Objects

I tried to run the simple example below (a $(hello string) function). It works if I first compile the hello.so. But it doesn't work if I run it as the example provided here (with a load directive) http://www.gnu.org/software/make/manual/make.html#Loading-Objects . Make4 is installed in the current directory.

./Makefile:

all:
    echo $(hello world)

load hello.so

hello.so: hello.c
    $(CC) -shared -I./include -fPIC -o $@ $<

./hello.c:

#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <gnumake.h>

int plugin_is_GPL_compatible;

char * hello(const char *nm, unsigned int argc, char **argv)
    {
    int len = strlen (argv[0]) + 7;
    char *buf = gmk_alloc (len);
    sprintf(buf,"Hello %s",argv[0]);
    return buf;
    }

int hello_gmk_setup ()
    {
    gmk_add_function("hello", hello, 1, 1, 1);
    return 1;
    }

running the example:

 ./bin/make -v
GNU Make 4.0
Built for i686-pc-linux-gnu

$ ./bin/make 
Makefile:4: hello.so: cannot open shared object file: No such file or directory
Makefile:4: *** hello.so: failed to load.  Stop.

How can I run this example with the 'load' directive ?

1
FYI: I've just posted my question to the make-help mailing list.Pierre
shouldn't all simply depend on hello.so?Michael Schubert
Try load ./hello.so ; because dlopen handles specially paths without /; also all should depend upon hello.soBasile Starynkevitch
@BasileStarynkevitch nice suggestion but it didn't work.Pierre
BTW, you need hello.so to exist before load-ing it. Perhaps use -load instead of loadBasile Starynkevitch

1 Answers

2
votes

I suggest in a comment to use

-load hello.so

instead of just load hello.so; this is analog to using -include in a Makefile.

The logic is that make plugins are generally expected to exit before you run some make using them (often, you would use a recursive make, e.g. run $(MAKE) -C subdir in a toplevel Makefile and ensure that the plugin does exist when your run $(MAKE) -C subdir)

If hello.so does not exist when -load hello.so is parsed, the GNU make would ignore that directive. (I am not sure you want that for a real plugin).

I still think that make plugins should generally not be built by the Makefile which is load-ing them.

I also believe that using Guile extensions in make is wiser than using plugins.