1This HOWTO file contains notes for maintainers and contributors to Newlib.
2For information on using Newlib (building, installing), see README.  (In
3particular, the "Regenerating Configuration Files" section in README is of
4interest to both users and contributors.)
5
6(This file could probably use some other "sections" in addition to the
7initially-provided sections.  Please help by adding as appropriate.)
8
9DOCUMENTATION
10=============
11
12     All the documentation for Newlib comes as part of the machine-readable
13distribution.  Functions are documented in the source files, although not
14every file contains documentation, as many functions share manual page
15information.  For example, lround(), lroundf(), llround(), and llroundf()
16share a single man page, which is in the source for lround().  The documentation
17is written partially in a custom format and partially in Texinfo format.
18
19     The custom part comprises makedoc.c and doc.str, both of which are in the
20doc directory.  doc.str is a macro file that can be used to define things to
21be done by makedoc, using building blocks that are built into makedoc.c.
22The basic function of makedoc is two-fold.  First, it recognizes comments in
23the proper format to pull out of source files.  Second, it adds some Texinfo
24commands as well as translating certain sequences into the appropriate
25Texinfo commands.  Refer to makedoc.c and doc.str for what these are.
26(makedoc.c is not particularly-well commented.)  Another way to see how they
27work is to simply examine the source files with documentation comments.
28
29(A couple of examples that use some of the fancier options:
30	libm/common/s_isnan.c ("o+" variable-"bullet" list),
31	libc/stdio/sprintf.c ("O+" bullet list; "." for example code).)
32
33     In addition to the makedoc.c stuff, Texinfo commands can be directly
34used.  Texinfo is a documentation system that uses a single source file to
35produce both on-line information and a printed manual.  You can use one of the
36Info formatting commands to create the on-line version of the documentation
37and TeX (or `texi2roff') to typeset the printed version.  While Newlib contains
38a copy of the texinfo package (texinfo.tex), the manual for it is not
39included.  The latest one may be found at
40
41https://www.gnu.org/software/texinfo/manual/texinfo/texinfo.html
42
43(which could be for a newer version of texinfo.tex than is included in Newlib).
44
45     In addition to Texinfo commands, straight TeX commands can also be used,
46but these, however, should be carefully limited and be given alternates for
47when a non-printed manual is produced--such as when info pages are produced.
48For an example of this kind of usage, see libm/common/s_logb.c.
49
50     If writing a new function that requires documentation, the required
51sections are FUNCTION, INDEX, SYNOPSIS, DESCRIPTION, RETURNS,
52and PORTABILITY.  BUGS, NOTES, SEEALSO and WARNINGS should be added as
53appropriate.
54
55     Source files which contain documentation are processed into ".def"
56files with the extracted information.  These .def files are noted in the
57makefiles as CHEWOUT_FILES.  These .def files need to be included into an
58appropriate .tex file for inclusion in the manuals (one each for libc and libm).
59Pay special attention under libc, as the manual is arranged by header file name,
60but not all header files are represented by directories (e.g.  wcsftime.c is
61found under libc/time, but goes under wchar.h in the manual.)
62
63In summary, to add new documentation:
64
651.  Add properly-formatted comments to source file (e.g. src.c);
662.  add "chewout" file to CHEWOUT_FILES list in Makefile.am (e.g. src.def),
67    re-generate Makefile.in;
683.  @include that .def file in the appropriate .tex file, add texinfo menu
69    entries that reference the @node(s) in that .def file;
704.  make ChangeLog entry and generate patch.
71
72EL/IX (ELIX_LEVEL_n, ELIX_n_SOURCES)
73====================================
74
75     Some of the Makefiles contain definitions of ELIX_LEVEL_1 ... ELIX_LEVEL_4,
76and corresponding definitions for ELIX_1_SOURCES ... ELIX_4_SOURCES.
77These are referring to the EL/IX standards created by Red Hat for the
78purpose of Linux-based open standards for embedded development.  In brief,
79the intent is to define multiple levels for API functions that allows the
80user to size the library appropriately for their application--at least in
81terms of the predefined lists.  For full details, refer to the EL/IX home
82page at https://sourceware.org/elix/.  The likely best way to tell how to
83classify any new functions in terms of needing an ELIX level qualification
84is to ask Jeff Johnston.  To see how it works and try classification on your
85own, refer to the API specification on the web site,
86
87https://sourceware.org/elix/api/current/api.pdf
88
89(Unfortunately, it is not complete with respect to either the C99 or POSIX
90standards, so particular C and POSIX functions may or may not be found.)
91
92     The following definitions of the levels are from the (draft) standard.
93
94Level 1
95     RTOS compatible layer.  Functions available in both Linux and a
96typical deeply embedded operating system (eCos, RTEMS, VxWorks, pSOS, VRTX32,
97etc.).  Some functions may have reduced or modified semantics.
98
99Level 2
100     Linux single process only. Includes level 1 plus any functions from Linux
101that are not easily implemented on an RTOS.  Also full implementations of
102reduced functions in Level 1.
103
104Level 3
105     Linux multiprocess for embedded applications.  This is basically POSIX.1
106with some of the functions that are obviously not for embedded applications
107(such as job control) removed.
108
109Level 4
110     Full POSIX or Linux compliance.  Essentially these are functions that are
111present in a standard Linux kernel but are irrelevant to an embedded system.
112These functions do not form part of the EL/IX API.
113
114BUILD SYSTEM
115============
116
117     Newlib utilizes GNU Autotools (GNU Autoconf & GNU Automake) for its build
118system.  It can be broken down into two phases: configure & compile (make).
119NB: GNU Libtool is not used.  Newlib only produces static archives, and GNU
120Automake is capable of handling that.
121
122     As noted in the README file, you do not need to worry about which tools to
123run at any particular point.  Simply invoke `autoreconf` and it will do it all.
124
125     Tool versions are tightly controlled.  GNU Autoconf 2.69 exactly must be
126used.  Attempts to use any other version, newer or older, will be rejected.  If
127the current system does not include that version, it can be downloaded from the
128GNU Autoconf website and installed into a local directory tree (like ~/.local).
129GNU Automake 1.15.1 is required as a minimum.  Newer versions are allowed when
130running locally, but developers submitting patches must use 1.15.1 exactly.
131
132     The configure phase naturally starts with the configure script.  It merges
133multiple inputs to produce the final script.
134* aclocal.m4: Generated by aclocal; do not edit.  Copies all the various m4
135  macros used by configure from the system install into the local copy.  This
136  allows regenerating configure without needing all the same system packages.
137* autom4te.cache/: Generated by aclocal to cache system search results.  Safe
138  to delete at any time, but never edit anything in this directory.
139* iconv.m4: Generated by libc/iconv/ces/mkdeps.pl; do not edit.  Keeps the
140  set of dynamic iconv synced between the source files and configure.
141* acinclude.m4: Historically, a way of sharing configure logic with subdirs.
142  At some point, it should all be merged into configure.ac directly.
143* configure.ac: The main configure script for supporting all Newlib options.
144* configure.host: An index of every supported Newlib host and the settings to
145  use when compiling for it.
146* **/acinclude.m4: Subdirectories may include custom configure tests without
147  having to clutter the common configure.ac or acinclude.m4 file.  When adding
148  a new file, it must be manually included -- there is no mechanism to search
149  and automatically include every such file in the tree.  Look for m4_include
150  lines in configure.ac or existing acinclude.m4 files as examples.  Care must
151  be taken to not run excessive tests for incompatible targets, or to declare
152  generic names or symbols that can collide with other targets.  Any such file
153  should try to match $host (or similar) settings before running.  Settings
154  from configure.host (e.g. machine_dir or sys_dir) are guaranteed to be
155  available.  When creating names (Makefile variables, defines, etc...), try to
156  include the respective library and machine or sys name.  Do not confuse these
157  with the historical top-level acinclude.m4 file!
158
159     The configure script has to take special consideration to the fact that it
160usually runs with a toolchain that lacks a C library (since Newlib will provide
161it once it's compiled & installed).  Newlib uses the uncommon AC_NO_EXECUTABLES
162macro to tell GNU Autoconf to not automatically require a toolchain that can
163link executables.  As a result, Newlib itself cannot perform any link tests.
164It shouldn't really need to, but configure authors must keep this in mind and
165stick to the appropriate source-level test -- AC_PREPROC for testing behavior
166of the preprocessor and AC_COMPILE for testing compiler behavior.
167
168     The newlib.h header is created from newlib.hin during the configure phase.
169The newlib.hin file is managed by autoheader, so do not edit it directly.  Any
170symbol declared in configure via AC_DEFINE is automatically included.  The file
171declares all the Newlib-specific configure settings and will be installed as
172part of the standard set of headers.  Critically, all symbols are namespaced
173with a leading _ prefix to avoid collisions with user code, and any symbols not
174namespaced are automatically removed.  This is necessary due to some system m4
175macros automatically adding their own defines that we cannot disable.  It also
176means that, when using AC_DEFINE, the _ prefix must be manually added.
177
178     A single Makefile is created during the configure phase.  This will build
179all of Newlib using non-recursive make (i.e., there are no subdirectories with
180their own Makefiles).  The single Makefile.am file includes Makefile.inc files
181in its immediate subdirectories, and those further include Makefile.inc files
182in their subdirectories.  This means all variables and targets exist in a
183single global flat namespace, so care must be taken to not use generic names
184like "SOURCES" in any Makefile.inc file.  Instead, use %C%_ on all variables to
185get a unique prefix, and GNU Automake will expand it.  In order to use one of
186the standard GNU Automake variables (e.g., CLEANFILES), first declare it at the
187top of the Makefile.am by assigning it (e.g., CLEANFILES =), and then always
188append to it in Makefile.inc (e.g. CLEANFILES += ...).  Again, GNU Automake
189will take care of combining all of this logic together to produce a portable
190Makefile.
191
192     Developers used to changing to a subdirectory and running make commands
193to iterate quickly on a smaller section of Newlib might be surprised that this
194does not work -- there is no Makefile in any subdirectory.  Instead, make has
195to be run from the top-level, and the full path to the target is specified.
196So instead of running `cd subdir && make foo.o`, run `make subdir/foo.o`.  This
197applies to any target, not just objects.  Some common targets:
198make libm.a
199make libc.a
200
201     Conditionals are allowed in Makefile.inc files, but using them to elide
202an entire subdir Makefile.inc should be avoided.  Instead, GNU Automake
203conditionals should be used only within a single Makefile.inc file to control
204settings specific to that directory.  See the documentation section below for
205more information.
206
207     Per-directory compiler settings are allowed.  Similar to the syntax used
208above, the _%C% suffix can be used.  See the Makefile.am file for the current
209set of flags (e.g. CFLAGS, CPPFLAGS, etc...) that are supported.
210AM_CFLAGS_%C% = ...
211libm_a_CFLAGS_%C% = ...
212libc_a_CFLAGS_%C% = ...
213
214     Per-file compiler settings are allowed.  Similar to the syntax used
215above, the _%C% suffix can be used.  See the Makefile.am file for the current
216set of flags (e.g. CFLAGS, CPPFLAGS, etc...) that are supported.
217AM_CFLAGS_%C%_moo.c = ...
218libm_a_CFLAGS_%C%_foo.c = ...
219libc_a_CFLAGS_%C%_bar.c = ...
220
221     An important aspect of the build system is allowing machine-specific
222directories to override common implementations.  For example, Newlib provides
223generic C implementations of string & memory functions in libc/string/ that
224perform adequately for any target, but many architectures include hand written
225assembly files for smaller & faster execution.  Those files live under the
226corresponding libc/machine/<ARCHITECTURE>/ directory.  Both versions are
227compiled when building Newlib, but when generating the libc.a archive, the
228common object is added first, followed by the machine-specific one.  This works
229because the object names follow a consistent convention: libm_a-<filename>.o
230for the math library and libc_a-<filename>.o for the C library.  If the common
231code stores a function foo in foo.c, then it can be overridden by another foo.c
232or foo.S or foo.s file since they all produce foo.o.  For this reason, all
233machine Makefile.inc files must be included last after all common Makefile.inc
234files.
235
236     Note that the Makefiles do not use VPATH to search source files across
237multiple source directories.  VPATH is used to match a single build directory
238to the corresponding source directory, but that is it.  For more details on
239how machine directories override functions in common directories, see the
240previous section.
241
242     Keep in mind that machine overrides only work at the file level, not at
243the symbol level.  If a common file defines multiple symbols, then the machine
244override must define all the same symbols.  This is why most common code stores
245only one function per source file.
246
247     The documentation, both the manual and man pages and header references,
248should be consistent regardless of what configure options are used (including
249the target system).  This makes it easy to get a complete documentation build
250at all times.  If inclusion of chapters depended on specific configure options,
251it would be difficult to find the set of options necessary to produce the
252complete manual, if it were even possible in the first place (as some configure
253options are incompatible).  Since documentation chapters & APIs are declared in
254the respective directories and not in a centralized location, it is important
255to not use any GNU Automake conditional to control whether Makefile.inc files
256are included.  Documentation that covers optional APIs should note that it is
257not guaranteed to be included in every build of Newlib for every target.  The
258level of detail is left up to the author's discretion.
259
260     Newlib automatically builds itself for all multilib configurations that
261the active toolchain supports.  This logic is provided by common code from the
262combined toolchain source tree, but bolts itself into the GNU Autoconf and
263Automake files.  The critical parts to keep in mind:
264* ../config/multi.m4: AM_ENABLE_MULTILIB adds the --{en,dis}able-multilib
265  options to configure for users, and takes care of calling ../config-ml.in
266  for the Makefile in the current directory.  While not applicable to Newlib
267  anymore (since Newlib only produces a single Makefile now), this does not
268  automatically run for any subdir Makefiles.  Instead, it must be run manually
269  in AC_CONFIG_FILES's commands argument.
270* ../config-ml.in: Rewrites the specified Makefile to include the various
271  multilib variables and the multi-do command.  Notably, this only makes the
272  multi-do rule available, it does not cause it to be used anywhere.
273* ../multilib.am: Manually included in Newlib's Makefile.am.  This adds calls
274  to multi-do for the common important rules: all (i.e. compiling), install,
275  clean, etc...
276* FLAGS_TO_PASS: Newlib's Makefile.am declares all the variables to pass to
277  each multilib configuration via the multi-do rule.  While the multi-do rule
278  includes many common flags automatically, Newlib passes along a few extra
279  that are unique to it.
280
281    The testsuite is not integrated at all currently.  It is not really well
282tested in general.  The README file has more information.  Hopefully someone
283someday will step up to integrate it into the existing framework.
284