summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/guile-example.html
blob: 5b3e50cd430b30c79beadd79fd84fd4348542130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  <h1 class="subsection">Example Using Guile in make</h1>   <p>Here is a very simple example using GNU Guile to manage writing to a file. These Guile procedures simply open a file, allow writing to the file (one string per line), and close the file. Note that because we cannot store complex values such as Guile ports in <code>make</code> variables, we’ll keep the port as a global variable in the Guile interpreter. </p> <p>You can create Guile functions easily using <code>define</code>/<code>endef</code> to create a Guile script, then use the <code>guile</code> function to internalize it: </p> <div class="example"> <pre class="example">define GUILEIO
;; A simple Guile IO library for GNU make

(define MKPORT #f)

(define (mkopen name mode)
  (set! MKPORT (open-file name mode))
  #f)

(define (mkwrite s)
  (display s MKPORT)
  (newline MKPORT)
  #f)

(define (mkclose)
  (close-port MKPORT)
  #f)

#f
endef

# Internalize the Guile IO functions
$(guile $(GUILEIO))
</pre>
</div> <p>If you have a significant amount of Guile support code, you might consider keeping it in a different file (e.g., <samp>guileio.scm</samp>) and then loading it in your makefile using the <code>guile</code> function: </p> <div class="example"> <pre class="example">$(guile (load "guileio.scm"))
</pre>
</div> <p>An advantage to this method is that when editing <samp>guileio.scm</samp>, your editor will understand that this file contains Scheme syntax rather than makefile syntax. </p> <p>Now you can use these Guile functions to create files. Suppose you need to operate on a very large list, which cannot fit on the command line, but the utility you’re using accepts the list as input as well: </p> <div class="example"> <pre class="example">prog: $(PREREQS)
        @$(guile (mkopen "tmp.out" "w")) \
         $(foreach X,$^,$(guile (mkwrite "$(X)"))) \
         $(guile (mkclose))
        $(LINK) &lt; tmp.out
</pre>
</div> <p>A more comprehensive suite of file manipulation procedures is possible of course. You could, for example, maintain multiple output files at the same time by choosing a symbol for each one and using it as the key to a hash table, where the value is a port, then returning the symbol to be stored in a <code>make</code> variable. </p><div class="_attribution">
  <p class="_attribution-p">
    Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br>
    <a href="https://www.gnu.org/software/make/manual/html_node/Guile-Example.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Guile-Example.html</a>
  </p>
</div>