trufflesuite / truffle-compile

Compiler helper and artifact manager
22 stars 46 forks source link

Add ability to use any solc-js #56

Closed cgewecke closed 6 years ago

cgewecke commented 6 years ago

Part I of "Bring your own compiler"

Adds a CompilerProvider class that can be configured to fetch an arbitrary solc version from the solc-bin remote. Defaults to truffle's installed solc. Versions fetched from the remote are cached and only need to be retrieved once. Unit tests running locally suggest download times are in the 3-5 second range over a typical wifi connection.

User API:

// truffle.js
module.exports = {
  networks: {
    ... etc ...
  },
  solc : {
    ... etc ...  // solc options (like optimization level)
  },
  compiler: {
     cache: <boolean>     // Cache remote fetches? (Default: true)
     cachePath: <string>  // (Default: `/var/lib/truffle/solc/cache/`)
     solc: <string>       // Version. ex:  "5.0.0". (Default: truffle's installed solc)
  } 
};

Idea for Part II of BYOC is to extend CompilerProvider to allow use of a natively compiled solc by setting the solc key to "native".

CompilerProvider API

const provider = new CompilerProvider(options.compiler);
const solc = await provider.load();
const nativeSolc = await provider.load(options.solc);

// Get a collection of usable release keys, 
// for something like `truffle compile --list`
const releases = await provider.getReleases(); 

PR includes logic to require solc-js from an arbitrary location on the local filesystem (use case is support for npm install -g solc) but unfortunately webpack mangles any require statement which relies on variables that resolve at run-time. This feature could probably be removed at no great loss, although there are other options.

PR also includes tests for the new code and misc fixes for the existing test suite.