Implemented fixes allowing libwebsockets to be built under Windows using MinGM/MSYS
[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 4. Finally you can build using the generated Makefile:
90
91     ```bash
92         $ make
93     ```
94
95 Quirk of cmake
96 --------------
97
98 When changing cmake options, for some reason the only way to get it to see the
99 changes sometimes is delete the contents of your build directory and do the
100 cmake from scratch.
101
102 Building on Windows (Visual Studio)
103 -----------------------------------
104 1. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
105
106 2. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
107
108    (**NOTE**: Preferably in the default location to make it easier for CMake to find them)
109
110    **NOTE2**: 
111    Be sure that OPENSSL_CONF environment variable is defined and points at 
112    <OpenSSL install location>\bin\openssl.cfg
113          
114 3. Generate the Visual studio project by opening the Visual Studio cmd prompt:
115
116    ```bash
117    cd <path to src>
118    md build
119    cd build
120    cmake -G "Visual Studio 10" ..
121    ```
122
123    (**NOTE**: There is also a cmake-gui available on Windows if you prefer that)
124    
125    **NOTE2**:
126    See this link to find out the version number corresponding to your Visual Studio edition:
127    http://superuser.com/a/194065
128
129 4. Now you should have a generated Visual Studio Solution in  your
130    `<path to src>/build` directory, which can be used to build.
131
132 Building on Windows (MinGW)
133 ---------------------------
134 1. Install MinGW: http://sourceforge.net/projects/mingw/files
135
136    (**NOTE**: Preferably in the default location C:\MinGW)
137
138 2. Fix up MinGW headers
139
140    a) Add the following lines to C:\MinGW\include\winsock2.h:
141    
142 #if(_WIN32_WINNT >= 0x0600)
143
144 typedef struct pollfd {
145
146     SOCKET  fd;
147     SHORT   events;
148     SHORT   revents;
149
150 } WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
151
152 WINSOCK_API_LINKAGE int WSAAPI WSAPoll(LPWSAPOLLFD fdArray, ULONG fds, INT timeout);
153
154 #endif // (_WIN32_WINNT >= 0x0600)
155
156    b) Create C:\MinGW\include\mstcpip.h and copy and paste the content from following link into it:
157     
158    http://wine-unstable.sourcearchive.com/documentation/1.1.32/mstcpip_8h-source.html
159
160 3. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
161
162 4. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
163
164    (**NOTE**: Preferably in the default location to make it easier for CMake to find them)
165
166    **NOTE2**: 
167    Be sure that OPENSSL_CONF environment variable is defined and points at 
168    <OpenSSL install location>\bin\openssl.cfg
169
170 5. Generate the build files (default is Make files) using MSYS shell:
171
172    ```bash
173    $ cd /drive/path/to/src
174    $ mkdir build
175    $ cd build
176    $ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=C:/MinGW ..
177    ```
178
179    (**NOTE**: The `build/`` directory can have any name and be located anywhere
180     on your filesystem, and that the argument `..` given to cmake is simply
181     the source directory of **libwebsockets** containing the [CMakeLists.txt](CMakeLists.txt)
182     project file. All examples in this file assumes you use "..")
183
184    **NOTE2**:
185    To generate build files allowing to create libwebsockets binaries with debug information
186    set the CMAKE_BUILD_TYPE flag to DEBUG:
187
188    ```bash
189    $ cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=C:/MinGW -DCMAKE_BUILD_TYPE=DEBUG ..
190    ```
191
192 6. Finally you can build using the generated Makefile and get the results deployed into your MinGW installation:
193
194    ```bash
195    $ make
196    $ make install
197    ```
198
199 Setting compile options
200 -----------------------
201
202 To set compile time flags you can either use one of the CMake gui applications
203 or do it via command line.
204
205 Command line
206 ------------
207 To list avaialable options (ommit the H if you don't want the help text):
208
209         cmake -LH ..
210
211 Then to set an option and build (for example turn off SSL support):
212
213         cmake -DLWS_WITH_SSL=0 ..
214 or
215         cmake -DLWS_WITH_SSL:BOOL=OFF ..
216
217 Unix GUI
218 --------
219 If you have a curses-enabled build you simply type:
220 (not all packages include this, my debian install does not for example).
221
222         ccmake
223
224 Windows GUI
225 -----------
226 On windows CMake comes with a gui application:
227         Start -> Programs -> CMake -> CMake (cmake-gui)
228
229 wolfSSL/CyaSSL replacement for OpenSSL
230 --------------------------------------
231 wolfSSL/CyaSSL is a lightweight SSL library targeted at embedded systems:
232 https://www.wolfssl.com/wolfSSL/Products-wolfssl.html
233
234 It contains a OpenSSL compatibility layer which makes it possible to pretty
235 much link to it instead of OpenSSL, giving a much smaller footprint.
236
237 **NOTE**: wolfssl needs to be compiled using the `--enable-opensslextra` flag for
238 this to work.
239
240 Compiling libwebsockets with wolfSSL
241 ------------------------------------
242
243 ```bash
244 cmake .. -DLWS_USE_WOLFSSL=1 \
245          -DLWS_WOLFSSL_INCLUDE_DIRS=/path/to/wolfssl \
246          -DLWS_WOLFSSL_LIBRARIES=/path/to/wolfssl/wolfssl.a ..
247 ```
248
249 **NOTE**: On windows use the .lib file extension for `LWS_WOLFSSL_LIBRARIES` instead.
250
251 Compiling libwebsockets with CyaSSL
252 -----------------------------------
253
254 ```bash
255 cmake .. -DLWS_USE_CYASSL=1 \
256          -DLWS_CYASSL_INCLUDE_DIRS=/path/to/cyassl \
257          -DLWS_CYASSL_LIBRARIES=/path/to/wolfssl/cyassl.a ..
258 ```
259
260 **NOTE**: On windows use the .lib file extension for `LWS_CYASSL_LIBRARIES` instead.
261
262 Reproducing HTTP2.0 tests
263 -------------------------
264
265 You must have built and be running lws against a version of openssl that has
266 ALPN / NPN.  Most distros still have older versions.  You'll know it's right by
267 seeing
268
269 ```bash
270 lwsts[4752]:  Compiled with OpenSSL support
271 lwsts[4752]:  Using SSL mode
272 lwsts[4752]:  HTTP2 / ALPN enabled
273 ```
274
275 at lws startup.
276
277 For non-SSL HTTP2.0 upgrade
278
279 ```bash
280 $ nghttp -nvasu http://localhost:7681/test.htm
281 ```
282
283 For SSL / ALPN HTTP2.0 upgrade
284
285 ```
286 $ nghttp -nvas https://localhost:7681/test.html
287 ```
288
289 Cross compiling
290 ---------------
291 To enable cross-compiling **libwebsockets** using CMake you need to create
292 a "Toolchain file" that you supply to CMake when generating your build files.
293 CMake will then use the cross compilers and build paths specified in this file
294 to look for dependencies and such.
295
296 **Libwebsockets** includes an example toolchain file [cross-arm-linux-gnueabihf.cmake](cross-arm-linux-gnueabihf.cmake)
297 you can use as a starting point.
298
299 The commandline to configure for cross with this would look like
300
301 ```bash
302 $ cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr \
303          -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake \
304          -DWITHOUT_EXTENSIONS=1 -DWITH_SSL=0
305 ```
306
307 The example shows how to build with no external cross lib dependencies, you
308 need to provide the cross libraries otherwise.
309
310 **NOTE**: start from an EMPTY build directory if you had a non-cross build in there
311         before the settings will be cached and your changes ignored.
312
313 Additional information on cross compilation with CMake:
314         http://www.vtk.org/Wiki/CMake_Cross_Compiling
315
316 Memory efficiency
317 -----------------
318
319 Embedded server-only configuration without extensions (ie, no compression
320 on websocket connections), but with full v13 websocket features and http
321 server, built on ARM Cortex-A9:
322
323 Update at 8dac94d (2013-02-18)
324
325 ```bash
326 $ ./configure --without-client --without-extensions --disable-debug --without-daemonize
327
328 Context Creation, 1024 fd limit[2]:   16720 (includes 12 bytes per fd)
329 Per-connection [3]:                      72 bytes, +1328 during headers
330
331 .text   .rodata .data   .bss
332 11512   2784    288     4
333 ```
334
335 This shows the impact of the major configuration with/without options at
336 13ba5bbc633ea962d46d using Ubuntu ARM on a PandaBoard ES.
337
338 These are accounting for static allocations from the library elf, there are
339 additional dynamic allocations via malloc.  These are a bit old now but give
340 the right idea for relative "expense" of features.
341
342 Static allocations, ARM9
343
344 |                                | .text   | .rodata | .data | .bss |
345 |--------------------------------|---------|---------|-------|------|
346 | All (no without)               | 35024   | 9940    | 336   | 4104 |
347 | without client                 | 25684   | 7144    | 336   | 4104 |
348 | without client, exts           | 21652   | 6288    | 288   | 4104 |
349 | without client, exts, debug[1] | 19756   | 3768    | 288   | 4104 |
350 | without server                 | 30304   | 8160    | 336   | 4104 |
351 | without server, exts           | 25382   | 7204    | 288   | 4104 |
352 | without server, exts, debug[1] | 23712   | 4256    | 288   | 4104 |
353
354 [1] `--disable-debug` only removes messages below `lwsl_notice`.  Since that is
355 the default logging level the impact is not noticeable, error, warn and notice
356 logs are all still there.
357
358 [2] `1024` fd per process is the default limit (set by ulimit) in at least Fedora
359 and Ubuntu.  You can make significant savings tailoring this to actual expected
360 peak fds, ie, at a limit of `20`, context creation allocation reduces to `4432 +
361 240 = 4672`)
362
363 [3] known header content is freed after connection establishment