make.js 11 KB

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