From a7869567ff72b3bc4f6b8ac6fa6d0f34d83b1719 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 24 Oct 2021 22:39:41 +0200 Subject: [PATCH] Build against BoringSSL --- .github/workflows/c-cpp.yml | 1 + build.c | 78 +++++++++++++++++++++++++++++-------- uWebSockets | 2 +- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 9d1c610..1397d60 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -9,6 +9,7 @@ jobs: runs-on: windows-latest steps: - uses: ilammy/msvc-dev-cmd@v1 + - uses: ilammy/setup-nasm@v1.2.1 - name: Update binaries run: | $ErrorActionPreference = 'SilentlyContinue' diff --git a/build.c b/build.c index 07e5b81..87542c9 100644 --- a/build.c +++ b/build.c @@ -17,9 +17,12 @@ #define IS_MACOS #endif +const char *ARM64 = "arm64"; +const char *X64 = "x64"; + /* System, but with string replace */ int run(const char *cmd, ...) { - char buf[512]; + char buf[2048]; va_list args; va_start(args, cmd); vsprintf(buf, cmd, args); @@ -53,15 +56,39 @@ void prepare() { } } +/* Build boringssl */ +void build_boringssl(const char *arch) { + +#ifdef IS_MACOS + /* Build for x64 (the host) */ + run("cd uWebSockets/uSockets/boringssl && mkdir -p x64 && cd x64 && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_DEPLOYMENT_TARGET=10.14 .. && make crypto ssl"); + + /* Build for arm64 (cross compile) */ + run("cd uWebSockets/uSockets/boringssl && mkdir -p arm64 && cd arm64 && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES=arm64 .. && make crypto ssl"); +#endif + +#ifdef IS_LINUX + /* Build for x64 or arm64 (depending on the host) */ + run("cd uWebSockets/uSockets/boringssl && mkdir -p %s && cd %s && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release .. && make crypto ssl", arch, arch); +#endif + +#ifdef IS_WINDOWS + /* Build for x64 (the host) */ + run("cd uWebSockets/uSockets/boringssl && mkdir -p x64 && cd x64 && cmake -DCMAKE_BUILD_TYPE=Release -GNinja .. && ninja crypto ssl"); +#endif + +} + /* Build for Unix systems */ -void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, char *arch) { - char *c_shared = "-DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c"; - char *cpp_shared = "-DUWS_WITH_PROXY -DLIBUS_USE_LIBUV -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp uWebSockets/uSockets/src/crypto/sni_tree.cpp"; +void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, const char *arch) { + + char *c_shared = "-DLIBUS_USE_LIBUV -I uWebSockets/uSockets/boringssl/include -pthread -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c"; + char *cpp_shared = "-DUWS_WITH_PROXY -DLIBUS_USE_LIBUV -I uWebSockets/uSockets/boringssl/include -pthread -DLIBUS_USE_OPENSSL -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp uWebSockets/uSockets/src/crypto/sni_tree.cpp"; for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { run("%s %s -I targets/node-%s/include/node", compiler, c_shared, versions[i].name); run("%s %s -I targets/node-%s/include/node", cpp_compiler, cpp_shared, versions[i].name); - run("%s %s %s -o dist/uws_%s_%s_%s.node", cpp_compiler, "-flto -O3 *.o -std=c++17 -shared", cpp_linker, os, arch, versions[i].abi); + run("%s -pthread -flto -O3 *.o uWebSockets/uSockets/boringssl/%s/ssl/libssl.a uWebSockets/uSockets/boringssl/%s/crypto/libcrypto.a -std=c++17 -shared %s -o dist/uws_%s_%s_%s.node", cpp_compiler, arch, arch, cpp_linker, os, arch, versions[i].abi); } } @@ -74,12 +101,13 @@ void copy_files() { } /* Special case for windows */ -void build_windows(char *arch) { +void build_windows(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, const char *arch) { + /* For all versions */ for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { - run("cl /W3 /D \"UWS_WITH_PROXY\" /D \"LIBUS_USE_LIBUV\" /D \"LIBUS_USE_OPENSSL\" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/crypto/sni_tree.cpp " + run("cl /MD /W3 /D WIN32_LEAN_AND_MEAN /D \"UWS_WITH_PROXY\" /D \"LIBUS_USE_LIBUV\" /I uWebSockets/uSockets/boringssl/include /D \"LIBUS_USE_OPENSSL\" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/crypto/sni_tree.cpp " "uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c /I targets/node-%s/include/node /I uWebSockets/src /EHsc " - "/Ox /LD /Fedist/uws_win32_%s_%s.node src/addon.cpp targets/node-%s/node.lib", + "/Ox /LD /Fedist/uws_win32_%s_%s.node src/addon.cpp advapi32.lib uWebSockets/uSockets/boringssl/x64/ssl/ssl.lib uWebSockets/uSockets/boringssl/x64/crypto/crypto.lib targets/node-%s/node.lib", versions[i].name, arch, versions[i].abi, versions[i].name); } } @@ -88,35 +116,53 @@ int main() { printf("[Preparing]\n"); prepare(); printf("\n[Building]\n"); + + const char *arch = X64; +#ifdef __aarch64__ + arch = ARM64; +#endif + + /* Build for x64 and/or arm64 */ + build_boringssl(arch); #ifdef IS_WINDOWS - build_windows("x64"); + /* We can use clang, but we currently do use cl.exe still */ + build_windows("clang", + "clang++", + "", + OS, + X64); #else #ifdef IS_MACOS + /* Apple special case */ build("clang -mmacosx-version-min=10.14", "clang++ -stdlib=libc++ -mmacosx-version-min=10.14", "-undefined dynamic_lookup", OS, - "x64"); + X64); - /* Try and build for arm64 macOS 11 */ - build("clang -target arm64-apple-macos11", + /* Try and build for arm64 macOS 11 */ + build("clang -target arm64-apple-macos11", "clang++ -stdlib=libc++ -target arm64-apple-macos11", "-undefined dynamic_lookup", OS, - "arm64"); + ARM64); #else - /* Linux */ + /* Linux does not cross-compile but picks whatever arch the host is on */ build("clang", "clang++", "-static-libstdc++ -static-libgcc -s", OS, - "x64"); + arch); /* If linux we also want arm64 */ - build("aarch64-linux-gnu-gcc", "aarch64-linux-gnu-g++", "-static-libstdc++ -static-libgcc -s", OS, "arm64"); + /*build("aarch64-linux-gnu-gcc", + "aarch64-linux-gnu-g++", + "-static-libstdc++ -static-libgcc -s", + OS, + ARM64);*/ #endif #endif diff --git a/uWebSockets b/uWebSockets index 92a9b3f..80cd187 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 92a9b3f9923768b0367d3ee53779ec16cbf0d45d +Subproject commit 80cd18781b34cb7012ae52640d9ccbf0e8e8bb6c