diff --git a/Makefile b/Makefile index 6b8b619..2117785 100644 --- a/Makefile +++ b/Makefile @@ -1,46 +1,3 @@ -# Note: for Windows, run "nmake Windows" in x64 developer terminal - -C_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c -CPP_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp -C_OSX = -mmacosx-version-min=10.7 -CPP_OSX = -stdlib=libc++ $(C_OSX) - -Unices: - make targets - cp src/uws.js dist/uws.js - NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)` - NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)` - for f in dist/*.node; do chmod +x $$f; done - -targets: - mkdir dist -# Node.js 10 - mkdir targets - curl -OJ https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz - tar xzf node-v10.0.0-headers.tar.gz -C targets - curl https://nodejs.org/dist/v10.0.0/win-x64/node.lib > targets/node-v10.0.0/node.lib -# Node.js 11 - curl -OJ https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz - tar xzf node-v11.1.0-headers.tar.gz -C targets - curl https://nodejs.org/dist/v11.1.0/win-x64/node.lib > targets/node-v11.1.0/node.lib - -Linux: - clang $(C_SHARED) -I $$NODE/include/node - clang++ $(CPP_SHARED) -I $$NODE/include/node - clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node - -Darwin: - clang $(C_OSX) $(C_SHARED) -I $$NODE/include/node - clang++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node - clang++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node - -FreeBSD: - clang $(C_SHARED) -I $$NODE/include/node - clang++ $(CPP_SHARED) -I $$NODE/include/node - clang++ -fuse-ld=lld -flto -O3 *.o -std=c++17 -shared -s -o dist/uws_freebsd_$$ABI.node - Windows: - nmake targets - copy "src\uws.js" dist /Y - cl /D "LIBUS_USE_LIBUV" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c /I targets/node-v10.0.0/include/node /I uWebSockets/src /EHsc /Ox /LD /Fedist/uws_win32_64.node src/addon.cpp targets/node-v10.0.0/node.lib - cl /D "LIBUS_USE_LIBUV" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c /I targets/node-v11.1.0/include/node /I uWebSockets/src /EHsc /Ox /LD /Fedist/uws_win32_67.node src/addon.cpp targets/node-v11.1.0/node.lib + $(CC) build.c + ./a.out || build.exe diff --git a/build.c b/build.c new file mode 100644 index 0000000..b8b2e87 --- /dev/null +++ b/build.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +/* List of platform features */ +#ifdef _WIN32 +#define OS "win32" +#define IS_WINDOWS +#endif +#ifdef __linux +#define OS "linux" +#define IS_LINUX +#endif +#ifdef __APPLE__ +#define OS "darwin" +#define IS_MACOS +#endif + +/* System, but with string replace */ +int run(const char *cmd, ...) { + char buf[512]; + va_list args; + va_start(args, cmd); + vsprintf(buf, cmd, args); + va_end(args); + printf("--> %s\n\n", buf); + return system(buf); +} + +/* List of Node.js versions */ +struct node_version { + char *name; + char *abi; +} versions[] = { + {"v10.0.0", "64"}, + {"v11.1.0", "67"} +}; + +/* Downloads headers, creates folders */ +void prepare() { + if (run("mkdir dist") || run("mkdir targets")) { + return; + } + + /* For all versions */ + for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { + run("curl -OJ https://nodejs.org/dist/%s/node-%s-headers.tar.gz", versions[i].name, versions[i].name); + run("tar xzf node-%s-headers.tar.gz -C targets", versions[i].name); + run("curl https://nodejs.org/dist/%s/win-x64/node.lib > targets/node-%s/node.lib", versions[i].name, versions[i].name); + } +} + +/* Build for Unix systems */ +void build(char *compiler, char *cpp_compiler, char *cpp_linker, char *os, char *arch) { + char *c_shared = "-DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c"; + char *cpp_shared = "-DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.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); + } +} + +void copy_files() { +#ifdef IS_WINDOWS + run("copy \"src\\uws.js\" dist /Y"); +#else + run("cp src/uws.js dist/uws.js"); +#endif +} + +/* Special case for windows */ +void build_windows(char *arch) { + /* For all versions */ + for (unsigned int i = 0; i < sizeof(versions) / sizeof(struct node_version); i++) { + run("cl /D \"LIBUS_USE_LIBUV\" /std:c++17 /I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c " + "uWebSockets/uSockets/src/eventing/*.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", + versions[i].name, arch, versions[i].abi, versions[i].name); + } +} + +int main() { + printf("[Preparing]\n"); + prepare(); + printf("\n[Building]\n"); + +#ifdef IS_WINDOWS + build_windows("x64"); +#else +#ifdef IS_MACOS + /* Apple special case */ + build("clang -mmacosx-version-min=10.7", + "clang++ -stdlib=libc++ -mmacosx-version-min=10.7", + "-undefined dynamic_lookup", + OS, + "x64"); +#else + /* Linux */ + build("clang", + "clang++", + "-static-libstdc++ -static-libgcc -s", + OS, + "x64"); + + /* If linux we also want arm64 */ + build("aarch64-linux-gnu-gcc", "aarch64-linux-gnu-g++", "-static-libstdc++ -static-libgcc -s", OS, "arm64"); +#endif +#endif + + copy_files(); +} + diff --git a/src/uws.js b/src/uws.js index c0a2cae..2ce00ea 100644 --- a/src/uws.js +++ b/src/uws.js @@ -17,20 +17,15 @@ module.exports = (() => { try { - const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`); - /* We are not compatible with Node.js nextTick and/or domains */ + const uWS = require('./uws_' + process.platform + '_' + process.arch + '_' + process.versions.modules + '.node'); process.nextTick = (f, ...args) => { Promise.resolve().then(() => { f(...args); }); }; - /* You are not allowed to use the lib past here */ - process.on('exit', () => { - uWS.free(); - }); - + process.on('exit', uWS.free); return uWS; } catch (e) { - throw new Error('This version of µWS is not compatible with your Node.js build.\n\n' + e.toString()); + throw new Error('This version of µWS is not compatible with your Node.js build:\n\n' + e.toString()); } })(); diff --git a/uWebSockets b/uWebSockets index 7ea680e..c1ea38a 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 7ea680e187be8db7d5a3133e049a1fd6a7800d3d +Subproject commit c1ea38ae10f7c9e4acf80c590d455e8f779d53ab