Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare library to be used with wasm32-wasi #2907

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -166,18 +175,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 @@ -203,8 +222,8 @@ 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)
Expand All @@ -217,6 +236,9 @@ 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 @@ -283,7 +305,9 @@ $(SASSC_BIN): $(BUILD)
$(MAKE) -C $(SASS_SASSC_PATH) build-$(BUILD)

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

version: $(SASSC_BIN)
$(SASSC_BIN) -v
Expand Down Expand Up @@ -311,7 +335,7 @@ test_interactive: $(SASSC_BIN)
--interactive $(LOG_FLAGS) $(SASS_SPEC_PATH)/$(SASS_SPEC_SPEC_DIR)

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

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

namespace Sass {
namespace File {

Expand All @@ -56,6 +66,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 @@ -72,6 +87,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 Expand Up @@ -173,7 +189,9 @@ namespace Sass {
while((pos = path.find("/./", pos)) != std::string::npos) path.erase(pos, 2);

// remove all leading and trailing self references
while(path.size() >= 2 && path[0] == '.' && path[1] == '/') path.erase(0, 2);
#ifndef PREOPEN_COMPATIBLE
while(path.size() >= 2 && path[0] == '.' && path[1] == '/') path.erase(0, 2);
#endif
while((pos = path.length()) > 1 && path[pos - 2] == '/' && path[pos - 1] == '.') path.erase(pos - 2);


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
7 changes: 7 additions & 0 deletions src/sass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
# endif
#endif

// enable loading of plugins for non-wasm
#ifndef _WASM
# define ENABLE_LOAD_PLUGINS
#else
# define PREOPEN_COMPATIBLE
#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();
}