1Building CivetWeb
2=========
3
4This guide covers the build instructions for the stand-alone web server.
5See [Embedding.md](https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md) for information on extending an existing C or C++ application. A brief overview of the source code files can be found in [Embedding.md](https://github.com/civetweb/civetweb/blob/master/docs/Embedding.md) as well.
6
7#### Where to get the source code?
8
9The latest development version can be found at
10https://github.com/civetweb/civetweb
11
12Tested and released versions can be found at
13https://github.com/civetweb/civetweb/releases
14
15
16Building for Windows
17---------
18
19#### Using Visual Studio
20
21Open the *VS/civetweb.sln* in Visual Studio.
22To include SSL support, you may have to add an extra library for the cryptography support. You might wish to use yaSSL.  However, it is GPL licensed or uses a commercial license. See [yaSSL.md](https://github.com/civetweb/civetweb/blob/master/docs/yaSSL.md) for more information.
23Alternatively, you might wish to use OpenSSL. See [OpenSSL.md](https://github.com/civetweb/civetweb/blob/master/docs/OpenSSL.md) for more information.
24
25#### Using MinGW-w64 or TDM-GCC
26In the start menu locate and run the "Run terminal" batch file. For TDM-GCC this is named "MinGW Command Prompt".
27Navigate to the civetweb sources directory and run:
28```
29mingw32-make CC=gcc
30```
31
32#### Using Qt Creator
33Open the Qt Designer project in the Qt folder
34
35#### Using CMake
36Except for the components in the `third_party` folder (e.g., Lua and Duktape), CivetWeb can also be built with CMake.
37CMake can be used for all supported operating systems.
38
39
40Building for Linux, BSD, and OSX
41---------
42
43## Using Make
44
45```
46make help
47```
48Get a list of all supported make option
49
50```
51make build
52```
53compile the code
54
55```
56make install
57```
58Install on the system, Linux only.
59
60```
61make lib WITH_CPP=1 WITH_IPV6=1
62make clean slib WITH_CPP=1 WITH_LUA=1 WITH_WEBSOCKET=1
63```
64Build the static and shared libraries.
65The *WITH_CPP* make option is to include the CivetServer class.
66The additional make options configure the library just as it would the application.
67
68The *slib* option should be done on a separate clean build as position
69independent code (PIC) is required for it.  Trying to run it after
70building the static library or the server will result in a link error.
71
72```
73make clean
74```
75Clean up files generated during the build
76
77
78## Setting build options
79
80Make options can be set on the command line with the make command like so.
81```
82make build WITH_LUA=1
83```
84
85
86| Make Options                | Description                                       |
87| --------------------------- | ------------------------------------------------- |
88| `WITH_LUA=1`                | build with Lua support                            |
89| `WITH_DUKTAPE=1`            | build with server-side JavaScript support         |
90| `WITH_IPV6=1`               | with IPV6 support                                 |
91| `WITH_WEBSOCKET=1`          | build with web socket support                     |
92| `WITH_SERVER_STATS=1`       | build with support for server statistics          |
93| `WITH_EXPERIMENTAL=1`       | include experimental features (version depending) |
94| `WITH_ALL=1`                | Include all of the above features                 |
95| `WITH_DEBUG=1`              | build with GDB debug support                      |
96| `WITH_CPP=1`                | build libraries with c++ classes                  |
97| `CONFIG_FILE=file`          | use 'file' as the config file                     |
98| `CONFIG_FILE2=file`         | use 'file' as the backup config file              |
99| `HTMLDIR=/path`             | place to install initial web pages                |
100| `DOCUMENT_ROOT=/path`       | default document root                             |
101| `PORTS=8080`                | listening ports override when installing          |
102| `SSL_LIB=libssl.so.0`       | use versioned SSL library                         |
103| `CRYPTO_LIB=libcrypto.so.0` | system versioned CRYPTO library                   |
104| `PREFIX=/usr/local`         | sets the install directory                        |
105| `COPT='-DNO_SSL'`           | method to insert compile flags                    |
106
107Note that the WITH_* options used for *make* are not identical to the
108preprocessor defines in the source code - usually USE_* is used there.
109
110
111## Changing PREFIX
112
113To change the target destination pass the `PREFIX` option to the command `make install` (not `make build`). Example usage:
114
115```
116$ make build
117$ make -n install PREFIX=/opt/civetweb
118```
119Note: The `-n` corresponds to the `--dry-run` option (it does not make any changes): You can see where `make install` would install. Example output of the above command:
120
121```
122$ make -n install PREFIX=/opt/civetweb
123install -d -m 755  "/opt/civetweb/share/doc/civetweb"
124install -m 644 resources/itworks.html /opt/civetweb/share/doc/civetweb/index.html
125install -m 644 resources/civetweb_64x64.png /opt/civetweb/share/doc/civetweb/
126install -d -m 755  "/opt/civetweb/etc"
127install -m 644 resources/civetweb.conf  "/opt/civetweb/etc/"
128sed -i 's#^document_root.*$#document_root /opt/civetweb/share/doc/civetweb#' "/opt/civetweb/etc/civetweb.conf"
129sed -i 's#^listening_ports.*$#listening_ports 8080#' "/opt/civetweb/etc/civetweb.conf"
130install -d -m 755  "/opt/civetweb/share/doc/civetweb"
131install -m 644 *.md "/opt/civetweb/share/doc/civetweb"
132install -d -m 755 "/opt/civetweb/bin"
133install -m 755 civetweb "/opt/civetweb/bin/"
134```
135
136If the output looks good: Just remove the `-n` option to actually install the software on your system.
137
138
139## Setting compile flags
140
141Compile flags can be set using the *COPT* make option like so.
142```
143make build COPT="-DNDEBUG -DNO_CGI"
144```
145
146| Compile Flags                | Description                                               |
147| ---------------------------- | --------------------------------------------------------- |
148| `NDEBUG`                     | strip off all debug code                                  |
149| `DEBUG`                      | build debug version (very noisy)                          |
150|                              |                                                           |
151| `NO_FILES`                   | do not serve files from a directory                       |
152| `NO_FILESYSTEMS`             | comletely disable filesystems usage (requires NO_FILES)   |
153| `NO_SSL`                     | disable SSL functionality                                 |
154| `NO_CGI`                     | disable CGI support                                       |
155| `NO_CACHING`                 | disable caching functionality                             |
156|                              |                                                           |
157| `USE_IPV6`                   | enable IPv6 support                                       |
158| `USE_WEBSOCKET`              | enable websocket support                                  |
159| `USE_LUA`                    | enable Lua support                                        |
160| `USE_DUKTAPE`                | enable server-side JavaScript (using Duktape library)     |
161| `USE_SERVER_STATS`           | enable server statistics support                          |
162| `USE_ZLIB`                   | enable on-the-fly compression of files (using zlib)       |
163| `MG_EXPERIMENTAL_INTERFACES` | include experimental interfaces                           |
164| `MG_LEGACY_INTERFACE`        | include obsolete interfaces (candidates for deletion)     |
165|                              |                                                           |
166| `NO_SSL_DL`                  | link against system libssl library                        |
167| `SQLITE_DISABLE_LFS`         | disables large files (Lua only)                           |
168| `SSL_ALREADY_INITIALIZED`    | do not initialize libcrypto                               |
169| `OPENSSL_API_1_1`            | Use OpenSSL V1.1.x interface                              |
170
171
172Note: If `make` is used (with this [Makefile](https://github.com/civetweb/civetweb/blob/master/Makefile)), you should not pass the `USE_<feature>` flags using `COPT`, but use the `WITH_<feature>` syntax above, since additional features may also use additional source code files.
173
174
175## Cross Compiling
176
177Take total control with *CC*, *COPT* and *TARGET_OS* as make options.
178TARGET_OS is used to determine some compile details as will as code function.
179TARGET_OS values should be be one found in *resources/Makefile.in-os*.
180
181```
182make CC=arm-none-linux-gnueabi-gcc COPT="-march=armv7-a  -mfpu=vfp -mfloat-abi=softfp" TARGET_OS=FROG
183```
184
185## Cocoa DMG Packaging (OSX Only)
186
187Use the alternate *Makefile.osx* to do the build.  The entire build has
188to be done using *Makefile.osx* because additional compile and link options
189are required.  This Makefile has all the same options as the other one plus
190one additional *package* rule.
191
192```
193make -f Makefile.osx package
194```
195
196Building with Buildroot
197---------
198
199[Buildroot](http://buildroot.uclibc.org/) is a tool for creating cross compiled file systems.  Including Civetweb in buildroot is fairly easy.  There is even support for various build options.
200
2011. First, check if it already there.
202  - In buildroot, make menuconfig
203     - Package Selection for the target --->
204     - Networking applications  --->
205     - civetweb
2062. If not there, just add it
207  - copy *Config.in* and *civetweb.mk* from Civetweb's *contrib/buildroot/* to Buildroot's *package/civetweb/* directory.
208  - In Buildroot's *package/Config.in, insert the following line in were you will know how to find it in the menu.
209    > ``` source "package/civetweb/Config.in" ```
210
211
212Building on Android
213---------
214
215This is a small guide to help you run civetweb on Android, originally
216tested on the HTC Wildfire.
217Note: You do not need root access to run civetweb on Android.
218
219- Download the source from the Downloads page.
220- Download the Android NDK from [http://developer.android.com/tools/sdk/ndk/index.html](http://developer.android.com/tools/sdk/ndk/index.html)
221- Run `/path-to-ndk/ndk-build -C /path-to-civetweb/resources`
222  That should generate civetweb/lib/armeabi/civetweb
223- Using the adb tool (you need to have Android SDK installed for that),
224  push the generated civetweb binary to `/data/local` folder on device.
225- From adb shell, navigate to `/data/local` and execute `./civetweb`.
226- To test if the server is running fine, visit your web-browser and
227  navigate to `http://127.0.0.1:8080` You should see the `Index of /` page.
228
229
230Notes:
231
232- `jni` stands for Java Native Interface. Read up on Android NDK if you want
233  to know how to interact with the native C functions of civetweb in Android
234  Java applications.
235
236
237
238