client: getaddrinfo refactor
[platform/upstream/libwebsockets.git] / README.build.md
1 Notes about building lws
2 ========================
3
4
5 @section cm Introduction to CMake
6
7 CMake is a multi-platform build tool that can generate build files for many
8 different target platforms. See more info at http://www.cmake.org
9
10 CMake also allows/recommends you to do "out of source"-builds, that is,
11 the build files are separated from your sources, so there is no need to
12 create elaborate clean scripts to get a clean source tree, instead you
13 simply remove your build directory.
14
15 Libwebsockets has been tested to build successfully on the following platforms
16 with SSL support (for OpenSSL/wolfSSL/BoringSSL):
17
18 - Windows (Visual Studio)
19 - Windows (MinGW)
20 - Linux (x86 and ARM)
21 - OSX
22 - NetBSD
23
24
25 @section build1 Building the library and test apps
26
27 The project settings used by CMake to generate the platform specific build
28 files is called [CMakeLists.txt](CMakeLists.txt). CMake then uses one of its "Generators" to
29 output a Visual Studio project or Make file for instance. To see a list of
30 the available generators for your platform, simply run the "cmake" command.
31
32 Note that by default OpenSSL will be linked, if you don't want SSL support
33 see below on how to toggle compile options.
34
35
36 @section bu Building on Unix:
37
38 1. Install CMake 2.8 or greater: http://cmake.org/cmake/resources/software.html
39    (Most Unix distributions comes with a packaged version also)
40
41 2. Install OpenSSL.
42
43 3. Generate the build files (default is Make files):
44 ```
45         $ cd /path/to/src
46         $ mkdir build
47         $ cd build
48         $ cmake ..
49 ```
50
51 4. Finally you can build using the generated Makefile:
52 ```
53         $ make && sudo make install
54 ```
55 **NOTE**: The `build/`` directory can have any name and be located anywhere
56  on your filesystem, and that the argument `..` given to cmake is simply
57  the source directory of **libwebsockets** containing the [CMakeLists.txt](CMakeLists.txt)
58  project file. All examples in this file assumes you use ".."
59
60 **NOTE2**:
61 A common option you may want to give is to set the install path, same
62 as --prefix= with autotools.  It defaults to /usr/local.
63 You can do this by, eg
64 ```
65         $ cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr .
66 ```
67
68 **NOTE3**:
69 On machines that want libraries in lib64, you can also add the
70 following to the cmake line
71 ```
72         -DLIB_SUFFIX=64
73 ```
74
75 **NOTE4**:
76 If you are building against a non-distro OpenSSL (eg, in order to get
77 access to ALPN support only in newer OpenSSL versions) the nice way to
78 express that in one cmake command is eg,
79 ```
80         $ cmake .. -DOPENSSL_ROOT_DIR=/usr/local/ssl \
81                  -DCMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE=/usr/local/ssl \
82                  -DLWS_WITH_HTTP2=1
83 ```
84
85 When you run the test apps using non-distro SSL, you have to force them
86 to use your libs, not the distro ones
87 ```
88         $ LD_LIBRARY_PATH=/usr/local/ssl/lib libwebsockets-test-server --ssl
89 ```
90
91 To get it to build on latest openssl (2016-04-10) it needed this approach
92 ```
93         cmake .. -DLWS_WITH_HTTP2=1 -DLWS_OPENSSL_INCLUDE_DIRS=/usr/local/include/openssl -DLWS_OPENSSL_LIBRARIES="/usr/local/lib64/libssl.so;/usr/local/lib64/libcrypto.so"
94 ```
95
96 Mac users have reported
97
98 ```
99  $ export OPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2k; cmake ..; make -j4
100 ```
101
102 worked for them when using "homebrew" OpenSSL
103
104 **NOTE5**:
105 To build with debug info and _DEBUG for lower priority debug messages
106 compiled in, use
107 ```
108         $ cmake .. -DCMAKE_BUILD_TYPE=DEBUG
109 ```
110
111 **NOTE6**
112 To build on Solaris the linker needs to be informed to use lib socket
113 and libnsl, and only builds in 64bit mode.
114
115 ```bash
116         $ cmake .. -DCMAKE_C_FLAGS=-m64 -DCMAKE_EXE_LINKER_FLAGS="-lsocket -lnsl"
117 ```
118
119 4. Finally you can build using the generated Makefile:
120
121 ```bash
122         $ make
123  ```
124
125 @section lcap Linux Capabilities
126
127 On Linux, lws now lets you retain selected root capabilities when dropping
128 privileges.  If libcap-dev or similar package is installed providing
129 sys/capabilities.h, and libcap or similar package is installed providing
130 libcap.so, CMake will enable the capability features.
131
132 The context creation info struct .caps[] and .count_caps members can then
133 be set by user code to enable selected root capabilities to survive the
134 transition to running under an unprivileged user.
135
136 @section cmq Quirk of cmake
137
138 When changing cmake options, for some reason the only way to get it to see the
139 changes sometimes is delete the contents of your build directory and do the
140 cmake from scratch.
141
142 deleting build/CMakeCache.txt may be enough.
143
144
145 @section cmw Building on Windows (Visual Studio)
146
147 1. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
148
149 2. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
150
151    (**NOTE**: Preferably in the default location to make it easier for CMake to find them)
152
153    **NOTE2**: 
154    Be sure that OPENSSL_CONF environment variable is defined and points at 
155    <OpenSSL install location>\bin\openssl.cfg
156          
157 3. Generate the Visual studio project by opening the Visual Studio cmd prompt:
158
159 ```
160         cd <path to src>
161         md build
162         cd build
163         cmake -G "Visual Studio 10" ..
164 ```
165
166    (**NOTE**: There is also a cmake-gui available on Windows if you prefer that)
167    
168    **NOTE2**:
169    See this link to find out the version number corresponding to your Visual Studio edition:
170    http://superuser.com/a/194065
171
172 4. Now you should have a generated Visual Studio Solution in  your
173    `<path to src>/build` directory, which can be used to build.
174
175 5. Some additional deps may be needed
176
177  - iphlpapi.lib
178  - psapi.lib
179  - userenv.lib
180
181 6. If you're using libuv, you must make sure to compile libuv with the same multithread-dll / Mtd attributes as libwebsockets itself
182
183
184 @section cmwmgw Building on Windows (MinGW)
185
186 1. Install MinGW: http://sourceforge.net/projects/mingw/files
187
188    (**NOTE**: Preferably in the default location C:\MinGW)
189
190 2. Fix up MinGW headers
191
192    a) If still necessary, sdd the following lines to C:\MinGW\include\winsock2.h:
193 ```
194         #if(_WIN32_WINNT >= 0x0600)
195
196         typedef struct pollfd {
197
198                 SOCKET  fd;
199                 SHORT   events;
200                 SHORT   revents;
201
202         } WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
203
204         WINSOCK_API_LINKAGE int WSAAPI WSAPoll(LPWSAPOLLFD fdArray, ULONG fds, INT timeout);
205
206         #endif // (_WIN32_WINNT >= 0x0600)
207 ```
208
209        Update crtdefs.h line 47 to say:
210
211 ```
212         typedef __int64 ssize_t;
213 ```
214
215    b) Create C:\MinGW\include\mstcpip.h and copy and paste the content from following link into it:
216
217    https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-headers/include/mstcpip.h
218
219 3. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
220
221 4. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
222
223    (**NOTE**: Preferably in the default location to make it easier for CMake to find them)
224
225    **NOTE2**: 
226    Be sure that OPENSSL_CONF environment variable is defined and points at 
227    <OpenSSL install location>\bin\openssl.cfg
228
229 5. Generate the build files (default is Make files) using MSYS shell:
230 ```
231         $ cd /drive/path/to/src
232         $ mkdir build
233         $ cd build
234         $ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=C:/MinGW ..
235 ```
236    (**NOTE**: The `build/`` directory can have any name and be located anywhere
237     on your filesystem, and that the argument `..` given to cmake is simply
238     the source directory of **libwebsockets** containing the [CMakeLists.txt](CMakeLists.txt)
239     project file. All examples in this file assumes you use "..")
240
241    **NOTE2**:
242    To generate build files allowing to create libwebsockets binaries with debug information
243    set the CMAKE_BUILD_TYPE flag to DEBUG:
244 ```
245         $ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=C:/MinGW -DCMAKE_BUILD_TYPE=DEBUG ..
246 ```
247 6. Finally you can build using the generated Makefile and get the results deployed into your MinGW installation:
248
249 ```
250         $ make
251         $ make install
252 ```
253
254 @section optee Building for OP-TEE
255
256 OP-TEE is a "Secure World" Trusted Execution Environment.
257
258 Although lws is only part of the necessary picture to have an https-enabled
259 TA, it does support OP-TEE as a platform and if you provide the other
260 pieces, does work very well.
261
262 Select it in cmake with `-DLWS_PLAT_OPTEE=1`
263
264
265 @section cmco Setting compile options
266
267 To set compile time flags you can either use one of the CMake gui applications
268 or do it via the command line.
269
270 @subsection cmcocl Command line
271
272 To list available options (omit the H if you don't want the help text):
273
274         cmake -LH ..
275
276 Then to set an option and build (for example turn off SSL support):
277
278         cmake -DLWS_WITH_SSL=0 ..
279 or
280         cmake -DLWS_WITH_SSL:BOOL=OFF ..
281
282 @subsection cmcoug Unix GUI
283
284 If you have a curses-enabled build you simply type:
285 (not all packages include this, my debian install does not for example).
286
287         ccmake
288
289 @subsection cmcowg Windows GUI
290
291 On windows CMake comes with a gui application:
292         Start -> Programs -> CMake -> CMake (cmake-gui)
293
294
295 @section wolf wolfSSL/CyaSSL replacement for OpenSSL
296
297 wolfSSL/CyaSSL is a lightweight SSL library targeted at embedded systems:
298 https://www.wolfssl.com/wolfSSL/Products-wolfssl.html
299
300 It contains a OpenSSL compatibility layer which makes it possible to pretty
301 much link to it instead of OpenSSL, giving a much smaller footprint.
302
303 **NOTE**: wolfssl needs to be compiled using the `--enable-opensslextra` flag for
304 this to work.
305
306 @section wolf1 Compiling libwebsockets with wolfSSL
307
308 ```
309         cmake .. -DLWS_USE_WOLFSSL=1 \
310                  -DLWS_WOLFSSL_INCLUDE_DIRS=/path/to/wolfssl \
311                  -DLWS_WOLFSSL_LIBRARIES=/path/to/wolfssl/wolfssl.a ..
312 ```
313
314 **NOTE**: On windows use the .lib file extension for `LWS_WOLFSSL_LIBRARIES` instead.
315
316 @section cya Compiling libwebsockets with CyaSSL
317
318 ```
319         cmake .. -DLWS_USE_CYASSL=1 \
320                  -DLWS_CYASSL_INCLUDE_DIRS=/path/to/cyassl \
321                  -DLWS_CYASSL_LIBRARIES=/path/to/wolfssl/cyassl.a ..
322 ```
323
324 **NOTE**: On windows use the .lib file extension for `LWS_CYASSL_LIBRARIES` instead.
325
326 @section esp32 Building for ESP32
327
328 Step 1, get ESP-IDF with lws integrated as a component
329
330 ```
331     $ git clone --int --recursive https://github.com/lws-team/lws-esp-idf
332 ```
333
334 Step 2: Get Application including the test plugins
335
336 ```
337     $ git clone https://github.com/lws-team/lws-esp32
338 ```
339
340 Set your IDF_PATH to point to the esp-idf you downloaded in 1)
341
342 There's docs for how to build the lws-esp32 test app and reproduce it in the README.md here
343
344 https://github.com/lws-team/lws-esp32/blob/master/README.md
345
346
347 @section extplugins Building plugins outside of lws itself
348
349 The directory ./plugin-standalone/ shows how easy it is to create plugins
350 outside of lws itself.  First build lws itself with -DLWS_WITH_PLUGINS,
351 then use the same flow to build the standalone plugin
352 ```
353         cd ./plugin-standalone
354         mkdir build
355         cd build
356         cmake ..
357         make && sudo make install
358 ```
359
360 if you changed the default plugin directory when you built lws, you must
361 also give the same arguments to cmake here (eg,
362 ` -DCMAKE_INSTALL_PREFIX:PATH=/usr/something/else...` )
363
364 Otherwise if you run lwsws or libwebsockets-test-server-v2.0, it will now
365 find the additional plugin "libprotocol_example_standalone.so"
366 ```
367         lwsts[21257]:   Plugins:
368         lwsts[21257]:    libprotocol_dumb_increment.so
369         lwsts[21257]:    libprotocol_example_standalone.so
370         lwsts[21257]:    libprotocol_lws_mirror.so
371         lwsts[21257]:    libprotocol_lws_server_status.so
372         lwsts[21257]:    libprotocol_lws_status.so
373 ```
374 If you have multiple vhosts, you must enable plugins at the vhost
375 additionally, discovered plugins are not enabled automatically for security
376 reasons.  You do this using info->pvo or for lwsws, in the JSON config.
377
378
379 @section http2rp Reproducing HTTP2.0 tests
380
381 You must have built and be running lws against a version of openssl that has
382 ALPN / NPN.  Most distros still have older versions.  You'll know it's right by
383 seeing
384 ```
385         lwsts[4752]:  Compiled with OpenSSL support
386         lwsts[4752]:  Using SSL mode
387         lwsts[4752]:  HTTP2 / ALPN enabled
388 ```
389 at lws startup.
390
391 For non-SSL HTTP2.0 upgrade
392 ```
393         $ nghttp -nvasu http://localhost:7681/test.htm
394 ```
395 For SSL / ALPN HTTP2.0 upgrade
396 ```
397         $ nghttp -nvas https://localhost:7681/test.html
398 ```
399
400 @section cross Cross compiling
401
402 To enable cross-compiling **libwebsockets** using CMake you need to create
403 a "Toolchain file" that you supply to CMake when generating your build files.
404 CMake will then use the cross compilers and build paths specified in this file
405 to look for dependencies and such.
406
407 **Libwebsockets** includes an example toolchain file [cross-arm-linux-gnueabihf.cmake](cross-arm-linux-gnueabihf.cmake)
408 you can use as a starting point.
409
410 The commandline to configure for cross with this would look like
411 ```
412         $ cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr \
413                  -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake \
414                  -DLWS_WITHOUT_EXTENSIONS=1 -DLWS_WITH_SSL=0
415 ```
416 The example shows how to build with no external cross lib dependencies, you
417 need to provide the cross libraries otherwise.
418
419 **NOTE**: start from an EMPTY build directory if you had a non-cross build in there
420         before the settings will be cached and your changes ignored.
421
422 Additional information on cross compilation with CMake:
423         http://www.vtk.org/Wiki/CMake_Cross_Compiling
424
425 @section mem Memory efficiency
426
427 Embedded server-only configuration without extensions (ie, no compression
428 on websocket connections), but with full v13 websocket features and http
429 server, built on ARM Cortex-A9:
430
431 Update at 8dac94d (2013-02-18)
432 ```
433         $ ./configure --without-client --without-extensions --disable-debug --without-daemonize
434
435         Context Creation, 1024 fd limit[2]:   16720 (includes 12 bytes per fd)
436         Per-connection [3]:                      72 bytes, +1328 during headers
437
438         .text   .rodata .data   .bss
439         11512   2784    288     4
440 ```
441 This shows the impact of the major configuration with/without options at
442 13ba5bbc633ea962d46d using Ubuntu ARM on a PandaBoard ES.
443
444 These are accounting for static allocations from the library elf, there are
445 additional dynamic allocations via malloc.  These are a bit old now but give
446 the right idea for relative "expense" of features.
447
448 Static allocations, ARM9
449
450 |                                | .text   | .rodata | .data | .bss |
451 |--------------------------------|---------|---------|-------|------|
452 | All (no without)               | 35024   | 9940    | 336   | 4104 |
453 | without client                 | 25684   | 7144    | 336   | 4104 |
454 | without client, exts           | 21652   | 6288    | 288   | 4104 |
455 | without client, exts, debug[1] | 19756   | 3768    | 288   | 4104 |
456 | without server                 | 30304   | 8160    | 336   | 4104 |
457 | without server, exts           | 25382   | 7204    | 288   | 4104 |
458 | without server, exts, debug[1] | 23712   | 4256    | 288   | 4104 |
459
460 [1] `--disable-debug` only removes messages below `lwsl_notice`.  Since that is
461 the default logging level the impact is not noticeable, error, warn and notice
462 logs are all still there.
463
464 [2] `1024` fd per process is the default limit (set by ulimit) in at least Fedora
465 and Ubuntu.  You can make significant savings tailoring this to actual expected
466 peak fds, ie, at a limit of `20`, context creation allocation reduces to `4432 +
467 240 = 4672`)
468
469 [3] known header content is freed after connection establishment