Previous: Source Layout, Up: Maintenance

### D.2 Porting the GNU C Library

The GNU C Library is written to be easily portable to a variety of machines and operating systems. Machine- and operating system-dependent functions are well separated to make it easy to add implementations for new machines or operating systems. This section describes the layout of the library source tree and explains the mechanisms used to select machine-dependent code to use.

All the machine-dependent and operating system-dependent files in the library are in the subdirectory `sysdeps` under the top-level library source directory. This directory contains a hierarchy of subdirectories (see Hierarchy Conventions).

Each subdirectory of `sysdeps` contains source files for a particular machine or operating system, or for a class of machine or operating system (for example, systems by a particular vendor, or all machines that use IEEE 754 floating-point format). A configuration specifies an ordered list of these subdirectories. Each subdirectory implicitly appends its parent directory to the list. For example, specifying the list `unix/bsd/vax` is equivalent to specifying the list `unix/bsd/vax unix/bsd unix`. A subdirectory can also specify that it implies other subdirectories which are not directly above it in the directory hierarchy. If the file `Implies` exists in a subdirectory, it lists other subdirectories of `sysdeps` which are appended to the list, appearing after the subdirectory containing the `Implies` file. Lines in an `Implies` file that begin with a ‘`#`’ character are ignored as comments. For example, `unix/bsd/Implies` contains:

         # BSD has Internet-related things.
         unix/inet

and `unix/Implies` contains:

         posix

So the final list is `unix/bsd/vax unix/bsd unix/inet unix posix`.

`sysdeps` has a “special” subdirectory called `generic`. It is always implicitly appended to the list of subdirectories, so you needn't put it in an `Implies` file, and you should not create any subdirectories under it intended to be new specific categories. `generic` serves two purposes. First, the makefiles do not bother to look for a system-dependent version of a file that's not in `generic`. This means that any system-dependent source file must have an analogue in `generic`, even if the routines defined by that file are not implemented on other platforms. Second, the `generic` version of a system-dependent file is used if the makefiles do not find a version specific to the system you're compiling for.

If it is possible to implement the routines in a `generic` file in machine-independent C, using only other machine-independent functions in the C library, then you should do so. Otherwise, make them stubs. A stub function is a function which cannot be implemented on a particular machine or operating system. Stub functions always return an error, and set `errno` to `ENOSYS` (Function not implemented). See Error Reporting. If you define a stub function, you must place the statement `stub_warning(`function`)`, where function is the name of your function, after its definition. This causes the function to be listed in the installed `<gnu/stubs.h>`, and makes GNU ld warn when the function is used.

Some rare functions are only useful on specific systems and aren't defined at all on others; these do not appear anywhere in the system-independent source code or makefiles (including the `generic` directory), only in the system-dependent `Makefile` in the specific system's subdirectory.

If you come across a file that is in one of the main source directories (`string`, `stdio`, etc.), and you want to write a machine- or operating system-dependent version of it, move the file into `sysdeps/generic` and write your new implementation in the appropriate system-specific subdirectory. Note that if a file is to be system-dependent, it must not appear in one of the main source directories.

There are a few special files that may exist in each subdirectory of `sysdeps`:

`Makefile`
  A makefile for this machine or operating system, or class of machine or operating system. This file is included by the library makefile `Makerules`, which is used by the top-level makefile and the subdirectory makefiles. It can change the variables set in the including makefile or add new rules. It can use GNU `make` conditional directives based on the variable ‘`subdir`’ (see above) to select different sets of variables and rules for different sections of the library. It can also set the `make` variable ‘`sysdep-routines`’, to specify extra modules to be included in the library. You should use ‘`sysdep-routines`’ rather than adding modules to ‘`routines`’ because the latter is used in determining what to distribute for each subdirectory of the main source tree.

  Each makefile in a subdirectory in the ordered list of subdirectories to be searched is included in order. Since several system-dependent makefiles may be included, each should append to ‘`sysdep-routines`’ rather than simply setting it:

                sysdep-routines := $(sysdep-routines) foo bar

`Subdirs`
  This file contains the names of new whole subdirectories under the top-level library source tree that should be included for this system. These subdirectories are treated just like the system-independent subdirectories in the library source tree, such as `stdio` and `math`.

  Use this when there are completely new sets of functions and header files that should go into the library for the system this subdirectory of `sysdeps` implements. For example, `sysdeps/unix/inet/Subdirs` contains `inet`; the `inet` directory contains various network-oriented operations which only make sense to put in the library on systems that support the Internet.

`configure`
  This file is a shell script fragment to be run at configuration time. The top-level `configure` script uses the shell `.` command to read the `configure` file in each system-dependent directory chosen, in order. The `configure` files are often generated from `configure.in` files using Autoconf.

  A system-dependent `configure` script will usually add things to the shell variables ‘`DEFS`’ and ‘`config_vars`’; see the top-level `configure` script for details. The script can check for ‘`--with-package`’ options that were passed to the top-level `configure`. For an option ‘`--with-package=value`’ `configure` sets the shell variable ‘`with_package`’ (with any dashes in package converted to underscores) to value; if the option is just ‘`--with-package`’ (no argument), then it sets ‘`with_package`’ to ‘`yes`’.

`configure.in`
  This file is an Autoconf input fragment to be processed into the file `configure` in this subdirectory. See Introduction, for a description of Autoconf. You should write either `configure` or `configure.in`, but not both. The first line of `configure.in` should invoke the `m4` macro ‘`GLIBC_PROVIDES`’. This macro does several `AC_PROVIDE` calls for Autoconf macros which are used by the top-level `configure` script; without this, those macros might be invoked again unnecessarily by Autoconf.

That is the general system for how system-dependencies are isolated.