1# Some notes for the windows jungle 2 3This was how I compiled libwebsockets starting from a blank windows install 4in March - April 2020. Doing this on a linux distro is way simpler and quicker 5than all this! 6 7## Notes on vm installation 8 9### Disk size 10 11For building you'll need 40GB+ available for the guest storage. 12 13### Required: Windows product key 14 15Assuming like me the first thing you do with a new laptop is install Linux over 16the windows it came with, you can recover your 'windows tax' windows product key 17from your device typically using `sudo strings /sys/firmware/acpi/tables/MSDM`, 18and use that for your VM install. 19 20### Required: Spice guest 21 22To have shared clipboard, and for windows video driver to match your vm window 23resolution, you must install spice guest tools inside the windows VM. It also 24installs some virtio pieces you will want. 25 26https://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-latest.exe 27 28### Blood-pressure reduction: Firefox 29 30https://www.mozilla.org/en-US/exp/firefox/ 31 32When it's up, add-ons: ublock origin, privacy badger, noscript, disable search 33bar prediction 34 35### Blood-pressure reduction: Clink 36 37This is a hack on cmd.exe that lets it understand Ctrl-R and fixup unix-style 38slashes automagically. 39 40https://github.com/mridgers/clink/releases/download/0.4.9/clink_0.4.9_setup.exe 41 42If you're usually using *nix, you definitely need this to keep your sanity. 43 44### Required: cmake 45 46CMake have a windows installer thing downloadable from here 47 48[cmake](https://cmake.org/download/) 49 50after that you can use `cmake` from the terminal OK. 51 52### Required: git 53 54Visit the canonical git site to download their windows installer thing 55 56[git](https://git-scm.com/download/win) 57 58**Select the install option for "extra unix commands"** so you can get `ls -l`, 59`cp`, `mv` and suchlike working in cmd.exe... that's awesome, thanks git! 60 61Afterwards you can just use `git` as normal from cmd.exe as well. 62 63### Required: Install the "free" "community" visual studio 64 65You can do this through "windows store" by searching for "visual studio" 66 67I installed as little as possible, we just want the C "C++" tools... 7GB :-) 68 69It still wouldn't link without the "mt" helper tool from the 70huge windows SDK, so you have to install GB of that as well. 71 72They don't mention it during the install, but after 30 days this "free" 73"community" edition demands you open a microsoft account or it stops working. 74In the install they give you the option to add a microsoft account and the 75alternative is, "not now, maybe later". Compare and contrast to gcc or git or 76the other FOSS projects. 77 78### Required: OpenSSL 79 80Ugh... I tried using prebuilts but it's unreliable and needs an unfeasible 81amount of trust. So I recommend bite the bullet and build your own... that's 82trivial on Linux but of course windows makes everything nasty. 83 84At least hopefully all the "research" is done and listed out here. 85 86#### OpenSSL build Prerequisite: install perl binary 87 88Move the git version of perl out of the way, it won't work for OpenSSL build 89 90``` 91mv /usr/bin/perl /usr/bin/perl-git 92``` 93 94For windows, OpenSSL "recommends" ActiveState perl but it doesn't work for me, 95complaining about stuff needed from cpan and then dying when it was installed. 96"Strawberry Perl" is installed in `C:\Strawberry` and worked out the box. 97 98http://strawberryperl.com/download/5.30.2.1/strawberry-perl-5.30.2.1-64bit.msi 99 100The installer sets up `%PATH%` if you open a new cmd window. 101 102#### OpenSSL build Prerequisite: NASM 103 104Go here and click on the latest stable, download the win32 .exe 105 106https://nasm.us/ 107 108Just install via the defaults. Then add it to the PATH temporarily... 109 110``` 111$ set PATH=%PATH%;C:\Program Files (x86)\NASM 112``` 113 114#### OpenSSL build setup: source VC env vars 115 116These fix up the PATH and include dirs etc necessary for VC build in the cmd 117window. 118 119``` 120$ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 121``` 122 123### OpenSSL build: 124 125Grab openssl from git... assuming the prerequisites above went well it will 126just sit there building for 30 minutes or whatever. 127 128``` 129$ git clone https://github.com/openssl/openssl 130$ cd openssl 131$ perl Configure VC-WIN64A 132$ nmake 133``` 134 135Afterwards, open an Administrator mode cmd.exe, redo the msvc path and then 136install the build. 137 138``` 139$ cd openssl 140$ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 141$ nmake install 142``` 143 144Oh another grindingly slow windows build action. Finally it's in there in 145`C:\Program Files\OpenSSL`. 146 147libraries are looking for a cert bundle at "C:\Program Files\Common Files\SSL\cert.pem"... 148it's not documented or included in the zip file from the above, so... 149 150#### Installing a cert bundle 151 152You can get a trusted cert bundle from here 153 154[drwetter/testssl cert bundle](https://raw.githubusercontent.com/drwetter/testssl.sh/3.1dev/etc/Microsoft.pem) 155 156Save it into `C:\Program Files\Common Files\SSL\cert.pem` where openssl will be able to see it. 157 158## Required: pthreads 159 160It's amazing but after all these years windows doesn't offer pthreads compatibility 161itself. Just like the many other missing POSIX bits like fork(). 162 163I downloaded the latest (2012) zip release of pthreads-win32 from here 164 165ftp://sourceware.org/pub/pthreads-win32 166 167Then I created a dir "C:\Program Files (x86)\pthreads", and copied the `dll`, 168`include` and `lib` subdirs from the `prebuilt` folder in the zip there. 169 170The cmake incantation to build against pthreads set up like that is 171 172``` 173 $ cmake .. -DLWS_HAVE_PTHREAD_H=1 -DLWS_EXT_PTHREAD_INCLUDE_DIR="C:\Program Files (x86)\pthreads\include" -DLWS_EXT_PTHREAD_LIBRARIES="C:\Program Files (x86)\pthreads\lib\x64\libpthreadGC2.a" -DLWS_WITH_MINIMAL_EXAMPLES=1 174``` 175 176## Building libwebsockets 177 178We'll clone libwebsockets then use cmake to build via vs tools 179 180``` 181> git clone https://libwebsockets.org/repo/libwebsockets 182> cd libwebsockets 183> mkdir build 184> cd build 185> cmake .. 186> cmake --build . --config DEBUG 187``` 188 189Installing requires admin privs, I opened a second cmd window as admin and did it 190there. 191 192``` 193> cmake --install . --config DEBUG 194``` 195 196### Hack the libs into view 197 198The libs we built against aren't visible in the system, I don't know what 199Real Windows Programmers are supposed to do about that, but I used an Admin cmd 200prompt to copy them into C:\windows\system32 201 202``` 203$ cp "C:\Program Files (x86)\pthreads\dll\x64\pthreadGC2.dll" "C:\Program Files\OpenSSL\bin\libcrypto-3.dll" "C:\Program Files\OpenSSL\bin\libssl-3.dll" C:\Windows\system32 204``` 205 206After that you can run the test apps OK, eg 207 208``` 209$ libwebsockets-test-server.exe -s 210``` 211 212## Note about using paths with spaces in with cmake 213 214 215