3 @c Copyright 2001, 2003--2025 Free Software Foundation, Inc.
5 @c Permission is granted to copy, distribute and/or modify this document
6 @c under the terms of the GNU Free Documentation License, Version 1.3 or
7 @c any later version published by the Free Software Foundation; with no
8 @c Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
9 @c copy of the license is at <https://www.gnu.org/licenses/fdl-1.3.en.html>.
14 * Keeping Up-to-date::
15 * Contributing to Gnulib::
16 * Portability guidelines::
22 @section Gnulib Basics
24 While portability across operating systems is not one of GNU's primary
25 goals, it has helped introduce many people to the GNU system, and is
26 worthwhile when it can be achieved at a low cost. This collection helps
29 Gnulib is intended to be the canonical source for most of the important
30 ``portability'' and/or common files for GNU projects. These are files
31 intended to be shared at the source level; Gnulib is not a typical
32 library meant to be installed and linked against. Thus, unlike most
33 projects, Gnulib does not normally generate a source tarball
34 distribution; instead, developers grab modules directly from the
37 The easiest, and recommended, way to do this is to use the
38 @command{gnulib-tool} script. Since there is no installation
39 procedure for Gnulib, @command{gnulib-tool} needs to be run directly
40 in the directory that contains the Gnulib source code. You can do
41 this either by specifying the absolute filename of
42 @command{gnulib-tool}, or by using a symbolic link from a place inside
43 your @env{PATH} to the @command{gnulib-tool} file of your preferred
44 Gnulib checkout. For example:
47 $ ln -s $HOME/gnu/src/gnulib.git/gnulib-tool $HOME/bin/gnulib-tool
53 Gnulib is available for anonymous checkout. In any Bourne-shell the
54 following should work:
57 $ git clone https://git.savannah.gnu.org/git/gnulib.git
60 For a read-write checkout you need to have a login on
61 @samp{savannah.gnu.org} and be a member of the Gnulib project at
62 @url{https://savannah.gnu.org/projects/gnulib}. Then, instead of the
63 URL @url{https://git.savannah.gnu.org/git/gnulib.git}, use the URL
64 @samp{ssh://@var{user}@@git.savannah.gnu.org/srv/git/gnulib} where
65 @var{user} is your login name on savannah.gnu.org.
71 @url{https://en.wikipedia.org/wiki/Git_(software)}
73 @url{https://git-scm.com/}
76 When you use @code{git annotate} or @code{git blame} with Gnulib, it's
77 recommended that you use the @option{-w} option, in order to ignore
78 massive whitespace changes that happened in 2009.
80 @node Keeping Up-to-date
81 @section Keeping Up-to-date
83 The best way to work with Gnulib is to check it out of git.
84 To synchronize, you can use @code{git pull}.
86 Subscribing to the @email{bug-gnulib@@gnu.org} mailing list will help
87 you to plan when to update your local copy of Gnulib (which you use to
88 maintain your software) from git. You can review the archives,
90 @url{https://lists.gnu.org/mailman/listinfo/bug-gnulib}.
92 Sometimes, using an updated version of Gnulib will require you to use
93 newer versions of GNU Automake or Autoconf. You may find it helpful
94 to join the autotools-announce mailing list to be advised of such
97 @node Contributing to Gnulib
98 @section Contributing to Gnulib
100 All software here is copyrighted by the Free Software Foundation---you need
101 to have filled out an assignment form for a project that uses the
102 module for that contribution to be accepted here.
104 If you have a piece of code that you would like to contribute, please
105 email @email{bug-gnulib@@gnu.org}.
107 Generally we are looking for files that fulfill at least one of the
108 following requirements:
112 If your @file{.c} and @file{.h} files define functions that are broken or
113 missing on some other system, we should be able to include it.
116 If your functions remove arbitrary limits from existing
117 functions (either under the same name, or as a slightly different
118 name), we should be able to include it.
121 If your functions define completely new but rarely used functionality,
122 you should probably consider packaging it as a separate library.
126 * Indent with spaces not TABs::
127 * How to add a new module::
130 @node Gnulib licensing
131 @subsection Gnulib licensing
133 Gnulib contains code both under GPL and LGPL@. Because several packages
134 that use Gnulib are GPL, the files state they are licensed under GPL@.
135 However, to support LGPL projects as well, you may use some of the
136 files under LGPL@. The ``License:'' information in the files under
137 modules/ clarifies the real license that applies to the module source.
139 Keep in mind that if you submit patches to files in Gnulib, you should
140 license them under a compatible license, which means that sometimes
141 the contribution will have to be LGPL, if the original file is
142 available under LGPL via a ``License: LGPL'' information in the
143 projects' modules/ file.
145 @node Indent with spaces not TABs
146 @subsection Indent with spaces not TABs
148 We use space-only indentation in nearly all files. This includes all
149 @file{*.h}, @file{*.c}, @file{*.y} files, except for the @code{regex}
150 module. Makefile and ChangeLog files are excluded, since TAB
151 characters are part of their format.
153 In order to tell your editor to produce space-only indentation, you
154 can use these instructions.
158 For Emacs: Add these lines to your Emacs initialization file
159 (@file{$HOME/.emacs} or similar):
162 ;; In Gnulib, indent with spaces everywhere (not TABs).
163 ;; Exceptions: Makefile and ChangeLog modes.
164 (add-hook 'find-file-hook '(lambda ()
165 (if (and buffer-file-name
166 (string-match "/gnulib\\>" (buffer-file-name))
167 (not (string-equal mode-name "Change Log"))
168 (not (string-equal mode-name "Makefile")))
169 (setq indent-tabs-mode nil))))
173 For vi (vim): Add these lines to your @file{$HOME/.vimrc} file:
176 " Don't use tabs for indentation. Spaces are nicer to work with.
180 For Makefile and ChangeLog files, compensate for this by adding this
181 to your @file{$HOME/.vim/after/indent/make.vim} file, and similarly
182 for your @file{$HOME/.vim/after/indent/changelog.vim} file:
185 " Use tabs for indentation, regardless of the global setting.
190 For Eclipse: In the ``Window|Preferences'' dialog (or ``Eclipse|Preferences''
195 Under ``General|Editors|Text Editors'', select the ``Insert spaces for tabs''
199 Under ``C/C++|Code Style'', select a code style profile that has the
200 ``Indentation|Tab policy'' combobox set to ``Spaces only'', such as the
201 ``GNU [built-in]'' policy.
204 If you use the GNU indent program, pass it the option @option{--no-tabs}.
207 @node How to add a new module
208 @subsection How to add a new module
212 Add the header files and source files to @file{lib/}.
215 If the module needs configure-time checks, write an Autoconf
216 macro for it in @file{m4/@var{module}.m4}. See @file{m4/README} for details.
219 Write a module description @file{modules/@var{module}}, based on
220 @file{modules/TEMPLATE}.
223 If the module contributes a section to the end-user documentation,
224 put this documentation in @file{doc/@var{module}.texi} and add it to the ``Files''
225 section of @file{modules/@var{module}}. Most modules don't do this; they have only
226 documentation for the programmer (= Gnulib user). Such documentation
227 usually goes into the @file{lib/} source files. It may also go into @file{doc/};
228 but don't add it to the module description in this case.
231 Add the module to the list in @file{MODULES.html.sh}.
235 You can test that a module builds correctly with:
238 $ ./gnulib-tool --create-testdir --dir=/tmp/testdir module1 ... moduleN
240 $ ./configure && make
248 Check the license and copyright year of headers.
251 Check that the source code follows the GNU coding standards;
252 see @url{https://www.gnu.org/prep/standards}.
255 Add source files to @file{config/srclist*} if they are identical to upstream
256 and should be upgraded in Gnulib whenever the upstream source changes.
259 Include header files in source files to verify the function prototypes.
262 Make sure a replacement function doesn't cause warnings or clashes on
263 systems that have the function.
266 Autoconf functions can use @samp{gl_*} prefix. The @samp{AC_*} prefix is for
267 autoconf internal functions.
270 Build files only if they are needed on a platform. Look at the
271 @code{alloca} and @code{fnmatch} modules for how to achieve this. If
272 for some reason you cannot do this, and you have a @file{.c} file that
273 leads to an empty @file{.o} file on some platforms (through some big
274 @code{#if} around all the code), then ensure that the compilation unit
275 is not empty after preprocessing. One way to do this is to
276 @code{#include <stddef.h>} or @code{<stdio.h>} before the big
280 @node Portability guidelines
281 @section Portability guidelines
283 Gnulib code is intended to be portable to a wide variety of platforms,
284 not just GNU platforms. Gnulib typically attempts to support a
285 platform as long as it is still supported by its provider, even if the
286 platform is not the latest version. @xref{Target Platforms}.
288 Many Gnulib modules exist so that applications need not worry about
289 undesirable variability in implementations. For example, an
290 application that uses the @code{malloc} module need not worry about
291 @code{malloc@ (0)} returning a null pointer on some Standard C
292 platforms; and @code{glob} users need not worry about @code{glob}
293 silently omitting symbolic links to nonexistent files on some
294 platforms that do not conform to POSIX.
296 Gnulib code is intended to port without problem to new hosts, e.g.,
297 hosts conforming to recent C and POSIX standards. Hence Gnulib code
298 should avoid using constructs that these newer standards no longer
299 require, without first testing for the presence of these constructs.
301 For example, because C11 made variable length arrays optional, Gnulib
302 code should avoid them unless it first uses the @code{vararrays}
303 module to check whether they are supported.
305 The following subsections discuss some exceptions and caveats to the
306 general Gnulib portability guidelines.
309 * C language versions::
310 * C99 features assumed::
311 * C99 features avoided::
312 * Other portability assumptions::
315 @node C language versions
316 @subsection C language versions
318 Currently Gnulib assumes at least a freestanding C99 compiler,
319 possibly operating with a C library that predates C99; with time this
320 assumption will likely be strengthened to later versions of the C
321 standard. Old platforms currently supported include AIX 6.1, HP-UX
322 11i v1 and Solaris 10, though these platforms are rarely tested.
323 Gnulib itself is so old that it contains many fixes for obsolete
324 platforms, fixes that may be removed in the future.
326 Because of the freestanding C99 assumption, Gnulib code can include
327 @code{<float.h>}, @code{<limits.h>}, @code{<stdarg.h>},
328 @code{<stddef.h>}, and @code{<stdint.h>}
329 unconditionally; @code{<stdbool.h>} is also in the C99 freestanding
330 list but is obsolescent as of C23. Gnulib code can also assume the existence
331 of @code{<ctype.h>}, @code{<errno.h>}, @code{<fcntl.h>},
332 @code{<locale.h>}, @code{<signal.h>}, @code{<stdio.h>},
333 @code{<stdlib.h>}, @code{<string.h>}, and @code{<time.h>}. Similarly,
334 many modules include @code{<sys/types.h>} even though it's not even in
335 C11; that's OK since @code{<sys/types.h>} has been around nearly
338 Even if the include files exist, they may not conform to the C standard.
339 However, GCC has a @command{fixincludes} script that attempts to fix most
340 conformance problems. Gnulib currently assumes include files
341 largely conform to C99 or better. People still using ancient hosts
342 should use fixincludes or fix their include files manually.
344 Even if the include files conform, the library itself may not.
345 For example, @code{strtod} and @code{mktime} have some bugs on some platforms.
346 You can work around some of these problems by requiring the relevant
347 modules, e.g., the Gnulib @code{mktime} module supplies a working and
348 conforming @code{mktime}.
350 @node C99 features assumed
351 @subsection C99 features assumed by Gnulib
353 Although the C99 standard specifies many features, Gnulib code
354 is conservative about using them, partly because Gnulib predates
355 the widespread adoption of C99, and partly because many C99
356 features are not well-supported in practice. C99 features that
357 are reasonably portable nowadays include:
361 A declaration after a statement, or as the first clause in a
362 @code{for} statement.
365 @code{long long int}.
370 although Gnulib code no longer uses
371 it directly, preferring plain @code{bool} via the
372 @code{bool} module instead.
378 assuming the @code{stdint-h} module is used.
382 Compound literals and designated initializers.
385 Empty arguments in macro calls.
390 Note: The handling of @code{__VA_ARGS__} in MSVC differs from the one
391 in ISO C 99, see @url{https://stackoverflow.com/questions/5134523/}.
392 But usually this matters only for macros that decompose @code{__VA_ARGS__}.
395 @code{static inline} functions.
399 @code{__func__}, assuming the @code{func} module is used. @xref{func}.
402 The @code{restrict} qualifier, assuming
403 @code{AC_REQUIRE([AC_C_RESTRICT])} is used.
404 This qualifier is sometimes implemented via a macro, so C++ code that
405 uses Gnulib should avoid using @code{restrict} as an identifier.
409 Flexible array members (however, see the @code{flexmember} module).
412 @node C99 features avoided
413 @subsection C99 features avoided by Gnulib
415 Gnulib avoids some features even though they are standardized by C99,
416 as they have portability problems in practice. Here is a partial list
417 of avoided C99 features. Many other C99 features are portable only if
418 their corresponding modules are used; Gnulib code that uses such a
419 feature should require the corresponding module.
424 Variable length arrays (VLAs) or variably modified types,
425 without checking whether @code{__STDC_NO_VLA__} is defined.
426 See the @code{vararrays} and @code{vla} modules.
429 Block-scope variable length arrays, without checking whether either
430 @code{GNULIB_NO_VLA} or @code{__STDC_NO_VLA__} is defined.
431 This lets you define @code{GNULIB_NO_VLA} to pacify GCC when
432 using its @option{-Wvla-larger-than warnings} option,
433 and to avoid large stack usage that may have security implications.
434 @code{GNULIB_NO_VLA} does not affect Gnulib's other uses of VLAs and
435 variably modified types, such as array declarations in function
439 Converting to pointers via integer types other than @code{intptr_t} or
440 @code{uintptr_t}. Although the C standard says that values of these
441 integer types, if they exist, should be convertible to and from
442 @code{intmax_t} and @code{uintmax_t} without loss of information, on
443 CHERI platforms such conversions result in integers that, if converted
444 back to a pointer, cannot be dereferenced.
447 @code{extern inline} functions, without checking whether they are
448 supported. @xref{extern inline}.
451 Type-generic math functions.
454 Universal character names in source code.
457 @code{<iso646.h>}, since GNU programs need not worry about deficient
458 source-code encodings.
461 Comments beginning with @samp{//}. This is mostly for style reasons.
464 @node Other portability assumptions
465 @subsection Other portability assumptions made by Gnulib
467 Gnulib code makes the following assumptions
468 that go beyond what C and POSIX require:
472 Standard internal types like @code{ptrdiff_t} and @code{size_t} are no
473 wider than @code{long}. The GNU coding standards allow code to make
474 this assumption, POSIX requires implementations to support at
475 least one programming environment where this is true, and such
476 environments are recommended for Gnulib-using applications. When it
477 is easy to port to non-POSIX platforms like MinGW where these types
478 are wider than @code{long}, new Gnulib code should do so, e.g., by
479 using @code{ptrdiff_t} instead of @code{long}. However, it is not
480 always that easy, and no effort has been made to check that all Gnulib
481 modules work on MinGW-like environments.
484 @code{int} and @code{unsigned int} are at least 32 bits wide. POSIX
485 and the GNU coding standards both require this.
488 Signed integer arithmetic is two's complement.
490 Previously, Gnulib code sometimes also assumed that signed integer
491 arithmetic wraps around, but modern compiler optimizations
492 sometimes do not guarantee this, and Gnulib code with this
493 assumption is now considered to be questionable.
494 @xref{Integer Properties}.
496 Although some Gnulib modules contain explicit support for
497 ones' complement and signed magnitude integer representations,
498 which are allowed by C17 and earlier,
499 these modules are the exception rather than the rule.
500 All practical Gnulib targets use two's complement, which is required by C23.
503 There are no ``holes'' in integer values: all the bits of an integer
504 contribute to its value in the usual way.
505 In particular, an unsigned type and its signed counterpart have the
506 same number of bits when you count the latter's sign bit.
507 (As an exception, Gnulib code is portable to CHERI platforms
508 even though this assumption is false for CHERI.)
511 Objects with all bits zero are treated as zero or as null pointers.
512 For example, @code{memset@ (A, 0, sizeof@ A)} initializes an array
513 @code{A} of pointers to null pointers.
516 The types @code{intptr_t} and @code{uintptr_t} exist, and pointers
517 can be converted to and from these types without loss of information.
520 Addresses and sizes behave as if objects reside in a flat address space.
525 If two nonoverlapping objects have sizes @var{S} and @var{T} represented as
526 @code{ptrdiff_t} or @code{size_t} values, then @code{@var{S} + @var{T}}
530 A pointer @var{P} points within an object @var{O} if and only if
531 @code{(char *) &@var{O} <= (char *) @var{P} && (char *) @var{P} <
532 (char *) (&@var{O} + 1)}.
535 Arithmetic on a valid pointer is equivalent to the same arithmetic on
536 the pointer converted to @code{uintptr_t}, except that offsets are
537 multiplied by the size of the pointed-to objects.
538 For example, if @code{P + I} is a valid expression involving a pointer
539 @var{P} and an integer @var{I}, then @code{(uintptr_t) (P + I) ==
540 (uintptr_t) ((uintptr_t) P + I * sizeof *P)}.
541 Similar arithmetic can be done with @code{intptr_t}, although more
542 care must be taken in case of integer overflow or negative integers.
545 A pointer @code{P} has alignment @code{A} if and only if
546 @code{(uintptr_t) P % A} is zero, and similarly for @code{intptr_t}.
549 If an existing object has size @var{S}, and if @var{T} is sufficiently
550 small (e.g., 8 KiB), then @code{@var{S} + @var{T}} cannot overflow.
551 Overflow in this case would mean that the rest of your program fits
552 into @var{T} bytes, which can't happen in realistic flat-address-space
556 Adding zero to a null pointer does not change the pointer.
557 For example, @code{0 + (char *) NULL == (char *) NULL}.
558 Similarly, subtracting zero does not change a null pointer,
559 and subtracting two null pointers results in zero.
560 A future C standard is planned to require this behavior; see
561 ``@url{https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf,
562 Allow zero length operations on null pointers}'', WG14 N3322 (2024-08-28).
566 Some system platforms violate these assumptions and are therefore not
567 Gnulib porting targets. @xref{Unsupported Platforms}.
570 @section High Quality
572 We develop and maintain a testsuite for Gnulib. The goal is to have a
573 100% firm interface so that maintainers can feel free to update to the
574 code in git at @emph{any} time and know that their application will not
575 break. This means that before any change can be committed to the
576 repository, a test suite program must be produced that exposes the bug
577 for regression testing.
579 @node Stable Branches
580 @subsection Stable Branches
582 In Gnulib, we don't use topic branches for experimental work.
583 Therefore, occasionally a broken commit may be pushed in Gnulib.
584 It does not happen often, but it does happen.
586 To compensate for this, Gnulib offers ``stable branches''. These
587 are branches of the Gnulib code that are maintained over some
588 longer period (a year, for example) and include
593 portability enhancements (to existing as well as to new platforms),
595 updates to @code{config.guess} and @code{config.sub}.
599 Not included in the stable branches are:
602 new features, such as new modules,
608 complex or risky changes in general,
610 updates to @code{texinfo.tex},
612 documentation updates.
615 So far, we have seven stable branches:
618 A stable branch that starts at the beginning of January 2025.
620 A stable branch that starts at the beginning of July 2024.
622 A stable branch that starts at the beginning of January 2024.
623 It is no longer updated.
625 A stable branch that starts at the beginning of July 2023.
626 It is no longer updated.
628 A stable branch that starts at the beginning of January 2023.
629 It is no longer updated.
631 A stable branch that starts at the beginning of July 2022.
632 It is no longer updated.
634 A stable branch that starts at the beginning of January 2022.
635 It is no longer updated.
638 The two use-cases of stable branches are thus:
641 You want to protect yourself from occasional breakage in Gnulib.
643 When making a bug-fix release of your code, you can incorporate
644 bug fixes in Gnulib, by pulling in the newest commits from the
645 same stable branch that you were already using for the previous
649 @node Writing reliable code
650 @subsection Writing reliable code
652 When compiling and testing Gnulib and Gnulib-using programs, certain
653 compiler options can help improve reliability. First of all, make it
654 a habit to use @samp{-Wall} in all compilation commands. Beyond that,
655 the @code{manywarnings} module enables several forms of static checking in
656 GCC and related compilers (@pxref{manywarnings}).
658 For dynamic checking, you can run @code{configure} with @code{CFLAGS}
659 options appropriate for your compiler. For example:
665 ' -D_FORTIFY_SOURCE=2'\
666 ' -fsanitize=undefined'\
667 ' -fsanitize-undefined-trap-on-error'
675 @code{-D_FORTIFY_SOURCE=2} enables extra security hardening checks in
678 @code{-fsanitize=undefined} enables GCC's undefined behavior sanitizer
681 @code{-fsanitize-undefined-trap-on-error} causes @code{ubsan} to
682 abort the program (through an ``illegal instruction'' signal). This
683 measure stops exploit attempts and also allows you to debug the issue.
686 Without the @code{-fsanitize-undefined-trap-on-error} option,
687 @code{-fsanitize=undefined} causes messages to be printed, and
688 execution continues after an undefined behavior situation.
689 The message printing causes GCC-like compilers to arrange for the
690 program to dynamically link to libraries it might not otherwise need.
691 With GCC, instead of @code{-fsanitize-undefined-trap-on-error} you can
692 use the @code{-static-libubsan} option to arrange for two of the extra
693 libraries (@code{libstdc++} and @code{libubsan}) to be linked
694 statically rather than dynamically, though this typically bloats the
695 executable and the remaining extra libraries are still linked
698 It is also good to occasionally run the programs under @code{valgrind}
699 (@pxref{Running self-tests under valgrind}).
701 @include join-gnu.texi