-
Notifications
You must be signed in to change notification settings - Fork 475
/
solc.ts
executable file
·258 lines (220 loc) · 8.73 KB
/
solc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env node
import * as commander from 'commander';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import solc from './index';
import smtchecker from './smtchecker';
import smtsolver from './smtsolver';
// hold on to any exception handlers that existed prior to this script running, we'll be adding them back at the end
const originalUncaughtExceptionListeners = process.listeners('uncaughtException');
// FIXME: remove annoying exception catcher of Emscripten
// see https://github.com/chriseth/browser-solidity/issues/167
process.removeAllListeners('uncaughtException');
const program: any = new commander.Command();
const commanderParseInt = function (value) {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a valid integer.');
}
return parsedValue;
};
program.name('solcjs');
program.version(solc.version());
program
.option('--version', 'Show version and exit.')
.option('--optimize', 'Enable bytecode optimizer.', false)
.option(
'--optimize-runs <optimize-runs>',
'The number of runs specifies roughly how often each opcode of the deployed code will be executed across the lifetime of the contract. ' +
'Lower values will optimize more for initial deployment cost, higher values will optimize more for high-frequency usage.',
commanderParseInt
)
.option('--bin', 'Binary of the contracts in hex.')
.option('--abi', 'ABI of the contracts.')
.option('--standard-json', 'Turn on Standard JSON Input / Output mode.')
.option('--base-path <path>', 'Root of the project source tree. ' +
'The import callback will attempt to interpret all import paths as relative to this directory.'
)
.option('--include-path <path...>', 'Extra source directories available to the import callback. ' +
'When using a package manager to install libraries, use this option to specify directories where packages are installed. ' +
'Can be used multiple times to provide multiple locations.'
)
.option('-o, --output-dir <output-directory>', 'Output directory for the contracts.')
.option('-p, --pretty-json', 'Pretty-print all JSON output.', false)
.option('-v, --verbose', 'More detailed console output.', false);
program.parse(process.argv);
const options = program.opts();
const files = program.args;
const destination = options.outputDir || '.';
function abort (msg) {
console.error(msg || 'Error occurred');
process.exit(1);
}
function readFileCallback (sourcePath) {
const prefixes = [options.basePath ? options.basePath : ''].concat(
options.includePath ? options.includePath : []
);
for (const prefix of prefixes) {
const prefixedSourcePath = (prefix ? prefix + '/' : '') + sourcePath;
if (fs.existsSync(prefixedSourcePath)) {
try {
return { contents: fs.readFileSync(prefixedSourcePath).toString('utf8') };
} catch (e) {
return { error: 'Error reading ' + prefixedSourcePath + ': ' + e };
}
}
}
return { error: 'File not found inside the base path or any of the include paths.' };
}
function withUnixPathSeparators (filePath) {
// On UNIX-like systems forward slashes in paths are just a part of the file name.
if (os.platform() !== 'win32') {
return filePath;
}
return filePath.replace(/\\/g, '/');
}
function makeSourcePathRelativeIfPossible (sourcePath) {
const absoluteBasePath = (options.basePath ? path.resolve(options.basePath) : path.resolve('.'));
const absoluteIncludePaths = (
options.includePath
? options.includePath.map((prefix) => { return path.resolve(prefix); })
: []
);
// Compared to base path stripping logic in solc this is much simpler because path.resolve()
// handles symlinks correctly (does not resolve them except in work dir) and strips .. segments
// from paths going beyond root (e.g. `/../../a/b/c` -> `/a/b/c/`). It's simpler also because it
// ignores less important corner cases: drive letters are not stripped from absolute paths on
// Windows and UNC paths are not handled in a special way (at least on Linux). Finally, it has
// very little test coverage so there might be more differences that we are just not aware of.
const absoluteSourcePath = path.resolve(sourcePath);
for (const absolutePrefix of [absoluteBasePath].concat(absoluteIncludePaths)) {
const relativeSourcePath = path.relative(absolutePrefix, absoluteSourcePath);
if (!relativeSourcePath.startsWith('../')) { return withUnixPathSeparators(relativeSourcePath); }
}
// File is not located inside base path or include paths so use its absolute path.
return withUnixPathSeparators(absoluteSourcePath);
}
function toFormattedJson (input) {
return JSON.stringify(input, null, program.prettyJson ? 4 : 0);
}
function reformatJsonIfRequested (inputJson) {
return (program.prettyJson ? toFormattedJson(JSON.parse(inputJson)) : inputJson);
}
let callbacks;
if (options.basePath || !options.standardJson) { callbacks = { import: readFileCallback }; }
if (options.standardJson) {
const input = fs.readFileSync(process.stdin.fd).toString('utf8');
if (program.verbose) { console.log('>>> Compiling:\n' + reformatJsonIfRequested(input) + '\n'); }
let output = reformatJsonIfRequested(solc.compile(input, callbacks));
try {
if (smtsolver.availableSolvers.length === 0) {
console.log('>>> Cannot retry compilation with SMT because there are no SMT solvers available.');
} else {
const inputJSON = smtchecker.handleSMTQueries(JSON.parse(input), JSON.parse(output), smtsolver.smtSolver, smtsolver.availableSolvers[0]);
if (inputJSON) {
if (program.verbose) { console.log('>>> Retrying compilation with SMT:\n' + toFormattedJson(inputJSON) + '\n'); }
output = reformatJsonIfRequested(solc.compile(JSON.stringify(inputJSON), callbacks));
}
}
} catch (e) {
const addError = {
component: 'general',
formattedMessage: e.toString(),
message: e.toString(),
type: 'Warning'
};
const outputJSON = JSON.parse(output);
if (!outputJSON.errors) {
outputJSON.errors = [];
}
outputJSON.errors.push(addError);
output = toFormattedJson(outputJSON);
}
if (program.verbose) { console.log('>>> Compilation result:'); }
console.log(output);
process.exit(0);
} else if (files.length === 0) {
console.error('Must provide a file');
process.exit(1);
}
if (!(options.bin || options.abi)) {
abort('Invalid option selected, must specify either --bin or --abi');
}
if (!options.basePath && options.includePath && options.includePath.length > 0) {
abort('--include-path option requires a non-empty base path.');
}
if (options.includePath) {
for (const includePath of options.includePath) {
if (!includePath) { abort('Empty values are not allowed in --include-path.'); }
}
}
const sources = {};
for (let i = 0; i < files.length; i++) {
try {
sources[makeSourcePathRelativeIfPossible(files[i])] = {
content: fs.readFileSync(files[i]).toString()
};
} catch (e) {
abort('Error reading ' + files[i] + ': ' + e);
}
}
const cliInput = {
language: 'Solidity',
settings: {
optimizer: {
enabled: options.optimize,
runs: options.optimizeRuns
},
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode']
}
}
},
sources: sources
};
if (program.verbose) { console.log('>>> Compiling:\n' + toFormattedJson(cliInput) + '\n'); }
const output = JSON.parse(solc.compile(JSON.stringify(cliInput), callbacks));
let hasError = false;
if (!output) {
abort('No output from compiler');
} else if (output.errors) {
for (const error in output.errors) {
const message = output.errors[error];
if (message.severity === 'warning') {
console.log(message.formattedMessage);
} else {
console.error(message.formattedMessage);
hasError = true;
}
}
}
fs.mkdirSync(destination, { recursive: true });
function writeFile (file, content) {
file = path.join(destination, file);
fs.writeFile(file, content, function (err) {
if (err) {
console.error('Failed to write ' + file + ': ' + err);
}
});
}
for (const fileName in output.contracts) {
for (const contractName in output.contracts[fileName]) {
let contractFileName = fileName + ':' + contractName;
contractFileName = contractFileName.replace(/[:./\\]/g, '_');
if (options.bin) {
writeFile(contractFileName + '.bin', output.contracts[fileName][contractName].evm.bytecode.object);
}
if (options.abi) {
writeFile(contractFileName + '.abi', toFormattedJson(output.contracts[fileName][contractName].abi));
}
}
}
// Put back original exception handlers.
originalUncaughtExceptionListeners.forEach(function (listener) {
process.addListener('uncaughtException', listener);
});
if (hasError) {
process.exit(1);
}