make.js 15 KB

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