Use emscripten from Clang compiled executable

1.5k views Asked by At

Is it possible to execute emcc (from emscripten) on a clang compiled executable ?

I tried but the result is:

ERROR root: pdfium_test: Input file has an unknown suffix, don't know what to do with it!

I try that because I'm not able to find a solution to compile PDFium project with emcc, but with clang everything is fine.

The reason is:

Emscripten is a cross-compiler, and therefore the OS-specific macros of the host system should all be undefined when building C/C++ code. If you look at tools/shared.py, Emscripten gives special attention to -U all host-specific flags that Clang may automatically try to add in.

But there is lots of Platform specific code in PDFium, so I get:

#error Sorry, can not figure out target OS. Please specify _FX_OS_ macro.

This macro is defined if the __linux__ macro (for example) is defined, here is the code snippet:

#ifndef _FX_OS_
#if defined(__ANDROID__)
#define _FX_OS_ _FX_ANDROID_
#define _FXM_PLATFORM_ _FXM_PLATFORM_ANDROID_
#elif defined(_WIN32)
#define _FX_OS_ _FX_WIN32_DESKTOP_
#define _FXM_PLATFORM_ _FXM_PLATFORM_WINDOWS_
#elif defined(_WIN64)
#define _FX_OS_ _FX_WIN64_DESKTOP_
#define _FXM_PLATFORM_ _FXM_PLATFORM_WINDOWS_
#elif defined(__linux__)
#define _FX_OS_ _FX_LINUX_DESKTOP_
#define _FXM_PLATFORM_ _FXM_PLATFORM_LINUX_
#elif defined(__APPLE__)
#define _FX_OS_ _FX_MACOSX_
#define _FXM_PLATFORM_ _FXM_PLATFORM_APPLE_
#endif
#endif  // _FX_OS_ 

#if !defined(_FX_OS_) || _FX_OS_ == 0
#error Sorry, can not figure out target OS. Please specify _FX_OS_ macro.
#endif

So, I tried to define manually the __linux__ macro with:

emmake make -j5 BUILDTYPE=Release __linux__=1

... but same error. Maybe it's not the good way ?

Thank you in advance.

EDIT: The answer of JF Bastien helps me a lot. But now I've this build error and I've any idea of what to do. If someone have an idea...

clang-3.7: warning: argument unused during compilation: '-msse2'
clang-3.7: warning: argument unused during compilation: '-mmmx'
error: unknown FP unit 'sse'

EDIT 2: Solution for above problem: remove "-msse2, -mmmx and -mfpmath" flags from v8/build/toolchain.gypi

1

There are 1 answers

1
JF Bastien On BEST ANSWER

Porting to Emscripten is the same as porting to any other platform: you have to use that's platform's own platform-specific headers. Some will have nice equivalents, and some won't.

In most cases you'll need to find these chains of platform-specific #if defined(...) and add an #elif defined(__EMSCRIPTEN__), and do the right thing there. That's more complicated than it sounds: you can't do inline assembly, you can't rely on (most) platform-specific headers, ... But in some cases it's easy.

Emscripten has examples which do this, and has a porting guide.

For PDFium in particular, you'll have to avoid all the platform-specific font rendering, any threading-related things, and the sandboxing (security shouldn't be as big of an issue since JavaScript itself is a sandbox). You'll also have to figure out how to do file I/O, and probably want to disable all networking code.

Or you could use other ports of PDFium to Emscripten.