aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2016-01-29 00:05:12 +0800
committerchriseth <c@ethdev.com>2016-01-29 00:05:12 +0800
commitbdbb7d8a40d787104a5b6a9de265bf972daeac86 (patch)
treea8eba66e89605f7685282a078151bb989713da6e
parent06cf19515c02b84243d2d717cc107d4239e3f50a (diff)
parent92e8c9aec956ff3c72ba400c8275e0a90303dcf0 (diff)
downloaddexon-solidity-bdbb7d8a40d787104a5b6a9de265bf972daeac86.tar.gz
dexon-solidity-bdbb7d8a40d787104a5b6a9de265bf972daeac86.tar.zst
dexon-solidity-bdbb7d8a40d787104a5b6a9de265bf972daeac86.zip
Merge pull request #370 from chriseth/redirects
Path remappings for solc.
-rw-r--r--docs/miscellaneous.rst11
-rw-r--r--solc/CommandLineInterface.cpp105
-rw-r--r--solc/CommandLineInterface.h4
3 files changed, 87 insertions, 33 deletions
diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst
index dcd12c60..3d95ef57 100644
--- a/docs/miscellaneous.rst
+++ b/docs/miscellaneous.rst
@@ -103,7 +103,16 @@ One of the build targets of the Solidity repository is `solc`, the solidity comm
Using `solc --help` provides you with an explanation of all options. The compiler can produce various outputs, ranging from simple binaries and assembly over an abstract syntax tree (parse tree) to estimations of gas usage.
If you only want to compile a single file, you run it as `solc --bin sourceFile.sol` and it will print the binary. Before you deploy your contract, activate the optimizer while compiling using `solc --optimize --bin sourceFile.sol`. If you want to get some of the more advanced output variants of `solc`, it is probably better to tell it to output everything to separate files using `solc -o outputDirectory --bin --ast --asm sourceFile.sol`.
-Of course, you can also specify several source files and actually that is also required if you use the `import` statement in Solidity: The compiler will (for now) not automatically discover source files for you, so you have to provide it with all source files your project consists of.
+The commandline compiler will automatically read imported files from the filesystem, but
+it is also possible to provide path redirects using `prefix=path` in the following way:
+
+ solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ =/usr/local/lib/fallback file.sol
+
+This essentially instructs the compiler to search for anything starting with
+`github.com/ethereum/dapp-bin/` under `/usr/local/lib/dapp-bin` and if it does not
+find the file there, it will look at `/usr/local/lib/fallback` (the empty prefix
+always matches) and if also that fails, it will make a full path lookup
+on the filesystem.
If your contracts use [libraries](#libraries), you will notice that the bytecode contains substrings of the form `__LibraryName______`. You can use `solc` as a linker meaning that it will insert the library addresses for you at those points:
diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp
index 4c9de3c8..7c842c83 100644
--- a/solc/CommandLineInterface.cpp
+++ b/solc/CommandLineInterface.cpp
@@ -297,6 +297,48 @@ void CommandLineInterface::handleFormal()
cout << "Formal version:" << endl << m_compiler->formalTranslation() << endl;
}
+void CommandLineInterface::readInputFilesAndConfigureRemappings()
+{
+ if (!m_args.count("input-file"))
+ {
+ string s;
+ while (!cin.eof())
+ {
+ getline(cin, s);
+ m_sourceCodes[g_stdinFileName].append(s + '\n');
+ }
+ }
+ else
+ for (string const& infile: m_args["input-file"].as<vector<string>>())
+ {
+ auto eq = find(infile.begin(), infile.end(), '=');
+ if (eq != infile.end())
+ m_remappings.push_back(make_pair(
+ string(infile.begin(), eq),
+ string(eq + 1, infile.end())
+ ));
+ else
+ {
+ auto path = boost::filesystem::path(infile);
+ if (!boost::filesystem::exists(path))
+ {
+ cerr << "Skipping non existant input file \"" << infile << "\"" << endl;
+ continue;
+ }
+
+ if (!boost::filesystem::is_regular_file(path))
+ {
+ cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl;
+ continue;
+ }
+
+ m_sourceCodes[infile] = dev::contentsString(infile);
+ }
+ }
+ // Add empty remapping to try the path itself.
+ m_remappings.push_back(make_pair(string(), string()));
+}
+
bool CommandLineInterface::parseLibraryOption(string const& _input)
{
namespace fs = boost::filesystem;
@@ -457,33 +499,7 @@ Allowed options)",
bool CommandLineInterface::processInput()
{
- if (!m_args.count("input-file"))
- {
- string s;
- while (!cin.eof())
- {
- getline(cin, s);
- m_sourceCodes[g_stdinFileName].append(s + '\n');
- }
- }
- else
- for (string const& infile: m_args["input-file"].as<vector<string>>())
- {
- auto path = boost::filesystem::path(infile);
- if (!boost::filesystem::exists(path))
- {
- cerr << "Skipping non existant input file \"" << infile << "\"" << endl;
- continue;
- }
-
- if (!boost::filesystem::is_regular_file(path))
- {
- cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl;
- continue;
- }
-
- m_sourceCodes[infile] = dev::contentsString(infile);
- }
+ readInputFilesAndConfigureRemappings();
if (m_args.count("libraries"))
for (string const& library: m_args["libraries"].as<vector<string>>())
@@ -499,13 +515,38 @@ bool CommandLineInterface::processInput()
function<pair<string,string>(string const&)> fileReader = [this](string const& _path)
{
- auto path = boost::filesystem::path(_path);
- if (!boost::filesystem::exists(path))
+ // Try to find the longest prefix match in all remappings. At the end, there will be an
+ // empty remapping so that we also try the path itself.
+ int errorLevel = 0;
+ size_t longestPrefix = 0;
+ string bestMatchPath;
+ for (auto const& redir: m_remappings)
+ {
+ auto const& virt = redir.first;
+ if (longestPrefix > 0 && virt.length() <= longestPrefix)
+ continue;
+ if (virt.length() > _path.length() || !std::equal(virt.begin(), virt.end(), _path.begin()))
+ continue;
+ string path = redir.second;
+ path.append(_path.begin() + virt.length(), _path.end());
+ auto boostPath = boost::filesystem::path(path);
+ if (!boost::filesystem::exists(boostPath))
+ errorLevel = max(errorLevel, 0);
+ else if (!boost::filesystem::is_regular_file(boostPath))
+ errorLevel = max(errorLevel, 1);
+ else
+ {
+ longestPrefix = virt.length();
+ bestMatchPath = path;
+ }
+ }
+ if (!bestMatchPath.empty())
+ return make_pair(m_sourceCodes[bestMatchPath] = dev::contentsString(bestMatchPath), string());
+ if (errorLevel == 0)
return make_pair(string(), string("File not found."));
- else if (!boost::filesystem::is_regular_file(path))
- return make_pair(string(), string("Not a valid file."));
else
- return make_pair(m_sourceCodes[_path] = dev::contentsString(_path), string());
+ return make_pair(string(), string("Not a valid file."));
+
};
m_compiler.reset(new CompilerStack(m_args.count(g_argAddStandard) > 0, fileReader));
diff --git a/solc/CommandLineInterface.h b/solc/CommandLineInterface.h
index 7c7aa4b4..7fdc9c0d 100644
--- a/solc/CommandLineInterface.h
+++ b/solc/CommandLineInterface.h
@@ -61,6 +61,8 @@ private:
void handleGasEstimation(std::string const& _contract);
void handleFormal();
+ /// Fills @a m_sourceCodes initially and @a m_redirects.
+ void readInputFilesAndConfigureRemappings();
/// Tries to read from the file @a _input or interprets _input literally if that fails.
/// It then tries to parse the contents and appends to m_libraries.
bool parseLibraryOption(std::string const& _input);
@@ -76,6 +78,8 @@ private:
boost::program_options::variables_map m_args;
/// map of input files to source code strings
std::map<std::string, std::string> m_sourceCodes;
+ /// list of path prefix remappings, e.g. github.com/ethereum -> /usr/local/ethereum
+ std::vector<std::pair<std::string, std::string>> m_remappings;
/// map of library names to addresses
std::map<std::string, h160> m_libraries;
/// Solidity compiler stack