logs document logrotate configuration for lwsws
[platform/upstream/libwebsockets.git] / README.build.md
1 Introduction to CMake
2 ---------------------
3
4 CMake is a multi-platform build tool that can generate build files for many
5 different target platforms. See more info at http://www.cmake.org
6
7 CMake also allows/recommends you to do "out of source"-builds, that is,
8 the build files are separated from your sources, so there is no need to
9 create elaborate clean scripts to get a clean source tree, instead you
10 simply remove your build directory.
11
12 Libwebsockets has been tested to build successfully on the following platforms
13 with SSL support (both OpenSSL/wolfSSL):
14
15 - Windows (Visual Studio)
16 - Windows (MinGW)
17 - Linux (x86 and ARM)
18 - OSX
19 - NetBSD
20
21 Building the library and test apps
22 ----------------------------------
23
24 The project settings used by CMake to generate the platform specific build
25 files is called [CMakeLists.txt](CMakeLists.txt). CMake then uses one of its "Generators" to
26 output a Visual Studio project or Make file for instance. To see a list of
27 the available generators for your platform, simply run the "cmake" command.
28
29 Note that by default OpenSSL will be linked, if you don't want SSL support
30 see below on how to toggle compile options.
31
32 Building on Unix:
33 -----------------
34
35 1. Install CMake 2.8 or greater: http://cmake.org/cmake/resources/software.html
36    (Most Unix distributions comes with a packaged version also)
37
38 2. Install OpenSSL.
39
40 3. Generate the build files (default is Make files):
41
42     ```bash
43         $ cd /path/to/src
44         $ mkdir build
45         $ cd build
46         $ cmake ..
47     ```
48
49         (**NOTE**: The `build/`` directory can have any name and be located anywhere
50          on your filesystem, and that the argument `..` given to cmake is simply
51          the source directory of **libwebsockets** containing the [CMakeLists.txt](CMakeLists.txt)
52          project file. All examples in this file assumes you use "..")
53
54         **NOTE2**:
55         A common option you may want to give is to set the install path, same
56         as --prefix= with autotools.  It defaults to /usr/local.
57         You can do this by, eg
58
59     ```bash
60         $ cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..
61     ```
62
63         **NOTE3**:
64         On machines that want libraries in lib64, you can also add the
65         following to the cmake line
66
67     ```bash
68         -DLIB_SUFFIX=64
69     ```
70
71         **NOTE4**:
72         If you are building against a non-distro OpenSSL (eg, in order to get
73         access to ALPN support only in newer OpenSSL versions) the nice way to
74         express that in one cmake command is eg,
75
76     ```bash
77         $ cmake .. -DOPENSSL_ROOT_DIR=/usr/local/ssl \
78                  -DCMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE=/usr/local/ssl \
79                  -DLWS_WITH_HTTP2=1
80     ```
81
82         When you run the test apps using non-distro SSL, you have to force them
83         to use your libs, not the distro ones
84
85     ```bash
86         $ LD_LIBRARY_PATH=/usr/local/ssl/lib libwebsockets-test-server --ssl
87     ```
88
89         To get it to build on latest openssl (2016-04-10) it needed this approach
90
91     ```bash
92         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"
93     ```
94
95         **NOTE5**:
96         To build with debug info and _DEBUG for lower priority debug messages
97         compiled in, use
98
99     ```bash
100         $ cmake .. -DCMAKE_BUILD_TYPE=DEBUG
101     ````
102
103 4. Finally you can build using the generated Makefile:
104
105     ```bash
106         $ make
107     ```
108
109 Quirk of cmake
110 --------------
111
112 When changing cmake options, for some reason the only way to get it to see the
113 changes sometimes is delete the contents of your build directory and do the
114 cmake from scratch.
115
116 Building on Windows (Visual Studio)
117 -----------------------------------
118 1. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
119
120 2. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
121
122    (**NOTE**: Preferably in the default location to make it easier for CMake to find them)
123
124    **NOTE2**: 
125    Be sure that OPENSSL_CONF environment variable is defined and points at 
126    <OpenSSL install location>\bin\openssl.cfg
127          
128 3. Generate the Visual studio project by opening the Visual Studio cmd prompt:
129
130    ```bash
131    cd <path to src>
132    md build
133    cd build
134    cmake -G "Visual Studio 10" ..
135    ```
136
137    (**NOTE**: There is also a cmake-gui available on Windows if you prefer that)
138    
139    **NOTE2**:
140    See this link to find out the version number corresponding to your Visual Studio edition:
141    http://superuser.com/a/194065
142
143 4. Now you should have a generated Visual Studio Solution in  your
144    `<path to src>/build` directory, which can be used to build.
145
146 5. Some additional deps may be needed
147
148  - iphlpapi.lib
149  - psapi.lib
150  - userenv.lib
151
152 6. If you're using libuv, you must make sure to compile libuv with the same multithread-dll / Mtd attributes as libwebsockets itself
153
154
155 Building on Windows (MinGW)
156 ---------------------------
157 1. Install MinGW: http://sourceforge.net/projects/mingw/files
158
159    (**NOTE**: Preferably in the default location C:\MinGW)
160
161 2. Fix up MinGW headers
162
163    a) Add the following lines to C:\MinGW\include\winsock2.h:
164    
165    ```c
166    #if(_WIN32_WINNT >= 0x0600)
167
168    typedef struct pollfd {
169
170        SOCKET  fd;
171        SHORT   events;
172        SHORT   revents;
173
174    } WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
175
176    WINSOCK_API_LINKAGE int WSAAPI WSAPoll(LPWSAPOLLFD fdArray, ULONG fds, INT timeout);
177
178    #endif // (_WIN32_WINNT >= 0x0600)
179    ```
180
181    b) Create C:\MinGW\include\mstcpip.h and copy and paste the content from following link into it:
182     
183    http://wine-unstable.sourcearchive.com/documentation/1.1.32/mstcpip_8h-source.html
184
185 3. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
186
187 4. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
188
189    (**NOTE**: Preferably in the default location to make it easier for CMake to find them)
190
191    **NOTE2**: 
192    Be sure that OPENSSL_CONF environment variable is defined and points at 
193    <OpenSSL install location>\bin\openssl.cfg
194
195 5. Generate the build files (default is Make files) using MSYS shell:
196
197    ```bash
198    $ cd /drive/path/to/src
199    $ mkdir build
200    $ cd build
201    $ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=C:/MinGW ..
202    ```
203
204    (**NOTE**: The `build/`` directory can have any name and be located anywhere
205     on your filesystem, and that the argument `..` given to cmake is simply
206     the source directory of **libwebsockets** containing the [CMakeLists.txt](CMakeLists.txt)
207     project file. All examples in this file assumes you use "..")
208
209    **NOTE2**:
210    To generate build files allowing to create libwebsockets binaries with debug information
211    set the CMAKE_BUILD_TYPE flag to DEBUG:
212
213    ```bash
214    $ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=C:/MinGW -DCMAKE_BUILD_TYPE=DEBUG ..
215    ```
216
217 6. Finally you can build using the generated Makefile and get the results deployed into your MinGW installation:
218
219    ```bash
220    $ make
221    $ make install
222    ```
223
224 Setting compile options
225 -----------------------
226
227 To set compile time flags you can either use one of the CMake gui applications
228 or do it via command line.
229
230 Command line
231 ------------
232 To list avaialable options (ommit the H if you don't want the help text):
233
234         cmake -LH ..
235
236 Then to set an option and build (for example turn off SSL support):
237
238         cmake -DLWS_WITH_SSL=0 ..
239 or
240         cmake -DLWS_WITH_SSL:BOOL=OFF ..
241
242 Building on mbed3
243 -----------------
244 MBED3 is a non-posix embedded OS targeted on Cortex M class chips.
245
246 https://www.mbed.com/
247
248 It's quite unlike any other Posixy platform since the OS is linked statically
249 in with lws to form one binary.
250
251 At the minute server-only is supported and due to bugs in mbed3 network support,
252 the port is of alpha quality.  However it can serve the test html, favicon.ico
253 and logo png and may be able to make ws connections.  The binary for that
254 including the OS, test app, lws and all the assets is only 117KB.
255
256 0) Today mbed3 only properly works on FRDM K64F $35 Freescale Dev Board with
257 1MB Flash, 256KB SRAM and Ethernet.
258
259 http://www.freescale.com/products/arm-processors/kinetis-cortex-m/k-series/k6x-ethernet-mcus/freescale-freedom-development-platform-for-kinetis-k64-k63-and-k24-mcus:FRDM-K64F
260
261 1) Get a working mbed3 environment with arm-none-eabi-cs toolchain
262 (available in Fedora, Ubuntu and other distros)
263
264 2) Confirm you can build things using yotta by following the getting started guide here
265
266 https://docs.mbed.com/docs/getting-started-mbed-os/en/latest/
267
268 3)
269
270 git clone https://github.com/warmcat/lws-test-server
271
272 and cd into it
273
274 4) mkdir -p yotta_modules ; cd yotta_modules
275
276 5) git clone https://github.com/warmcat/libwebsockets ; mv libwebsockets websockets ; cd ..
277
278 6) yotta target frdm-k64f-gcc
279
280 7) yotta install
281
282 8) yotta build
283
284
285 Unix GUI
286 --------
287 If you have a curses-enabled build you simply type:
288 (not all packages include this, my debian install does not for example).
289
290         ccmake
291
292 Windows GUI
293 -----------
294 On windows CMake comes with a gui application:
295         Start -> Programs -> CMake -> CMake (cmake-gui)
296
297 wolfSSL/CyaSSL replacement for OpenSSL
298 --------------------------------------
299 wolfSSL/CyaSSL is a lightweight SSL library targeted at embedded systems:
300 https://www.wolfssl.com/wolfSSL/Products-wolfssl.html
301
302 It contains a OpenSSL compatibility layer which makes it possible to pretty
303 much link to it instead of OpenSSL, giving a much smaller footprint.
304
305 **NOTE**: wolfssl needs to be compiled using the `--enable-opensslextra` flag for
306 this to work.
307
308 Compiling libwebsockets with wolfSSL
309 ------------------------------------
310
311 ```bash
312 cmake .. -DLWS_USE_WOLFSSL=1 \
313          -DLWS_WOLFSSL_INCLUDE_DIRS=/path/to/wolfssl \
314          -DLWS_WOLFSSL_LIBRARIES=/path/to/wolfssl/wolfssl.a ..
315 ```
316
317 **NOTE**: On windows use the .lib file extension for `LWS_WOLFSSL_LIBRARIES` instead.
318
319 Compiling libwebsockets with CyaSSL
320 -----------------------------------
321
322 ```bash
323 cmake .. -DLWS_USE_CYASSL=1 \
324          -DLWS_CYASSL_INCLUDE_DIRS=/path/to/cyassl \
325          -DLWS_CYASSL_LIBRARIES=/path/to/wolfssl/cyassl.a ..
326 ```
327
328 **NOTE**: On windows use the .lib file extension for `LWS_CYASSL_LIBRARIES` instead.
329
330 Compiling libwebsockets with PolarSSL
331 -------------------------------------
332
333 Caution... at some point PolarSSL became MbedTLS.  But it did not happen all at once.
334 The name changed first then at mbedTLS 2.0 the apis changed.  So eg in Fedora 22,
335 there is an "mbedtls" package which is actually using polarssl for the include dir
336 and polarssl apis... this should be treated as polarssl then.
337
338 Example config for this case is
339
340 cmake .. -DLWS_USE_POLARSSL=1 -DLWS_POLARSSL_LIBRARIES=/usr/lib64/libmbedtls.so \
341          -DLWS_POLARSSL_INCLUDE_DIRS=/usr/include/polarssl/
342
343 Reproducing HTTP2.0 tests
344 -------------------------
345
346 You must have built and be running lws against a version of openssl that has
347 ALPN / NPN.  Most distros still have older versions.  You'll know it's right by
348 seeing
349
350 ```bash
351 lwsts[4752]:  Compiled with OpenSSL support
352 lwsts[4752]:  Using SSL mode
353 lwsts[4752]:  HTTP2 / ALPN enabled
354 ```
355
356 at lws startup.
357
358 For non-SSL HTTP2.0 upgrade
359
360 ```bash
361 $ nghttp -nvasu http://localhost:7681/test.htm
362 ```
363
364 For SSL / ALPN HTTP2.0 upgrade
365
366 ```
367 $ nghttp -nvas https://localhost:7681/test.html
368 ```
369
370 Cross compiling
371 ---------------
372 To enable cross-compiling **libwebsockets** using CMake you need to create
373 a "Toolchain file" that you supply to CMake when generating your build files.
374 CMake will then use the cross compilers and build paths specified in this file
375 to look for dependencies and such.
376
377 **Libwebsockets** includes an example toolchain file [cross-arm-linux-gnueabihf.cmake](cross-arm-linux-gnueabihf.cmake)
378 you can use as a starting point.
379
380 The commandline to configure for cross with this would look like
381
382 ```bash
383 $ cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr \
384          -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake \
385          -DWITHOUT_EXTENSIONS=1 -DWITH_SSL=0
386 ```
387
388 The example shows how to build with no external cross lib dependencies, you
389 need to provide the cross libraries otherwise.
390
391 **NOTE**: start from an EMPTY build directory if you had a non-cross build in there
392         before the settings will be cached and your changes ignored.
393
394 Additional information on cross compilation with CMake:
395         http://www.vtk.org/Wiki/CMake_Cross_Compiling
396
397 Memory efficiency
398 -----------------
399
400 Embedded server-only configuration without extensions (ie, no compression
401 on websocket connections), but with full v13 websocket features and http
402 server, built on ARM Cortex-A9:
403
404 Update at 8dac94d (2013-02-18)
405
406 ```bash
407 $ ./configure --without-client --without-extensions --disable-debug --without-daemonize
408
409 Context Creation, 1024 fd limit[2]:   16720 (includes 12 bytes per fd)
410 Per-connection [3]:                      72 bytes, +1328 during headers
411
412 .text   .rodata .data   .bss
413 11512   2784    288     4
414 ```
415
416 This shows the impact of the major configuration with/without options at
417 13ba5bbc633ea962d46d using Ubuntu ARM on a PandaBoard ES.
418
419 These are accounting for static allocations from the library elf, there are
420 additional dynamic allocations via malloc.  These are a bit old now but give
421 the right idea for relative "expense" of features.
422
423 Static allocations, ARM9
424
425 |                                | .text   | .rodata | .data | .bss |
426 |--------------------------------|---------|---------|-------|------|
427 | All (no without)               | 35024   | 9940    | 336   | 4104 |
428 | without client                 | 25684   | 7144    | 336   | 4104 |
429 | without client, exts           | 21652   | 6288    | 288   | 4104 |
430 | without client, exts, debug[1] | 19756   | 3768    | 288   | 4104 |
431 | without server                 | 30304   | 8160    | 336   | 4104 |
432 | without server, exts           | 25382   | 7204    | 288   | 4104 |
433 | without server, exts, debug[1] | 23712   | 4256    | 288   | 4104 |
434
435 [1] `--disable-debug` only removes messages below `lwsl_notice`.  Since that is
436 the default logging level the impact is not noticeable, error, warn and notice
437 logs are all still there.
438
439 [2] `1024` fd per process is the default limit (set by ulimit) in at least Fedora
440 and Ubuntu.  You can make significant savings tailoring this to actual expected
441 peak fds, ie, at a limit of `20`, context creation allocation reduces to `4432 +
442 240 = 4672`)
443
444 [3] known header content is freed after connection establishment