xref: /openbsd-src/lib/libexpat/README.md (revision aa071e6ed2e21e8e72a6aac46533908f2defbdef)
1bd8f1dc3Sbluhm[![Run Linux CI tasks](https://github.com/libexpat/libexpat/actions/workflows/linux.yml/badge.svg)](https://github.com/libexpat/libexpat/actions/workflows/linux.yml)
29b8e2351Sbluhm[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/libexpat/libexpat?svg=true)](https://ci.appveyor.com/project/libexpat/libexpat)
304da3532Sbluhm[![Packaging status](https://repology.org/badge/tiny-repos/expat.svg)](https://repology.org/metapackage/expat/versions)
408819b41Sbluhm[![Downloads SourceForge](https://img.shields.io/sourceforge/dt/expat?label=Downloads%20SourceForge)](https://sourceforge.net/projects/expat/files/)
508819b41Sbluhm[![Downloads GitHub](https://img.shields.io/github/downloads/libexpat/libexpat/total?label=Downloads%20GitHub)](https://github.com/libexpat/libexpat/releases)
69b8e2351Sbluhm
761ad8a07Sbluhm> [!CAUTION]
861ad8a07Sbluhm>
961ad8a07Sbluhm> Expat is **understaffed** and without funding.
1061ad8a07Sbluhm> There is a [call for help with details](https://github.com/libexpat/libexpat/blob/master/expat/Changes)
1161ad8a07Sbluhm> at the top of the `Changes` file.
129b8e2351Sbluhm
1361ad8a07Sbluhm
14*aa071e6eSbluhm# Expat, Release 2.6.4
155837d4fcSbluhm
16bd8f1dc3SbluhmThis is Expat, a C99 library for parsing
17bd8f1dc3Sbluhm[XML 1.0 Fourth Edition](https://www.w3.org/TR/2006/REC-xml-20060816/), started by
1808819b41Sbluhm[James Clark](https://en.wikipedia.org/wiki/James_Clark_%28programmer%29) in 1997.
195837d4fcSbluhmExpat is a stream-oriented XML parser.  This means that you register
205837d4fcSbluhmhandlers with the parser before starting the parse.  These handlers
215837d4fcSbluhmare called when the parser discovers the associated structures in the
225837d4fcSbluhmdocument being parsed.  A start tag is an example of the kind of
235837d4fcSbluhmstructures for which you may register handlers.
245837d4fcSbluhm
2528ce3119SbluhmExpat supports the following compilers:
26326b8ed6Sbluhm
2728ce3119Sbluhm- GNU GCC >=4.5
2828ce3119Sbluhm- LLVM Clang >=3.5
2961ad8a07Sbluhm- Microsoft Visual Studio >=16.0/2019 (rolling `${today} minus 5 years`)
3028ce3119Sbluhm
312a4a206eSbluhmWindows users can use the
3208819b41Sbluhm[`expat-win32bin-*.*.*.{exe,zip}` download](https://github.com/libexpat/libexpat/releases),
33326b8ed6Sbluhmwhich includes both pre-compiled libraries and executables, and source code for
345837d4fcSbluhmdevelopers.
355837d4fcSbluhm
365837d4fcSbluhmExpat is [free software](https://www.gnu.org/philosophy/free-sw.en.html).
375837d4fcSbluhmYou may copy, distribute, and modify it under the terms of the License
385837d4fcSbluhmcontained in the file
395837d4fcSbluhm[`COPYING`](https://github.com/libexpat/libexpat/blob/master/expat/COPYING)
405837d4fcSbluhmdistributed with this package.
415837d4fcSbluhmThis license is the same as the MIT/X Consortium license.
425837d4fcSbluhm
43326b8ed6Sbluhm
44326b8ed6Sbluhm## Using libexpat in your CMake-Based Project
45326b8ed6Sbluhm
46*aa071e6eSbluhmThere are three documented ways of using libexpat with CMake:
47326b8ed6Sbluhm
48*aa071e6eSbluhm### a) `find_package` with Module Mode
49326b8ed6Sbluhm
50326b8ed6SbluhmThis approach leverages CMake's own [module `FindEXPAT`](https://cmake.org/cmake/help/latest/module/FindEXPAT.html).
51326b8ed6Sbluhm
5208819b41SbluhmNotice the *uppercase* `EXPAT` in the following example:
53326b8ed6Sbluhm
54326b8ed6Sbluhm```cmake
5508819b41Sbluhmcmake_minimum_required(VERSION 3.0)  # or 3.10, see below
56326b8ed6Sbluhm
57326b8ed6Sbluhmproject(hello VERSION 1.0.0)
58326b8ed6Sbluhm
59326b8ed6Sbluhmfind_package(EXPAT 2.2.8 MODULE REQUIRED)
60326b8ed6Sbluhm
61326b8ed6Sbluhmadd_executable(hello
62326b8ed6Sbluhm    hello.c
63326b8ed6Sbluhm)
64326b8ed6Sbluhm
6508819b41Sbluhm# a) for CMake >=3.10 (see CMake's FindEXPAT docs)
66326b8ed6Sbluhmtarget_link_libraries(hello PUBLIC EXPAT::EXPAT)
6708819b41Sbluhm
6808819b41Sbluhm# b) for CMake >=3.0
69326b8ed6Sbluhmtarget_include_directories(hello PRIVATE ${EXPAT_INCLUDE_DIRS})
70326b8ed6Sbluhmtarget_link_libraries(hello PUBLIC ${EXPAT_LIBRARIES})
71326b8ed6Sbluhm```
72326b8ed6Sbluhm
73*aa071e6eSbluhm### b) `find_package` with Config Mode
74326b8ed6Sbluhm
7508819b41SbluhmThis approach requires files from…
76326b8ed6Sbluhm
7708819b41Sbluhm- libexpat >=2.2.8 where packaging uses the CMake build system
7808819b41Sbluhmor
7908819b41Sbluhm- libexpat >=2.3.0 where packaging uses the GNU Autotools build system
8008819b41Sbluhm  on Linux
8108819b41Sbluhmor
8208819b41Sbluhm- libexpat >=2.4.0 where packaging uses the GNU Autotools build system
8308819b41Sbluhm  on macOS or MinGW.
8408819b41Sbluhm
8508819b41SbluhmNotice the *lowercase* `expat` in the following example:
86326b8ed6Sbluhm
87326b8ed6Sbluhm```cmake
88326b8ed6Sbluhmcmake_minimum_required(VERSION 3.0)
89326b8ed6Sbluhm
90326b8ed6Sbluhmproject(hello VERSION 1.0.0)
91326b8ed6Sbluhm
92326b8ed6Sbluhmfind_package(expat 2.2.8 CONFIG REQUIRED char dtd ns)
93326b8ed6Sbluhm
94326b8ed6Sbluhmadd_executable(hello
95326b8ed6Sbluhm    hello.c
96326b8ed6Sbluhm)
97326b8ed6Sbluhm
98326b8ed6Sbluhmtarget_link_libraries(hello PUBLIC expat::expat)
99326b8ed6Sbluhm```
100326b8ed6Sbluhm
101*aa071e6eSbluhm### c) The `FetchContent` module
102*aa071e6eSbluhm
103*aa071e6eSbluhmThis approach — as demonstrated below — requires CMake >=3.18 for both the
104*aa071e6eSbluhm[`FetchContent` module](https://cmake.org/cmake/help/latest/module/FetchContent.html)
105*aa071e6eSbluhmand its support for the `SOURCE_SUBDIR` option to be available.
106*aa071e6eSbluhm
107*aa071e6eSbluhmPlease note that:
108*aa071e6eSbluhm- Use of the `FetchContent` module with *non-release* SHA1s or `master`
109*aa071e6eSbluhm  of libexpat is neither advised nor considered officially supported.
110*aa071e6eSbluhm- Pinning to a specific commit is great for robust CI.
111*aa071e6eSbluhm- Pinning to a specific commit needs updating every time there is a new
112*aa071e6eSbluhm  release of libexpat — either manually or through automation —,
113*aa071e6eSbluhm  to not miss out on libexpat security updates.
114*aa071e6eSbluhm
115*aa071e6eSbluhmFor an example that pulls in libexpat via Git:
116*aa071e6eSbluhm
117*aa071e6eSbluhm```cmake
118*aa071e6eSbluhmcmake_minimum_required(VERSION 3.18)
119*aa071e6eSbluhm
120*aa071e6eSbluhminclude(FetchContent)
121*aa071e6eSbluhm
122*aa071e6eSbluhmproject(hello VERSION 1.0.0)
123*aa071e6eSbluhm
124*aa071e6eSbluhmFetchContent_Declare(
125*aa071e6eSbluhm    expat
126*aa071e6eSbluhm    GIT_REPOSITORY https://github.com/libexpat/libexpat/
127*aa071e6eSbluhm    GIT_TAG        000000000_GIT_COMMIT_SHA1_HERE_000000000  # i.e. Git tag R_0_Y_Z
128*aa071e6eSbluhm    SOURCE_SUBDIR  expat/
129*aa071e6eSbluhm)
130*aa071e6eSbluhm
131*aa071e6eSbluhmFetchContent_MakeAvailable(expat)
132*aa071e6eSbluhm
133*aa071e6eSbluhmadd_executable(hello
134*aa071e6eSbluhm    hello.c
135*aa071e6eSbluhm)
136*aa071e6eSbluhm
137*aa071e6eSbluhmtarget_link_libraries(hello PUBLIC expat)
138*aa071e6eSbluhm```
139*aa071e6eSbluhm
140326b8ed6Sbluhm
14108819b41Sbluhm## Building from a Git Clone
142326b8ed6Sbluhm
1435837d4fcSbluhmIf you are building Expat from a check-out from the
1445837d4fcSbluhm[Git repository](https://github.com/libexpat/libexpat/),
1455837d4fcSbluhmyou need to run a script that generates the configure script using the
1465837d4fcSbluhmGNU autoconf and libtool tools.  To do this, you need to have
1475837d4fcSbluhmautoconf 2.58 or newer. Run the script like this:
1485837d4fcSbluhm
1495837d4fcSbluhm```console
1505837d4fcSbluhm./buildconf.sh
1515837d4fcSbluhm```
1525837d4fcSbluhm
1535837d4fcSbluhmOnce this has been done, follow the same instructions as for building
1545837d4fcSbluhmfrom a source distribution.
1555837d4fcSbluhm
156326b8ed6Sbluhm
15708819b41Sbluhm## Building from a Source Distribution
158326b8ed6Sbluhm
159326b8ed6Sbluhm### a) Building with the configure script (i.e. GNU Autotools)
160326b8ed6Sbluhm
1615837d4fcSbluhmTo build Expat from a source distribution, you first run the
1625837d4fcSbluhmconfiguration shell script in the top level distribution directory:
1635837d4fcSbluhm
1645837d4fcSbluhm```console
1655837d4fcSbluhm./configure
1665837d4fcSbluhm```
1675837d4fcSbluhm
1685837d4fcSbluhmThere are many options which you may provide to configure (which you
1695837d4fcSbluhmcan discover by running configure with the `--help` option).  But the
1705837d4fcSbluhmone of most interest is the one that sets the installation directory.
1715837d4fcSbluhmBy default, the configure script will set things up to install
1725837d4fcSbluhmlibexpat into `/usr/local/lib`, `expat.h` into `/usr/local/include`, and
1735837d4fcSbluhm`xmlwf` into `/usr/local/bin`.  If, for example, you'd prefer to install
1745837d4fcSbluhminto `/home/me/mystuff/lib`, `/home/me/mystuff/include`, and
1755837d4fcSbluhm`/home/me/mystuff/bin`, you can tell `configure` about that with:
1765837d4fcSbluhm
1775837d4fcSbluhm```console
1785837d4fcSbluhm./configure --prefix=/home/me/mystuff
1795837d4fcSbluhm```
1805837d4fcSbluhm
1815837d4fcSbluhmAnother interesting option is to enable 64-bit integer support for
1825837d4fcSbluhmline and column numbers and the over-all byte index:
1835837d4fcSbluhm
1845837d4fcSbluhm```console
1855837d4fcSbluhm./configure CPPFLAGS=-DXML_LARGE_SIZE
1865837d4fcSbluhm```
1875837d4fcSbluhm
1885837d4fcSbluhmHowever, such a modification would be a breaking change to the ABI
1895837d4fcSbluhmand is therefore not recommended for general use — e.g. as part of
1905837d4fcSbluhma Linux distribution — but rather for builds with special requirements.
1915837d4fcSbluhm
1925837d4fcSbluhmAfter running the configure script, the `make` command will build
1935837d4fcSbluhmthings and `make install` will install things into their proper
1945837d4fcSbluhmlocation.  Have a look at the `Makefile` to learn about additional
1955837d4fcSbluhm`make` options.  Note that you need to have write permission into
1965837d4fcSbluhmthe directories into which things will be installed.
1975837d4fcSbluhm
1985837d4fcSbluhmIf you are interested in building Expat to provide document
1995837d4fcSbluhminformation in UTF-16 encoding rather than the default UTF-8, follow
2002e724bc9Sbluhmthese instructions (after having run `make distclean`).
2012e724bc9SbluhmPlease note that we configure with `--without-xmlwf` as xmlwf does not
2022e724bc9Sbluhmsupport this mode of compilation (yet):
2032e724bc9Sbluhm
2042e724bc9Sbluhm1. Mass-patch `Makefile.am` files to use `libexpatw.la` for a library name:
2052e724bc9Sbluhm   <br/>
20661ad8a07Sbluhm   `find . -name Makefile.am -exec sed
2079b8e2351Sbluhm       -e 's,libexpat\.la,libexpatw.la,'
2082e724bc9Sbluhm       -e 's,libexpat_la,libexpatw_la,'
20961ad8a07Sbluhm       -i.bak {} +`
2102e724bc9Sbluhm
2112e724bc9Sbluhm1. Run `automake` to re-write `Makefile.in` files:<br/>
2122e724bc9Sbluhm   `automake`
2135837d4fcSbluhm
2145837d4fcSbluhm1. For UTF-16 output as unsigned short (and version/error strings as char),
2155837d4fcSbluhm   run:<br/>
2162e724bc9Sbluhm   `./configure CPPFLAGS=-DXML_UNICODE --without-xmlwf`<br/>
2175837d4fcSbluhm   For UTF-16 output as `wchar_t` (incl. version/error strings), run:<br/>
2182e724bc9Sbluhm   `./configure CFLAGS="-g -O2 -fshort-wchar" CPPFLAGS=-DXML_UNICODE_WCHAR_T
2192e724bc9Sbluhm       --without-xmlwf`
2205837d4fcSbluhm   <br/>Note: The latter requires libc compiled with `-fshort-wchar`, as well.
2215837d4fcSbluhm
2222e724bc9Sbluhm1. Run `make` (which excludes xmlwf).
2235837d4fcSbluhm
2242e724bc9Sbluhm1. Run `make install` (again, excludes xmlwf).
2255837d4fcSbluhm
2262e724bc9SbluhmUsing `DESTDIR` is supported.  It works as follows:
2275837d4fcSbluhm
2285837d4fcSbluhm```console
2295837d4fcSbluhmmake install DESTDIR=/path/to/image
2305837d4fcSbluhm```
2315837d4fcSbluhm
2322e724bc9Sbluhmoverrides the in-makefile set `DESTDIR`, because variable-setting priority is
2335837d4fcSbluhm
2345837d4fcSbluhm1. commandline
2352e724bc9Sbluhm1. in-makefile
2362e724bc9Sbluhm1. environment
2375837d4fcSbluhm
2385837d4fcSbluhmNote: This only applies to the Expat library itself, building UTF-16 versions
2395837d4fcSbluhmof xmlwf and the tests is currently not supported.
2405837d4fcSbluhm
2415837d4fcSbluhmWhen using Expat with a project using autoconf for configuration, you
2425837d4fcSbluhmcan use the probing macro in `conftools/expat.m4` to determine how to
2435837d4fcSbluhminclude Expat.  See the comments at the top of that file for more
2445837d4fcSbluhminformation.
2455837d4fcSbluhm
2465837d4fcSbluhmA reference manual is available in the file `doc/reference.html` in this
2475837d4fcSbluhmdistribution.
24828ce3119Sbluhm
24928ce3119Sbluhm
250326b8ed6Sbluhm### b) Building with CMake
251326b8ed6Sbluhm
252326b8ed6SbluhmThe CMake build system is still *experimental* and may replace the primary
2532a4a206eSbluhmbuild system based on GNU Autotools at some point when it is ready.
254326b8ed6Sbluhm
255326b8ed6Sbluhm
256326b8ed6Sbluhm#### Available Options
257326b8ed6Sbluhm
25828ce3119SbluhmFor an idea of the available (non-advanced) options for building with CMake:
25928ce3119Sbluhm
26028ce3119Sbluhm```console
26128ce3119Sbluhm# rm -f CMakeCache.txt ; cmake -D_EXPAT_HELP=ON -LH . | grep -B1 ':.*=' | sed 's,^--$,,'
26228ce3119Sbluhm// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
26328ce3119SbluhmCMAKE_BUILD_TYPE:STRING=
26428ce3119Sbluhm
26528ce3119Sbluhm// Install path prefix, prepended onto install directories.
26628ce3119SbluhmCMAKE_INSTALL_PREFIX:PATH=/usr/local
26728ce3119Sbluhm
26828ce3119Sbluhm// Path to a program.
26928ce3119SbluhmDOCBOOK_TO_MAN:FILEPATH=/usr/bin/docbook2x-man
27028ce3119Sbluhm
2719029d806Sbluhm// Build man page for xmlwf
27228ce3119SbluhmEXPAT_BUILD_DOCS:BOOL=ON
27328ce3119Sbluhm
2749029d806Sbluhm// Build the examples for expat library
27528ce3119SbluhmEXPAT_BUILD_EXAMPLES:BOOL=ON
27628ce3119Sbluhm
2779029d806Sbluhm// Build fuzzers for the expat library
27828ce3119SbluhmEXPAT_BUILD_FUZZERS:BOOL=OFF
27928ce3119Sbluhm
2809029d806Sbluhm// Build pkg-config file
2812a4a206eSbluhmEXPAT_BUILD_PKGCONFIG:BOOL=ON
2822a4a206eSbluhm
2839029d806Sbluhm// Build the tests for expat library
28428ce3119SbluhmEXPAT_BUILD_TESTS:BOOL=ON
28528ce3119Sbluhm
2869029d806Sbluhm// Build the xmlwf tool for expat library
28728ce3119SbluhmEXPAT_BUILD_TOOLS:BOOL=ON
28828ce3119Sbluhm
28928ce3119Sbluhm// Character type to use (char|ushort|wchar_t) [default=char]
29028ce3119SbluhmEXPAT_CHAR_TYPE:STRING=char
29128ce3119Sbluhm
2929029d806Sbluhm// Install expat files in cmake install target
29328ce3119SbluhmEXPAT_ENABLE_INSTALL:BOOL=ON
29428ce3119Sbluhm
29528ce3119Sbluhm// Use /MT flag (static CRT) when compiling in MSVC
29628ce3119SbluhmEXPAT_MSVC_STATIC_CRT:BOOL=OFF
29728ce3119Sbluhm
2989029d806Sbluhm// Build fuzzers via ossfuzz for the expat library
2992a4a206eSbluhmEXPAT_OSSFUZZ_BUILD:BOOL=OFF
3002a4a206eSbluhm
3019029d806Sbluhm// Build a shared expat library
30228ce3119SbluhmEXPAT_SHARED_LIBS:BOOL=ON
30328ce3119Sbluhm
30428ce3119Sbluhm// Treat all compiler warnings as errors
30528ce3119SbluhmEXPAT_WARNINGS_AS_ERRORS:BOOL=OFF
30628ce3119Sbluhm
30728ce3119Sbluhm// Make use of getrandom function (ON|OFF|AUTO) [default=AUTO]
30828ce3119SbluhmEXPAT_WITH_GETRANDOM:STRING=AUTO
30928ce3119Sbluhm
3109029d806Sbluhm// Utilize libbsd (for arc4random_buf)
31128ce3119SbluhmEXPAT_WITH_LIBBSD:BOOL=OFF
31228ce3119Sbluhm
31328ce3119Sbluhm// Make use of syscall SYS_getrandom (ON|OFF|AUTO) [default=AUTO]
31428ce3119SbluhmEXPAT_WITH_SYS_GETRANDOM:STRING=AUTO
31528ce3119Sbluhm```
316