Joakim Soderberg | 7df9908 | 2013-02-07 20:24:19 +0800 | [diff] [blame^] | 1 | Introduction |
| 2 | ------------ |
| 3 | Libwebsockets can be built using two different build systems |
| 4 | autoconf or CMake. autoconf only works on Unix systems, or mingw/cygwin |
| 5 | on Windows. CMake works differently and can generate platform specific |
| 6 | project files for most popular IDEs and build systems. |
| 7 | |
| 8 | ################################### Autoconf ################################### |
| 9 | |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 10 | Building the library and test apps |
| 11 | ---------------------------------- |
| 12 | |
| 13 | You need to regenerate the autotools and libtoolize stuff for your system |
| 14 | |
| 15 | $ ./autogen.sh |
| 16 | |
Andy Green | a2156aa | 2013-02-02 18:10:29 +0800 | [diff] [blame] | 17 | |
| 18 | ------Fedora x86_64 |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 19 | |
| 20 | ./configure --prefix=/usr --libdir=/usr/lib64 --enable-openssl |
| 21 | |
Andy Green | a2156aa | 2013-02-02 18:10:29 +0800 | [diff] [blame] | 22 | ------Apple |
| 23 | |
| 24 | Christopher Baker reported that this is needed |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 25 | (and I was told separately enabling openssl makes trouble somehow) |
| 26 | |
| 27 | ./configure CC="gcc -arch i386 -arch x86_64" CXX="g++ -arch i386 -arch |
| 28 | x86_64" CPP="gcc -E" CXXCPP="g++ -E" --enable-nofork |
| 29 | |
Andy Green | a2156aa | 2013-02-02 18:10:29 +0800 | [diff] [blame] | 30 | ------mingw |
| 31 | |
| 32 | I did the following to get working build, ping test is disabled when |
| 33 | building this way |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 34 | |
| 35 | 1) install mingw64_w32 compiler packages from Fedora |
| 36 | 2) additionally install mingw64-zlib package |
| 37 | 3) ./configure --prefix=/usr --enable-mingw --host=x86_64-w64-mingw32 |
| 38 | 4) make |
| 39 | |
Andy Green | a2156aa | 2013-02-02 18:10:29 +0800 | [diff] [blame] | 40 | ------MIPS cross-build using OpenWRT |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 41 | |
Andy Green | a2156aa | 2013-02-02 18:10:29 +0800 | [diff] [blame] | 42 | ./configure --prefix=/usr --without-extensions --host mips-openwrt-linux |
| 43 | |
| 44 | I did not try building the extensions since they need cross-zlib, but it |
| 45 | should also be workable. |
| 46 | |
| 47 | ------Other uClibc |
| 48 | |
| 49 | you may need --enable-builtin-getifaddrs if your toolchain |
| 50 | doesn't have it - openWRT uclibc has it so you don't need this option. |
| 51 | |
| 52 | ------ARM cross-build |
Andy Green | 5c81e80 | 2013-01-20 20:14:42 +0800 | [diff] [blame] | 53 | |
| 54 | ./configure --prefix=/usr --host=arm-linux-gnueabi --without-client --without-extensions |
| 55 | |
| 56 | you can build cross with client and extensions perfectly well, but |
| 57 | apart from the size shrink this has the nice characteristic that no |
| 58 | non-toolchain libraries are needed to build it. |
| 59 | |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 60 | |
| 61 | otherwise if /usr/local/... and /usr/local/lib are OK then... |
| 62 | |
| 63 | $ ./configure |
| 64 | $ make clean |
| 65 | $ make && sudo make install |
| 66 | $ libwebsockets-test-server |
| 67 | |
| 68 | should be enough to get a test server listening on port 7861. |
| 69 | |
| 70 | |
| 71 | Configure script options |
| 72 | ------------------------ |
| 73 | |
| 74 | There are several other possible configure options |
| 75 | |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 76 | --enable-libcrypto by default libwebsockets uses its own |
| 77 | built-in md5 and sha-1 implementation for |
| 78 | simplicity. However the libcrypto ones |
| 79 | may be faster, and in a distro context it |
| 80 | may be highly desirable to use a common |
| 81 | library implementation for ease of security |
| 82 | upgrades. Give this configure option |
| 83 | to disable the built-in ones and force use |
| 84 | of the libcrypto (part of openssl) ones. |
| 85 | |
| 86 | --with-client-cert-dir=dir tells the client ssl support where to |
| 87 | look for trust certificates to validate |
| 88 | the remote certificate against. |
| 89 | |
| 90 | --enable-noping Don't try to build the ping test app |
| 91 | It needs some unixy environment that |
| 92 | may choke in other build contexts, this |
| 93 | lets you cleanly stop it being built |
| 94 | |
| 95 | --enable-builtin-getifaddrs if your libc lacks getifaddrs, you can build an |
| 96 | implementation into the library. By default your libc |
| 97 | one is used. |
| 98 | |
| 99 | --without-testapps Just build the library not the test apps |
| 100 | |
| 101 | --without-client Don't build the client part of the library nor the |
| 102 | test apps that need the client part. Useful to |
| 103 | minimize library footprint for embedded server-only |
| 104 | case |
| 105 | |
| 106 | --without-server Don't build the server part of the library nor the |
| 107 | test apps that need the server part. Useful to |
| 108 | minimize library footprint for embedded client-only |
| 109 | case |
| 110 | |
| 111 | --without-daemonize Don't build daemonize.c / lws_daemonize |
| 112 | |
| 113 | --disable-debug Remove all debug logging below lwsl_notice in severity |
| 114 | from the code -- it's not just defeated from logging |
| 115 | but removed from compilation |
| 116 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 117 | --without-extensions Remove all code and data around protocol extensions. |
| 118 | This reduces the code footprint considerably but |
| 119 | you will lose extension features like compression. |
| 120 | However that may be irrelevant for embedded use and |
| 121 | the code / data size / speed improvements may be |
| 122 | critical. |
| 123 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 124 | --with-latency Builds the latency-tracking code into the library... |
| 125 | this slows your library down a bit but is very useful |
| 126 | to find the cause of unexpected latencies occurring |
| 127 | inside the library. See README.test-apps for more |
| 128 | info |
| 129 | |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 130 | |
| 131 | Externally configurable important constants |
| 132 | ------------------------------------------- |
| 133 | |
| 134 | You can control these from configure by just setting them as commandline |
| 135 | args throgh CFLAGS, eg |
| 136 | |
| 137 | ./configure CFLAGS="-DLWS_MAX_ZLIB_CONN_BUFFER=8192" |
| 138 | |
| 139 | |
| 140 | They all have reasonable defaults usable for all use-cases except resource- |
| 141 | constrained, so you only need to take care about them if you want to tune them |
| 142 | to the amount of memory available. |
| 143 | |
| 144 | - LWS_MAX_HEADER_NAME_LENGTH default 64: max characters in an HTTP header |
| 145 | name that libwebsockets can cope with |
| 146 | |
| 147 | - LWS_MAX_HEADER_LEN default 4096: largest HTTP header value string length |
| 148 | libwebsockets can cope with |
| 149 | |
| 150 | - LWS_INITIAL_HDR_ALLOC default 256: amount of memory to allocate initially, |
| 151 | tradeoff between taking too much and needless realloc |
| 152 | |
| 153 | - LWS_ADDITIONAL_HDR_ALLOC default 64: how much to additionally realloc if |
| 154 | the header value string keeps coming |
| 155 | |
| 156 | - MAX_USER_RX_BUFFER default 4096: max amount of user rx data to buffer at a |
| 157 | time and pass to user callback LWS_CALLBACK_RECEIVE or |
| 158 | LWS_CALLBACK_CLIENT_RECEIVE. Large frames are passed to the user callback |
| 159 | in chunks of this size. Tradeoff between per-connection static memory |
| 160 | allocation and if you expect to deal with large frames, how much you can |
| 161 | see at once which can affect efficiency. |
| 162 | |
Andy Green | 6c1f64e | 2013-01-20 11:28:06 +0800 | [diff] [blame] | 163 | - LWS_MAX_PROTOCOLS default 10: largest amount of different protocols the |
| 164 | server can serve |
| 165 | |
| 166 | - LWS_MAX_EXTENSIONS_ACTIVE default 10: largest amount of extensions we can |
| 167 | choose to have active on one connection |
| 168 | |
| 169 | - SPEC_LATEST_SUPPORTED default 13: only change if you want to remove support |
| 170 | for later protocol versions... unlikely |
| 171 | |
| 172 | - AWAITING_TIMEOUT default 5: after this many seconds without a response, the |
| 173 | server will hang up on the client |
| 174 | |
| 175 | - CIPHERS_LIST_STRING default "DEFAULT": SSL Cipher selection. It's advisable |
| 176 | to tweak the ciphers allowed to be negotiated on secure connections for |
| 177 | performance reasons, otherwise a slow algorithm may be selected by the two |
| 178 | endpoints and the server could expend most of its time just encrypting and |
| 179 | decrypting data, severely limiting the amount of messages it will be able to |
| 180 | handle per second. For example:: |
| 181 | |
| 182 | "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL" |
| 183 | |
| 184 | - SYSTEM_RANDOM_FILEPATH default "/dev/urandom": if your random device differs |
| 185 | you can set it here |
| 186 | |
| 187 | - LWS_MAX_ZLIB_CONN_BUFFER maximum size a compression buffer is allowed to |
| 188 | grow to before closing the connection. Some limit is needed or any connecton |
| 189 | can exhaust all server memory by sending it 4G buffers full of zeros which the |
| 190 | server is expect to expand atomically. Default is 64KBytes. |
| 191 | |
| 192 | - LWS_SOMAXCONN maximum number of pending connect requests the listening |
| 193 | socket can cope with. Default is SOMAXCONN. If you need to use synthetic |
| 194 | tests that just spam hundreds of connect requests at once without dropping |
| 195 | any, you can try messing with these as well as ulimit (see later) |
| 196 | (courtesy Edwin van der Oetelaar) |
| 197 | |
| 198 | echo "2048 64512" > /proc/sys/net/ipv4/ip_local_port_range |
| 199 | echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle |
| 200 | echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse |
| 201 | echo "10" > /proc/sys/net/ipv4/tcp_fin_timeout |
| 202 | echo "65536" > /proc/sys/net/core/somaxconn |
| 203 | echo "65536" > /proc/sys/net/ipv4/tcp_max_syn_backlog |
| 204 | echo "262144" > /proc/sys/net/netfilter/nf_conntrack_max |
| 205 | |
Andy Green | 5c81e80 | 2013-01-20 20:14:42 +0800 | [diff] [blame] | 206 | |
| 207 | Memory efficiency |
| 208 | ----------------- |
| 209 | |
Andy Green | ab40eaa | 2013-01-21 13:20:33 +0800 | [diff] [blame] | 210 | Update at 35f332bb46464feb87eb |
| 211 | |
| 212 | Embedded server-only configuration without extensions (ie, no compression |
| 213 | on websocket connections), but with full v13 websocket features and http |
| 214 | server, built on ARM Cortex-A9: |
| 215 | |
| 216 | ./configure --without-client --without-extensions --disable-debug --enable-nofork --without-daemonize |
| 217 | |
| 218 | .text .rodata .data .bss |
| 219 | 11476 2664 288 4 |
| 220 | |
| 221 | Context Creation, 1024 fd limit[2]: 12288 (12 bytes per fd) |
| 222 | Per-connection [3]: 4400 bytes |
| 223 | |
| 224 | |
Andy Green | 5c81e80 | 2013-01-20 20:14:42 +0800 | [diff] [blame] | 225 | This shows the impact of the major configuration with/without options at |
| 226 | 13ba5bbc633ea962d46d using Ubuntu ARM on a PandaBoard ES. |
| 227 | |
| 228 | These are accounting for static allocations from the library elf, there are |
| 229 | additional dynamic allocations via malloc |
| 230 | |
| 231 | Static allocations, ARM9 |
| 232 | .text .rodata .data .bss |
| 233 | All (no without) 35024 9940 336 4104 |
| 234 | without client 25684 7144 336 4104 |
| 235 | without client, exts 21652 6288 288 4104 |
| 236 | without client, exts, debug[1] 19756 3768 288 4104 |
| 237 | without server 30304 8160 336 4104 |
| 238 | without server, exts 25382 7204 288 4104 |
| 239 | without server, exts, debug[1] 23712 4256 288 4104 |
| 240 | |
| 241 | Dynamic allocations: ARM9 (32 bit) |
| 242 | |
| 243 | Context Creation, 1024 fd limit[2] in ulimit: 12288 (12 bytes per fd) |
| 244 | Per-connection (excluding headers[3]): 8740 |
| 245 | |
| 246 | Dynamic allocations: x86_64 (64 bit) |
| 247 | |
| 248 | Context Creation, 1024 fd limit[2] in ulimit: 16384 (16 bytes per fd) |
| 249 | Per-connection (excluding headers[3]): 9224 |
| 250 | |
| 251 | [1] --disable-debug only removes messages below lwsl_notice. Since that is |
| 252 | the default logging level the impact is not noticable, error, warn and notice |
| 253 | logs are all still there. |
| 254 | |
| 255 | [2] 1024 fd per process is the default limit (set by ulimit) in at least Fedora |
| 256 | and Ubuntu. |
| 257 | |
| 258 | [3] known headers are retained via additional mallocs for the lifetime of the |
| 259 | connection |
Joakim Soderberg | 7df9908 | 2013-02-07 20:24:19 +0800 | [diff] [blame^] | 260 | |
| 261 | |
| 262 | #################################### CMake #################################### |
| 263 | |
| 264 | CMake is a multi-platform build tool that can generate build files for many |
| 265 | different target platforms. See more info at http://www.cmake.org |
| 266 | |
| 267 | CMake also allows/recommends you to do "out of source"-builds, that is, |
| 268 | the build files are separated from your sources, so there is no need to |
| 269 | create elaborate clean scripts to get a clean source tree, instead you |
| 270 | simply remove your build directory. |
| 271 | |
| 272 | Libwebsockets has been tested to build successfully on the following platforms |
| 273 | with SSL support (both OpenSSL/CyaSSL): |
| 274 | |
| 275 | - Windows |
| 276 | - Linux (x86 and ARM) |
| 277 | - OSX |
| 278 | - NetBSD |
| 279 | |
| 280 | Building the library and test apps |
| 281 | ---------------------------------- |
| 282 | |
| 283 | The project settings used by CMake to generate the platform specific build |
| 284 | files is called CMakeLists.txt. CMake then uses one of its "Generators" to |
| 285 | output a Visual Studio project or Make file for instance. To see a list of |
| 286 | the available generators for your platform, simply run the "cmake" command. |
| 287 | |
| 288 | Note that by default OpenSSL will be linked, if you don't want SSL support |
| 289 | see below on how to toggle compile options. |
| 290 | |
| 291 | Building on Unix: |
| 292 | ----------------- |
| 293 | |
| 294 | 1. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html |
| 295 | (Most Unix distributions comes with a packaged version also) |
| 296 | |
| 297 | 2. Install OpenSSL. |
| 298 | |
| 299 | 3. Generate the build files (default is Make files): |
| 300 | |
| 301 | cd /path/to/src |
| 302 | mkdir build |
| 303 | cd build |
| 304 | cmake .. |
| 305 | |
| 306 | (NOTE: The build/ directory can have any name and be located anywhere |
| 307 | on your filesystem, and that the argument ".." given to cmake is simply |
| 308 | the source directory of libwebsockets containing the CMakeLists.txt project |
| 309 | file. All examples in this file assumes you use "..") |
| 310 | |
| 311 | 4. Finally you can build using the generated Makefile: |
| 312 | |
| 313 | make |
| 314 | |
| 315 | Building on Windows (Visual Studio) |
| 316 | ----------------------------------- |
| 317 | 1. Install CMake 2.6 or greater: http://cmake.org/cmake/resources/software.html |
| 318 | |
| 319 | 2. Install OpenSSL binaries. http://www.openssl.org/related/binaries.html |
| 320 | (Preferably in the default location to make it easier for CMake to find them) |
| 321 | |
| 322 | 3. Generate the Visual studio project by opening the Visual Studio cmd prompt: |
| 323 | |
| 324 | cd <path to src> |
| 325 | md build |
| 326 | cd build |
| 327 | cmake -G "Visual Studio 10" .. |
| 328 | |
| 329 | (NOTE: There is also a cmake-gui available on Windows if you prefer that) |
| 330 | |
| 331 | 4. Now you should have a generated Visual Studio Solution in your |
| 332 | <path to src>/build directory, which can be used to build. |
| 333 | |
| 334 | Setting compile options |
| 335 | ----------------------- |
| 336 | |
| 337 | To set compile time flags you can either use one of the CMake gui applications |
| 338 | or do it via command line. |
| 339 | |
| 340 | Command line |
| 341 | ------------ |
| 342 | To list avaialable options (ommit the H if you don't want the help text): |
| 343 | |
| 344 | cmake -LH .. |
| 345 | |
| 346 | Then to set an option and build (for example turn off SSL support): |
| 347 | |
| 348 | cmake -DWITH_SSL=0 .. |
| 349 | or |
| 350 | cmake -DWITH_SSL:BOOL=OFF .. |
| 351 | |
| 352 | Unix GUI |
| 353 | -------- |
| 354 | If you have a curses enabled build you simply type: |
| 355 | (not all packages include this, my debian install does not for example). |
| 356 | |
| 357 | ccmake |
| 358 | |
| 359 | Windows GUI |
| 360 | ----------- |
| 361 | On windows CMake comes with a gui application: |
| 362 | Start -> Programs -> CMake -> CMake (cmake-gui) |
| 363 | |
| 364 | CyaSSL replacement for OpenSSL |
| 365 | ------------------------------ |
| 366 | CyaSSL is a lightweight SSL library targeted at embedded system: |
| 367 | http://www.yassl.com/yaSSL/Products-cyassl.html |
| 368 | |
| 369 | It contains a OpenSSL compatability layer which makes it possible to pretty |
| 370 | much link to it instead of OpenSSL, giving a much smaller footprint. |
| 371 | |
| 372 | NOTE: At the time of writing this the current release of CyaSSL contains a |
| 373 | crash bug due to some APIs libwebsocket uses. To be able to use this you will |
| 374 | need to use the current HEAD in their official repository: |
| 375 | https://github.com/cyassl/cyassl |
| 376 | |
| 377 | NOTE: cyassl needs to be compiled using the --enable-opensslExtra flag for |
| 378 | this to work. |
| 379 | |
| 380 | Compiling libwebsockets with CyaSSL |
| 381 | ----------------------------------- |
| 382 | |
| 383 | cmake -DUSE_CYASSL=1 |
| 384 | -DCYASSL_INCLUDE_DIRS=/path/to/cyassl |
| 385 | -DCYASSL_LIB=/path/to/cyassl/cyassl.a .. |
| 386 | |
| 387 | NOTE: On windows use the .lib file extension for CYASSL_LIB instead. |
| 388 | |
| 389 | Cross compiling |
| 390 | --------------- |
| 391 | To enable cross compiling libwebsockets using CMake you need to create |
| 392 | a "Toolchain file" that you supply to CMake when generating your build files. |
| 393 | CMake will then use the cross compilers and build paths specified in this file |
| 394 | to look for dependencies and such. |
| 395 | |
| 396 | Below is an example of how one of these files might look like: |
| 397 | |
| 398 | # |
| 399 | # CMake Toolchain file for crosscompiling on ARM. |
| 400 | # |
| 401 | # This can be used when running cmake in the following way: |
| 402 | # cd build/ |
| 403 | # cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/this/file/TC_arm-linux-gcc.cmake |
| 404 | # |
| 405 | |
| 406 | set(CROSS_PATH /path/to/cross_environment/uClibc) |
| 407 | |
| 408 | # Target operating system name. |
| 409 | set(CMAKE_SYSTEM_NAME Linux) |
| 410 | |
| 411 | # Name of C compiler. |
| 412 | set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-uclibc-gcc") |
| 413 | set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-uclibc-g++") |
| 414 | |
| 415 | # Where to look for the target environment. (More paths can be added here) |
| 416 | set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}") |
| 417 | |
| 418 | # Adjust the default behavior of the FIND_XXX() commands: |
| 419 | # search programs in the host environment only. |
| 420 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) |
| 421 | |
| 422 | # Search headers and libraries in the target environment only. |
| 423 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) |
| 424 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) |
| 425 | |
| 426 | Additional information on cross compilation with CMake: |
| 427 | http://www.vtk.org/Wiki/CMake_Cross_Compiling |