Skip to content
24 changes: 11 additions & 13 deletions src/support/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ template<> std::string do_read_stdin<std::string>::operator()() {
}

template<typename T>
T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) {
T wasm::read_file(const fs::path& filename,
Flags::BinaryOption binary) {
if (filename == "-") {
return do_read_stdin<T>{}();
}
Expand Down Expand Up @@ -95,11 +96,13 @@ std::string wasm::read_possible_response_file(const std::string& input) {
}

// Explicit instantiations for the explicit specializations.
template std::string wasm::read_file<>(const std::string&, Flags::BinaryOption);
template std::vector<char> wasm::read_file<>(const std::string&,
template std::string wasm::read_file<>(const fs::path&,
Flags::BinaryOption);
template std::vector<char> wasm::read_file<>(const fs::path&,
Flags::BinaryOption);

wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary)
wasm::Output::Output(const fs::path& filename,
Flags::BinaryOption binary)
: outfile(), out([this, filename, binary]() {
// Ensure a single return at the very end, to avoid clang-tidy warnings
// about the types of different returns here.
Expand All @@ -121,13 +124,8 @@ wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary)
return buffer;
}()) {}

void wasm::copy_file(std::string input, std::string output) {
std::ifstream src(input, std::ios::binary);
std::ofstream dst(output, std::ios::binary);
dst << src.rdbuf();
}

size_t wasm::file_size(std::string filename) {
std::ifstream infile(filename, std::ifstream::ate | std::ifstream::binary);
return infile.tellg();
void wasm::copy_file(fs::path input,
fs::path output) {
fs::copy_file(
input, output, fs::copy_options::overwrite_existing);
}
18 changes: 9 additions & 9 deletions src/support/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
#ifndef wasm_support_file_h
#define wasm_support_file_h

#include <experimental/filesystem>
#include <fstream>
#include <string>
#include <utility>
#include <vector>

namespace fs = std::experimental::filesystem;

namespace wasm {

namespace Flags {
Expand All @@ -35,12 +38,12 @@ enum BinaryOption { Binary, Text };
std::vector<char> read_stdin();

template<typename T>
T read_file(const std::string& filename, Flags::BinaryOption binary);
T read_file(const fs::path& filename, Flags::BinaryOption binary);

// Declare the valid explicit specializations.
extern template std::string read_file<>(const std::string&,
extern template std::string read_file<>(const fs::path&,
Flags::BinaryOption);
extern template std::vector<char> read_file<>(const std::string&,
extern template std::vector<char> read_file<>(const fs::path&,
Flags::BinaryOption);

// Given a string which may be a response file (i.e., a filename starting
Expand All @@ -51,7 +54,7 @@ std::string read_possible_response_file(const std::string&);
class Output {
public:
// An empty filename or "-" will open stdout instead.
Output(const std::string& filename, Flags::BinaryOption binary);
Output(const fs::path& filename, Flags::BinaryOption binary);
~Output() = default;
template<typename T> std::ostream& operator<<(const T& v) { return out << v; }

Expand All @@ -69,11 +72,8 @@ class Output {
std::ostream out;
};

// Copies a file to another file
void copy_file(std::string input, std::string output);

// Retusn the size of a file
size_t file_size(std::string filename);
// Copies a file to another file, overwriting if the file exists
void copy_file(fs::path input, fs::path output);

} // namespace wasm

Expand Down
16 changes: 8 additions & 8 deletions src/support/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,34 @@ std::string getBaseName(const std::string& path) {
return path;
}

std::string getBinaryenRoot() {
fs::path getBinaryenRoot() {
auto* envVar = getenv("BINARYEN_ROOT");
if (envVar) {
return envVar;
}
return ".";
}

static std::string binDir;
static fs::path binDir;

std::string getBinaryenBinDir() {
fs::path getBinaryenBinDir() {
if (binDir.empty()) {
return getBinaryenRoot() + getPathSeparator() + "bin" + getPathSeparator();
return getBinaryenRoot() / "bin" / "";
} else {
return binDir;
}
}

void setBinaryenBinDir(const std::string& dir) {
void setBinaryenBinDir(const fs::path& dir) {
binDir = dir;
if (binDir.empty() || binDir.back() != getPathSeparator()) {
if (binDir.empty() || binDir.string().back() != getPathSeparator()) {
binDir += getPathSeparator();
}
}

// Gets the path to a binaryen binary tool, like wasm-opt
std::string getBinaryenBinaryTool(const std::string& name) {
return getBinaryenBinDir() + name;
fs::path getBinaryenBinaryTool(const std::string& name) {
return getBinaryenBinDir().append(name);
}

} // namespace wasm::Path
11 changes: 7 additions & 4 deletions src/support/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@
#define wasm_support_path_h

#include <cstdlib>
#include <experimental/filesystem>
#include <string>

namespace fs = std::experimental::filesystem;

namespace wasm::Path {

char getPathSeparator();
std::string getDirName(const std::string& path);
std::string getBaseName(const std::string& path);

// Get the binaryen root dor.
std::string getBinaryenRoot();
fs::path getBinaryenRoot();

// Get the binaryen bin dir.
std::string getBinaryenBinDir();
fs::path getBinaryenBinDir();

// Set the binaryen bin dir (allows tools to change it based on user input).
void setBinaryenBinDir(const std::string& dir);
void setBinaryenBinDir(const fs::path& dir);

// Gets the path to a binaryen binary tool, like wasm-opt.
std::string getBinaryenBinaryTool(const std::string& name);
fs::path getBinaryenBinaryTool(const std::string& name);

} // namespace wasm::Path

Expand Down
8 changes: 4 additions & 4 deletions src/tools/wasm-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ using namespace wasm;

int main(int argc, const char* argv[]) {
bool debugInfo = false;
std::string symbolMap;
std::string sourceMapFilename;
fs::path symbolMap;
fs::path sourceMapFilename;
std::string sourceMapUrl;

const std::string WasmAsOption = "wasm-as options";
Expand Down Expand Up @@ -142,11 +142,11 @@ int main(int argc, const char* argv[]) {
ModuleWriter writer;
writer.setBinary(true);
writer.setDebugInfo(debugInfo);
if (sourceMapFilename.size()) {
if (!sourceMapFilename.empty()) {
writer.setSourceMapFilename(sourceMapFilename);
writer.setSourceMapUrl(sourceMapUrl);
}
if (symbolMap.size() > 0) {
if (!symbolMap.empty() > 0) {
writer.setSymbolMap(symbolMap);
}
writer.write(wasm, options.extra["output"]);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
using namespace wasm;

int main(int argc, const char* argv[]) {
std::string sourceMapFilename;
fs::path sourceMapFilename;

const std::string WasmDisOption = "wasm-dis options";

Expand Down
14 changes: 7 additions & 7 deletions src/tools/wasm-emscripten-finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ using namespace wasm;
int main(int argc, const char* argv[]) {
const uint64_t INVALID_BASE = -1;

std::string infile;
std::string outfile;
std::string inputSourceMapFilename;
std::string outputSourceMapFilename;
fs::path infile;
fs::path outfile;
fs::path inputSourceMapFilename;
fs::path outputSourceMapFilename;
std::string outputSourceMapUrl;
std::string dataSegmentFile;
fs::path dataSegmentFile;
bool emitBinary = true;
bool debugInfo = false;
bool DWARF = false;
Expand Down Expand Up @@ -213,7 +213,7 @@ int main(int argc, const char* argv[]) {
// We will write the modified wasm if the user asked us to, either by
// specifying an output file or requesting text output (which goes to stdout
// by default).
auto writeOutput = outfile.size() > 0 || !emitBinary;
auto writeOutput = !outfile.empty() || !emitBinary;

Module wasm;
options.applyFeatures(wasm);
Expand Down Expand Up @@ -314,7 +314,7 @@ int main(int argc, const char* argv[]) {
writer.setDebugInfo(debugInfo);
// writer.setSymbolMap(symbolMap);
writer.setBinary(emitBinary);
if (outputSourceMapFilename.size()) {
if (!outputSourceMapFilename.empty()) {
writer.setSourceMapFilename(outputSourceMapFilename);
writer.setSourceMapUrl(outputSourceMapUrl);
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/wasm-metadce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ int main(int argc, const char* argv[]) {
std::vector<std::string> passes;
bool emitBinary = true;
bool debugInfo = false;
std::string graphFile;
fs::path graphFile;
bool dump = false;

const std::string WasmMetaDCEOption = "wasm-opt options";
Expand Down Expand Up @@ -512,7 +512,7 @@ int main(int argc, const char* argv[]) {
});
options.parse(argc, argv);

if (graphFile.size() == 0) {
if (graphFile.empty()) {
Fatal() << "no graph file provided.";
}

Expand Down
8 changes: 4 additions & 4 deletions src/tools/wasm-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ int main(int argc, const char* argv[]) {
std::string emitJSWrapper;
std::string emitSpecWrapper;
std::string emitWasm2CWrapper;
std::string inputSourceMapFilename;
std::string outputSourceMapFilename;
fs::path inputSourceMapFilename;
fs::path outputSourceMapFilename;
std::string outputSourceMapUrl;

const std::string WasmOptOption = "wasm-opt options";
Expand Down Expand Up @@ -266,7 +266,7 @@ int main(int argc, const char* argv[]) {
// down in TranslateToFuzzReader, but there is also an optional initial fuzz
// file that if it exists we read it, then add more fuzz on top.
if (!translateToFuzz || initialFuzz.size()) {
std::string inputFile =
fs::path inputFile =
translateToFuzz ? initialFuzz : options.extra["infile"];
ModuleReader reader;
// Enable DWARF parsing if we were asked for debug info, and were not
Expand Down Expand Up @@ -412,7 +412,7 @@ int main(int argc, const char* argv[]) {
ModuleWriter writer;
writer.setBinary(emitBinary);
writer.setDebugInfo(options.passOptions.debugInfo);
if (outputSourceMapFilename.size()) {
if (!outputSourceMapFilename.empty()) {
writer.setSourceMapFilename(outputSourceMapFilename);
writer.setSourceMapUrl(outputSourceMapUrl);
}
Expand Down
Loading