make: Guile Example

1 
1 12.1.3 Example Using Guile in 'make'
1 ------------------------------------
1 
1 Here is a very simple example using GNU Guile to manage writing to a
1 file.  These Guile procedures simply open a file, allow writing to the
1 file (one string per line), and close the file.  Note that because we
1 cannot store complex values such as Guile ports in 'make' variables,
1 we'll keep the port as a global variable in the Guile interpreter.
1 
1    You can create Guile functions easily using 'define'/'endef' to
1 create a Guile script, then use the 'guile' function to internalize it:
1 
1      define GUILEIO
1      ;; A simple Guile IO library for GNU make
1 
1      (define MKPORT #f)
1 
1      (define (mkopen name mode)
1        (set! MKPORT (open-file name mode))
1        #f)
1 
1      (define (mkwrite s)
1        (display s MKPORT)
1        (newline MKPORT)
1        #f)
1 
1      (define (mkclose)
1        (close-port MKPORT)
1        #f)
1 
1      #f
1      endef
1 
1      # Internalize the Guile IO functions
1      $(guile $(GUILEIO))
1 
1    If you have a significant amount of Guile support code, you might
1 consider keeping it in a different file (e.g., 'guileio.scm') and then
1 loading it in your makefile using the 'guile' function:
1 
1      $(guile (load "guileio.scm"))
1 
1    An advantage to this method is that when editing 'guileio.scm', your
1 editor will understand that this file contains Scheme syntax rather than
1 makefile syntax.
1 
1    Now you can use these Guile functions to create files.  Suppose you
1 need to operate on a very large list, which cannot fit on the command
1 line, but the utility you're using accepts the list as input as well:
1 
1      prog: $(PREREQS)
1              @$(guile (mkopen "tmp.out" "w")) \
1               $(foreach X,$^,$(guile (mkwrite "$(X)"))) \
1               $(guile (mkclose))
1              $(LINK) < tmp.out
1 
1    A more comprehensive suite of file manipulation procedures is
1 possible of course.  You could, for example, maintain multiple output
1 files at the same time by choosing a symbol for each one and using it as
1 the key to a hash table, where the value is a port, then returning the
1 symbol to be stored in a 'make' variable.
1