|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdarg.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +/* List of platform features */ |
| 7 | +#ifdef _WIN32 |
| 8 | +#define OS "win32" |
| 9 | +#define IS_WINDOWS |
| 10 | +#endif |
| 11 | +#ifdef __linux |
| 12 | +#define OS "linux" |
| 13 | +#define IS_LINUX |
| 14 | +#endif |
| 15 | +#ifdef __APPLE__ |
| 16 | +#define OS "darwin" |
| 17 | +#define IS_MACOS |
| 18 | +#endif |
| 19 | + |
| 20 | +/* System, but with string replace */ |
| 21 | +int run(const char *cmd, ...) { |
| 22 | + char buf[512]; |
| 23 | + va_list args; |
| 24 | + va_start(args, cmd); |
| 25 | + vsprintf(buf, cmd, args); |
| 26 | + va_end(args); |
| 27 | + printf("--> %s\n\n", buf); |
| 28 | + return system(buf); |
| 29 | +} |
| 30 | + |
| 31 | +/* List of Node.js versions */ |
| 32 | +struct node_version { |
| 33 | + char *name; |
| 34 | + char *abi; |
| 35 | +} versions[] = { |
| 36 | + {"v10.0.0", "64"}, |
| 37 | + {"v11.1.0", "67"} |
| 38 | +}; |
| 39 | + |
| 40 | +/* Downloads headers, creates folders */ |
| 41 | +void prepare() { |
| 42 | + if (run("mkdir dist") || run("mkdir targets")) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + /* For all versions */ |
| 47 | + for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { |
| 48 | + run("curl -OJ https://nodejs.org/dist/%s/node-%s-headers.tar.gz", versions[i].name, versions[i].name); |
| 49 | + run("tar xzf node-%s-headers.tar.gz -C targets", versions[i].name); |
| 50 | + run("curl https://nodejs.org/dist/%s/win-x64/node.lib > targets/node-%s/node.lib", versions[i].name, versions[i].name); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/* Build for Unix systems */ |
| 55 | +void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, char *arch) { |
| 56 | + char *c_shared = "-DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c"; |
| 57 | + char *cpp_shared = "-DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp"; |
| 58 | + |
| 59 | + for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { |
| 60 | + run("%s %s -I targets/node-%s/include/node", compiler, c_shared, versions[i].name); |
| 61 | + run("%s %s -I targets/node-%s/include/node", cpp_compiler, cpp_shared, versions[i].name); |
| 62 | + 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); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void copy_files() { |
| 67 | +#ifdef IS_WINDOWS |
| 68 | + run("copy \"src\\uws.js\" dist /Y"); |
| 69 | +#else |
| 70 | + run("cp src/uws.js dist/uws.js"); |
| 71 | +#endif |
| 72 | +} |
| 73 | + |
| 74 | +/* Special case for windows */ |
| 75 | +void build_windows(char *arch) { |
| 76 | + /* For all versions */ |
| 77 | + for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { |
| 78 | + run("cl /D \"LIBUS_USE_LIBUV\" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c " |
| 79 | + "uWebSockets/uSockets/src/eventing/*.c /I targets/node-%s/include/node /I uWebSockets/src /EHsc " |
| 80 | + "/Ox /LD /Fedist/uws_win32_%s_%s.node src/addon.cpp targets/node-%s/node.lib", |
| 81 | + versions[i].name, arch, versions[i].abi, versions[i].name); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +int main() { |
| 86 | + printf("[Preparing]\n"); |
| 87 | + prepare(); |
| 88 | + printf("\n[Building]\n"); |
| 89 | + |
| 90 | +#ifdef IS_WINDOWS |
| 91 | + build_windows("x64"); |
| 92 | +#else |
| 93 | +#ifdef IS_MACOS |
| 94 | + /* Apple special case */ |
| 95 | + build("clang -mmacosx-version-min=10.7", |
| 96 | + "clang++ -stdlib=libc++ -mmacosx-version-min=10.7", |
| 97 | + "-undefined dynamic_lookup", |
| 98 | + OS, |
| 99 | + "x64"); |
| 100 | +#else |
| 101 | + /* Linux */ |
| 102 | + build("clang", |
| 103 | + "clang++", |
| 104 | + "-static-libstdc++ -static-libgcc -s", |
| 105 | + OS, |
| 106 | + "x64"); |
| 107 | + |
| 108 | + /* If linux we also want arm64 */ |
| 109 | + build("aarch64-linux-gnu-gcc", "aarch64-linux-gnu-g++", "-static-libstdc++ -static-libgcc -s", OS, "arm64"); |
| 110 | +#endif |
| 111 | +#endif |
| 112 | + |
| 113 | + copy_files(); |
| 114 | +} |
| 115 | + |
0 commit comments