diff options
author | jaseg <git@jaseg.de> | 2021-08-29 16:27:23 +0200 |
---|---|---|
committer | jaseg <git@jaseg.de> | 2021-09-29 16:37:59 +0200 |
commit | 7eb0b9d7e42a20b85bd7afbfe3d33d9dd5fa5794 (patch) | |
tree | 5ac1f274b0c933234535c6b8f19a8d270f9e2d83 /svg-flatten | |
parent | c58b6573f2a0694c0c1942e7c36704e0517ced19 (diff) | |
download | gerbolyze-7eb0b9d7e42a20b85bd7afbfe3d33d9dd5fa5794.tar.gz gerbolyze-7eb0b9d7e42a20b85bd7afbfe3d33d9dd5fa5794.tar.bz2 gerbolyze-7eb0b9d7e42a20b85bd7afbfe3d33d9dd5fa5794.zip |
svg-flatten: Add wasmtime support
Diffstat (limited to 'svg-flatten')
-rw-r--r-- | svg-flatten/Makefile | 60 | ||||
-rw-r--r-- | svg-flatten/src/main.cpp | 48 | ||||
-rw-r--r-- | svg-flatten/src/svg_import_util.cpp | 7 | ||||
-rw-r--r-- | svg-flatten/src/util.cpp | 12 | ||||
l--------- | svg-flatten/upstream | 1 |
5 files changed, 100 insertions, 28 deletions
diff --git a/svg-flatten/Makefile b/svg-flatten/Makefile index fc5820f..94dab9b 100644 --- a/svg-flatten/Makefile +++ b/svg-flatten/Makefile @@ -6,10 +6,17 @@ PKG_CONFIG ?= pkg-config PYTHON3 ?= python3 BUILDDIR ?= build +CACHEDIR ?= cache PREFIX ?= /usr/local -UPSTREAM_DIR ?= ../upstream +UPSTREAM_DIR ?= upstream -SOURCES := src/svg_color.cpp \ +WASI_SDK ?= wasi-sdk-11.0 +WASI_SDK_URL ?= https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-linux.tar.gz +WASI_SDK_PATH ?= $(CACHEDIR)/$(WASI_SDK) +WASI_CXX ?= $(WASI_SDK_PATH)/bin/clang++ --sysroot=$(WASI_SDK_PATH)/share/wasi-sysroot + +SOURCES := \ + src/svg_color.cpp \ src/svg_doc.cpp \ src/svg_geom.cpp \ src/svg_import_util.cpp \ @@ -47,28 +54,47 @@ STB_INCLUDES ?= -isystem$(UPSTREAM_DIR)/stb INCLUDES := -Iinclude -Isrc $(CLIPPER_INCLUDES) $(VORONOI_INCLUDES) $(POISSON_INCLUDES) $(BASE64_INCLUDES) $(ARGAGG_INCLUDES) $(CAVC_INCLUDES) $(SUBPROCESS_INCLUDES) $(MINUNIT_INCLUDES) $(STB_INCLUDES) CXXFLAGS := -std=c++2a -g -Wall -Wextra -O2 -LDFLAGS := -lm -lstdc++ -lstdc++fs # for debian's ancient compilers +LDFLAGS := -lm -lstdc++ -PKG_CONFIG_DEPS := ifdef USE_SYSTEM_PUGIXML - CXXFLAGS += $(shell $(PKG_CONFIG) --cflags pugixml) - LDFLAGS += $(shell $(PKG_CONFIG) --libs pugixml) + HOST_CXXFLAGS := $(CXXFLAGS) $(shell $(PKG_CONFIG) --cflags pugixml) + HOST_LDFLAGS := $(LDFLAGS) $(shell $(PKG_CONFIG) --libs pugixml) + HOST_SOURCES := $(SOURCES) + HOST_INCLUDES := $(INCLUDES) else - SOURCES += $(PUGIXML_SOURCES) - INCLUDES += $(PUGIXML_INCLUDES) + HOST_CXXFLAGS := $(CXXFLAGS) + HOST_LDFLAGS := $(LDFLAGS) + HOST_SOURCES := $(SOURCES) $(PUGIXML_SOURCES) + HOST_INCLUDES := $(INCLUDES) $(PUGIXML_INCLUDES) endif -TARGET := svg-flatten +HOST_LDFLAGS += -lstdc++fs # for debian's ancient compilers + +WASI_CXXFLAGS ?= -DNOFORK -DNOTHROW -DWASI -DPUGIXML_NO_EXCEPTIONS -fno-exceptions $(CXXFLAGS) + +BINARY := svg-flatten + +all: $(BUILDDIR)/$(BINARY) -all: $(BUILDDIR)/$(TARGET) +$(CACHEDIR)/$(WASI_SDK): + mkdir -p $(dir $@) + cd $(dir $@); curl -L ${WASI_SDK_URL} | tar xzf - -$(BUILDDIR)/%.o: %.cpp +$(BUILDDIR)/wasi/%.o: %.cpp $(CACHEDIR)/$(WASI_SDK) @mkdir -p $(dir $@) - $(CXX) -c $(CXXFLAGS) $(CXXFLAGS) $(INCLUDES) -o $@ $^ + $(WASI_CXX) -c $(WASI_CXXFLAGS) $(INCLUDES) $(PUGIXML_INCLUDES) -o $@ $< -$(BUILDDIR)/$(TARGET): $(SOURCES:%.cpp=$(BUILDDIR)/%.o) +$(BUILDDIR)/$(BINARY).wasm: $(patsubst %.cpp,$(BUILDDIR)/wasi/%.o,$(SOURCES) $(PUGIXML_SOURCES)) + @mkdir -p $(dir $@) + $(WASI_CXX) $(WASI_CXXFLAGS) -o $@ $^ $(LDFLAGS) + +$(BUILDDIR)/host/%.o: %.cpp + @mkdir -p $(dir $@) + $(CXX) -c $(HOST_CXXFLAGS) $(HOST_CXXFLAGS) $(HOST_INCLUDES) -o $@ $< + +$(BUILDDIR)/$(BINARY): $(HOST_SOURCES:%.cpp=$(BUILDDIR)/host/%.o) @mkdir -p $(dir $@) - $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) + $(CXX) $(HOST_CXXFLAGS) -o $@ $^ $(HOST_LDFLAGS) $(BUILDDIR)/nopencv-test: src/test/nopencv_test.cpp src/nopencv.cpp src/util.cpp @mkdir -p $(dir $@) @@ -82,8 +108,12 @@ tests: $(BUILDDIR)/nopencv-test .PHONY: install install: - $(INSTALL) $(BUILDDIR)/$(TARGET) $(PREFIX)/bin + $(INSTALL) $(BUILDDIR)/$(BINARY) $(PREFIX)/bin .PHONY: clean clean: rm -rf $(BUILDDIR) + +.PHONY: mrproper +mrproper: clean + rm -rf $(CACHEDIR) diff --git a/svg-flatten/src/main.cpp b/svg-flatten/src/main.cpp index 8512547..2ebcfe9 100644 --- a/svg-flatten/src/main.cpp +++ b/svg-flatten/src/main.cpp @@ -3,6 +3,7 @@ #include <cstdio> #include <filesystem> #include <iostream> +#include <iomanip> #include <fstream> #include <algorithm> #include <string> @@ -17,6 +18,34 @@ using argagg::parser; using namespace std; using namespace gerbolyze; +string temp_file_path(const char *suffix) { + ifstream rnd; + rnd.open("/dev/urandom", ios::in | ios::binary); + + char fn_buf[8]; + rnd.read(fn_buf, sizeof(fn_buf)); + + if (rnd.rdstate()) { + cerr << "Error getting random data for temporary file name" << endl; + abort(); + } + + ostringstream out; + out << "tmp_"; + for (size_t i=0; i<sizeof(fn_buf); i++) { + out << setfill('0') << setw(2) << setbase(16) << static_cast<int>(fn_buf[i] & 0xff); + } + out << suffix; + + cerr << "out \"" << out.str() << "\"" << endl; +#ifndef WASI + filesystem::path base = filesystem::temp_directory_path(); + return (base / out.str()).native(); +#else + return "/tmp/" + out.str(); +#endif +} + int main(int argc, char **argv) { parser argparser {{ {"help", {"-h", "--help"}, @@ -114,15 +143,7 @@ int main(int argc, char **argv) { << endl; argagg::parser_results args; - try { - args = argparser.parse(argc, argv); - } catch (const std::exception& e) { - argagg::fmt_ostream fmt(cerr); - fmt << usage.str() << argparser << '\n' - << "Encountered exception while parsing arguments: " << e.what() - << '\n'; - return EXIT_FAILURE; - } + args = argparser.parse(argc, argv); if (args["help"]) { argagg::fmt_ostream fmt(cerr); @@ -267,8 +288,8 @@ int main(int argc, char **argv) { transform(ending.begin(), ending.end(), ending.begin(), [](unsigned char c){ return std::tolower(c); }); /* c++ yeah */ } - filesystem::path barf = { filesystem::temp_directory_path() /= (std::tmpnam(nullptr) + string(".svg")) }; - filesystem::path frob = { filesystem::temp_directory_path() /= (std::tmpnam(nullptr) + string(".svg")) }; + string barf = temp_file_path(".svg"); + string frob = temp_file_path(".svg"); bool is_svg = args["force_svg"] || (ending == ".svg" && !args["force_png"]); if (!is_svg) { @@ -352,10 +373,15 @@ int main(int argc, char **argv) { } string dpi_str = to_string(dpi); +#ifndef NOFORK const char *command_line[] = {nullptr, "--keep-named-groups", "--dpi", dpi_str.c_str(), barf.c_str(), frob.c_str(), nullptr}; if (run_cargo_command("usvg", command_line, "USVG")) { return EXIT_FAILURE; } +#else + cerr << "Error: The caller of svg-flatten (you?) must use --no-usvg and run usvg externally since wasi does not yet support fork/exec." << endl; + return EXIT_FAILURE; +#endif } VectorizerSelectorizer vec_sel(vectorizer, args["vectorizer_map"] ? args["vectorizer_map"].as<string>() : ""); diff --git a/svg-flatten/src/svg_import_util.cpp b/svg-flatten/src/svg_import_util.cpp index cb60482..85db3ad 100644 --- a/svg-flatten/src/svg_import_util.cpp +++ b/svg-flatten/src/svg_import_util.cpp @@ -61,6 +61,11 @@ string gerbolyze::parse_data_iri(const string &data_url) { size_t b64_begin = data_url.find_first_not_of(" ", foo + strlen("base64,")); assert(b64_begin != string::npos); - return base64_decode(data_url.substr(b64_begin)); + bool err_out; + string out = base64_decode(data_url.substr(b64_begin), false, &err_out); + + if (err_out) + return ""; + return out; } diff --git a/svg-flatten/src/util.cpp b/svg-flatten/src/util.cpp index e88f4e9..d9a5a09 100644 --- a/svg-flatten/src/util.cpp +++ b/svg-flatten/src/util.cpp @@ -1,16 +1,19 @@ -#include <pwd.h> #include <sys/types.h> #include <string> #include <iostream> +#ifndef NOFORK +#include <pwd.h> #include <subprocess.h> +#endif #include <filesystem> #include "util.h" using namespace std; +#ifndef NOFORK int gerbolyze::run_cargo_command(const char *cmd_name, const char *cmdline[], const char *envvar) { const char *homedir; if ((homedir = getenv("HOME")) == NULL) { @@ -81,4 +84,11 @@ int gerbolyze::run_cargo_command(const char *cmd_name, const char *cmdline[], co return 0; } +#else +int gerbolyze::run_cargo_command(const char *cmd_name, const char *cmdline[], const char *envvar) { + (void) cmd_name, (void) cmdline, (void) envvar; + cerr << "Error: Cannot spawn " << cmd_name << " subprocess since binary was built with fork/exec disabled (-DNOFORK=1)" << endl; + return EXIT_FAILURE; +} +#endif diff --git a/svg-flatten/upstream b/svg-flatten/upstream new file mode 120000 index 0000000..f84a283 --- /dev/null +++ b/svg-flatten/upstream @@ -0,0 +1 @@ +../upstream
\ No newline at end of file |