\subsection*{Introduction to C\+Make }

C\+Make is a multi-\/platform build tool that can generate build files for many different target platforms. See more info at \href{http://www.cmake.org}{\tt http\+://www.\+cmake.\+org}

C\+Make also allows/recommends you to do \char`\"{}out of source\char`\"{}-\/builds, that is, the build files are separated from your sources, so there is no need to create elaborate clean scripts to get a clean source tree, instead you simply remove your build directory.

Libwebsockets has been tested to build successfully on the following platforms with S\+SL support (both Open\+S\+S\+L/wolf\+S\+SL)\+:


\begin{DoxyItemize}
\item Windows (Visual Studio)
\item Windows (Min\+GW)
\item Linux (x86 and A\+RM)
\item O\+SX
\item Net\+B\+SD
\end{DoxyItemize}

\subsection*{Building the library and test apps }

The project settings used by C\+Make to generate the platform specific build files is called \href{CMakeLists.txt}{\tt C\+Make\+Lists.\+txt}. C\+Make then uses one of its \char`\"{}\+Generators\char`\"{} to output a Visual Studio project or Make file for instance. To see a list of the available generators for your platform, simply run the \char`\"{}cmake\char`\"{} command.

Note that by default Open\+S\+SL will be linked, if you don\textquotesingle{}t want S\+SL support see below on how to toggle compile options.

\subsection*{Building on Unix\+: }


\begin{DoxyEnumerate}
\item Install C\+Make 2.\+8 or greater\+: \href{http://cmake.org/cmake/resources/software.html}{\tt http\+://cmake.\+org/cmake/resources/software.\+html} (Most Unix distributions comes with a packaged version also)
\item Install Open\+S\+SL.
\item Generate the build files (default is Make files)\+: 
\begin{DoxyCode}
1 $ cd /path/to/src
2 $ mkdir build
3 $ cd build
4 $ cmake ..
\end{DoxyCode}

\item Finally you can build using the generated Makefile\+: 
\begin{DoxyCode}
1 $ make && sudo make install
\end{DoxyCode}
 {\bfseries N\+O\+TE}\+: The {\ttfamily build/}{\ttfamily directory can have any name and be located anywhere on your filesystem, and that the argument}..` given to cmake is simply the source directory of {\bfseries libwebsockets} containing the \href{CMakeLists.txt}{\tt C\+Make\+Lists.\+txt} project file. All examples in this file assumes you use \char`\"{}..\char`\"{}
\end{DoxyEnumerate}

{\bfseries N\+O\+T\+E2}\+: A common option you may want to give is to set the install path, same as --prefix= with autotools. It defaults to /usr/local. You can do this by, eg 
\begin{DoxyCode}
1 $ cmake -DCMAKE\_INSTALL\_PREFIX:PATH=/usr .
\end{DoxyCode}


{\bfseries N\+O\+T\+E3}\+: On machines that want libraries in lib64, you can also add the following to the cmake line 
\begin{DoxyCode}
1 -DLIB\_SUFFIX=64
\end{DoxyCode}


{\bfseries N\+O\+T\+E4}\+: If you are building against a non-\/distro Open\+S\+SL (eg, in order to get access to A\+L\+PN support only in newer Open\+S\+SL versions) the nice way to express that in one cmake command is eg, 
\begin{DoxyCode}
1 $ cmake .. -DOPENSSL\_ROOT\_DIR=/usr/local/ssl \(\backslash\)
2          -DCMAKE\_INCLUDE\_DIRECTORIES\_PROJECT\_BEFORE=/usr/local/ssl \(\backslash\)
3          -DLWS\_WITH\_HTTP2=1
\end{DoxyCode}


When you run the test apps using non-\/distro S\+SL, you have to force them to use your libs, not the distro ones 
\begin{DoxyCode}
1 $ LD\_LIBRARY\_PATH=/usr/local/ssl/lib libwebsockets-test-server --ssl
\end{DoxyCode}


To get it to build on latest openssl (2016-\/04-\/10) it needed this approach 
\begin{DoxyCode}
1 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"
\end{DoxyCode}


{\bfseries N\+O\+T\+E5}\+: To build with debug info and \+\_\+\+D\+E\+B\+UG for lower priority debug messages compiled in, use 
\begin{DoxyCode}
1 $ cmake .. -DCMAKE\_BUILD\_TYPE=DEBUG
\end{DoxyCode}


\subsection*{Quirk of cmake }

When changing cmake options, for some reason the only way to get it to see the changes sometimes is delete the contents of your build directory and do the cmake from scratch.

\subsection*{Building on Windows (Visual Studio) }


\begin{DoxyEnumerate}
\item Install C\+Make 2.\+6 or greater\+: \href{http://cmake.org/cmake/resources/software.html}{\tt http\+://cmake.\+org/cmake/resources/software.\+html}
\item Install Open\+S\+SL binaries. \href{http://www.openssl.org/related/binaries.html}{\tt http\+://www.\+openssl.\+org/related/binaries.\+html}

({\bfseries N\+O\+TE}\+: Preferably in the default location to make it easier for C\+Make to find them)

{\bfseries N\+O\+T\+E2}\+: Be sure that O\+P\+E\+N\+S\+S\+L\+\_\+\+C\+O\+NF environment variable is defined and points at $<$\+Open\+S\+S\+L install=\char`\"{}\char`\"{} location$>$=\char`\"{}\char`\"{}$>$.cfg
\item Generate the Visual studio project by opening the Visual Studio cmd prompt\+:
\end{DoxyEnumerate}


\begin{DoxyCode}
1 cd <path to src>
2 md build
3 cd build
4 cmake -G "Visual Studio 10" ..
\end{DoxyCode}


({\bfseries N\+O\+TE}\+: There is also a cmake-\/gui available on Windows if you prefer that)

{\bfseries N\+O\+T\+E2}\+: See this link to find out the version number corresponding to your Visual Studio edition\+: \href{http://superuser.com/a/194065}{\tt http\+://superuser.\+com/a/194065}


\begin{DoxyEnumerate}
\item Now you should have a generated Visual Studio Solution in your {\ttfamily $<$path to src$>$/build} directory, which can be used to build.
\item Some additional deps may be needed
\begin{DoxyItemize}
\item iphlpapi.\+lib
\item psapi.\+lib
\item userenv.\+lib
\end{DoxyItemize}
\item If you\textquotesingle{}re using libuv, you must make sure to compile libuv with the same multithread-\/dll / Mtd attributes as libwebsockets itself
\end{DoxyEnumerate}

\subsection*{Building on Windows (Min\+GW) }


\begin{DoxyEnumerate}
\item Install Min\+GW\+: \href{http://sourceforge.net/projects/mingw/files}{\tt http\+://sourceforge.\+net/projects/mingw/files}

({\bfseries N\+O\+TE}\+: Preferably in the default location C\+:)
\item Fix up Min\+GW headers

a) Add the following lines to C\+:.h\+: 
\begin{DoxyCode}
1 #if(\_WIN32\_WINNT >= 0x0600)
2 
3 typedef struct pollfd \{
4 
5         SOCKET  fd;
6         SHORT   events;
7         SHORT   revents;
8 
9 \} WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
10 
11 WINSOCK\_API\_LINKAGE int WSAAPI WSAPoll(LPWSAPOLLFD fdArray, ULONG fds, INT timeout);
12 
13 #endif // (\_WIN32\_WINNT >= 0x0600)
\end{DoxyCode}
 b) Create C\+:.h and copy and paste the content from following link into it\+:

\href{http://wine-unstable.sourcearchive.com/documentation/1.1.32/mstcpip_8h-source.html}{\tt http\+://wine-\/unstable.\+sourcearchive.\+com/documentation/1.\+1.\+32/mstcpip\+\_\+8h-\/source.\+html}
\item Install C\+Make 2.\+6 or greater\+: \href{http://cmake.org/cmake/resources/software.html}{\tt http\+://cmake.\+org/cmake/resources/software.\+html}
\item Install Open\+S\+SL binaries. \href{http://www.openssl.org/related/binaries.html}{\tt http\+://www.\+openssl.\+org/related/binaries.\+html}

({\bfseries N\+O\+TE}\+: Preferably in the default location to make it easier for C\+Make to find them)

{\bfseries N\+O\+T\+E2}\+: Be sure that O\+P\+E\+N\+S\+S\+L\+\_\+\+C\+O\+NF environment variable is defined and points at $<$\+Open\+S\+S\+L install=\char`\"{}\char`\"{} location$>$=\char`\"{}\char`\"{}$>$.cfg
\item Generate the build files (default is Make files) using M\+S\+YS shell\+: 
\begin{DoxyCode}
1 $ cd /drive/path/to/src
2 $ mkdir build
3 $ cd build
4 $ cmake -G "MSYS Makefiles" -DCMAKE\_INSTALL\_PREFIX=C:/MinGW ..
\end{DoxyCode}
 ({\bfseries N\+O\+TE}\+: The {\ttfamily build/}{\ttfamily directory can have any name and be located anywhere on your filesystem, and that the argument}..` given to cmake is simply the source directory of {\bfseries libwebsockets} containing the \href{CMakeLists.txt}{\tt C\+Make\+Lists.\+txt} project file. All examples in this file assumes you use \char`\"{}..\char`\"{})

{\bfseries N\+O\+T\+E2}\+: To generate build files allowing to create libwebsockets binaries with debug information set the C\+M\+A\+K\+E\+\_\+\+B\+U\+I\+L\+D\+\_\+\+T\+Y\+PE flag to D\+E\+B\+UG\+: 
\begin{DoxyCode}
1 $ cmake -G "MSYS Makefiles" -DCMAKE\_INSTALL\_PREFIX=C:/MinGW -DCMAKE\_BUILD\_TYPE=DEBUG ..
\end{DoxyCode}

\item Finally you can build using the generated Makefile and get the results deployed into your Min\+GW installation\+:
\end{DoxyEnumerate}


\begin{DoxyCode}
1 $ make
2 $ make install
\end{DoxyCode}


\subsection*{Setting compile options }

To set compile time flags you can either use one of the C\+Make gui applications or do it via command line.

\subsection*{Command line }

To list avaialable options (ommit the H if you don\textquotesingle{}t want the help text)\+: \begin{DoxyVerb}    cmake -LH ..
\end{DoxyVerb}


Then to set an option and build (for example turn off S\+SL support)\+: \begin{DoxyVerb}    cmake -DLWS_WITH_SSL=0 ..
\end{DoxyVerb}
 or cmake -\/\+D\+L\+W\+S\+\_\+\+W\+I\+T\+H\+\_\+\+S\+SL\+:B\+O\+OL=O\+FF ..

\subsection*{Building on mbed3 }

M\+B\+E\+D3 is a non-\/posix embedded OS targeted on Cortex M class chips.

\href{https://www.mbed.com/}{\tt https\+://www.\+mbed.\+com/}

It\textquotesingle{}s quite unlike any other Posixy platform since the OS is linked statically in with lws to form one binary.

At the minute server-\/only is supported and due to bugs in mbed3 network support, the port is of alpha quality. However it can serve the test html, favicon.\+ico and logo png and may be able to make ws connections. The binary for that including the OS, test app, lws and all the assets is only 117\+KB.

0) Today mbed3 only properly works on F\+R\+DM K64F \$35 Freescale Dev Board with 1\+MB Flash, 256\+KB S\+R\+AM and Ethernet.

\href{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}{\tt 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\+:\+F\+R\+D\+M-\/\+K64F}

1) Get a working mbed3 environment with arm-\/none-\/eabi-\/cs toolchain (available in Fedora, Ubuntu and other distros)

2) Confirm you can build things using yotta by following the getting started guide here

\href{https://docs.mbed.com/docs/getting-started-mbed-os/en/latest/}{\tt https\+://docs.\+mbed.\+com/docs/getting-\/started-\/mbed-\/os/en/latest/}

3)

git clone \href{https://github.com/warmcat/lws-test-server}{\tt https\+://github.\+com/warmcat/lws-\/test-\/server}

and cd into it

4) mkdir -\/p yotta\+\_\+modules ; cd yotta\+\_\+modules

5) git clone \href{https://github.com/warmcat/libwebsockets}{\tt https\+://github.\+com/warmcat/libwebsockets} ; mv libwebsockets websockets ; cd ..

6) yotta target frdm-\/k64f-\/gcc

7) yotta install

8) yotta build

\subsection*{Unix G\+UI }

If you have a curses-\/enabled build you simply type\+: (not all packages include this, my debian install does not for example). \begin{DoxyVerb}    ccmake
\end{DoxyVerb}


\subsection*{Windows G\+UI }

On windows C\+Make comes with a gui application\+: Start -\/$>$ Programs -\/$>$ C\+Make -\/$>$ C\+Make (cmake-\/gui)

\subsection*{wolf\+S\+S\+L/\+Cya\+S\+SL replacement for Open\+S\+SL }

wolf\+S\+S\+L/\+Cya\+S\+SL is a lightweight S\+SL library targeted at embedded systems\+: \href{https://www.wolfssl.com/wolfSSL/Products-wolfssl.html}{\tt https\+://www.\+wolfssl.\+com/wolf\+S\+S\+L/\+Products-\/wolfssl.\+html}

It contains a Open\+S\+SL compatibility layer which makes it possible to pretty much link to it instead of Open\+S\+SL, giving a much smaller footprint.

{\bfseries N\+O\+TE}\+: wolfssl needs to be compiled using the {\ttfamily -\/-\/enable-\/opensslextra} flag for this to work.

\subsection*{Compiling libwebsockets with wolf\+S\+SL }


\begin{DoxyCode}
1 cmake .. -DLWS\_USE\_WOLFSSL=1 \(\backslash\)
2          -DLWS\_WOLFSSL\_INCLUDE\_DIRS=/path/to/wolfssl \(\backslash\)
3          -DLWS\_WOLFSSL\_LIBRARIES=/path/to/wolfssl/wolfssl.a ..
\end{DoxyCode}


{\bfseries N\+O\+TE}\+: On windows use the .lib file extension for {\ttfamily L\+W\+S\+\_\+\+W\+O\+L\+F\+S\+S\+L\+\_\+\+L\+I\+B\+R\+A\+R\+I\+ES} instead.

\subsection*{Compiling libwebsockets with Cya\+S\+SL }


\begin{DoxyCode}
1 cmake .. -DLWS\_USE\_CYASSL=1 \(\backslash\)
2          -DLWS\_CYASSL\_INCLUDE\_DIRS=/path/to/cyassl \(\backslash\)
3          -DLWS\_CYASSL\_LIBRARIES=/path/to/wolfssl/cyassl.a ..
\end{DoxyCode}


{\bfseries N\+O\+TE}\+: On windows use the .lib file extension for {\ttfamily L\+W\+S\+\_\+\+C\+Y\+A\+S\+S\+L\+\_\+\+L\+I\+B\+R\+A\+R\+I\+ES} instead.

\subsection*{Compiling libwebsockets with Polar\+S\+SL }

Caution... at some point Polar\+S\+SL became Mbed\+T\+LS. But it did not happen all at once. The name changed first then at mbed\+T\+LS 2.\+0 the apis changed. So eg in Fedora 22, there is an \char`\"{}mbedtls\char`\"{} package which is actually using polarssl for the include dir and polarssl apis... this should be treated as polarssl then.

Example config for this case is 
\begin{DoxyCode}
1 cmake .. -DLWS\_USE\_POLARSSL=1 -DLWS\_POLARSSL\_LIBRARIES=/usr/lib64/libmbedtls.so \(\backslash\)
2          -DLWS\_POLARSSL\_INCLUDE\_DIRS=/usr/include/polarssl/
\end{DoxyCode}


\subsection*{Building plugins outside of lws itself }

The directory ./plugin-\/standalone/ shows how easy it is to create plugins outside of lws itself. First build lws itself with -\/\+D\+L\+W\+S\+\_\+\+W\+I\+T\+H\+\_\+\+P\+L\+U\+G\+I\+NS, then use the same flow to build the standalone plugin 
\begin{DoxyCode}
1 cd ./plugin-standalone
2 mkdir build
3 cd build
4 cmake ..
5 make && sudo make install
\end{DoxyCode}


if you changed the default plugin directory when you built lws, you must also give the same arguments to cmake here (eg, {\ttfamily -\/\+D\+C\+M\+A\+K\+E\+\_\+\+I\+N\+S\+T\+A\+L\+L\+\_\+\+P\+R\+E\+F\+IX\+:P\+A\+TH=/usr/something/else...} )

Otherwise if you run lwsws or libwebsockets-\/test-\/server-\/v2.\+0, it will now find the additional plugin \char`\"{}libprotocol\+\_\+example\+\_\+standalone.\+so\char`\"{} 
\begin{DoxyCode}
1 lwsts[21257]:   Plugins:
2 lwsts[21257]:    libprotocol\_dumb\_increment.so
3 lwsts[21257]:    libprotocol\_example\_standalone.so
4 lwsts[21257]:    libprotocol\_lws\_mirror.so
5 lwsts[21257]:    libprotocol\_lws\_server\_status.so
6 lwsts[21257]:    libprotocol\_lws\_status.so
\end{DoxyCode}
 If you have multiple vhosts, you must enable plugins at the vhost additionally, discovered plugins are not enabled automatically for security reasons. You do this using info-\/$>$pvo or for lwsws, in the J\+S\+ON config.

\subsection*{Reproducing H\+T\+T\+P2.\+0 tests }

You must have built and be running lws against a version of openssl that has A\+L\+PN / N\+PN. Most distros still have older versions. You\textquotesingle{}ll know it\textquotesingle{}s right by seeing 
\begin{DoxyCode}
1 lwsts[4752]:  Compiled with OpenSSL support
2 lwsts[4752]:  Using SSL mode
3 lwsts[4752]:  HTTP2 / ALPN enabled
\end{DoxyCode}
 at lws startup.

For non-\/\+S\+SL H\+T\+T\+P2.\+0 upgrade 
\begin{DoxyCode}
1 $ nghttp -nvasu http://localhost:7681/test.htm
\end{DoxyCode}
 For S\+SL / A\+L\+PN H\+T\+T\+P2.\+0 upgrade 
\begin{DoxyCode}
1 $ nghttp -nvas https://localhost:7681/test.html
\end{DoxyCode}


\subsection*{Cross compiling }

To enable cross-\/compiling {\bfseries libwebsockets} using C\+Make you need to create a \char`\"{}\+Toolchain file\char`\"{} that you supply to C\+Make when generating your build files. C\+Make will then use the cross compilers and build paths specified in this file to look for dependencies and such.

{\bfseries Libwebsockets} includes an example toolchain file \href{cross-arm-linux-gnueabihf.cmake}{\tt cross-\/arm-\/linux-\/gnueabihf.\+cmake} you can use as a starting point.

The commandline to configure for cross with this would look like 
\begin{DoxyCode}
1 $ cmake .. -DCMAKE\_INSTALL\_PREFIX:PATH=/usr \(\backslash\)
2          -DCMAKE\_TOOLCHAIN\_FILE=../cross-arm-linux-gnueabihf.cmake \(\backslash\)
3          -DWITHOUT\_EXTENSIONS=1 -DWITH\_SSL=0
\end{DoxyCode}
 The example shows how to build with no external cross lib dependencies, you need to provide the cross libraries otherwise.

{\bfseries N\+O\+TE}\+: start from an E\+M\+P\+TY build directory if you had a non-\/cross build in there before the settings will be cached and your changes ignored.

Additional information on cross compilation with C\+Make\+: \href{http://www.vtk.org/Wiki/CMake_Cross_Compiling}{\tt http\+://www.\+vtk.\+org/\+Wiki/\+C\+Make\+\_\+\+Cross\+\_\+\+Compiling}

\subsection*{Memory efficiency }

Embedded server-\/only configuration without extensions (ie, no compression on websocket connections), but with full v13 websocket features and http server, built on A\+RM Cortex-\/\+A9\+:

Update at 8dac94d (2013-\/02-\/18) 
\begin{DoxyCode}
1 $ ./configure --without-client --without-extensions --disable-debug --without-daemonize
2 
3 Context Creation, 1024 fd limit[2]:   16720 (includes 12 bytes per fd)
4 Per-connection [3]:                      72 bytes, +1328 during headers
5 
6 .text   .rodata .data   .bss
7 11512   2784    288     4
\end{DoxyCode}
 This shows the impact of the major configuration with/without options at 13ba5bbc633ea962d46d using Ubuntu A\+RM on a Panda\+Board ES.

These are accounting for static allocations from the library elf, there are additional dynamic allocations via malloc. These are a bit old now but give the right idea for relative \char`\"{}expense\char`\"{} of features.

Static allocations, A\+R\+M9

\tabulinesep=1mm
\begin{longtabu} spread 0pt [c]{*5{|X[-1]}|}
\hline
\rowcolor{\tableheadbgcolor}{\bf }&{\bf .text }&{\bf .rodata }&{\bf .data }&{\bf .bss  }\\\cline{1-5}
\endfirsthead
\hline
\endfoot
\hline
\rowcolor{\tableheadbgcolor}{\bf }&{\bf .text }&{\bf .rodata }&{\bf .data }&{\bf .bss  }\\\cline{1-5}
\endhead
All (no without) &35024 &9940 &336 &4104 \\\cline{1-5}
without client &25684 &7144 &336 &4104 \\\cline{1-5}
without client, exts &21652 &6288 &288 &4104 \\\cline{1-5}
without client, exts, debug\mbox{[}1\mbox{]} &19756 &3768 &288 &4104 \\\cline{1-5}
without server &30304 &8160 &336 &4104 \\\cline{1-5}
without server, exts &25382 &7204 &288 &4104 \\\cline{1-5}
without server, exts, debug\mbox{[}1\mbox{]} &23712 &4256 &288 &4104 \\\cline{1-5}
\end{longtabu}
\mbox{[}1\mbox{]} {\ttfamily -\/-\/disable-\/debug} only removes messages below {\ttfamily lwsl\+\_\+notice}. Since that is the default logging level the impact is not noticeable, error, warn and notice logs are all still there.

\mbox{[}2\mbox{]} {\ttfamily 1024} fd per process is the default limit (set by ulimit) in at least Fedora and Ubuntu. You can make significant savings tailoring this to actual expected peak fds, ie, at a limit of {\ttfamily 20}, context creation allocation reduces to {\ttfamily 4432 + 240 = 4672})

\mbox{[}3\mbox{]} known header content is freed after connection establishment 