Skip to content

Commit 0686ce8

Browse files
committed
Move build logic to C program, add ARM64 target
1 parent c9d781a commit 0686ce8

4 files changed

Lines changed: 121 additions & 54 deletions

File tree

‎Makefile‎

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,3 @@
1-
# Note: for Windows, run "nmake Windows" in x64 developer terminal
2-
3-
C_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I uWebSockets/uSockets/src uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/eventing/*.c
4-
CPP_SHARED = -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I uWebSockets/uSockets/src -I uWebSockets/src src/addon.cpp
5-
C_OSX = -mmacosx-version-min=10.7
6-
CPP_OSX = -stdlib=libc++ $(C_OSX)
7-
8-
Unices:
9-
make targets
10-
cp src/uws.js dist/uws.js
11-
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
12-
NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)`
13-
for f in dist/*.node; do chmod +x $$f; done
14-
15-
targets:
16-
mkdir dist
17-
# Node.js 10
18-
mkdir targets
19-
curl -OJ https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz
20-
tar xzf node-v10.0.0-headers.tar.gz -C targets
21-
curl https://nodejs.org/dist/v10.0.0/win-x64/node.lib > targets/node-v10.0.0/node.lib
22-
# Node.js 11
23-
curl -OJ https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz
24-
tar xzf node-v11.1.0-headers.tar.gz -C targets
25-
curl https://nodejs.org/dist/v11.1.0/win-x64/node.lib > targets/node-v11.1.0/node.lib
26-
27-
Linux:
28-
clang $(C_SHARED) -I $$NODE/include/node
29-
clang++ $(CPP_SHARED) -I $$NODE/include/node
30-
clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node
31-
32-
Darwin:
33-
clang $(C_OSX) $(C_SHARED) -I $$NODE/include/node
34-
clang++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node
35-
clang++ $(CPP_OSX) -flto -O3 *.o -std=c++17 -shared -undefined dynamic_lookup -o dist/uws_darwin_$$ABI.node
36-
37-
FreeBSD:
38-
clang $(C_SHARED) -I $$NODE/include/node
39-
clang++ $(CPP_SHARED) -I $$NODE/include/node
40-
clang++ -fuse-ld=lld -flto -O3 *.o -std=c++17 -shared -s -o dist/uws_freebsd_$$ABI.node
41-
421
Windows:
43-
nmake targets
44-
copy "src\uws.js" dist /Y
45-
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
46-
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
2+
$(CC) build.c
3+
./a.out || build.exe

‎build.c‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+

‎src/uws.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,15 @@
1717

1818
module.exports = (() => {
1919
try {
20-
const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`);
21-
/* We are not compatible with Node.js nextTick and/or domains */
20+
const uWS = require('./uws_' + process.platform + '_' + process.arch + '_' + process.versions.modules + '.node');
2221
process.nextTick = (f, ...args) => {
2322
Promise.resolve().then(() => {
2423
f(...args);
2524
});
2625
};
27-
/* You are not allowed to use the lib past here */
28-
process.on('exit', () => {
29-
uWS.free();
30-
});
31-
26+
process.on('exit', uWS.free);
3227
return uWS;
3328
} catch (e) {
34-
throw new Error('This version of µWS is not compatible with your Node.js build.\n\n' + e.toString());
29+
throw new Error('This version of µWS is not compatible with your Node.js build:\n\n' + e.toString());
3530
}
3631
})();

‎uWebSockets‎

0 commit comments

Comments
 (0)