make.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. var Fs = require('fs');
  16. var nThen = require('nthen');
  17. var Codestyle = require('./Codestyle');
  18. var Cp = require('./Cp');
  19. var Spawn = require('child_process').spawn;
  20. var Os = require('os');
  21. var FindPython2 = require('./FindPython2');
  22. var CanCompile = require('./CanCompile');
  23. var Builder = require('./builder');
  24. var TestRunner = require('./TestRunner');
  25. // ['linux','darwin','sunos','win32','freebsd','openbsd','netbsd']
  26. var SYSTEM = process.env['SYSTEM'] || process.platform;
  27. var GCC = process.env['CC'];
  28. var CFLAGS = process.env['CFLAGS'];
  29. var LDFLAGS = process.env['LDFLAGS'];
  30. var NO_MARCH_FLAG = ['ppc', 'ppc64'];
  31. if (GCC) {
  32. // Already specified.
  33. } else if (SYSTEM === 'openbsd') {
  34. GCC = 'egcc';
  35. } else if (SYSTEM === 'freebsd') {
  36. GCC = 'clang';
  37. } else {
  38. GCC = 'gcc';
  39. }
  40. Builder.configure({
  41. systemName: SYSTEM,
  42. crossCompiling: process.env['CROSS'] !== undefined,
  43. gcc: GCC,
  44. tempDir: process.env['CJDNS_BUILD_TMPDIR'] || '/tmp',
  45. optimizeLevel: '-O3',
  46. logLevel: process.env['Log_LEVEL'] || 'DEBUG'
  47. }, function (builder, waitFor) {
  48. builder.config.cflags.push(
  49. '-std=c99',
  50. '-Wall',
  51. '-Wextra',
  52. '-Werror',
  53. '-Wno-pointer-sign',
  54. '-pedantic',
  55. '-D', builder.config.systemName + '=1',
  56. '-D', 'CJD_PACKAGE_VERSION="' + builder.config.version + '"',
  57. '-Wno-unused-parameter',
  58. // '-fomit-frame-pointer',
  59. '-D', 'Log_' + builder.config.logLevel,
  60. '-g',
  61. // f4 = 16 peers max, fixed width 4 bit
  62. // f8 = 241 peers max, fixed width 8 bit
  63. // v3x5x8 = 256 peers max, variable width, 3, 5 or 8 bits plus 1 or 2 bits of prefix
  64. // v4x8 = 256 peers max, variable width, 4, or 8 bits plus 1 bit prefix
  65. '-D', 'NumberCompress_TYPE=v3x5x8',
  66. // enable for safety (don't worry about speed, profiling shows they add ~nothing)
  67. '-D', 'Identity_CHECK=1',
  68. '-D', 'Allocator_USE_CANARIES=1',
  69. '-D', 'PARANOIA=1'
  70. );
  71. if (process.env['SUBNODE']) { builder.config.cflags.push('-DSUBNODE=1'); }
  72. if (process.env['GCOV']) {
  73. builder.config.cflags.push('-fprofile-arcs', '-ftest-coverage');
  74. builder.config.ldflags.push('-fprofile-arcs', '-ftest-coverage');
  75. }
  76. var android = /android/i.test(builder.config.gcc);
  77. if (process.env['TESTING']) {
  78. builder.config.cflags.push('-D', 'TESTING=1');
  79. }
  80. if (process.env['ADDRESS_PREFIX'] !== undefined) {
  81. builder.config.cflags.push('-D', 'ADDRESS_PREFIX=' + process.env['ADDRESS_PREFIX']);
  82. }
  83. if (process.env['ADDRESS_PREFIX_BITS'] !== undefined) {
  84. builder.config.cflags.push('-D', 'ADDRESS_PREFIX_BITS=' + process.env['ADDRESS_PREFIX_BITS']);
  85. }
  86. if (!builder.config.crossCompiling) {
  87. if (NO_MARCH_FLAG.indexOf(process.arch) == -1) {
  88. builder.config.cflags.push('-march=native');
  89. }
  90. }
  91. if (builder.config.systemName === 'win32') {
  92. builder.config.cflags.push('-Wno-format');
  93. } else if (builder.config.systemName === 'linux') {
  94. builder.config.ldflags.push('-Wl,-z,relro,-z,now,-z,noexecstack');
  95. builder.config.cflags.push('-DHAS_ETH_INTERFACE=1');
  96. } else if (builder.config.systemName === 'darwin') {
  97. builder.config.cflags.push('-DHAS_ETH_INTERFACE=1');
  98. }
  99. if (process.env['NO_PIE'] === undefined && builder.config.systemName !== 'freebsd'
  100. && builder.config.systemName !== 'win32')
  101. {
  102. builder.config.cflags.push('-fPIE');
  103. // just using `-pie` on OS X >= 10.10 results in this warning:
  104. // clang: warning: argument unused during compilation: '-pie'
  105. if (builder.config.systemName !== "darwin")
  106. {
  107. builder.config.ldflags.push('-pie');
  108. } else {
  109. builder.config.ldflags.push('-Wl,-pie');
  110. }
  111. }
  112. if (builder.config.compilerType.isClang) {
  113. // blows up when preprocessing before js preprocessor
  114. builder.config.cflags.push(
  115. '-Wno-invalid-pp-token',
  116. '-Wno-dollar-in-identifier-extension',
  117. '-Wno-newline-eof',
  118. '-Wno-unused-value',
  119. // lots of places where depending on preprocessor conditions, a statement might be
  120. // a case of if (1 == 1)
  121. '-Wno-tautological-compare',
  122. '-Wno-error'
  123. );
  124. builder.config.cflags.slice(builder.config.cflags.indexOf('-Werror'), 1);
  125. }
  126. // Install any user-defined CFLAGS. Necessary if you are messing about with building cnacl
  127. // with NEON on the BBB, or want to set -Os (OpenWrt)
  128. // Allow -O0 so while debugging all variables are present.
  129. if (CFLAGS) {
  130. var cflags = CFLAGS.split(' ');
  131. cflags.forEach(function(flag) {
  132. if (/^\-O[^02s]$/.test(flag)) {
  133. console.log("Skipping " + flag + ", assuming " +
  134. builder.config.optimizeLevel + " instead.");
  135. } else if (/^\-O[02s]$/.test(flag)) {
  136. builder.config.optimizeLevel = flag;
  137. } else {
  138. [].push.apply(builder.config.cflags, cflags);
  139. }
  140. });
  141. }
  142. if (!/^\-O0$/.test(builder.config.optimizeLevel)) {
  143. builder.config.cflags.push('-D_FORTIFY_SOURCE=2');
  144. }
  145. // We also need to pass various architecture/floating point flags to GCC when invoked as
  146. // a linker.
  147. if (LDFLAGS) {
  148. [].push.apply(builder.config.ldflags, LDFLAGS.split(' '));
  149. }
  150. if (android) {
  151. builder.config.cflags.push('-Dandroid=1');
  152. }
  153. CanCompile.check(builder,
  154. 'int main() { return 0; }',
  155. [ builder.config.cflags, '-flto', '-x', 'c' ],
  156. function (err, can) {
  157. if (can) {
  158. console.log("Compiler supports link time optimization");
  159. builder.config.ldflags.push(
  160. '-flto',
  161. builder.config.optimizeLevel
  162. );
  163. } else {
  164. console.log("Link time optimization not supported [" + err + "]");
  165. }
  166. builder.config.cflags.push(builder.config.optimizeLevel);
  167. });
  168. var uclibc = process.env['UCLIBC'] == '1';
  169. var libssp;
  170. switch (process.env['SSP_SUPPORT']) {
  171. case 'y':
  172. case '1': libssp = true; break;
  173. case 'n':
  174. case '' :
  175. case '0': libssp = false; break;
  176. case undefined: break;
  177. default: throw new Error();
  178. }
  179. if (libssp === false) {
  180. console.log("Stack Smashing Protection (security feature) is disabled");
  181. } else if (builder.config.systemName == 'win32') {
  182. builder.config.libs.push('-lssp');
  183. } else if ((!uclibc && builder.config.systemName !== 'sunos') || libssp === true) {
  184. builder.config.cflags.push(
  185. // Broken GCC patch makes -fstack-protector-all not work
  186. // workaround is to give -fno-stack-protector first.
  187. // see: https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/691722
  188. '-fno-stack-protector',
  189. '-fstack-protector-all',
  190. '-Wstack-protector'
  191. );
  192. // Static libssp provides __stack_chk_fail_local, which x86 needs in
  193. // order to avoid expensively looking up the location of __stack_chk_fail.
  194. var x86 = process.env['TARGET_ARCH'] == 'i386';
  195. if (uclibc) {
  196. if (x86) {
  197. builder.config.libs.push('-Wl,-Bstatic', '-lssp', '-Wl,-Bdynamic');
  198. } else {
  199. builder.config.libs.push('-lssp');
  200. }
  201. }
  202. } else {
  203. console.log("Stack Smashing Protection (security feature) is disabled");
  204. }
  205. if (process.env['Pipe_PREFIX'] !== undefined) {
  206. builder.config.cflags.push(
  207. '-D', 'Pipe_PREFIX="' + process.env['Pipe_PREFIX'] + '"'
  208. );
  209. }
  210. var dependencyDir = builder.config.buildDir + '/dependencies';
  211. var libuvLib = dependencyDir + '/libuv/out/Release/libuv.a';
  212. if (['win32', 'netbsd'].indexOf(builder.config.systemName) >= 0) {//this might be needed for other BSDs
  213. libuvLib = dependencyDir + '/libuv/out/Release/obj.target/libuv.a';
  214. }
  215. // Build dependencies
  216. nThen(function (waitFor) {
  217. Fs.exists(dependencyDir, waitFor(function (exists) {
  218. if (exists) { return; }
  219. console.log("Copy dependencies");
  220. Cp('./node_build/dependencies', dependencyDir, waitFor());
  221. }));
  222. }).nThen(function (waitFor) {
  223. builder.config.libs.push(dependencyDir + '/cnacl/jsbuild/libnacl.a');
  224. builder.config.includeDirs.push(dependencyDir + '/cnacl/jsbuild/include/');
  225. // needed for Sign.c which pulls in crypto_int32.h
  226. builder.config.includeDirs.push(dependencyDir + '/cnacl/jsbuild/include_internal/');
  227. Fs.exists(dependencyDir + '/cnacl/jsbuild/libnacl.a', waitFor(function (exists) {
  228. if (exists) { return; }
  229. console.log("Build NaCl");
  230. var cwd = process.cwd();
  231. process.chdir(dependencyDir + '/cnacl/');
  232. var NaCl = require(process.cwd() + '/node_build/make.js');
  233. NaCl.build(function (args, callback) {
  234. if (builder.config.systemName !== 'win32') {
  235. args.unshift('-fPIC');
  236. }
  237. args.unshift(builder.config.optimizeLevel, '-fomit-frame-pointer');
  238. if (!/^\-O0$/.test(builder.config.optimizeLevel)) {
  239. args.unshift('-D_FORTIFY_SOURCE=2');
  240. }
  241. if (CFLAGS) {
  242. [].push.apply(args, CFLAGS.split(' '));
  243. }
  244. if (!builder.config.crossCompiling) {
  245. if (NO_MARCH_FLAG.indexOf(process.arch) == -1) {
  246. args.unshift('-march=native');
  247. }
  248. }
  249. builder.cc(args, callback);
  250. },
  251. builder.config,
  252. waitFor(function () {
  253. process.chdir(cwd);
  254. }));
  255. }));
  256. }).nThen(function (waitFor) {
  257. builder.config.libs.push(libuvLib);
  258. if (!android) {
  259. builder.config.libs.push('-lpthread');
  260. }
  261. if (builder.config.systemName === 'win32') {
  262. builder.config.libs.push(
  263. '-lws2_32',
  264. '-lpsapi', // GetProcessMemoryInfo()
  265. '-liphlpapi' // GetAdapterAddresses()
  266. );
  267. } else if (builder.config.systemName === 'linux' && !android) {
  268. builder.config.libs.push('-lrt'); // clock_gettime()
  269. } else if (builder.config.systemName === 'darwin') {
  270. builder.config.libs.push('-framework', 'CoreServices');
  271. } else if (['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) >= 0) {
  272. builder.config.cflags.push('-Wno-overlength-strings');
  273. builder.config.libs.push('-lkvm');
  274. } else if (builder.config.systemName === 'sunos') {
  275. builder.config.libs.push(
  276. '-lsocket',
  277. '-lsendfile',
  278. '-lkstat',
  279. '-lnsl'
  280. );
  281. }
  282. builder.config.includeDirs.push(dependencyDir + '/libuv/include/');
  283. var libuvBuilt;
  284. var python;
  285. nThen(function (waitFor) {
  286. Fs.exists(libuvLib, waitFor(function (exists) {
  287. if (exists) { libuvBuilt = true; }
  288. }));
  289. }).nThen(function (waitFor) {
  290. if (libuvBuilt) { return; }
  291. FindPython2.find(builder.tmpFile(), waitFor(function (err, pythonExec) {
  292. if (err) { throw err; }
  293. python = pythonExec;
  294. }));
  295. }).nThen(function (waitFor) {
  296. if (libuvBuilt) { return; }
  297. console.log("Build Libuv");
  298. var cwd = process.cwd();
  299. process.chdir(dependencyDir + '/libuv/');
  300. var args = ['./gyp_uv.py'];
  301. var env = process.env;
  302. env.CC = builder.config.gcc;
  303. if (env.TARGET_ARCH) {
  304. args.push('-Dtarget_arch=' + env.TARGET_ARCH);
  305. }
  306. //args.push('--root-target=libuv');
  307. if (android) {
  308. args.push('-DOS=android');
  309. }
  310. if (builder.config.systemName === 'win32') {
  311. args.push('-DOS=win');
  312. }
  313. if (env.GYP_ADDITIONAL_ARGS) {
  314. args.push.apply(args, env.GYP_ADDITIONAL_ARGS.split(' '));
  315. }
  316. if (['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) !== -1) {
  317. // This platform lacks a functioning sem_open implementation, therefore...
  318. args.push('--no-parallel');
  319. args.push('-DOS=' + builder.config.systemName);
  320. }
  321. var gyp = Spawn(python, args, {env:env, stdio:'inherit'});
  322. gyp.on('error', function () {
  323. console.error("couldn't launch gyp [" + python + "]");
  324. });
  325. gyp.on('close', waitFor(function () {
  326. var args = [
  327. '-j', builder.processors,
  328. '-C', 'out',
  329. 'BUILDTYPE=Release',
  330. 'CC=' + builder.config.gcc,
  331. 'CXX=' + builder.config.gcc,
  332. 'V=1'
  333. ];
  334. var cflags = [builder.config.optimizeLevel, '-DNO_EMFILE_TRICK=1'];
  335. if (!/^\-O0$/.test(builder.config.optimizeLevel)) {
  336. cflags.push('-D_FORTIFY_SOURCE=2');
  337. }
  338. if (!(/darwin|win32/i.test(builder.config.systemName))) {
  339. cflags.push('-fPIC');
  340. }
  341. args.push('CFLAGS=' + cflags.join(' '));
  342. var makeCommand = ['freebsd', 'openbsd', 'netbsd'].indexOf(builder.config.systemName) >= 0 ? 'gmake' : 'make';
  343. var make = Spawn(makeCommand, args, {stdio: 'inherit'});
  344. make.on('error', function (err) {
  345. if (err.code === 'ENOENT') {
  346. console.error('\033[1;31mError: ' + makeCommand + ' is required!\033[0m');
  347. } else {
  348. console.error(
  349. '\033[1;31mFail run ' + process.cwd() + ': ' + makeCommand + ' '
  350. + args.join(' ') + '\033[0m'
  351. );
  352. console.error('Message:', err);
  353. }
  354. waitFor.abort();
  355. });
  356. make.on('close', waitFor(function () {
  357. process.chdir(cwd);
  358. }));
  359. }));
  360. }).nThen(waitFor());
  361. }).nThen(waitFor());
  362. }).build(function (builder, waitFor) {
  363. builder.buildExecutable('client/cjdroute2.c', 'cjdroute');
  364. builder.buildExecutable('contrib/c/publictoip6.c');
  365. builder.buildExecutable('contrib/c/privatetopublic.c');
  366. builder.buildExecutable('contrib/c/sybilsim.c');
  367. builder.buildExecutable('contrib/c/makekeys.c');
  368. builder.buildExecutable('contrib/c/mkpasswd.c');
  369. builder.buildExecutable('crypto/random/randombytes.c');
  370. builder.lintFiles(function (fileName, file, callback) {
  371. if (/dependencies/.test(fileName)) {
  372. callback('', false);
  373. return;
  374. }
  375. Codestyle.lint(fileName, file, callback);
  376. });
  377. var testcjdroute = builder.buildTest('test/testcjdroute.c');
  378. if (builder.config.crossCompiling) {
  379. console.log("Cross compiling. Building, but not running tests.");
  380. return;
  381. }
  382. var testRunner = TestRunner.local(['all']);
  383. if (process.env['REMOTE_TEST']) {
  384. testRunner = TestRunner.remote(process.env['REMOTE_TEST'], ['all']);
  385. }
  386. if (!process.env['NO_TEST']) {
  387. builder.runTest(testcjdroute, testRunner);
  388. }
  389. }).success(function (builder, waitFor) {
  390. console.log('\033[1;32mBuild completed successfully, type ./cjdroute to begin setup.\033[0m');
  391. }).failure(function (builder, waitFor) {
  392. console.log('\033[1;31mFailed to build cjdns.\033[0m');
  393. process.exit(1);
  394. }).complete(function (builder, waitFor) {
  395. if (builder.failure) {
  396. process.exit(1);
  397. }
  398. });