Search code examples
cwebassembly

What is the compiler-defined macro for WASM?


What is the macro that clang and/or gcc would define when compiling for a WASM backend?

To clarify, one can write platform-specific code using macros the compiler defines like so:

#if _WIN32
// Windows-specific code
#elif __linux__
// Linux-specific code
#elif __APPLE__
// macOS-specific code
#else
#error Unsupported platform
#endif

I would like to do the same thing specifying WebAssembly as one of the potential backends.


Solution

  • As per @Jonathan Leffler's comment, there does not appear to be a standard macro that is defined across compilers.

    My current solution for working with different compilers is to create a separate build job for WASM that defines a macro. For gcc and clang, it passes the flag -D__WASM__ to define a __WASM__ macro.

    In my setup, I just change an environment variable and my build script selects the appropriate build flags.