Skip to content

Commit

Permalink
WebAssembly: Prepare library to be used with wasm32-wasi
Browse files Browse the repository at this point in the history
It is the changes I made to make compilation work for clang with "wasm32-wasi" target and the wasi-sdk.
  • Loading branch information
yurydelendik committed Jun 14, 2019
1 parent 5ebc5d5 commit b58bef8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
30 changes: 27 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ else
endif
CAT ?= $(if $(filter $(OS),Windows_NT),type,cat)

ifdef WASM
CFLAGS += -D_WASM
CXXFLAGS += -D_WASM
endif

ifdef WASM
UNAME := WebAssembly
else
ifneq (,$(findstring /cygdrive/,$(PATH)))
UNAME := Cygwin
else
Expand All @@ -44,6 +52,7 @@ endif
endif
endif
endif
endif

ifndef LIBSASS_VERSION
ifneq ($(wildcard ./.git/ ),)
Expand Down Expand Up @@ -161,18 +170,28 @@ ifeq (Windows,$(UNAME))
LIB_SHARED = $(SASS_LIBSASS_PATH)/lib/libsass.dll
endif
else
ifdef WASM
SASSC_BIN = $(SASS_SASSC_PATH)/bin/sassc.wasm
SHAREDLIB = lib/libsass.wasm
LIB_SHARED = $(SASS_LIBSASS_PATH)/lib/libsass.wasm
else
ifneq (Cygwin,$(UNAME))
CFLAGS += -fPIC
CXXFLAGS += -fPIC
LDFLAGS += -fPIC
endif
endif
endif

include Makefile.conf
OBJECTS = $(addprefix src/,$(SOURCES:.cpp=.o))
COBJECTS = $(addprefix src/,$(CSOURCES:.c=.o))
RCOBJECTS = $(RESOURCES:.rc=.o)

ifdef WASM
WASMOBJECTS = wasm/libcxxabi_stubs.o
endif

DEBUG_LVL ?= NONE

CLEANUPS ?=
Expand All @@ -198,15 +217,18 @@ debug-shared: shared
lib:
$(MKDIR) lib

lib/libsass.a: $(COBJECTS) $(OBJECTS) | lib
$(AR) rcvs $@ $(COBJECTS) $(OBJECTS)
lib/libsass.a: $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) | lib
$(AR) rcvs $@ $(COBJECTS) $(OBJECTS) $(WASMOBJECTS)

lib/libsass.so: $(COBJECTS) $(OBJECTS) | lib
$(CXX) -shared $(LDFLAGS) -o $@ $(COBJECTS) $(OBJECTS) $(LDLIBS)

lib/libsass.dll: $(COBJECTS) $(OBJECTS) $(RCOBJECTS) | lib
$(CXX) -shared $(LDFLAGS) -o $@ $(COBJECTS) $(OBJECTS) $(RCOBJECTS) $(LDLIBS) -s -Wl,--subsystem,windows,--out-implib,lib/libsass.a

lib/libsass.wasm: $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) | lib
$(CXX) $(LDFLAGS) -o $@ $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) $(LDLIBS)

%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

Expand Down Expand Up @@ -269,7 +291,9 @@ $(SASSC_BIN): $(BUILD)
$(MAKE) -C $(SASS_SASSC_PATH) build-$(BUILD)-dev

sassc: $(SASSC_BIN)
ifndef WASM
$(SASSC_BIN) -v
endif

version: $(SASSC_BIN)
$(SASSC_BIN) -v
Expand All @@ -286,7 +310,7 @@ test_probe: $(SASSC_BIN)
$(RUBY_BIN) $(SASS_SPEC_PATH)/sass-spec.rb -c $(SASSC_BIN) --impl libsass --probe-todo $(LOG_FLAGS) $(SASS_SPEC_PATH)/$(SASS_SPEC_SPEC_DIR)

clean-objects: | lib
-$(RM) lib/*.a lib/*.so lib/*.dll lib/*.la
-$(RM) lib/*.a lib/*.so lib/*.dll lib/*.la lib/*.wasm
-$(RMDIR) lib
clean: clean-objects
$(RM) $(CLEANUPS)
Expand Down
16 changes: 16 additions & 0 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cctype>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <sys/stat.h>
#include "file.hpp"
#include "context.hpp"
Expand Down Expand Up @@ -49,6 +50,15 @@ inline static std::string wstring_to_string(const std::wstring &wstr)
# endif
#endif

#ifdef _WASM
inline static std::string get_cwd_from_env()
{
char* value = getenv("PWD");
if (!value) return "/";
return value;
}
#endif

namespace Sass {
namespace File {

Expand All @@ -57,6 +67,11 @@ namespace Sass {
// always with trailing slash
std::string get_cwd()
{
#ifdef _WASM
// the WASI does not implement getcwd() yet --
// check the environment variables or default to "/".
std::string cwd = get_cwd_from_env();
#else
const size_t wd_len = 4096;
#ifndef _WIN32
char wd[wd_len];
Expand All @@ -73,6 +88,7 @@ namespace Sass {
//convert backslashes to forward slashes
replace(cwd.begin(), cwd.end(), '\\', '/');
#endif
#endif
if (cwd[cwd.length() - 1] != '/') cwd += '/';
return cwd;
}
Expand Down
7 changes: 6 additions & 1 deletion src/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#ifndef _WASM
#include <dlfcn.h>
#endif
#endif

namespace Sass {

Expand Down Expand Up @@ -57,7 +59,7 @@ namespace Sass {
// load one specific plugin
bool Plugins::load_plugin (const std::string& path)
{

#ifdef ENABLE_LOAD_PLUGINS
typedef const char* (*__plugin_version__)(void);
typedef Sass_Function_List (*__plugin_load_fns__)(void);
typedef Sass_Importer_List (*__plugin_load_imps__)(void);
Expand Down Expand Up @@ -107,6 +109,9 @@ namespace Sass {
std::cerr << "failed loading plugin <" << path << ">" << std::endl;
if (const char* dlopen_error = dlerror()) std::cerr << dlopen_error << std::endl;
}
#else
std::cerr << "plugins loading is unsupported <" << path << ">" << std::endl;
#endif

return false;

Expand Down
2 changes: 2 additions & 0 deletions src/plugins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "utf8_string.hpp"
#include "sass/functions.h"

#ifdef ENABLE_LOAD_PLUGINS
#ifdef _WIN32

#define LOAD_LIB(var, path) HMODULE var = LoadLibraryW(UTF_8::convert_to_utf16(path).c_str())
Expand All @@ -24,6 +25,7 @@
#define CLOSE_LIB(var) dlclose(var)

#endif
#endif

namespace Sass {

Expand Down
5 changes: 5 additions & 0 deletions src/sass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
# endif
#endif

// enable loading of plugins for non-wasm
#ifndef _WASM
# define ENABLE_LOAD_PLUGINS
#endif

// path separation char
#ifndef PATH_SEP
# ifdef _WIN32
Expand Down
25 changes: 25 additions & 0 deletions wasm/libcxxabi_stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdlib.h>

// -fno-exceptions is not an option with libsass yet,
// stubbing libc++abi functions

void __cxa_throw(void *thrown_exception, void *tinfo,
void (*dest)(void *))
{
abort();
}

void *__cxa_allocate_exception(size_t thrown_size)
{
abort();
}

void __cxa_rethrow()
{
abort();
}

void* __cxa_begin_catch(void* exceptionObject)
{
abort();
}

0 comments on commit b58bef8

Please sign in to comment.