Allow using CyaSSL in stead of wolfSSL
[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
16 - Linux (x86 and ARM)
17 - OSX
18 - NetBSD
19
20 Building the library and test apps
21 ----------------------------------
22
23 The project settings used by CMake to generate the platform specific build
24 files is called [CMakeLists.txt](CMakeLists.txt). CMake then uses one of its "Generators" to
25 output a Visual Studio project or Make file for instance. To see a list of
26 the available generators for your platform, simply run the "cmake" command.
27
28 Note that by default OpenSSL will be linked, if you don't want SSL support
29 see below on how to toggle compile options.
30
31 Building on Unix:
32 -----------------
33
34 1. Install CMake 2.8 or greater: http://cmake.org/cmake/resources/software.html
35    (Most Unix distributions comes with a packaged version also)
36
37 2. Install OpenSSL.
38
39 3. Generate the build files (default is Make files):
40
41     ```bash
42         $ cd /path/to/src
43         $ mkdir build
44         $ cd build
45         $ cmake ..
46     ```
47
48         (**NOTE**: The `build/`` directory can have any name and be located anywhere
49          on your filesystem, and that the argument `..` given to cmake is simply
50          the source directory of **libwebsockets** containing the [CMakeLists.txt](CMakeLists.txt)
51          project file. All examples in this file assumes you use "..")
52
53         **NOTE2**:
54         A common option you may want to give is to set the install path, same
55         as --prefix= with autotools.  It defaults to /usr/local.
56         You can do this by, eg
57
58     ```bash
59         $ cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr ..
60     ```
61
62         **NOTE3**:
63         On machines that want libraries in lib64, you can also add the
64         following to the cmake line
65
66     ```bash
67         -DLIB_SUFFIX=64
68     ```
69
70         **NOTE4**:
71         If you are building against a non-distro OpenSSL (eg, in order to get
72         access to ALPN support only in newer OpenSSL versions) the nice way to
73         express that in one cmake command is eg,
74
75     ```bash
76         $ cmake .. -DOPENSSL_ROOT_DIR=/usr/local/ssl \
77                  -DCMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE=/usr/local/ssl \
78                  -DLWS_WITH_HTTP2=1
79     ```
80
81         When you run the test apps using non-distro SSL, you have to force them
82         to use your libs, not the distro ones
83
84     ```bash
85         $ LD_LIBRARY_PATH=/usr/local/ssl/lib libwebsockets-test-server --ssl
86     ```
87
88 4. Finally you can build using the generated Makefile:
89
90     ```bash
91         $ make
92     ```
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
103 Building on Windows (Visual Studio)
104 -----------------------------------
105 1. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html
106
107 2. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html
108    (Preferably in the default location to make it easier for CMake to find them)
109
110 3. Generate the Visual studio project by opening the Visual Studio cmd prompt:
111
112    ```bash
113    cd <path to src>
114    md build
115    cd build
116    cmake -G "Visual Studio 10" ..
117    ```
118
119    (**NOTE**: There is also a cmake-gui available on Windows if you prefer that)
120
121 4. Now you should have a generated Visual Studio Solution in  your
122    `<path to src>/build` directory, which can be used to build.
123
124 Setting compile options
125 -----------------------
126
127 To set compile time flags you can either use one of the CMake gui applications
128 or do it via command line.
129
130 Command line
131 ------------
132 To list avaialable options (ommit the H if you don't want the help text):
133
134         cmake -LH ..
135
136 Then to set an option and build (for example turn off SSL support):
137
138         cmake -DLWS_WITH_SSL=0 ..
139 or
140         cmake -DLWS_WITH_SSL:BOOL=OFF ..
141
142 Unix GUI
143 --------
144 If you have a curses enabled build you simply type:
145 (not all packages include this, my debian install does not for example).
146
147         ccmake
148
149 Windows GUI
150 -----------
151 On windows CMake comes with a gui application:
152         Start -> Programs -> CMake -> CMake (cmake-gui)
153
154 wolfSSL/CyaSSL replacement for OpenSSL
155 --------------------------------------
156 wolfSSL/CyaSSL is a lightweight SSL library targeted at embedded systems:
157 https://www.wolfssl.com/wolfSSL/Products-wolfssl.html
158
159 It contains a OpenSSL compatability layer which makes it possible to pretty
160 much link to it instead of OpenSSL, giving a much smaller footprint.
161
162 **NOTE**: wolfssl needs to be compiled using the `--enable-opensslextra` flag for
163 this to work.
164
165 Compiling libwebsockets with wolfSSL
166 ------------------------------------
167
168 ```bash
169 cmake .. -DLWS_USE_WOLFSSL=1 \
170          -DLWS_WOLFSSL_INCLUDE_DIRS=/path/to/wolfssl \
171          -DLWS_WOLFSSL_LIBRARIES=/path/to/wolfssl/wolfssl.a ..
172 ```
173
174 **NOTE**: On windows use the .lib file extension for `LWS_WOLFSSL_LIBRARIES` instead.
175
176 Compiling libwebsockets with CyaSSL
177 -----------------------------------
178
179 ```bash
180 cmake .. -DLWS_USE_CYASSL=1 \
181          -DLWS_CYASSL_INCLUDE_DIRS=/path/to/cyassl \
182          -DLWS_CYASSL_LIBRARIES=/path/to/wolfssl/cyassl.a ..
183 ```
184
185 **NOTE**: On windows use the .lib file extension for `LWS_CYASSL_LIBRARIES` instead.
186
187
188 Reproducing HTTP2.0 tests
189 -------------------------
190
191 You must have built and be running lws against a version of openssl that has
192 ALPN / NPN.  Most distros still have older versions.  You'll know it's right by
193 seeing
194
195 ```bash
196 lwsts[4752]:  Compiled with OpenSSL support
197 lwsts[4752]:  Using SSL mode
198 lwsts[4752]:  HTTP2 / ALPN enabled
199 ```
200
201 at lws startup.
202
203 For non-SSL HTTP2.0 upgrade
204
205 ```bash
206 $ nghttp -nvasu http://localhost:7681/test.htm
207 ```
208
209 For SSL / ALPN HTTP2.0 upgrade
210
211 ```
212 $ nghttp -nvas https://localhost:7681/test.html
213 ```
214
215 Cross compiling
216 ---------------
217 To enable cross compiling **libwebsockets** using CMake you need to create
218 a "Toolchain file" that you supply to CMake when generating your build files.
219 CMake will then use the cross compilers and build paths specified in this file
220 to look for dependencies and such.
221
222 **Libwebsockets** includes an example toolchain file [cross-arm-linux-gnueabihf.cmake](cross-arm-linux-gnueabihf.cmake)
223 you can use as a starting point.
224
225 The commandline to configure for cross with this would look like
226
227 ```bash
228 $ cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/usr \
229          -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake \
230          -DWITHOUT_EXTENSIONS=1 -DWITH_SSL=0
231 ```
232
233 The example shows how to build with no external cross lib dependencies, you
234 need to provide the cross libraries otherwise.
235
236 **NOTE**: start from an EMPTY build directory if you had a non-cross build in there
237         before the settings will be cached and your changes ignored.
238
239 Additional information on cross compilation with CMake:
240         http://www.vtk.org/Wiki/CMake_Cross_Compiling
241
242
243 Memory efficiency
244 -----------------
245
246 Embedded server-only configuration without extensions (ie, no compression
247 on websocket connections), but with full v13 websocket features and http
248 server, built on ARM Cortex-A9:
249
250 Update at 8dac94d (2013-02-18)
251
252 ```bash
253 $ ./configure --without-client --without-extensions --disable-debug --without-daemonize
254
255 Context Creation, 1024 fd limit[2]:   16720 (includes 12 bytes per fd)
256 Per-connection [3]:                      72 bytes, +1328 during headers
257
258 .text   .rodata .data   .bss
259 11512   2784    288     4
260 ```
261
262 This shows the impact of the major configuration with/without options at
263 13ba5bbc633ea962d46d using Ubuntu ARM on a PandaBoard ES.
264
265 These are accounting for static allocations from the library elf, there are
266 additional dynamic allocations via malloc.  These are a bit old now but give
267 the right idea for relative "expense" of features.
268
269 Static allocations, ARM9
270
271 |                                | .text   | .rodata | .data | .bss |
272 |--------------------------------|---------|---------|-------|------|
273 | All (no without)               | 35024   | 9940    | 336   | 4104 |
274 | without client                 | 25684   | 7144    | 336   | 4104 |
275 | without client, exts           | 21652   | 6288    | 288   | 4104 |
276 | without client, exts, debug[1] | 19756   | 3768    | 288   | 4104 |
277 | without server                 | 30304   | 8160    | 336   | 4104 |
278 | without server, exts           | 25382   | 7204    | 288   | 4104 |
279 | without server, exts, debug[1] | 23712   | 4256    | 288   | 4104 |
280
281 [1] `--disable-debug` only removes messages below `lwsl_notice`.  Since that is
282 the default logging level the impact is not noticeable, error, warn and notice
283 logs are all still there.
284
285 [2] `1024` fd per process is the default limit (set by ulimit) in at least Fedora
286 and Ubuntu.  You can make significant savings tailoring this to actual expected
287 peak fds, ie, at a limit of `20`, context creation allocation reduces to `4432 +
288 240 = 4672`)
289
290 [3] known header content is freed after connection establishment