summaryrefslogtreecommitdiff
path: root/devdocs/gnu_make/loaded-object-example.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/gnu_make/loaded-object-example.html')
-rw-r--r--devdocs/gnu_make/loaded-object-example.html67
1 files changed, 67 insertions, 0 deletions
diff --git a/devdocs/gnu_make/loaded-object-example.html b/devdocs/gnu_make/loaded-object-example.html
new file mode 100644
index 00000000..bc4ca849
--- /dev/null
+++ b/devdocs/gnu_make/loaded-object-example.html
@@ -0,0 +1,67 @@
+ <h1 class="subsection">Example Loaded Object</h1> <p>Let’s suppose we wanted to write a new GNU <code>make</code> function that would create a temporary file and return its name. We would like our function to take a prefix as an argument. First we can write the function in a file <samp>mk_temp.c</samp>: </p> <div class="example"> <pre class="example">#include &lt;stdlib.h&gt;
+#include &lt;stdio.h&gt;
+#include &lt;string.h&gt;
+#include &lt;unistd.h&gt;
+#include &lt;errno.h&gt;
+
+#include &lt;gnumake.h&gt;
+
+int plugin_is_GPL_compatible;
+
+char *
+gen_tmpfile(const char *nm, int argc, char **argv)
+{
+ int fd;
+
+ /* Compute the size of the filename and allocate space for it. */
+ int len = strlen (argv[0]) + 6 + 1;
+ char *buf = gmk_alloc (len);
+
+ strcpy (buf, argv[0]);
+ strcat (buf, "XXXXXX");
+
+ fd = mkstemp(buf);
+ if (fd &gt;= 0)
+ {
+ /* Don't leak the file descriptor. */
+ close (fd);
+ return buf;
+ }
+
+ /* Failure. */
+ fprintf (stderr, "mkstemp(%s) failed: %s\n", buf, strerror (errno));
+ gmk_free (buf);
+ return NULL;
+}
+
+int
+mk_temp_gmk_setup (const gmk_floc *floc)
+{
+ printf ("mk_temp plugin loaded from %s:%lu\n", floc-&gt;filenm, floc-&gt;lineno);
+ /* Register the function with make name "mk-temp". */
+ gmk_add_function ("mk-temp", gen_tmpfile, 1, 1, 1);
+ return 1;
+}
+</pre>
+</div> <p>Next, we will write a <samp>Makefile</samp> that can build this shared object, load it, and use it: </p> <div class="example"> <pre class="example">all:
+ @echo Temporary file: $(mk-temp tmpfile.)
+
+load mk_temp.so
+
+mk_temp.so: mk_temp.c
+ $(CC) -shared -fPIC -o $@ $&lt;
+</pre>
+</div> <p>On MS-Windows, due to peculiarities of how shared objects are produced, the compiler needs to scan the <em>import library</em> produced when building <code>make</code>, typically called <samp>libgnumake-<var>version</var>.dll.a</samp>, where <var>version</var> is the version of the load object API. So the recipe to produce a shared object will look on Windows like this (assuming the API version is 1): </p> <div class="example"> <pre class="example">mk_temp.dll: mk_temp.c
+ $(CC) -shared -o $@ $&lt; -lgnumake-1
+</pre>
+</div> <p>Now when you run <code>make</code> you’ll see something like: </p> <div class="example"> <pre class="example">$ make
+mk_temp plugin loaded from Makefile:4
+cc -shared -fPIC -o mk_temp.so mk_temp.c
+Temporary filename: tmpfile.A7JEwd
+</pre>
+</div><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/Loaded-Object-Example.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Loaded-Object-Example.html</a>
+ </p>
+</div>