diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-02-02 02:58:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-02 02:58:29 +0800 |
commit | ae8d2ae2cb68995f5190d9450480811e512489e6 (patch) | |
tree | 8aaf8a479ce791cde6989749ad88826a83120022 /packages/sol-compiler | |
parent | 78bdc2d6b4aa87e1e973d90ad83ee97ecaf4615e (diff) | |
parent | 79adf5cec69cf397d4e7165ee69b3df9ee84acae (diff) | |
download | dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.tar dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.tar.gz dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.tar.bz2 dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.tar.lz dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.tar.xz dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.tar.zst dexon-0x-contracts-ae8d2ae2cb68995f5190d9450480811e512489e6.zip |
Merge pull request #1542 from 0xProject/feature/solc-docker
Implement docker as another backend for sol-compiler
Diffstat (limited to 'packages/sol-compiler')
-rw-r--r-- | packages/sol-compiler/src/compiler.ts | 75 | ||||
-rw-r--r-- | packages/sol-compiler/src/schemas/compiler_options_schema.ts | 1 | ||||
-rw-r--r-- | packages/sol-compiler/src/utils/compiler.ts | 112 |
3 files changed, 151 insertions, 37 deletions
diff --git a/packages/sol-compiler/src/compiler.ts b/packages/sol-compiler/src/compiler.ts index d38ccbf39..efee3eb8a 100644 --- a/packages/sol-compiler/src/compiler.ts +++ b/packages/sol-compiler/src/compiler.ts @@ -10,6 +10,7 @@ import { URLResolver, } from '@0x/sol-resolver'; import { logUtils } from '@0x/utils'; +import { execSync } from 'child_process'; import * as chokidar from 'chokidar'; import { CompilerOptions, ContractArtifact, ContractVersionData, StandardOutput } from 'ethereum-types'; import * as fs from 'fs'; @@ -23,13 +24,16 @@ import { compilerOptionsSchema } from './schemas/compiler_options_schema'; import { binPaths } from './solc/bin_paths'; import { addHexPrefixToContractBytecode, - compile, + compileDockerAsync, + compileSolcJSAsync, createDirIfDoesNotExistAsync, getContractArtifactIfExistsAsync, - getSolcAsync, + getDependencyNameToPackagePath, getSourcesWithDependencies, getSourceTreeHash, + makeContractPathsRelative, parseSolidityVersionRange, + printCompilationErrorsAndWarnings, } from './utils/compiler'; import { constants } from './utils/constants'; import { fsWrapper } from './utils/fs_wrapper'; @@ -40,6 +44,7 @@ const ALL_CONTRACTS_IDENTIFIER = '*'; const ALL_FILES_IDENTIFIER = '*'; const DEFAULT_CONTRACTS_DIR = path.resolve('contracts'); const DEFAULT_ARTIFACTS_DIR = path.resolve('artifacts'); +const DEFAULT_USE_DOCKERISED_SOLC = false; // Solc compiler settings cannot be configured from the commandline. // If you need this configured, please create a `compiler.json` config file // with your desired configurations. @@ -84,6 +89,7 @@ export class Compiler { private readonly _artifactsDir: string; private readonly _solcVersionIfExists: string | undefined; private readonly _specifiedContracts: string[] | TYPE_ALL_FILES_IDENTIFIER; + private readonly _useDockerisedSolc: boolean; /** * Instantiates a new instance of the Compiler class. * @param opts Optional compiler options @@ -97,16 +103,17 @@ export class Compiler { : {}; const passedOpts = opts || {}; assert.doesConformToSchema('compiler.json', config, compilerOptionsSchema); - this._contractsDir = passedOpts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR; + this._contractsDir = path.resolve(passedOpts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR); this._solcVersionIfExists = passedOpts.solcVersion || config.solcVersion; this._compilerSettings = passedOpts.compilerSettings || config.compilerSettings || DEFAULT_COMPILER_SETTINGS; this._artifactsDir = passedOpts.artifactsDir || config.artifactsDir || DEFAULT_ARTIFACTS_DIR; this._specifiedContracts = passedOpts.contracts || config.contracts || ALL_CONTRACTS_IDENTIFIER; - this._nameResolver = new NameResolver(path.resolve(this._contractsDir)); + this._useDockerisedSolc = + passedOpts.useDockerisedSolc || config.useDockerisedSolc || DEFAULT_USE_DOCKERISED_SOLC; + this._nameResolver = new NameResolver(this._contractsDir); const resolver = new FallthroughResolver(); resolver.appendResolver(new URLResolver()); - const packagePath = path.resolve(''); - resolver.appendResolver(new NPMResolver(packagePath)); + resolver.appendResolver(new NPMResolver(this._contractsDir)); resolver.appendResolver(new RelativeFSResolver(this._contractsDir)); resolver.appendResolver(new FSResolver()); resolver.appendResolver(this._nameResolver); @@ -206,10 +213,12 @@ export class Compiler { // map contract paths to data about them for later verification and persistence const contractPathToData: ContractPathToData = {}; + const resolvedContractSources = []; for (const contractName of contractNames) { - const contractSource = this._resolver.resolve(contractName); + const spyResolver = new SpyResolver(this._resolver); + const contractSource = spyResolver.resolve(contractName); const sourceTreeHashHex = getSourceTreeHash( - this._resolver, + spyResolver, path.join(this._contractsDir, contractSource.path), ).toString('hex'); const contractData = { @@ -236,26 +245,54 @@ 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 compilerOutputs: StandardOutput[] = []; + const dependencyNameToPath = getDependencyNameToPackagePath(resolvedContractSources); - const solcVersions = _.keys(versionToInputs); - for (const solcVersion of solcVersions) { + const compilerOutputs: StandardOutput[] = []; + for (const solcVersion of _.keys(versionToInputs)) { const input = versionToInputs[solcVersion]; logUtils.warn( `Compiling ${input.contractsToCompile.length} contracts (${ input.contractsToCompile }) with Solidity v${solcVersion}...`, ); - - const { solcInstance, fullSolcVersion } = await getSolcAsync(solcVersion); - const compilerOutput = compile(this._resolver, solcInstance, input.standardInput); - compilerOutputs.push(compilerOutput); + let compilerOutput; + let fullSolcVersion; + input.standardInput.settings.remappings = _.map( + dependencyNameToPath, + (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 = await compileDockerAsync(solcVersion, input.standardInput); + } else { + fullSolcVersion = binPaths[solcVersion]; + compilerOutput = await compileSolcJSAsync(solcVersion, input.standardInput); + } + if (!_.isUndefined(compilerOutput.errors)) { + printCompilationErrorsAndWarnings(compilerOutput.errors); + } + compilerOutput.sources = makeContractPathsRelative( + compilerOutput.sources, + this._contractsDir, + dependencyNameToPath, + ); + compilerOutput.contracts = makeContractPathsRelative( + compilerOutput.contracts, + this._contractsDir, + dependencyNameToPath, + ); for (const contractPath of input.contractsToCompile) { const contractName = contractPathToData[contractPath].contractName; @@ -280,6 +317,8 @@ export class Compiler { ); } } + + compilerOutputs.push(compilerOutput); } return compilerOutputs; diff --git a/packages/sol-compiler/src/schemas/compiler_options_schema.ts b/packages/sol-compiler/src/schemas/compiler_options_schema.ts index d4d1b6017..c0766b625 100644 --- a/packages/sol-compiler/src/schemas/compiler_options_schema.ts +++ b/packages/sol-compiler/src/schemas/compiler_options_schema.ts @@ -19,6 +19,7 @@ export const compilerOptionsSchema = { }, ], }, + useDockerisedSolc: { type: 'boolean' }, }, type: 'object', required: [], diff --git a/packages/sol-compiler/src/utils/compiler.ts b/packages/sol-compiler/src/utils/compiler.ts index db308f2b5..c75f76dac 100644 --- a/packages/sol-compiler/src/utils/compiler.ts +++ b/packages/sol-compiler/src/utils/compiler.ts @@ -1,6 +1,7 @@ import { ContractSource, Resolver } from '@0x/sol-resolver'; import { fetchAsync, logUtils } from '@0x/utils'; import chalk from 'chalk'; +import { execSync } from 'child_process'; import { ContractArtifact } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -117,31 +118,74 @@ 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 compile( - 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); + return compiled; +} + +/** + * Compiles the contracts and prints errors/warnings + * @param solcVersion Version of a solc compiler + * @param standardInput Solidity standard JSON input + */ +export async function compileDockerAsync( + solcVersion: string, + standardInput: solc.StandardInput, +): Promise<solc.StandardOutput> { + const standardInputStr = JSON.stringify(standardInput, null, 2); + const dockerCommand = `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); - if (!_.isUndefined(compiled.errors)) { - printCompilationErrorsAndWarnings(compiled.errors); - } return compiled; } + +/** + * Example "relative" paths: + * /user/leo/0x-monorepo/contracts/extensions/contracts/extension.sol -> extension.sol + * /user/leo/0x-monorepo/node_modules/@0x/contracts-protocol/contracts/exchange.sol -> @0x/contracts-protocol/contracts/exchange.sol + */ +function makeContractPathRelative( + absolutePath: string, + contractsDir: string, + dependencyNameToPath: { [dependencyName: string]: string }, +): string { + let contractPath = absolutePath.replace(`${contractsDir}/`, ''); + _.map(dependencyNameToPath, (packagePath: string, dependencyName: string) => { + contractPath = contractPath.replace(packagePath, dependencyName); + }); + return contractPath; +} + +/** + * 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 dependencyNameToPath Mapping of dependency name to package path + */ +export function makeContractPathsRelative( + absolutePathToSmth: { [absoluteContractPath: string]: any }, + contractsDir: string, + dependencyNameToPath: { [dependencyName: string]: string }, +): { [contractPath: string]: any } { + return _.mapKeys(absolutePathToSmth, (_val: any, absoluteContractPath: string) => + makeContractPathRelative(absoluteContractPath, contractsDir, dependencyNameToPath), + ); +} + /** * 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); @@ -267,13 +311,11 @@ function recursivelyGatherDependencySources( } /** - * Gets the solidity compiler instance and full version name. If the compiler is already cached - gets it from FS, + * Gets the solidity compiler instance. If the compiler is already cached - gets it from FS, * otherwise - fetches it and caches it. * @param solcVersion The compiler version. e.g. 0.5.0 */ -export async function getSolcAsync( - solcVersion: string, -): Promise<{ solcInstance: solc.SolcInstance; fullSolcVersion: string }> { +export async function getSolcJSAsync(solcVersion: string): Promise<solc.SolcInstance> { const fullSolcVersion = binPaths[solcVersion]; if (_.isUndefined(fullSolcVersion)) { throw new Error(`${solcVersion} is not a known compiler version`); @@ -297,7 +339,7 @@ export async function getSolcAsync( throw new Error('No compiler available'); } const solcInstance = solc.setupMethods(requireFromString(solcjs, compilerBinFilename)); - return { solcInstance, fullSolcVersion }; + return solcInstance; } /** @@ -319,3 +361,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, as well as in generating the "relative" source paths saved to the artifact files. + * @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 dependencyNameToPath: { [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); + dependencyNameToPath[dependencyName] = dependencyPackagePath; + }); + return dependencyNameToPath; +} |