1
0

CanCompile.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. 'use strict';
  16. var nThen = require("nthen");
  17. var Fs = require("fs");
  18. module.exports.check = function (builder, code, cflags, callback) {
  19. var file = builder.tmpFile();
  20. var outputFile = builder.tmpFile();
  21. nThen(function (waitFor) {
  22. Fs.writeFile(file, code, waitFor(function (err, ret) {
  23. if (err) {
  24. waitFor.abort();
  25. callback(err);
  26. }
  27. }));
  28. }).nThen(function (waitFor) {
  29. var flags = [];
  30. flags.push.apply(flags, cflags);
  31. flags.push.apply(flags, ["-o", outputFile, file]);
  32. builder.cc([cflags, "-o", outputFile, file], waitFor(function (ret, out, err) {
  33. [file, outputFile].forEach(function(tmpFile) {
  34. Fs.exists(tmpFile, waitFor(function (exists) {
  35. if (!exists) { return; }
  36. Fs.unlink(tmpFile, waitFor(function (err) {
  37. if (err) { throw err; }
  38. }));
  39. }));
  40. });
  41. if (ret) {
  42. callback(err, false);
  43. } else {
  44. callback(undefined, true);
  45. }
  46. }));
  47. });
  48. };