make.js 15 KB

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