Skip to content

Commit b58bef8

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 5ebc5d5 commit b58bef8

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/ ),)
@@ -161,18 +170,28 @@ ifeq (Windows,$(UNAME))
161170
LIB_SHARED = $(SASS_LIBSASS_PATH)/lib/libsass.dll
162171
endif
163172
else
173+
ifdef WASM
174+
SASSC_BIN = $(SASS_SASSC_PATH)/bin/sassc.wasm
175+
SHAREDLIB = lib/libsass.wasm
176+
LIB_SHARED = $(SASS_LIBSASS_PATH)/lib/libsass.wasm
177+
else
164178
ifneq (Cygwin,$(UNAME))
165179
CFLAGS += -fPIC
166180
CXXFLAGS += -fPIC
167181
LDFLAGS += -fPIC
168182
endif
169183
endif
184+
endif
170185

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

191+
ifdef WASM
192+
WASMOBJECTS = wasm/libcxxabi_stubs.o
193+
endif
194+
176195
DEBUG_LVL ?= NONE
177196

178197
CLEANUPS ?=
@@ -198,15 +217,18 @@ debug-shared: shared
198217
lib:
199218
$(MKDIR) lib
200219

201-
lib/libsass.a: $(COBJECTS) $(OBJECTS) | lib
202-
$(AR) rcvs $@ $(COBJECTS) $(OBJECTS)
220+
lib/libsass.a: $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) | lib
221+
$(AR) rcvs $@ $(COBJECTS) $(OBJECTS) $(WASMOBJECTS)
203222

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

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

229+
lib/libsass.wasm: $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) | lib
230+
$(CXX) $(LDFLAGS) -o $@ $(COBJECTS) $(OBJECTS) $(WASMOBJECTS) $(LDLIBS)
231+
210232
%.o: %.c
211233
$(CC) $(CFLAGS) -c -o $@ $<
212234

@@ -269,7 +291,9 @@ $(SASSC_BIN): $(BUILD)
269291
$(MAKE) -C $(SASS_SASSC_PATH) build-$(BUILD)-dev
270292

271293
sassc: $(SASSC_BIN)
294+
ifndef WASM
272295
$(SASSC_BIN) -v
296+
endif
273297

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

288312
clean-objects: | lib
289-
-$(RM) lib/*.a lib/*.so lib/*.dll lib/*.la
313+
-$(RM) lib/*.a lib/*.so lib/*.dll lib/*.la lib/*.wasm
290314
-$(RMDIR) lib
291315
clean: clean-objects
292316
$(RM) $(CLEANUPS)

src/file.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cctype>
1919
#include <vector>
2020
#include <algorithm>
21+
#include <stdlib.h>
2122
#include <sys/stat.h>
2223
#include "file.hpp"
2324
#include "context.hpp"
@@ -49,6 +50,15 @@ inline static std::string wstring_to_string(const std::wstring &wstr)
4950
# endif
5051
#endif
5152

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

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

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)