make.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. /*@flow*/
  16. 'use strict';
  17. var Fs = require('fs');
  18. var nThen = require('nthen');
  19. var Cp = require('./Cp');
  20. var Builder = require('./builder');
  21. const CjdnsTest = require('./CjdnsTest');
  22. const GetVersion = require('./GetVersion');
  23. var CFLAGS = process.env['CFLAGS'];
  24. var LDFLAGS = process.env['LDFLAGS'];
  25. // march=native really only makes a lot of sense on x86/amd64 where the available features
  26. // are a hodgepodge per-CPU. On arm (32) you may or may not have NEON available but in any
  27. // case clang doesn't reliably support march except on x86/amd64.
  28. var NO_MARCH_FLAG = ['arm', 'ppc', 'ppc64', 'arm64'];
  29. if (process.version.replace('v','').split('.').map(Number)[0] >= 18) {
  30. // OK
  31. } else if ('OLD_NODE_VERSION_I_EXPECT_ERRORS' in process.env) {
  32. console.log('OLD_NODE_VERSION_I_EXPECT_ERRORS is set, ignoring old version');
  33. } else {
  34. throw new Error("Your version of nodejs is old/untested. " +
  35. "Old enough versions (pre-es6) do not work correctly and give weird build failures. " +
  36. "If you want to force building anyway, try: " +
  37. "OLD_NODE_VERSION_I_EXPECT_ERRORS=1 ./do");
  38. }
  39. Builder.configure({
  40. buildDir: process.env['OUT_DIR'], // set by cargo
  41. systemName: process.env['SYSTEM'] || process.platform,
  42. gcc: process.env['CC'],
  43. }, function (builder, waitFor) {
  44. builder.config.crossCompiling = process.env['CROSS'] !== undefined;
  45. let optimizeLevel = '-O2';
  46. builder.config.cflags.push(
  47. '-std=c99',
  48. '-Wall',
  49. '-Wextra',
  50. '-Werror',
  51. '-Wno-pointer-sign',
  52. '-Wno-strict-prototypes',
  53. '-Wmissing-prototypes',
  54. '-pedantic',
  55. '-D', builder.config.systemName + '=1',
  56. '-Wno-unused-parameter',
  57. '-fomit-frame-pointer',
  58. '-ffunction-sections',
  59. '-fdata-sections',
  60. '-D', 'Log_' + (process.env['Log_LEVEL'] || 'DEBUG'),
  61. '-g',
  62. // f4 = 16 peers max, fixed width 4 bit
  63. // f8 = 241 peers max, fixed width 8 bit
  64. // v3x5x8 = 256 peers max, variable width, 3, 5 or 8 bits plus 1 or 2 bits of prefix
  65. // v4x8 = 256 peers max, variable width, 4, or 8 bits plus 1 bit prefix
  66. '-D', 'NumberCompress_TYPE=v3x5x8',
  67. // enable for safety (don't worry about speed, profiling shows they add ~nothing)
  68. '-D', 'Identity_CHECK=1',
  69. '-D', 'Allocator_USE_CANARIES=1',
  70. '-D', 'PARANOIA=1'
  71. );
  72. if (process.env["CJDNS_RELEASE_VERSION"]) {
  73. builder.config.version = '' + process.env["CJDNS_RELEASE_VERSION"];
  74. }
  75. if (process.env['SUBNODE']) { builder.config.cflags.push('-DSUBNODE=1'); }
  76. if (process.env['GCOV']) {
  77. builder.config.cflags.push('-fprofile-arcs', '-ftest-coverage');
  78. builder.config.ldflags.push('-fprofile-arcs', '-ftest-coverage');
  79. }
  80. var android = /android/i.test(builder.config.gcc);
  81. if (process.env['TESTING']) {
  82. builder.config.cflags.push('-D', 'TESTING=1');
  83. }
  84. if (process.env['ADDRESS_PREFIX']) {
  85. builder.config.cflags.push('-D', 'ADDRESS_PREFIX=' + process.env['ADDRESS_PREFIX']);
  86. }
  87. if (process.env['ADDRESS_PREFIX_BITS']) {
  88. builder.config.cflags.push('-D', 'ADDRESS_PREFIX_BITS=' + process.env['ADDRESS_PREFIX_BITS']);
  89. }
  90. if (!builder.config.crossCompiling) {
  91. if (NO_MARCH_FLAG.indexOf(process.arch) == -1) {
  92. builder.config.cflags.push('-march=native');
  93. }
  94. }
  95. if (builder.config.systemName === 'win32') {
  96. builder.config.cflags.push('-Wno-format');
  97. } else if (builder.config.systemName === 'linux') {
  98. builder.config.ldflags.push('-Wl,-z,relro,-z,now,-z,noexecstack');
  99. builder.config.cflags.push('-DHAS_ETH_INTERFACE=1');
  100. } else if (builder.config.systemName === 'darwin') {
  101. builder.config.cflags.push('-DHAS_ETH_INTERFACE=1');
  102. }
  103. if (process.env['NO_PIE'] === undefined && builder.config.systemName !== 'freebsd'
  104. && builder.config.systemName !== 'win32') {
  105. builder.config.cflags.push('-fPIE');
  106. // just using `-pie` on OS X >= 10.10 results in this warning:
  107. // clang: warning: argument unused during compilation: '-pie'
  108. if (builder.config.systemName !== "darwin") {
  109. builder.config.ldflags.push('-pie');
  110. } else {
  111. builder.config.ldflags.push('-Wl,-pie');
  112. }
  113. }
  114. if (builder.compilerType().isClang) {
  115. // blows up when preprocessing before js preprocessor
  116. builder.config.cflags.push(
  117. '-Wno-invalid-pp-token',
  118. '-Wno-dollar-in-identifier-extension',
  119. '-Wno-newline-eof',
  120. '-Wno-unused-value',
  121. // lots of places where depending on preprocessor conditions, a statement might be
  122. // a case of if (1 == 1)
  123. '-Wno-tautological-compare',
  124. //'-Wno-error'
  125. '-Wno-gnu-line-marker'
  126. );
  127. } else {
  128. builder.config.cflags.push(
  129. '-fdiagnostics-color=always'
  130. );
  131. }
  132. // Install any user-defined CFLAGS. Necessary if you are messing about with building cnacl
  133. // with NEON on the BBB, or want to set -Os (OpenWrt)
  134. // Allow -O0 so while debugging all variables are present.
  135. if (CFLAGS) {
  136. var cflags = CFLAGS.split(' ');
  137. cflags.forEach(function (flag) {
  138. if (/^\-O[^02s]$/.test(flag)) {
  139. console.log("Skipping " + flag + ", assuming " + optimizeLevel + " instead.");
  140. } else if (/^\-O[02s]$/.test(flag)) {
  141. optimizeLevel = flag;
  142. } else {
  143. builder.config.cflags.push(flag);
  144. }
  145. });
  146. }
  147. builder.config.cflags.push(optimizeLevel);
  148. if (!/^\-O0$/.test(optimizeLevel)) {
  149. builder.config.cflags.push('-D_FORTIFY_SOURCE=2');
  150. }
  151. // We also need to pass various architecture/floating point flags to GCC when invoked as
  152. // a linker.
  153. if (LDFLAGS) {
  154. [].push.apply(builder.config.ldflags, LDFLAGS.split(' '));
  155. }
  156. if (android) {
  157. // NDK uses the word `android` in places
  158. builder.config.cflags.push('-DCjdns_android=1');
  159. }
  160. var uclibc = process.env['UCLIBC'] == '1';
  161. var libssp;
  162. switch (process.env['SSP_SUPPORT']) {
  163. case 'y':
  164. case '1': libssp = true; break;
  165. case 'n':
  166. case '':
  167. case '0': libssp = false; break;
  168. case undefined: break;
  169. default: throw new Error();
  170. }
  171. if (libssp === false) {
  172. console.log("Stack Smashing Protection (security feature) is disabled");
  173. } else if (builder.config.systemName == 'win32') {
  174. builder.config.libs.push('-lssp');
  175. } else if ((!uclibc && builder.config.systemName !== 'sunos') || libssp === true) {
  176. builder.config.cflags.push(
  177. // Broken GCC patch makes -fstack-protector-all not work
  178. // workaround is to give -fno-stack-protector first.
  179. // see: https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/691722
  180. '-fno-stack-protector',
  181. '-fstack-protector-all',
  182. '-Wstack-protector'
  183. );
  184. // Static libssp provides __stack_chk_fail_local, which x86 needs in
  185. // order to avoid expensively looking up the location of __stack_chk_fail.
  186. var x86 = process.env['TARGET_ARCH'] == 'i386';
  187. if (uclibc) {
  188. if (x86) {
  189. builder.config.libs.push('-Wl,-Bstatic', '-lssp', '-Wl,-Bdynamic');
  190. } else {
  191. builder.config.libs.push('-lssp');
  192. }
  193. }
  194. } else {
  195. console.log("Stack Smashing Protection (security feature) is disabled");
  196. }
  197. if (process.env['Pipe_PREFIX']) {
  198. builder.config.cflags.push(
  199. '-D', 'Pipe_PREFIX="' + process.env['Pipe_PREFIX'] + '"'
  200. );
  201. }
  202. if (typeof (builder.config.cjdnsTest_files) === 'undefined') {
  203. CjdnsTest.generate(builder, process.env['SUBNODE'] !== '', waitFor());
  204. }
  205. nThen((w) => {
  206. if (builder.config.version) { return; }
  207. GetVersion(w(function (err, data) {
  208. if (!err) {
  209. builder.config.version = ('' + data).replace(/(\r\n|\n|\r)/gm, "");
  210. } else {
  211. builder.config.version = 'unknown';
  212. }
  213. }));
  214. }).nThen((w) => {
  215. builder.config.cflags.push('-D', 'CJD_PACKAGE_VERSION="' + builder.config.version + '"');
  216. }).nThen(waitFor());
  217. // Build dependencies
  218. let foundSodium = false;
  219. nThen(function (waitFor) {
  220. const dir = `${builder.config.buildDir}/../..`;
  221. Fs.readdir(dir, waitFor((err, ret) => {
  222. if (err) { throw err; }
  223. ret.forEach((f) => {
  224. if (!/^libsodium-sys-/.test(f)) { return; }
  225. const inclPath = `${dir}/${f}/out/source/libsodium/src/libsodium/include`;
  226. Fs.readdir(inclPath, waitFor((err, ret) => {
  227. if (foundSodium) { return; }
  228. if (err && err.code === 'ENOENT') { return; }
  229. if (err) { throw err; }
  230. builder.config.includeDirs.push(inclPath);
  231. foundSodium = true;
  232. }));
  233. });
  234. }));
  235. }).nThen(function (waitFor) {
  236. if (!foundSodium) {
  237. throw new Error("Unable to find a path to libsodium headers");
  238. }
  239. if (!android) {
  240. builder.config.libs.push('-lpthread');
  241. }
  242. if (builder.config.systemName === 'win32') {
  243. builder.config.libs.push(
  244. '-lws2_32',
  245. '-lpsapi', // GetProcessMemoryInfo()
  246. '-liphlpapi' // GetAdapterAddresses()
  247. );
  248. } else if (builder.config.systemName === 'linux' && !android) {
  249. builder.config.libs.push('-lrt'); // clock_gettime()
  250. } else if (builder.config.systemName === 'darwin') {
  251. builder.config.libs.push('-framework', 'CoreServices');
  252. } else if (['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) >= 0) {
  253. builder.config.cflags.push('-Wno-overlength-strings');
  254. builder.config.libs.push('-lkvm');
  255. } else if (builder.config.systemName === 'sunos') {
  256. builder.config.libs.push(
  257. '-lsocket',
  258. '-lsendfile',
  259. '-lkstat',
  260. '-lnsl'
  261. );
  262. }
  263. builder.config.includeDirs.push('node_build/dependencies/libuv/include/');
  264. builder.config.includeDirs.push('node_build/dependencies/libuv/src/');
  265. }).nThen(waitFor());
  266. }).build(function (builder, waitFor) {
  267. builder.buildLibrary('client/cjdroute2.c');
  268. builder.buildLibrary('contrib/c/publictoip6.c');
  269. builder.buildLibrary('contrib/c/privatetopublic.c');
  270. builder.buildLibrary('contrib/c/makekeys.c');
  271. builder.buildLibrary('contrib/c/mkpasswd.c');
  272. builder.buildLibrary('crypto/random/randombytes.c');
  273. builder.buildLibrary('rust/cjdns_sys/cffi.h');
  274. builder.buildLibrary('test/testcjdroute.c');
  275. }).failure(function (builder, waitFor) {
  276. console.log('\x1b[1;31mFailed to build cjdns.\x1b[0m');
  277. process.exit(1);
  278. });