Matt Godbolt | 895bfd5 | 2019-05-30 16:33:07 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018, Compiler Explorer Authors |
Partouf | 5c17178 | 2018-10-05 16:26:41 +0200 | [diff] [blame] | 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer in the |
| 11 | // documentation and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | // POSSIBILITY OF SUCH DAMAGE. |
| 24 | |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 25 | const |
| 26 | BaseCompiler = require('../base-compiler'), |
| 27 | fs = require('fs-extra'), |
Partouf | 4225852 | 2018-10-06 23:19:48 +0200 | [diff] [blame] | 28 | path = require('path'), |
| 29 | utils = require('../utils'); |
Partouf | 5c17178 | 2018-10-05 16:26:41 +0200 | [diff] [blame] | 30 | |
| 31 | class CleanCompiler extends BaseCompiler { |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 32 | optionsForFilter(filters) { |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 33 | if (filters.binary) { |
Partouf | f7c0e75 | 2018-10-06 18:09:01 +0200 | [diff] [blame] | 34 | return []; |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 35 | } else { |
| 36 | return ['-S']; |
| 37 | } |
Partouf | 6a769c1 | 2018-10-06 13:58:59 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 40 | getOutputFilename(dirPath) { |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 41 | return path.join(dirPath, "Clean System Files/example.s"); |
Partouf | 6a769c1 | 2018-10-06 13:58:59 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 44 | preprocessOutput(output) { |
Partouf | 4978fc2 | 2018-10-14 02:20:18 +0200 | [diff] [blame] | 45 | const errorRegex = /^Error \[.*,(\d*),(.*)\]:\s?(.*)/i; |
| 46 | const errorLineRegex = /^Error \[.*,(\d*)\]:\s?(.*)/i; |
| 47 | const parseerrorRegex = /^Parse error \[.*,(\d*);(\d*),(.*)\]:\s?(.*)/i; |
| 48 | const typeeerrorRegex = /^Type error \[.*,(\d*),(.*)\]:\s?(.*)/i; |
Partouf | 4225852 | 2018-10-06 23:19:48 +0200 | [diff] [blame] | 49 | return utils.splitLines(output).map(line => { |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 50 | let matches = line.match(errorRegex); |
Partouf | 4938814 | 2018-10-14 02:14:22 +0200 | [diff] [blame] | 51 | if (!matches) matches = line.match(typeeerrorRegex); |
| 52 | |
| 53 | if (matches) { |
| 54 | return "<source>:" + matches[1] + ",0: error: (" + matches[2] + ") " + matches[3]; |
| 55 | } |
| 56 | |
| 57 | matches = line.match(errorLineRegex); |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 58 | if (matches) { |
Partouf | 8e47608 | 2018-10-14 02:04:03 +0200 | [diff] [blame] | 59 | return "<source>:" + matches[1] + ",0: error: " + matches[2]; |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 60 | } |
Partouf | 4938814 | 2018-10-14 02:14:22 +0200 | [diff] [blame] | 61 | |
| 62 | matches = line.match(parseerrorRegex); |
| 63 | if (matches) { |
Partouf | 4978fc2 | 2018-10-14 02:20:18 +0200 | [diff] [blame] | 64 | if (matches[3] === "") { |
| 65 | return "<source>:" + matches[1] + "," + matches[2] + ": error: " + matches[4]; |
| 66 | } else { |
| 67 | return "<source>:" + matches[1] + "," + matches[2] + ": error: (" + matches[3] + ") " + matches[4]; |
| 68 | } |
Partouf | 4938814 | 2018-10-14 02:14:22 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return line; |
Partouf | 4225852 | 2018-10-06 23:19:48 +0200 | [diff] [blame] | 72 | }).join("\n"); |
Partouf | ad1932f | 2018-10-06 17:16:39 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Partouf | 6a769c1 | 2018-10-06 13:58:59 +0200 | [diff] [blame] | 75 | runCompiler(compiler, options, inputFilename, execOptions) { |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 76 | const tmpDir = path.dirname(inputFilename); |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 77 | const moduleName = path.basename(inputFilename, '.icl'); |
| 78 | const compilerPath = path.dirname(compiler); |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 79 | execOptions = this.getDefaultExecOptions(); |
| 80 | execOptions.customCwd = tmpDir; |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 81 | execOptions.env.CLEANLIB = path.join(compilerPath, "../exe"); |
| 82 | execOptions.env.CLEANPATH = this.compiler.libPath; |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 83 | options.pop(); |
| 84 | options.push(moduleName); |
Partouf | 4225852 | 2018-10-06 23:19:48 +0200 | [diff] [blame] | 85 | |
| 86 | return this.exec(compiler, options, execOptions).then(result => { |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 87 | return new Promise((resolve) => { |
Partouf | 4225852 | 2018-10-06 23:19:48 +0200 | [diff] [blame] | 88 | result.inputFilename = inputFilename; |
| 89 | result.stdout = utils.parseOutput(this.preprocessOutput(result.stdout), inputFilename); |
| 90 | result.stderr = utils.parseOutput(this.preprocessOutput(result.stderr), inputFilename); |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 91 | if (!options.includes("-S")) { |
| 92 | const aout = path.join(tmpDir, "a.out"); |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 93 | fs.stat(aout).then(() => { |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 94 | fs.copyFile(aout, this.getOutputFilename(tmpDir)).then(() => { |
| 95 | result.code = 0; |
| 96 | resolve(result); |
| 97 | }); |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 98 | }).catch(() => { |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 99 | result.code = 1; |
| 100 | resolve(result); |
| 101 | }); |
| 102 | } else { |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 103 | fs.stat(this.getOutputFilename(tmpDir)).then(() => { |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 104 | result.code = 0; |
| 105 | resolve(result); |
Partouf | d700dfb | 2018-10-06 23:13:03 +0200 | [diff] [blame] | 106 | }).catch(() => { |
Partouf | b766e08 | 2018-10-06 18:07:11 +0200 | [diff] [blame] | 107 | resolve(result); |
| 108 | }); |
| 109 | } |
| 110 | }); |
Partouf | ad542bc | 2018-10-06 14:13:04 +0200 | [diff] [blame] | 111 | }); |
Partouf | 5c17178 | 2018-10-05 16:26:41 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | module.exports = CleanCompiler; |