CMake.README
1 == How to build expat with cmake (experimental) ==
2
3 The cmake based buildsystem for expat works on Windows (cygwin, mingw, Visual
4 Studio) and should work on all other platform cmake supports.
5
6 Assuming ~/expat-2.7.1 is the source directory of expat, add a subdirectory
7 build and change into that directory:
8 ~/expat-2.7.1$ mkdir build && cd build
9 ~/expat-2.7.1/build$
10
11 From that directory, call cmake first, then call make, make test and
12 make install in the usual way:
13 ~/expat-2.7.1/build$ cmake ..
14 -- The C compiler identification is GNU
15 -- The CXX compiler identification is GNU
16 ....
17 -- Configuring done
18 -- Generating done
19 -- Build files have been written to: /home/patrick/expat-2.7.1/build
20
21 If you want to specify the install location for your files, append
22 -DCMAKE_INSTALL_PREFIX=/your/install/path to the cmake call.
23
24 ~/expat-2.7.1/build$ make && make test && make install
25 Scanning dependencies of target expat
26 [ 5%] Building C object CMakeFiles/expat.dir/lib/xmlparse.c.o
27 [ 11%] Building C object CMakeFiles/expat.dir/lib/xmlrole.c.o
28 ....
29 -- Installing: /usr/local/lib/pkgconfig/expat.pc
30 -- Installing: /usr/local/bin/xmlwf
31 -- Installing: /usr/local/share/man/man1/xmlwf.1
32
33 For Windows builds, you must make sure to call cmake from an environment where
34 your compiler is reachable, that means either you call it from the
35 Visual Studio Command Prompt or when using mingw, you must open a cmd.exe and
36 make sure that gcc can be called. On Windows, you also might want to specify a
37 special Generator for CMake:
38 for Visual Studio builds do:
39 cmake .. -G "Visual Studio 16 2019" && msbuild /m expat.sln
40 for mingw builds do:
41 cmake .. -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX=D:\expat-install
42 && gmake && gmake install
43
README.md
1 [](https://github.com/libexpat/libexpat/actions/workflows/linux.yml)
2 [](https://ci.appveyor.com/project/libexpat/libexpat)
3 [](https://repology.org/metapackage/expat/versions)
4 [](https://sourceforge.net/projects/expat/files/)
5 [](https://github.com/libexpat/libexpat/releases)
6 [](https://www.bestpractices.dev/projects/10205)
7
8 > [!CAUTION]
9 >
10 > Expat is **understaffed** and without funding.
11 > There is a [call for help with details](https://github.com/libexpat/libexpat/blob/master/expat/Changes)
12 > at the top of the `Changes` file.
13
14
15 # Expat, Release 2.7.1
16
17 This is Expat, a C99 library for parsing
18 [XML 1.0 Fourth Edition](https://www.w3.org/TR/2006/REC-xml-20060816/), started by
19 [James Clark](https://en.wikipedia.org/wiki/James_Clark_%28programmer%29) in 1997.
20 Expat is a stream-oriented XML parser. This means that you register
21 handlers with the parser before starting the parse. These handlers
22 are called when the parser discovers the associated structures in the
23 document being parsed. A start tag is an example of the kind of
24 structures for which you may register handlers.
25
26 Expat supports the following C99 compilers:
27
28 - GNU GCC >=4.5 (for use from C) or GNU GCC >=4.8.1 (for use from C++)
29 - LLVM Clang >=3.5
30 - Microsoft Visual Studio >=16.0/2019 (rolling `${today} minus 5 years`)
31
32 Windows users can use the
33 [`expat-win32bin-*.*.*.{exe,zip}` download](https://github.com/libexpat/libexpat/releases),
34 which includes both pre-compiled libraries and executables, and source code for
35 developers.
36
37 Expat is [free software](https://www.gnu.org/philosophy/free-sw.en.html).
38 You may copy, distribute, and modify it under the terms of the License
39 contained in the file
40 [`COPYING`](https://github.com/libexpat/libexpat/blob/master/expat/COPYING)
41 distributed with this package.
42 This license is the same as the MIT/X Consortium license.
43
44
45 ## Using libexpat in your CMake-Based Project
46
47 There are three documented ways of using libexpat with CMake:
48
49 ### a) `find_package` with Module Mode
50
51 This approach leverages CMake's own [module `FindEXPAT`](https://cmake.org/cmake/help/latest/module/FindEXPAT.html).
52
53 Notice the *uppercase* `EXPAT` in the following example:
54
55 ```cmake
56 cmake_minimum_required(VERSION 3.10)
57
58 project(hello VERSION 1.0.0)
59
60 find_package(EXPAT 2.2.8 MODULE REQUIRED)
61
62 add_executable(hello
63 hello.c
64 )
65
66 target_link_libraries(hello PUBLIC EXPAT::EXPAT)
67 ```
68
69 ### b) `find_package` with Config Mode
70
71 This approach requires files from
72
73 - libexpat >=2.2.8 where packaging uses the CMake build system
74 or
75 - libexpat >=2.3.0 where packaging uses the GNU Autotools build system
76 on Linux
77 or
78 - libexpat >=2.4.0 where packaging uses the GNU Autotools build system
79 on macOS or MinGW.
80
81 Notice the *lowercase* `expat` in the following example:
82
83 ```cmake
84 cmake_minimum_required(VERSION 3.10)
85
86 project(hello VERSION 1.0.0)
87
88 find_package(expat 2.2.8 CONFIG REQUIRED char dtd ns)
89
90 add_executable(hello
91 hello.c
92 )
93
94 target_link_libraries(hello PUBLIC expat::expat)
95 ```
96
97 ### c) The `FetchContent` module
98
99 This approach as demonstrated below requires CMake >=3.18 for both the
100 [`FetchContent` module](https://cmake.org/cmake/help/latest/module/FetchContent.html)
101 and its support for the `SOURCE_SUBDIR` option to be available.
102
103 Please note that:
104 - Use of the `FetchContent` module with *non-release* SHA1s or `master`
105 of libexpat is neither advised nor considered officially supported.
106 - Pinning to a specific commit is great for robust CI.
107 - Pinning to a specific commit needs updating every time there is a new
108 release of libexpat either manually or through automation ,
109 to not miss out on libexpat security updates.
110
111 For an example that pulls in libexpat via Git:
112
113 ```cmake
114 cmake_minimum_required(VERSION 3.18)
115
116 include(FetchContent)
117
118 project(hello VERSION 1.0.0)
119
120 FetchContent_Declare(
121 expat
122 GIT_REPOSITORY https://github.com/libexpat/libexpat/
123 GIT_TAG 000000000_GIT_COMMIT_SHA1_HERE_000000000 # i.e. Git tag R_0_Y_Z
124 SOURCE_SUBDIR expat/
125 )
126
127 FetchContent_MakeAvailable(expat)
128
129 add_executable(hello
130 hello.c
131 )
132
133 target_link_libraries(hello PUBLIC expat)
134 ```
135
136
137 ## Building from a Git Clone
138
139 If you are building Expat from a check-out from the
140 [Git repository](https://github.com/libexpat/libexpat/),
141 you need to run a script that generates the configure script using the
142 GNU autoconf and libtool tools. To do this, you need to have
143 autoconf 2.58 or newer. Run the script like this:
144
145 ```console
146 ./buildconf.sh
147 ```
148
149 Once this has been done, follow the same instructions as for building
150 from a source distribution.
151
152
153 ## Building from a Source Distribution
154
155 ### a) Building with the configure script (i.e. GNU Autotools)
156
157 To build Expat from a source distribution, you first run the
158 configuration shell script in the top level distribution directory:
159
160 ```console
161 ./configure
162 ```
163
164 There are many options which you may provide to configure (which you
165 can discover by running configure with the `--help` option). But the
166 one of most interest is the one that sets the installation directory.
167 By default, the configure script will set things up to install
168 libexpat into `/usr/local/lib`, `expat.h` into `/usr/local/include`, and
169 `xmlwf` into `/usr/local/bin`. If, for example, you'd prefer to install
170 into `/home/me/mystuff/lib`, `/home/me/mystuff/include`, and
171 `/home/me/mystuff/bin`, you can tell `configure` about that with:
172
173 ```console
174 ./configure --prefix=/home/me/mystuff
175 ```
176
177 Another interesting option is to enable 64-bit integer support for
178 line and column numbers and the over-all byte index:
179
180 ```console
181 ./configure CPPFLAGS=-DXML_LARGE_SIZE
182 ```
183
184 However, such a modification would be a breaking change to the ABI
185 and is therefore not recommended for general use — e.g. as part of
186 a Linux distribution — but rather for builds with special requirements.
187
188 After running the configure script, the `make` command will build
189 things and `make install` will install things into their proper
190 location. Have a look at the `Makefile` to learn about additional
191 `make` options. Note that you need to have write permission into
192 the directories into which things will be installed.
193
194 If you are interested in building Expat to provide document
195 information in UTF-16 encoding rather than the default UTF-8, follow
196 these instructions (after having run `make distclean`).
197 Please note that we configure with `--without-xmlwf` as xmlwf does not
198 support this mode of compilation (yet):
199
200 1. Mass-patch `Makefile.am` files to use `libexpatw.la` for a library name:
201 <br/>
202 `find . -name Makefile.am -exec sed
203 -e 's,libexpat\.la,libexpatw.la,'
204 -e 's,libexpat_la,libexpatw_la,'
205 -i.bak {} +`
206
207 1. Run `automake` to re-write `Makefile.in` files:<br/>
208 `automake`
209
210 1. For UTF-16 output as unsigned short (and version/error strings as char),
211 run:<br/>
212 `./configure CPPFLAGS=-DXML_UNICODE --without-xmlwf`<br/>
213 For UTF-16 output as `wchar_t` (incl. version/error strings), run:<br/>
214 `./configure CFLAGS="-g -O2 -fshort-wchar" CPPFLAGS=-DXML_UNICODE_WCHAR_T
215 --without-xmlwf`
216 <br/>Note: The latter requires libc compiled with `-fshort-wchar`, as well.
217
218 1. Run `make` (which excludes xmlwf).
219
220 1. Run `make install` (again, excludes xmlwf).
221
222 Using `DESTDIR` is supported. It works as follows:
223
224 ```console
225 make install DESTDIR=/path/to/image
226 ```
227
228 overrides the in-makefile set `DESTDIR`, because variable-setting priority is
229
230 1. commandline
231 1. in-makefile
232 1. environment
233
234 Note: This only applies to the Expat library itself, building UTF-16 versions
235 of xmlwf and the tests is currently not supported.
236
237 When using Expat with a project using autoconf for configuration, you
238 can use the probing macro in `conftools/expat.m4` to determine how to
239 include Expat. See the comments at the top of that file for more
240 information.
241
242 A reference manual is available in the file `doc/reference.html` in this
243 distribution.
244
245
246 ### b) Building with CMake
247
248 The CMake build system is still *experimental* and may replace the primary
249 build system based on GNU Autotools at some point when it is ready.
250
251
252 #### Available Options
253
254 For an idea of the available (non-advanced) options for building with CMake:
255
256 ```console
257 # rm -f CMakeCache.txt ; cmake -D_EXPAT_HELP=ON -LH . | grep -B1 ':.*=' | sed 's,^--$,,'
258 // Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
259 CMAKE_BUILD_TYPE:STRING=
260
261 // Install path prefix, prepended onto install directories.
262 CMAKE_INSTALL_PREFIX:PATH=/usr/local
263
264 // Path to a program.
265 DOCBOOK_TO_MAN:FILEPATH=/usr/bin/docbook2x-man
266
267 // Build man page for xmlwf
268 EXPAT_BUILD_DOCS:BOOL=ON
269
270 // Build the examples for expat library
271 EXPAT_BUILD_EXAMPLES:BOOL=ON
272
273 // Build fuzzers for the expat library
274 EXPAT_BUILD_FUZZERS:BOOL=OFF
275
276 // Build pkg-config file
277 EXPAT_BUILD_PKGCONFIG:BOOL=ON
278
279 // Build the tests for expat library
280 EXPAT_BUILD_TESTS:BOOL=ON
281
282 // Build the xmlwf tool for expat library
283 EXPAT_BUILD_TOOLS:BOOL=ON
284
285 // Character type to use (char|ushort|wchar_t) [default=char]
286 EXPAT_CHAR_TYPE:STRING=char
287
288 // Install expat files in cmake install target
289 EXPAT_ENABLE_INSTALL:BOOL=ON
290
291 // Use /MT flag (static CRT) when compiling in MSVC
292 EXPAT_MSVC_STATIC_CRT:BOOL=OFF
293
294 // Build fuzzers via OSS-Fuzz for the expat library
295 EXPAT_OSSFUZZ_BUILD:BOOL=OFF
296
297 // Build a shared expat library
298 EXPAT_SHARED_LIBS:BOOL=ON
299
300 // Treat all compiler warnings as errors
301 EXPAT_WARNINGS_AS_ERRORS:BOOL=OFF
302
303 // Make use of getrandom function (ON|OFF|AUTO) [default=AUTO]
304 EXPAT_WITH_GETRANDOM:STRING=AUTO
305
306 // Utilize libbsd (for arc4random_buf)
307 EXPAT_WITH_LIBBSD:BOOL=OFF
308
309 // Make use of syscall SYS_getrandom (ON|OFF|AUTO) [default=AUTO]
310 EXPAT_WITH_SYS_GETRANDOM:STRING=AUTO
311 ```
312