diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gnu_make/guile-example.html | |
new repository
Diffstat (limited to 'devdocs/gnu_make/guile-example.html')
| -rw-r--r-- | devdocs/gnu_make/guile-example.html | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/devdocs/gnu_make/guile-example.html b/devdocs/gnu_make/guile-example.html new file mode 100644 index 00000000..5b3e50cd --- /dev/null +++ b/devdocs/gnu_make/guile-example.html @@ -0,0 +1,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) < 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> |
