summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Guryanov <caiiiycuk@gmail.com>2021-08-23 22:08:26 +0700
committerGitHub <noreply@github.com>2021-08-23 15:08:26 +0000
commit3d88d11176a26b1156b83a71bbc033c71772dabe (patch)
tree6bea0d6a5fc77ce50a474eedefd1df4be9149d20
parentf1aa78c022eb3f96a0d97e28ce9c35edbe7c45a1 (diff)
downloadbinaryen-3d88d11176a26b1156b83a71bbc033c71772dabe.tar.gz
binaryen-3d88d11176a26b1156b83a71bbc033c71772dabe.tar.bz2
binaryen-3d88d11176a26b1156b83a71bbc033c71772dabe.zip
wasm-split: accept file in keep-funcs/split-funcs (#4053)
-rw-r--r--src/tools/wasm-split.cpp40
-rw-r--r--test/lit/help/wasm-split.test11
-rw-r--r--test/lit/wasm-split/bar.txt1
-rw-r--r--test/lit/wasm-split/basic.wast25
-rw-r--r--test/lit/wasm-split/both.txt2
-rw-r--r--test/lit/wasm-split/foo.txt1
-rw-r--r--test/lit/wasm-split/none.txt1
7 files changed, 75 insertions, 6 deletions
diff --git a/src/tools/wasm-split.cpp b/src/tools/wasm-split.cpp
index 6d4e8ed5c..83c170abe 100644
--- a/src/tools/wasm-split.cpp
+++ b/src/tools/wasm-split.cpp
@@ -39,15 +39,41 @@ namespace {
const std::string DEFAULT_PROFILE_EXPORT("__write_profile");
-std::set<Name> parseNameList(const std::string& list) {
+std::set<Name> parseNameListFromLine(const std::string& line) {
std::set<Name> names;
- std::istringstream stream(list);
+ std::istringstream stream(line);
for (std::string name; std::getline(stream, name, ',');) {
names.insert(name);
}
return names;
}
+std::set<Name> parseNameListFromFile(const std::string& filename) {
+ std::ifstream infile(filename);
+ if (!infile.is_open()) {
+ std::cerr << "Failed opening '" << filename << "'" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ std::set<Name> names;
+ std::string line;
+ while (std::getline(infile, line)) {
+ if (line.length() > 0) {
+ names.insert(line);
+ }
+ }
+
+ return names;
+}
+
+std::set<Name> parseNameList(const std::string& listOrFile) {
+ if (!listOrFile.empty() && listOrFile[0] == '@') {
+ return parseNameListFromFile(listOrFile.substr(1));
+ }
+
+ return parseNameListFromLine(listOrFile);
+}
+
struct WasmSplitOptions : ToolOptions {
enum class Mode : unsigned {
Split,
@@ -143,7 +169,10 @@ WasmSplitOptions::WasmSplitOptions()
.add("--keep-funcs",
"",
"Comma-separated list of functions to keep in the primary module, "
- "regardless of any profile.",
+ "regardless of any profile. "
+ "You can also pass a file with a list of functions separated by new "
+ "lines. "
+ "To do this, prepend @ before filename (--keep-funcs @myfile)",
{Mode::Split},
Options::Arguments::One,
[&](Options* o, const std::string& argument) {
@@ -153,7 +182,10 @@ WasmSplitOptions::WasmSplitOptions()
"",
"Comma-separated list of functions to split into the secondary "
"module, regardless of any profile. If there is no profile, then "
- "this defaults to all functions defined in the module.",
+ "this defaults to all functions defined in the module. "
+ "You can also pass a file with a list of functions separated by new "
+ "lines. "
+ "To do this, prepend @ before filename (--split-funcs @myfile)",
{Mode::Split},
Options::Arguments::One,
[&](Options* o, const std::string& argument) {
diff --git a/test/lit/help/wasm-split.test b/test/lit/help/wasm-split.test
index 28f079d2f..362c20d84 100644
--- a/test/lit/help/wasm-split.test
+++ b/test/lit/help/wasm-split.test
@@ -36,13 +36,20 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --keep-funcs [split] Comma-separated list of functions
;; CHECK-NEXT: to keep in the primary module, regardless
-;; CHECK-NEXT: of any profile.
+;; CHECK-NEXT: of any profile. You can also pass a file
+;; CHECK-NEXT: with a list of functions separated by new
+;; CHECK-NEXT: lines. To do this, prepend @ before
+;; CHECK-NEXT: filename (--keep-funcs @myfile)
;; CHECK-NEXT:
;; CHECK-NEXT: --split-funcs [split] Comma-separated list of functions
;; CHECK-NEXT: to split into the secondary module,
;; CHECK-NEXT: regardless of any profile. If there is no
;; CHECK-NEXT: profile, then this defaults to all
-;; CHECK-NEXT: functions defined in the module.
+;; CHECK-NEXT: functions defined in the module. You can
+;; CHECK-NEXT: also pass a file with a list of functions
+;; CHECK-NEXT: separated by new lines. To do this,
+;; CHECK-NEXT: prepend @ before filename (--split-funcs
+;; CHECK-NEXT: @myfile)
;; CHECK-NEXT:
;; CHECK-NEXT: --primary-output,-o1 [split] Output file for the primary
;; CHECK-NEXT: module.
diff --git a/test/lit/wasm-split/bar.txt b/test/lit/wasm-split/bar.txt
new file mode 100644
index 000000000..5716ca598
--- /dev/null
+++ b/test/lit/wasm-split/bar.txt
@@ -0,0 +1 @@
+bar
diff --git a/test/lit/wasm-split/basic.wast b/test/lit/wasm-split/basic.wast
index 7bbc14b49..6d5e6c870 100644
--- a/test/lit/wasm-split/basic.wast
+++ b/test/lit/wasm-split/basic.wast
@@ -1,23 +1,46 @@
+;; RUN: not wasm-split %s --export-prefix='%' -g -o1 %t.none.1.wasm -o2 %t.none.2.wasm --keep-funcs=@failed-to-open.txt -v 2>&1 \
+;; RUN: | filecheck %s --check-prefix FAILED-TO-OPEN
+
;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.none.1.wasm -o2 %t.none.2.wasm -v 2>&1 \
;; RUN: | filecheck %s --check-prefix KEEP-NONE
;; RUN: wasm-dis %t.none.1.wasm | filecheck %s --check-prefix KEEP-NONE-PRIMARY
;; RUN: wasm-dis %t.none.2.wasm | filecheck %s --check-prefix KEEP-NONE-SECONDARY
+;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.none.1.wasm -o2 %t.none.2.wasm --keep-funcs=@%S/none.txt -v 2>&1 \
+;; RUN: | filecheck %s --check-prefix KEEP-NONE
+;; RUN: wasm-dis %t.none.1.wasm | filecheck %s --check-prefix KEEP-NONE-PRIMARY
+;; RUN: wasm-dis %t.none.2.wasm | filecheck %s --check-prefix KEEP-NONE-SECONDARY
+
;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.foo.1.wasm -o2 %t.foo.2.wasm --keep-funcs=foo -v 2>&1 \
;; RUN: | filecheck %s --check-prefix KEEP-FOO
;; RUN: wasm-dis %t.foo.1.wasm | filecheck %s --check-prefix KEEP-FOO-PRIMARY
;; RUN: wasm-dis %t.foo.2.wasm | filecheck %s --check-prefix KEEP-FOO-SECONDARY
+;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.foo.1.wasm -o2 %t.foo.2.wasm --keep-funcs=@%S/foo.txt -v 2>&1 \
+;; RUN: | filecheck %s --check-prefix KEEP-FOO
+;; RUN: wasm-dis %t.foo.1.wasm | filecheck %s --check-prefix KEEP-FOO-PRIMARY
+;; RUN: wasm-dis %t.foo.2.wasm | filecheck %s --check-prefix KEEP-FOO-SECONDARY
+
;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.bar.1.wasm -o2 %t.bar.2.wasm --keep-funcs=bar -v 2>&1 \
;; RUN: | filecheck %s --check-prefix KEEP-BAR
;; RUN: wasm-dis %t.bar.1.wasm | filecheck %s --check-prefix KEEP-BAR-PRIMARY
;; RUN: wasm-dis %t.bar.2.wasm | filecheck %s --check-prefix KEEP-BAR-SECONDARY
+;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.bar.1.wasm -o2 %t.bar.2.wasm --keep-funcs=@%S/bar.txt -v 2>&1 \
+;; RUN: | filecheck %s --check-prefix KEEP-BAR
+;; RUN: wasm-dis %t.bar.1.wasm | filecheck %s --check-prefix KEEP-BAR-PRIMARY
+;; RUN: wasm-dis %t.bar.2.wasm | filecheck %s --check-prefix KEEP-BAR-SECONDARY
+
;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.both.1.wasm -o2 %t.both.2.wasm --keep-funcs=foo,bar -v 2>&1 \
;; RUN: | filecheck %s --check-prefix KEEP-BOTH
;; RUN: wasm-dis %t.both.1.wasm | filecheck %s --check-prefix KEEP-BOTH-PRIMARY
;; RUN: wasm-dis %t.both.2.wasm | filecheck %s --check-prefix KEEP-BOTH-SECONDARY
+;; RUN: wasm-split %s --export-prefix='%' -g -o1 %t.both.1.wasm -o2 %t.both.2.wasm --keep-funcs=@%S/both.txt -v 2>&1 \
+;; RUN: | filecheck %s --check-prefix KEEP-BOTH
+;; RUN: wasm-dis %t.both.1.wasm | filecheck %s --check-prefix KEEP-BOTH-PRIMARY
+;; RUN: wasm-dis %t.both.2.wasm | filecheck %s --check-prefix KEEP-BOTH-SECONDARY
+
(module
(table $table 1 1 funcref)
(elem (i32.const 0) $foo)
@@ -29,6 +52,8 @@
)
)
+;; FAILED-TO-OPEN: Failed opening 'failed-to-open.txt'
+
;; KEEP-NONE: warning: not keeping any functions in the primary module
;; KEEP-NONE-PRIMARY: (module
diff --git a/test/lit/wasm-split/both.txt b/test/lit/wasm-split/both.txt
new file mode 100644
index 000000000..3bd1f0e29
--- /dev/null
+++ b/test/lit/wasm-split/both.txt
@@ -0,0 +1,2 @@
+foo
+bar
diff --git a/test/lit/wasm-split/foo.txt b/test/lit/wasm-split/foo.txt
new file mode 100644
index 000000000..257cc5642
--- /dev/null
+++ b/test/lit/wasm-split/foo.txt
@@ -0,0 +1 @@
+foo
diff --git a/test/lit/wasm-split/none.txt b/test/lit/wasm-split/none.txt
new file mode 100644
index 000000000..8b1378917
--- /dev/null
+++ b/test/lit/wasm-split/none.txt
@@ -0,0 +1 @@
+