aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-compiler
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2019-01-28 23:24:54 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2019-01-29 00:24:44 +0800
commit0c12128f64f7d9a8de6088e98c2e638533d6f5bf (patch)
treec9770bf483655412a3144a07c219ddaed34ab064 /packages/sol-compiler
parent19064f8cbb21954108c4b8bd4a66879ddb240d7e (diff)
downloaddexon-0x-contracts-0c12128f64f7d9a8de6088e98c2e638533d6f5bf.tar.gz
dexon-0x-contracts-0c12128f64f7d9a8de6088e98c2e638533d6f5bf.tar.zst
dexon-0x-contracts-0c12128f64f7d9a8de6088e98c2e638533d6f5bf.zip
Temp CI
Diffstat (limited to 'packages/sol-compiler')
-rw-r--r--packages/sol-compiler/src/compiler.ts82
-rw-r--r--packages/sol-compiler/src/schemas/compiler_options_schema.ts1
-rw-r--r--packages/sol-compiler/src/utils/compiler.ts109
3 files changed, 86 insertions, 106 deletions
diff --git a/packages/sol-compiler/src/compiler.ts b/packages/sol-compiler/src/compiler.ts
index 856bcbd48..b5cdf9865 100644
--- a/packages/sol-compiler/src/compiler.ts
+++ b/packages/sol-compiler/src/compiler.ts
@@ -28,10 +28,13 @@ import {
compileSolcJS,
createDirIfDoesNotExistAsync,
getContractArtifactIfExistsAsync,
+ getDependencyNameToPackagePath,
getSolcJSAsync,
getSourcesWithDependencies,
getSourceTreeHash,
+ makeContractPathsRelative,
parseSolidityVersionRange,
+ printCompilationErrorsAndWarnings,
} from './utils/compiler';
import { constants } from './utils/constants';
import { fsWrapper } from './utils/fs_wrapper';
@@ -83,7 +86,6 @@ export class Compiler {
private readonly _resolver: Resolver;
private readonly _nameResolver: NameResolver;
private readonly _contractsDir: string;
- private readonly _workspaceDir: string;
private readonly _compilerSettings: solc.CompilerSettings;
private readonly _artifactsDir: string;
private readonly _solcVersionIfExists: string | undefined;
@@ -103,12 +105,6 @@ export class Compiler {
const passedOpts = opts || {};
assert.doesConformToSchema('compiler.json', config, compilerOptionsSchema);
this._contractsDir = path.resolve(passedOpts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR);
- this._workspaceDir = path.resolve(passedOpts.workspaceDir || config.workspaceDir || this._contractsDir);
- if (!this._contractsDir.includes(this._workspaceDir)) {
- throw new Error(
- `Contracts dir ${this._contractsDir} is outside of the workspace dir ${this._workspaceDir}`,
- );
- }
this._solcVersionIfExists = passedOpts.solcVersion || config.solcVersion;
this._compilerSettings = passedOpts.compilerSettings || config.compilerSettings || DEFAULT_COMPILER_SETTINGS;
this._artifactsDir = passedOpts.artifactsDir || config.artifactsDir || DEFAULT_ARTIFACTS_DIR;
@@ -118,7 +114,7 @@ export class Compiler {
this._nameResolver = new NameResolver(this._contractsDir);
const resolver = new FallthroughResolver();
resolver.appendResolver(new URLResolver());
- resolver.appendResolver(new NPMResolver(this._contractsDir, this._workspaceDir));
+ resolver.appendResolver(new NPMResolver(this._contractsDir));
resolver.appendResolver(new RelativeFSResolver(this._contractsDir));
resolver.appendResolver(new FSResolver());
resolver.appendResolver(this._nameResolver);
@@ -217,9 +213,10 @@ export class Compiler {
// map contract paths to data about them for later verification and persistence
const contractPathToData: ContractPathToData = {};
- const spyResolver = new SpyResolver(this._resolver);
+ const resolvedContractSources = [];
for (const contractName of contractNames) {
+ const spyResolver = new SpyResolver(this._resolver);
const contractSource = spyResolver.resolve(contractName);
const sourceTreeHashHex = getSourceTreeHash(
spyResolver,
@@ -249,40 +246,19 @@ export class Compiler {
};
}
// add input to the right version batch
- versionToInputs[solcVersion].standardInput.sources[contractSource.path] = {
- content: contractSource.source,
- };
+ for (const resolvedContractSource of spyResolver.resolvedContractSources) {
+ versionToInputs[solcVersion].standardInput.sources[resolvedContractSource.absolutePath] = {
+ content: resolvedContractSource.source,
+ };
+ }
+ resolvedContractSources.push(...spyResolver.resolvedContractSources);
versionToInputs[solcVersion].contractsToCompile.push(contractSource.path);
}
- const allTouchedFiles = spyResolver.resolvedContractSources.map(
- contractSource => `${contractSource.absolutePath}`,
- );
- const NODE_MODULES = 'node_modules';
- const allTouchedDependencies = _.filter(allTouchedFiles, filePath => filePath.includes(NODE_MODULES));
- const dependencyNameToPackagePath: { [dependencyName: string]: string } = {};
- _.map(allTouchedDependencies, dependencyFilePath => {
- const lastNodeModulesStart = dependencyFilePath.lastIndexOf(NODE_MODULES);
- const lastNodeModulesEnd = lastNodeModulesStart + NODE_MODULES.length;
- const importPath = dependencyFilePath.substr(lastNodeModulesEnd + 1);
- let packageName;
- let packageScopeIfExists;
- let dependencyName;
- if (_.startsWith(importPath, '@')) {
- [packageScopeIfExists, packageName] = importPath.split('/');
- dependencyName = `${packageScopeIfExists}/${packageName}`;
- } else {
- [packageName] = importPath.split('/');
- dependencyName = `${packageName}`;
- }
- const dependencyPackagePath = path.join(dependencyFilePath.substr(0, lastNodeModulesEnd), dependencyName);
- dependencyNameToPackagePath[dependencyName] = dependencyPackagePath;
- });
+ const dependencyNameToPackagePath = getDependencyNameToPackagePath(resolvedContractSources);
const compilerOutputs: StandardOutput[] = [];
-
- const solcVersions = _.keys(versionToInputs);
- for (const solcVersion of solcVersions) {
+ for (const solcVersion of _.keys(versionToInputs)) {
const input = versionToInputs[solcVersion];
logUtils.warn(
`Compiling ${input.contractsToCompile.length} contracts (${
@@ -291,29 +267,37 @@ export class Compiler {
);
let compilerOutput;
let fullSolcVersion;
+ input.standardInput.settings.remappings = _.map(
+ dependencyNameToPackagePath,
+ (dependencyPackagePath: string, dependencyName: string) => `${dependencyName}=${dependencyPackagePath}`,
+ );
if (this._useDockerisedSolc) {
const dockerCommand = `docker run ethereum/solc:${solcVersion} --version`;
const versionCommandOutput = execSync(dockerCommand).toString();
const versionCommandOutputParts = versionCommandOutput.split(' ');
fullSolcVersion = versionCommandOutputParts[versionCommandOutputParts.length - 1].trim();
- compilerOutput = compileDocker(
- this._resolver,
- this._contractsDir,
- this._workspaceDir,
- solcVersion,
- dependencyNameToPackagePath,
- input.standardInput,
- );
+ compilerOutput = compileDocker(solcVersion, input.standardInput);
} else {
fullSolcVersion = binPaths[solcVersion];
- const solcInstance = await getSolcJSAsync(solcVersion);
- compilerOutput = compileSolcJS(this._resolver, solcInstance, input.standardInput);
+ compilerOutput = compileSolcJS(solcVersion, input.standardInput);
+ }
+ compilerOutput.sources = makeContractPathsRelative(
+ compilerOutput.sources,
+ this._contractsDir,
+ dependencyNameToPackagePath,
+ );
+ compilerOutput.contracts = makeContractPathsRelative(
+ compilerOutput.contracts,
+ this._contractsDir,
+ dependencyNameToPackagePath,
+ );
+ if (!_.isUndefined(compilerOutput.errors)) {
+ printCompilationErrorsAndWarnings(compilerOutput.errors);
}
compilerOutputs.push(compilerOutput);
for (const contractPath of input.contractsToCompile) {
- // console.log('contractsPath', contractPath);
const contractName = contractPathToData[contractPath].contractName;
const compiledContract = compilerOutput.contracts[contractPath][contractName];
diff --git a/packages/sol-compiler/src/schemas/compiler_options_schema.ts b/packages/sol-compiler/src/schemas/compiler_options_schema.ts
index 657b801ad..c0766b625 100644
--- a/packages/sol-compiler/src/schemas/compiler_options_schema.ts
+++ b/packages/sol-compiler/src/schemas/compiler_options_schema.ts
@@ -2,7 +2,6 @@ export const compilerOptionsSchema = {
id: '/CompilerOptions',
properties: {
contractsDir: { type: 'string' },
- workspaceDir: { type: 'string' },
artifactsDir: { type: 'string' },
solcVersion: { type: 'string', pattern: '^\\d+.\\d+.\\d+$' },
compilerSettings: { type: 'object' },
diff --git a/packages/sol-compiler/src/utils/compiler.ts b/packages/sol-compiler/src/utils/compiler.ts
index 669bdd7a4..6381e525f 100644
--- a/packages/sol-compiler/src/utils/compiler.ts
+++ b/packages/sol-compiler/src/utils/compiler.ts
@@ -118,76 +118,35 @@ export function parseDependencies(contractSource: ContractSource): string[] {
/**
* Compiles the contracts and prints errors/warnings
- * @param resolver Resolver
- * @param solcInstance Instance of a solc compiler
+ * @param solcVersion Version of a solc compiler
* @param standardInput Solidity standard JSON input
*/
-export function compileSolcJS(
- resolver: Resolver,
- solcInstance: solc.SolcInstance,
+export async function compileSolcJSAsync(
+ solcVersion: string,
standardInput: solc.StandardInput,
-): solc.StandardOutput {
+): Promise<solc.StandardOutput> {
+ const solcInstance = await getSolcJSAsync(solcVersion);
const standardInputStr = JSON.stringify(standardInput);
- const standardOutputStr = solcInstance.compileStandardWrapper(standardInputStr, importPath => {
- const sourceCodeIfExists = resolver.resolve(importPath);
- return { contents: sourceCodeIfExists.source };
- });
+ const standardOutputStr = solcInstance.compileStandardWrapper(standardInputStr);
const compiled: solc.StandardOutput = JSON.parse(standardOutputStr);
- if (!_.isUndefined(compiled.errors)) {
- printCompilationErrorsAndWarnings(compiled.errors);
- }
return compiled;
}
/**
* Compiles the contracts and prints errors/warnings
- * @param resolver Resolver
- * @param contractsDir Contracts directory
- * @param workspaceDir Workspace directory
* @param solcVersion Version of a solc compiler
- * @param dependencyNameToPackagePath Mapping of dependency name to it's package path
* @param standardInput Solidity standard JSON input
*/
-export function compileDocker(
- resolver: Resolver,
- contractsDir: string,
- workspaceDir: string,
+export async function compileDockerAsync(
solcVersion: string,
- dependencyNameToPackagePath: { [dependencyName: string]: string },
standardInput: solc.StandardInput,
-): solc.StandardOutput {
- const standardInputDocker = _.cloneDeep(standardInput);
- standardInputDocker.settings.remappings = _.map(
- dependencyNameToPackagePath,
- (dependencyPackagePath: string, dependencyName: string) => `${dependencyName}=${dependencyPackagePath}`,
- );
- standardInputDocker.sources = _.mapKeys(
- standardInputDocker.sources,
- (_source: solc.Source, sourcePath: string) => resolver.resolve(sourcePath).absolutePath,
- );
-
- const standardInputStrDocker = JSON.stringify(standardInputDocker, null, 2);
+): Promise<solc.StandardOutput> {
+ const standardInputStr = JSON.stringify(standardInput, null, 2);
const dockerCommand =
- `docker run -i -a stdin -a stdout -a stderr -v ${workspaceDir}:${workspaceDir} ethereum/solc:${solcVersion} ` +
- `solc --standard-json --allow-paths ${workspaceDir}`;
- const standardOutputStrDocker = execSync(dockerCommand, { input: standardInputStrDocker }).toString();
- const compiledDocker: solc.StandardOutput = JSON.parse(standardOutputStrDocker);
-
- if (!_.isUndefined(compiledDocker.errors)) {
- printCompilationErrorsAndWarnings(compiledDocker.errors);
- }
-
- compiledDocker.sources = makeContractPathsRelative(
- compiledDocker.sources,
- contractsDir,
- dependencyNameToPackagePath,
- );
- compiledDocker.contracts = makeContractPathsRelative(
- compiledDocker.contracts,
- contractsDir,
- dependencyNameToPackagePath,
- );
- return compiledDocker;
+ `docker run -i -a stdin -a stdout -a stderr ethereum/solc:${solcVersion} ` + `solc --standard-json`;
+ const standardOutputStr = execSync(dockerCommand, { input: standardInputStr }).toString();
+ const compiled: solc.StandardOutput = JSON.parse(standardOutputStr);
+ return compiled;
}
function makeContractPathRelative(
@@ -202,7 +161,13 @@ function makeContractPathRelative(
return contractPath;
}
-function makeContractPathsRelative(
+/**
+ * Makes the path relative removing all system-dependent data. Converts absolute paths to a format suitable for artifacts.
+ * @param absolutePathToSmth Absolute path to contract or source
+ * @param contractsDir Current package contracts directory location
+ * @param dependencyNameToPackagePath Mapping of dependency name to package path
+ */
+export function makeContractPathsRelative(
absolutePathToSmth: { [absoluteContractPath: string]: any },
contractsDir: string,
dependencyNameToPackagePath: { [dependencyName: string]: string },
@@ -216,7 +181,7 @@ function makeContractPathsRelative(
* Separates errors from warnings, formats the messages and prints them. Throws if there is any compilation error (not warning).
* @param solcErrors The errors field of standard JSON output that contains errors and warnings.
*/
-function printCompilationErrorsAndWarnings(solcErrors: solc.SolcError[]): void {
+export function printCompilationErrorsAndWarnings(solcErrors: solc.SolcError[]): void {
const SOLIDITY_WARNING = 'warning';
const errors = _.filter(solcErrors, entry => entry.severity !== SOLIDITY_WARNING);
const warnings = _.filter(solcErrors, entry => entry.severity === SOLIDITY_WARNING);
@@ -392,3 +357,35 @@ export function addHexPrefixToContractBytecode(compiledContract: solc.StandardCo
}
}
}
+
+/**
+ * Takes the list of resolved contract sources from `SpyResolver` and produces a mapping from dependency name
+ * to package path used in `remappings` later.
+ * @param contractSources The list of resolved contract sources
+ */
+export function getDependencyNameToPackagePath(
+ contractSources: ContractSource[],
+): { [dependencyName: string]: string } {
+ const allTouchedFiles = contractSources.map(contractSource => `${contractSource.absolutePath}`);
+ const NODE_MODULES = 'node_modules';
+ const allTouchedDependencies = _.filter(allTouchedFiles, filePath => filePath.includes(NODE_MODULES));
+ const dependencyNameToPackagePath: { [dependencyName: string]: string } = {};
+ _.map(allTouchedDependencies, dependencyFilePath => {
+ const lastNodeModulesStart = dependencyFilePath.lastIndexOf(NODE_MODULES);
+ const lastNodeModulesEnd = lastNodeModulesStart + NODE_MODULES.length;
+ const importPath = dependencyFilePath.substr(lastNodeModulesEnd + 1);
+ let packageName;
+ let packageScopeIfExists;
+ let dependencyName;
+ if (_.startsWith(importPath, '@')) {
+ [packageScopeIfExists, packageName] = importPath.split('/');
+ dependencyName = `${packageScopeIfExists}/${packageName}`;
+ } else {
+ [packageName] = importPath.split('/');
+ dependencyName = `${packageName}`;
+ }
+ const dependencyPackagePath = path.join(dependencyFilePath.substr(0, lastNodeModulesEnd), dependencyName);
+ dependencyNameToPackagePath[dependencyName] = dependencyPackagePath;
+ });
+ return dependencyNameToPackagePath;
+}