/usr/share/doc/make-doc/make.html/Extending-make.html is in make-doc 4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the GNU make utility, which determines
automatically which pieces of a large program need to be recompiled,
and issues the commands to recompile them.
This is Edition 0.73, last updated 15 January 2016,
of The GNU Make Manual, for GNU make version 4.1.
Copyright (C) 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 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled "GNU Free Documentation
License."
(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
modify this GNU manual. Buying copies from the FSF supports it in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 6.0, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU make: Extending make</title>
<meta name="description" content="GNU make: Extending make">
<meta name="keywords" content="GNU make: Extending make">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="Features.html#Features" rel="next" title="Features">
<link href="Archives.html#Archive-Suffix-Rules" rel="prev" title="Archive Suffix Rules">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space: nowrap}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: serif; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="Extending-make"></a>
<div class="header">
<p>
Next: <a href="Features.html#Features" accesskey="n" rel="next">Features</a>, Previous: <a href="Archives.html#Archives" accesskey="p" rel="prev">Archives</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Extending-GNU-make"></a>
<h2 class="chapter">12 Extending GNU <code>make</code></h2>
<a name="index-make-extensions"></a>
<p>GNU <code>make</code> provides many advanced capabilities, including many
useful functions. However, it does not contain a complete programming
language and so it has limitations. Sometimes these limitations can be
overcome through use of the <code>shell</code> function to invoke a separate
program, although this can be inefficient.
</p>
<p>In cases where the built-in capabilities of GNU <code>make</code> are
insufficient to your requirements there are two options for extending
<code>make</code>. On systems where it’s provided, you can utilize GNU
Guile as an embedded scripting language (see <a href="#Guile-Integration">GNU
Guile Integration</a>). 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 (see <a href="#load-Directive">The <code>load</code> Directive</a>).
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Guile-Integration" accesskey="1">Guile Integration</a>:</td><td> </td><td align="left" valign="top">Using Guile as an embedded scripting language.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Loading-Objects" accesskey="2">Loading Objects</a>:</td><td> </td><td align="left" valign="top">Loading dynamic objects as extensions.
</td></tr>
</table>
<hr>
<a name="Guile-Integration"></a>
<div class="header">
<p>
Next: <a href="#Loading-Objects" accesskey="n" rel="next">Loading Objects</a>, Previous: <a href="#Extending-make" accesskey="p" rel="prev">Extending make</a>, Up: <a href="#Extending-make" accesskey="u" rel="up">Extending make</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="GNU-Guile-Integration"></a>
<h3 class="section">12.1 GNU Guile Integration</h3>
<a name="index-Guile-1"></a>
<a name="index-extensions_002c-Guile"></a>
<p>GNU <code>make</code> may be built with support for GNU Guile as an embedded
extension language. Guile implements the Scheme language. A review
of GNU Guile and the Scheme language and its features is beyond the
scope of this manual: see the documentation for GNU Guile and Scheme.
</p>
<p>You can determine if <code>make</code> contains support for Guile by
examining the <code>.FEATURES</code> variable; it will contain the word
<var>guile</var> if Guile support is available.
</p>
<p>The Guile integration provides one new <code>make</code> function: <code>guile</code>.
The <code>guile</code> function takes one argument which is first expanded
by <code>make</code> in the normal fashion, then passed to the GNU Guile
evaluator. The result of the evaluator is converted into a string and
used as the expansion of the <code>guile</code> function in the makefile.
</p>
<p>In addition, GNU <code>make</code> exposes Guile procedures for use in Guile
scripts.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Guile-Types" accesskey="1">Guile Types</a>:</td><td> </td><td align="left" valign="top">Converting Guile types to <code>make</code> strings.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Guile-Interface" accesskey="2">Guile Interface</a>:</td><td> </td><td align="left" valign="top">Invoking <code>make</code> functions from Guile.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Guile-Example" accesskey="3">Guile Example</a>:</td><td> </td><td align="left" valign="top">Example using Guile in <code>make</code>.
</td></tr>
</table>
<hr>
<a name="Guile-Types"></a>
<div class="header">
<p>
Next: <a href="#Guile-Interface" accesskey="n" rel="next">Guile Interface</a>, Previous: <a href="#Guile-Integration" accesskey="p" rel="prev">Guile Integration</a>, Up: <a href="#Guile-Integration" accesskey="u" rel="up">Guile Integration</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Conversion-of-Guile-Types"></a>
<h4 class="subsection">12.1.1 Conversion of Guile Types</h4>
<a name="index-convert-guile-types"></a>
<a name="index-guile_002c-conversion-of-types"></a>
<a name="index-types_002c-conversion-of"></a>
<p>There is only one “data type” in <code>make</code>: a string. GNU Guile,
on the other hand, provides a rich variety of different data types.
An important aspect of the interface between <code>make</code> and GNU Guile
is the conversion of Guile data types into <code>make</code> strings.
</p>
<p>This conversion is relevant in two places: when a makefile invokes the
<code>guile</code> function to evaluate a Guile expression, the result of
that evaluation must be converted into a make string so it can be
further evaluated by <code>make</code>. And secondly, when a Guile script
invokes one of the procedures exported by <code>make</code> the argument
provided to the procedure must be converted into a string.
</p>
<p>The conversion of Guile types into <code>make</code> strings is as below:
</p>
<dl compact="compact">
<dt><code>#f</code></dt>
<dd><p>False is converted into the empty string: in <code>make</code> conditionals
the empty string is considered false.
</p>
</dd>
<dt><code>#t</code></dt>
<dd><p>True is converted to the string ‘<samp>#t</samp>’: in <code>make</code> conditionals
any non-empty string is considered true.
</p>
</dd>
<dt><code>symbol</code></dt>
<dt><code>number</code></dt>
<dd><p>A symbol or number is converted into the string representation of that
symbol or number.
</p>
</dd>
<dt><code>character</code></dt>
<dd><p>A printable character is converted to the same character.
</p>
</dd>
<dt><code>string</code></dt>
<dd><p>A string containing only printable characters is converted to the same
string.
</p>
</dd>
<dt><code>list</code></dt>
<dd><p>A list is converted recursively according to the above rules. This
implies that any structured list will be flattened (that is, a result
of ‘<samp>'(a b (c d) e)</samp>’ will be converted to the <code>make</code> string
‘<samp>a b c d e</samp>’).
</p>
</dd>
<dt><code>other</code></dt>
<dd><p>Any other Guile type results in an error. In future versions of
<code>make</code>, other Guile types may be converted.
</p>
</dd>
</dl>
<p>The translation of ‘<samp>#f</samp>’ (to the empty string) and ‘<samp>#t</samp>’ (to
the non-empty string ‘<samp>#t</samp>’) is designed to allow you to use Guile
boolean results directly as <code>make</code> boolean conditions. For
example:
</p>
<div class="example">
<pre class="example">$(if $(guile (access? "myfile" R_OK)),$(info myfile exists))
</pre></div>
<p>As a consequence of these conversion rules you must consider the
result of your Guile script, as that result will be converted into a
string and parsed by <code>make</code>. If there is no natural result for
the script (that is, the script exists solely for its side-effects),
you should add ‘<samp>#f</samp>’ as the final expression in order to avoid
syntax errors in your makefile.
</p>
<hr>
<a name="Guile-Interface"></a>
<div class="header">
<p>
Next: <a href="#Guile-Example" accesskey="n" rel="next">Guile Example</a>, Previous: <a href="#Guile-Types" accesskey="p" rel="prev">Guile Types</a>, Up: <a href="#Guile-Integration" accesskey="u" rel="up">Guile Integration</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Interfaces-from-Guile-to-make"></a>
<h4 class="subsection">12.1.2 Interfaces from Guile to <code>make</code></h4>
<a name="index-make-interface-to-guile"></a>
<a name="index-make-procedures-in-guile"></a>
<p>In addition to the <code>guile</code> function available in makefiles,
<code>make</code> exposes some procedures for use in your Guile scripts. At
startup <code>make</code> creates a new Guile module, <code>gnu make</code>, and
exports these procedures as public interfaces from that module:
</p>
<dl compact="compact">
<dt><code>gmk-expand</code></dt>
<dd><a name="index-gmk_002dexpand"></a>
<p>This procedure takes a single argument which is converted into a
string. The string is expanded by <code>make</code> using normal
<code>make</code> expansion rules. The result of the expansion is converted
into a Guile string and provided as the result of the procedure.
</p>
</dd>
<dt><code>gmk-eval</code></dt>
<dd><a name="index-gmk_002deval"></a>
<p>This procedure takes a single argument which is converted into a
string. The string is evaluated by <code>make</code> as if it were a
makefile. This is the same capability available via the <code>eval</code>
function (see <a href="Functions.html#Eval-Function">Eval Function</a>). The result of the <code>gmk-eval</code>
procedure is always the empty string.
</p>
<p>Note that <code>gmk-eval</code> is not quite the same as using
<code>gmk-expand</code> with the <code>eval</code> function: in the latter case
the evaluated string will be expanded <em>twice</em>; first by
<code>gmk-expand</code>, then again by the <code>eval</code> function.
</p>
</dd>
</dl>
<hr>
<a name="Guile-Example"></a>
<div class="header">
<p>
Previous: <a href="#Guile-Interface" accesskey="p" rel="prev">Guile Interface</a>, Up: <a href="#Guile-Integration" accesskey="u" rel="up">Guile Integration</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Example-Using-Guile-in-make"></a>
<h4 class="subsection">12.1.3 Example Using Guile in <code>make</code></h4>
<a name="index-Guile-example"></a>
<a name="index-example-using-Guile"></a>
<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>
<hr>
<a name="Loading-Objects"></a>
<div class="header">
<p>
Previous: <a href="#Guile-Integration" accesskey="p" rel="prev">Guile Integration</a>, Up: <a href="#Extending-make" accesskey="u" rel="up">Extending make</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Loading-Dynamic-Objects"></a>
<h3 class="section">12.2 Loading Dynamic Objects</h3>
<a name="index-loaded-objects"></a>
<a name="index-objects_002c-loaded"></a>
<a name="index-extensions_002c-loading"></a>
<table class="cartouche" border="1"><tr><td>
<blockquote>
<p><b>Warning:</b> The <code>load</code> directive and extension capability is considered a
“technology preview” in this release of GNU make. We encourage you
to experiment with this feature and we appreciate any feedback on it.
However we cannot guarantee to maintain backward-compatibility in the
next release. Consider using GNU Guile instead for extending GNU make
(see <a href="Functions.html#Guile-Function">The <code>guile</code> Function</a>).
</p></blockquote>
</td></tr></table>
<p>Many operating systems provide a facility for dynamically loading
compiled objects. If your system provides this facility, GNU
<code>make</code> can make use of it to load dynamic objects at runtime,
providing new capabilities which may then be invoked by your makefile.
</p>
<p>The <code>load</code> directive is used to load a dynamic object. Once the
object is loaded, a “setup” function will be invoked to allow the
object to initialize itself and register new facilities with GNU
<code>make</code>. A dynamic object might include new <code>make</code> functions,
for example, and the “setup” function would register them with GNU
<code>make</code>’s function handling system.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#load-Directive" accesskey="1">load Directive</a>:</td><td> </td><td align="left" valign="top">Loading dynamic objects as extensions.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Remaking-Loaded-Objects" accesskey="2">Remaking Loaded Objects</a>:</td><td> </td><td align="left" valign="top">How loaded objects get remade.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Loaded-Object-API" accesskey="3">Loaded Object API</a>:</td><td> </td><td align="left" valign="top">Programmatic interface for loaded objects.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Loaded-Object-Example" accesskey="4">Loaded Object Example</a>:</td><td> </td><td align="left" valign="top">Example of a loaded object
</td></tr>
</table>
<hr>
<a name="load-Directive"></a>
<div class="header">
<p>
Next: <a href="#Remaking-Loaded-Objects" accesskey="n" rel="next">Remaking Loaded Objects</a>, Previous: <a href="#Loading-Objects" accesskey="p" rel="prev">Loading Objects</a>, Up: <a href="#Loading-Objects" accesskey="u" rel="up">Loading Objects</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-load-Directive"></a>
<h4 class="subsection">12.2.1 The <code>load</code> Directive</h4>
<a name="index-load-directive"></a>
<a name="index-extensions_002c-load-directive"></a>
<p>Objects are loaded into GNU <code>make</code> by placing the <code>load</code>
directive into your makefile. The syntax of the <code>load</code> directive
is as follows:
</p>
<a name="index-load"></a>
<div class="example">
<pre class="example">load <var>object-file</var> …
</pre></div>
<p>or:
</p>
<div class="example">
<pre class="example">load <var>object-file</var>(<var>symbol-name</var>) …
</pre></div>
<p>The file <var>object-file</var> is dynamically loaded by GNU <code>make</code>.
If <var>object-file</var> does not include a directory path then it is
first looked for in the current directory. If it is not found there,
or a directory path is included, then system-specific paths will be
searched. If the load fails for any reason, <code>make</code> will print a
message and exit.
</p>
<p>If the load succeeds <code>make</code> will invoke an initializing function.
</p>
<p>If <var>symbol-name</var> is provided, it will be used as the name of the
initializing function.
</p>
<p>If no <var>symbol-name</var> is provided, the initializing function name is
created by taking the base file name of <var>object-file</var>, up to the
first character which is not a valid symbol name character
(alphanumerics and underscores are valid symbol name characters). To
this prefix will be appended the suffix <code>_gmk_setup</code>.
</p>
<p>More than one object file may be loaded with a single <code>load</code>
directive, and both forms of <code>load</code> arguments may be used in the
same directive.
</p>
<p>The initializing function will be provided the file name and line
number of the invocation of the <code>load</code> operation. It should
return a value of type <code>int</code>, which must be <code>0</code> on failure
and non-<code>0</code> on success. If the return value is <code>-1</code>, then
GNU make will <em>not</em> attempt to rebuild the object file
(see <a href="#Remaking-Loaded-Objects">How Loaded Objects Are Remade</a>).
</p>
<p>For example:
</p>
<div class="example">
<pre class="example">load ../mk_funcs.so
</pre></div>
<p>will load the dynamic object <samp>../mk_funcs.so</samp>. After the object
is loaded, <code>make</code> will invoke the function (assumed to be defined
by the shared object) <code>mk_funcs_gmk_setup</code>.
</p>
<p>On the other hand:
</p>
<div class="example">
<pre class="example">load ../mk_funcs.so(init_mk_func)
</pre></div>
<p>will load the dynamic object <samp>../mk_funcs.so</samp>. After the object
is loaded, <code>make</code> will invoke the function <code>init_mk_func</code>.
</p>
<p>Regardless of how many times an object file appears in a <code>load</code>
directive, it will only be loaded (and its setup function will only
be invoked) once.
</p>
<a name="index-_002eLOADED"></a>
<p>After an object has been successfully loaded, its file name is
appended to the <code>.LOADED</code> variable.
</p>
<a name="index-_002dload"></a>
<p>If you would prefer that failure to load a dynamic object not be
reported as an error, you can use the <code>-load</code> directive instead
of <code>load</code>. GNU <code>make</code> will not fail and no message will be
generated if an object fails to load. The failed object is not added
to the <code>.LOADED</code> variable, which can then be consulted to
determine if the load was successful.
</p>
<hr>
<a name="Remaking-Loaded-Objects"></a>
<div class="header">
<p>
Next: <a href="#Loaded-Object-API" accesskey="n" rel="next">Loaded Object API</a>, Previous: <a href="#load-Directive" accesskey="p" rel="prev">load Directive</a>, Up: <a href="#Loading-Objects" accesskey="u" rel="up">Loading Objects</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="How-Loaded-Objects-Are-Remade"></a>
<h4 class="subsection">12.2.2 How Loaded Objects Are Remade</h4>
<a name="index-updating-loaded-objects"></a>
<a name="index-remaking-loaded-objects"></a>
<a name="index-loaded-objects_002c-remaking-of"></a>
<p>Loaded objects undergo the same re-make procedure as makefiles
(see <a href="Makefiles.html#Remaking-Makefiles">How Makefiles Are Remade</a>). If any
loaded object is recreated, then <code>make</code> will start from scratch
and re-read all the makefiles, and reload the object files again. It
is not necessary for the loaded object to do anything special to
support this.
</p>
<p>It’s up to the makefile author to provide the rules needed for
rebuilding the loaded object.
</p>
<hr>
<a name="Loaded-Object-API"></a>
<div class="header">
<p>
Next: <a href="#Loaded-Object-Example" accesskey="n" rel="next">Loaded Object Example</a>, Previous: <a href="#Remaking-Loaded-Objects" accesskey="p" rel="prev">Remaking Loaded Objects</a>, Up: <a href="#Loading-Objects" accesskey="u" rel="up">Loading Objects</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Loaded-Object-Interface"></a>
<h4 class="subsection">12.2.3 Loaded Object Interface</h4>
<a name="index-loaded-object-API"></a>
<a name="index-interface-for-loaded-objects"></a>
<table class="cartouche" border="1"><tr><td>
<blockquote>
<p><b>Warning:</b> For this feature to be useful your extensions will need to invoke
various functions internal to GNU <code>make</code>. The programming
interfaces provided in this release should not be considered stable:
functions may be added, removed, or change calling signatures or
implementations in future versions of GNU <code>make</code>.
</p></blockquote>
</td></tr></table>
<p>To be useful, loaded objects must be able to interact with GNU
<code>make</code>. This interaction includes both interfaces the loaded
object provides to makefiles and also interfaces <code>make</code> provides
to the loaded object to manipulate <code>make</code>’s operation.
</p>
<p>The interface between loaded objects and <code>make</code> is defined by the
<samp>gnumake.h</samp> C header file. All loaded objects written in C
should include this header file. Any loaded object not written in C
will need to implement the interface defined in this header file.
</p>
<p>Typically, a loaded object will register one or more new GNU
<code>make</code> functions using the <code>gmk_add_function</code> routine from
within its setup function. The implementations of these <code>make</code>
functions may make use of the <code>gmk_expand</code> and <code>gmk_eval</code>
routines to perform their tasks, then optionally return a string as
the result of the function expansion.
</p>
<a name="Loaded-Object-Licensing"></a>
<h4 class="subsubheading">Loaded Object Licensing</h4>
<a name="index-loaded-object-licensing"></a>
<a name="index-plugin_005fis_005fGPL_005fcompatible"></a>
<p>Every dynamic extension should define the global symbol
<code>plugin_is_GPL_compatible</code> to assert that it has been licensed
under a GPL-compatible license. If this symbol does not exist,
<code>make</code> emits a fatal error and exits when it tries to load your
extension.
</p>
<p>The declared type of the symbol should be <code>int</code>. It does not need
to be in any allocated section, though. The code merely asserts that
the symbol exists in the global scope. Something like this is enough:
</p>
<div class="example">
<pre class="example">int plugin_is_GPL_compatible;
</pre></div>
<a name="Data-Structures"></a>
<h4 class="subsubheading">Data Structures</h4>
<dl compact="compact">
<dt><code>gmk_floc</code></dt>
<dd><p>This structure represents a filename/location pair. It is provided
when defining items, so GNU <code>make</code> can inform the user later
where the definition occurred if necessary.
</p></dd>
</dl>
<a name="Registering-Functions"></a>
<h4 class="subsubheading">Registering Functions</h4>
<a name="index-gmk_005fadd_005ffunction"></a>
<p>There is currently one way for makefiles to invoke operations provided
by the loaded object: through the <code>make</code> function call
interface. A loaded object can register one or more new functions
which may then be invoked from within the makefile in the same way as
any other function.
</p>
<p>Use <code>gmk_add_function</code> to create a new <code>make</code> function. Its
arguments are as follows:
</p>
<dl compact="compact">
<dt><code>name</code></dt>
<dd><p>The function name. This is what the makefile should use to invoke the
function. The name must be between 1 and 255 characters long and it
may only contain alphanumeric, period (‘<samp>.</samp>’), dash (‘<samp>-</samp>’), and
underscore (‘<samp>_</samp>’) characters. It may not begin with a period.
</p>
</dd>
<dt><code>func_ptr</code></dt>
<dd><p>A pointer to a function that <code>make</code> will invoke when it expands
the function in a makefile. This function must be defined by the
loaded object.
</p>
</dd>
<dt><code>min_args</code></dt>
<dd><p>The minimum number of arguments the function will accept. Must be
between 0 and 255. GNU <code>make</code> will check this and fail before
invoking <code>func_ptr</code> if the function was invoked with too few
arguments.
</p>
</dd>
<dt><code>max_args</code></dt>
<dd><p>The maximum number of arguments the function will accept. Must be
between 0 and 255. GNU <code>make</code> will check this and fail before
invoking <code>func_ptr</code> if the function was invoked with too few
arguments. If the value is 0, then any number of arguments is
accepted. If the value is greater than 0, then it must be greater
than or equal to <code>min_args</code>.
</p>
</dd>
<dt><code>flags</code></dt>
<dd><p>Flags that specify how this function will operate; the desired flags
should be OR’d together. If the <code>GMK_FUNC_NOEXPAND</code> flag is
given then the function arguments will not be expanded before the
function is called; otherwise they will be expanded first.
</p></dd>
</dl>
<a name="Registered-Function-Interface"></a>
<h4 class="subsubheading">Registered Function Interface</h4>
<a name="index-gmk_005ffunc_005fptr"></a>
<p>A function registered with <code>make</code> must match the
<code>gmk_func_ptr</code> type. It will be invoked with three parameters:
<code>name</code> (the name of the function), <code>argc</code> (the number of
arguments to the function), and <code>argv</code> (an array of pointers to
arguments to the function). The last pointer (that is,
<code>argv[argc]</code>) will be null (<code>0</code>).
</p>
<p>The return value of the function is the result of expanding the
function. If the function expands to nothing the return value may be
null. Otherwise, it must be a pointer to a string created with
<code>gmk_alloc</code>. Once the function returns, <code>make</code> owns this
string and will free it when appropriate; it cannot be accessed by the
loaded object.
</p>
<a name="GNU-make-Facilities"></a>
<h4 class="subsubheading">GNU <code>make</code> Facilities</h4>
<p>There are some facilities exported by GNU <code>make</code> for use by
loaded objects. Typically these would be run from within the
setup function and/or the functions registered via
<code>gmk_add_function</code>, to retrieve or modify the data <code>make</code>
works with.
</p>
<dl compact="compact">
<dt><code>gmk_expand</code></dt>
<dd><a name="index-gmk_005fexpand"></a>
<p>This function takes a string and expands it using <code>make</code>
expansion rules. The result of the expansion is returned in a
nil-terminated string buffer. The caller is responsible for calling
<code>gmk_free</code> with a pointer to the returned buffer when done.
</p>
</dd>
<dt><code>gmk_eval</code></dt>
<dd><a name="index-gmk_005feval"></a>
<p>This function takes a buffer and evaluates it as a segment of makefile
syntax. This function can be used to define new variables, new rules,
etc. It is equivalent to using the <code>eval</code> <code>make</code> function.
</p></dd>
</dl>
<p>Note that there is a difference between <code>gmk_eval</code> and calling
<code>gmk_expand</code> with a string using the <code>eval</code> function: in
the latter case the string will be expanded <em>twice</em>; once by
<code>gmk_expand</code> and then again by the <code>eval</code> function. Using
<code>gmk_eval</code> the buffer is only expanded once, at most (as it’s
read by the <code>make</code> parser).
</p>
<a name="Memory-Management"></a>
<h4 class="subsubheading">Memory Management</h4>
<p>Some systems allow for different memory management schemes. Thus you
should never pass memory that you’ve allocated directly to any
<code>make</code> function, nor should you attempt to directly free any
memory returned to you by any <code>make</code> function. Instead, use the
<code>gmk_alloc</code> and <code>gmk_free</code> functions.
</p>
<p>In particular, the string returned to <code>make</code> by a function
registered using <code>gmk_add_function</code> <em>must</em> be allocated
using <code>gmk_alloc</code>, and the string returned from the <code>make</code>
<code>gmk_expand</code> function <em>must</em> be freed (when no longer
needed) using <code>gmk_free</code>.
</p>
<dl compact="compact">
<dt><code>gmk_alloc</code></dt>
<dd><a name="index-gmk_005falloc"></a>
<p>Return a pointer to a newly-allocated buffer. This function will
always return a valid pointer; if not enough memory is available
<code>make</code> will exit.
</p>
</dd>
<dt><code>gmk_free</code></dt>
<dd><a name="index-gmk_005ffree"></a>
<p>Free a buffer returned to you by <code>make</code>. Once the
<code>gmk_free</code> function returns the string will no longer be valid.
</p></dd>
</dl>
<hr>
<a name="Loaded-Object-Example"></a>
<div class="header">
<p>
Previous: <a href="#Loaded-Object-API" accesskey="p" rel="prev">Loaded Object API</a>, Up: <a href="#Loading-Objects" accesskey="u" rel="up">Loading Objects</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Example-Loaded-Object"></a>
<h4 class="subsection">12.2.4 Example Loaded Object</h4>
<a name="index-loaded-object-example"></a>
<a name="index-example-of-loaded-objects"></a>
<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 <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 *
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 >= 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 ()
{
/* 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 makefile 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 $ $<
</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 $ $< -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
cc -shared -fPIC -o mk_temp.so mk_temp.c
Temporary filename: tmpfile.A7JEwd
</pre></div>
<hr>
<div class="header">
<p>
Previous: <a href="#Loaded-Object-API" accesskey="p" rel="prev">Loaded Object API</a>, Up: <a href="#Loading-Objects" accesskey="u" rel="up">Loading Objects</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|