Skip to content

Commit 390e8e0

Browse files
committed
WebAssembly: Prepare library to be used with wasm32-wasi
It is the changes I made to make compilation work for clang with "wasm32-wasi" target and the wasi-sdk.
1 parent c713140 commit 390e8e0

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

Makefile

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ else
2727
endif
2828
CAT ?= $(if $(filter $(OS),Windows_NT),type,cat)
2929

30+
ifdef WASM
31+
CFLAGS += -D_WASM
32+
CXXFLAGS += -D_WASM
33+
endif
34+
35+
ifdef WASM
36+
UNAME := WebAssembly
37+
else
3038
ifneq (,$(findstring /cygdrive/,$(PATH)))
3139
UNAME := Cygwin
3240
else
@@ -44,6 +52,7 @@ endif
4452
endif
4553
endif
4654
endif
55+
endif
4756

4857
ifndef LIBSASS_VERSION
4958
ifneq ($(wildcard ./.git/ ),)
@@ -163,18 +172,28 @@ ifeq (Windows,$(UNAME))
163172
LIB_SHARED = $(SASS_LIBSASS_PATH)/lib/libsass.dll
164173
endif
165174
else
175+
ifdef WASM
176+
SASSC_BIN = $(SASS_SASSC_PATH)/bin/sassc.wasm
177+
SHAREDLIB = lib/libsass.wasm
178+
LIB_SHARED = $(SASS_LIBSASS_PATH)/lib/libsass.wasm
179+
else
166180
ifneq (Cygwin,$(UNAME))
167181
CFLAGS += -fPIC
168182
CXXFLAGS += -fPIC
169183
LDFLAGS += -fPIC
170184
endif
171185
endif
186+
endif
172187

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

193+
ifdef WASM
194+
WASMOBJECTS = wasm/libcxxabi_stubs.o
195+
endif
196+
178197
DEBUG_LVL ?= NONE
179198

180199
CLEANUPS ?=
@@ -200,15 +219,18 @@ debug-shared: shared
200219
lib:
201220
$(MKDIR) lib
202221

203-
lib/libsass.a: $(COBJECTS) $(OBJECTS) | lib
204-
$(AR) rcvs $@ $(COBJECTS) $(OBJECTS)
222+
lib/libsass.a: $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) | lib
223+
$(AR) rcvs $@ $(COBJECTS) $(OBJECTS) $(WASMOBJECTS)
205224

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

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

231+
lib/libsass.wasm: $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) | lib
232+
$(CXX) $(LDFLAGS) -o $@ $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) $(LDLIBS)
233+
212234
%.o: %.c
213235
$(CC) $(CFLAGS) -c -o $@ $<
214236

@@ -271,7 +293,9 @@ $(SASSC_BIN): $(BUILD)
271293
$(MAKE) -C $(SASS_SASSC_PATH) build-$(BUILD)-dev
272294

273295
sassc: $(SASSC_BIN)
296+
ifndef WASM
274297
$(SASSC_BIN) -v
298+
endif
275299

276300
version: $(SASSC_BIN)
277301
$(SASSC_BIN) -v
@@ -288,7 +312,7 @@ test_probe: $(SASSC_BIN)
288312
$(RUBY_BIN) $(SASS_SPEC_PATH)/sass-spec.rb -c $(SASSC_BIN) --impl libsass --probe-todo $(LOG_FLAGS) $(SASS_SPEC_PATH)/$(SASS_SPEC_SPEC_DIR)
289313

290314
clean-objects: | lib
291-
-$(RM) lib/*.a lib/*.so lib/*.dll lib/*.la
315+
-$(RM) lib/*.a lib/*.so lib/*.dll lib/*.la lib/*.wasm
292316
-$(RMDIR) lib
293317
clean: clean-objects
294318
$(RM) $(CLEANUPS)

src/file.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <cstdio>
1717
#include <vector>
1818
#include <algorithm>
19+
#include <stdlib.h>
1920
#include <sys/stat.h>
2021
#include "file.hpp"
2122
#include "context.hpp"
@@ -48,6 +49,15 @@ inline static std::string wstring_to_string(const std::wstring &wstr)
4849
# endif
4950
#endif
5051

52+
#ifdef _WASM
53+
inline static std::string get_cwd_from_env()
54+
{
55+
char* value = getenv("PWD");
56+
if (!value) return "/";
57+
return value;
58+
}
59+
#endif
60+
5161
namespace Sass {
5262
namespace File {
5363

@@ -56,6 +66,11 @@ namespace Sass {
5666
// always with trailing slash
5767
std::string get_cwd()
5868
{
69+
#ifdef _WASM
70+
// the WASI does not implement getcwd() yet --
71+
// check the environment variables or default to "/".
72+
std::string cwd = get_cwd_from_env();
73+
#else
5974
const size_t wd_len = 4096;
6075
#ifndef _WIN32
6176
char wd[wd_len];
@@ -72,6 +87,7 @@ namespace Sass {
7287
//convert backslashes to forward slashes
7388
replace(cwd.begin(), cwd.end(), '\\', '/');
7489
#endif
90+
#endif
7591
if (cwd[cwd.length() - 1] != '/') cwd += '/';
7692
return cwd;
7793
}

src/plugins.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include <sys/types.h>
1414
#include <dirent.h>
1515
#include <errno.h>
16+
#ifndef _WASM
1617
#include <dlfcn.h>
1718
#endif
19+
#endif
1820

1921
namespace Sass {
2022

@@ -57,7 +59,7 @@ namespace Sass {
5759
// load one specific plugin
5860
bool Plugins::load_plugin (const std::string& path)
5961
{
60-
62+
#ifdef ENABLE_LOAD_PLUGINS
6163
typedef const char* (*__plugin_version__)(void);
6264
typedef Sass_Function_List (*__plugin_load_fns__)(void);
6365
typedef Sass_Importer_List (*__plugin_load_imps__)(void);
@@ -107,6 +109,9 @@ namespace Sass {
107109
std::cerr << "failed loading plugin <" << path << ">" << std::endl;
108110
if (const char* dlopen_error = dlerror()) std::cerr << dlopen_error << std::endl;
109111
}
112+
#else
113+
std::cerr << "plugins loading is unsupported <" << path << ">" << std::endl;
114+
#endif
110115

111116
return false;
112117

src/plugins.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "utf8_string.hpp"
77
#include "sass/functions.h"
88

9+
#ifdef ENABLE_LOAD_PLUGINS
910
#ifdef _WIN32
1011

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

2627
#endif
28+
#endif
2729

2830
namespace Sass {
2931

src/sass.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
# endif
3939
#endif
4040

41+
// enable loading of plugins for non-wasm
42+
#ifndef _WASM
43+
# define ENABLE_LOAD_PLUGINS
44+
#endif
45+
4146
// path separation char
4247
#ifndef PATH_SEP
4348
# ifdef _WIN32

wasm/libcxxabi_stubs.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdlib.h>
2+
3+
// -fno-exceptions is not an option with libsass yet,
4+
// stubbing libc++abi functions
5+
6+
void __cxa_throw(void *thrown_exception, void *tinfo,
7+
void (*dest)(void *))
8+
{
9+
abort();
10+
}
11+
12+
void *__cxa_allocate_exception(size_t thrown_size)
13+
{
14+
abort();
15+
}
16+
17+
void __cxa_rethrow()
18+
{
19+
abort();
20+
}
21+
22+
void* __cxa_begin_catch(void* exceptionObject)
23+
{
24+
abort();
25+
}

0 commit comments

Comments
 (0)