Skip to content

Commit f99af02

Browse files
authored
Move emscripten version macros to emscripten/version.h (#16147)
This helper make emcc more like vanilla clang. See: #15703
1 parent d429da3 commit f99af02

File tree

7 files changed

+31
-7
lines changed

7 files changed

+31
-7
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.4
2222
-----
23+
- The `__EMSCRIPTEN_major__`, `__EMSCRIPTEN_minor__` and `__EMSCRIPTEN_tiny__`
24+
macros are now available via the `emscripten/version.h` header file. For the
25+
time being, unless you enable `-sSTRICT`, these are still also defined
26+
directly on the command line. If you use these macros please make sure you
27+
include `emscripten/version.h` (or `emscripten.h` which indirectly includes
28+
it). (#16147)
2329

2430
3.1.3 - 31/01/2022
2531
------------------

emcc.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,12 @@ def get_cflags(user_args):
836836
# The preprocessor define EMSCRIPTEN is deprecated. Don't pass it to code
837837
# in strict mode. Code should use the define __EMSCRIPTEN__ instead.
838838
cflags.append('-DEMSCRIPTEN')
839+
# The __EMSCRIPTEN_major__/__EMSCRIPTEN_minor__/__EMSCRIPTEN_tiny__ macros
840+
# are now defined in emscripten/version.h but in non-strict mode, we continue
841+
# to define them here (for now) for backwards compatibility.
842+
cflags += ['-D__EMSCRIPTEN_major__=' + str(shared.EMSCRIPTEN_VERSION_MAJOR),
843+
'-D__EMSCRIPTEN_minor__=' + str(shared.EMSCRIPTEN_VERSION_MINOR),
844+
'-D__EMSCRIPTEN_tiny__=' + str(shared.EMSCRIPTEN_VERSION_TINY)]
839845

840846
# if exception catching is disabled, we can prevent that code from being
841847
# generated in the frontend
@@ -873,10 +879,7 @@ def get_cflags(user_args):
873879

874880
# Set the LIBCPP ABI version to at least 2 so that we get nicely aligned string
875881
# data and other nice fixes.
876-
cflags += ['-D__EMSCRIPTEN_major__=' + str(shared.EMSCRIPTEN_VERSION_MAJOR),
877-
'-D__EMSCRIPTEN_minor__=' + str(shared.EMSCRIPTEN_VERSION_MINOR),
878-
'-D__EMSCRIPTEN_tiny__=' + str(shared.EMSCRIPTEN_VERSION_TINY),
879-
'-D_LIBCPP_ABI_VERSION=2']
882+
cflags += ['-D_LIBCPP_ABI_VERSION=2']
880883

881884
if not settings.USE_PTHREADS:
882885
# There is no point in using thread safe static inits in code that cannot

site/source/docs/compiling/Building-Projects.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ Detecting Emscripten in Preprocessor
339339
Emscripten provides the following preprocessor macros that can be used to identify the compiler version and platform:
340340

341341
* The preprocessor define ``__EMSCRIPTEN__`` is always defined when compiling programs with Emscripten.
342-
* The preprocessor variables ``__EMSCRIPTEN_major__``, ``__EMSCRIPTEN_minor__`` and ``__EMSCRIPTEN_tiny__`` specify, as integers, the currently used Emscripten compiler version.
342+
* The preprocessor variables ``__EMSCRIPTEN_major__``, ``__EMSCRIPTEN_minor__``
343+
and ``__EMSCRIPTEN_tiny__`` are defined in ``emscripten/version.h`` and
344+
specify, as integers, the currently used Emscripten compiler version.
343345
* Emscripten behaves like a variant of Unix, so the preprocessor defines ``unix``, ``__unix`` and ``__unix__`` are always present when compiling code with Emscripten.
344346
* Emscripten uses Clang/LLVM as its underlying codegen compiler, so the preprocessor defines ``__llvm__`` and ``__clang__`` are defined, and the preprocessor defines ``__clang_major__``, ``__clang_minor__`` and ``__clang_patchlevel__`` indicate the version of Clang that is used.
345347
* Clang/LLVM is GCC-compatible, so the preprocessor defines ``__GNUC__``, ``__GNUC_MINOR__`` and ``__GNUC_PATCHLEVEL__`` are also defined to represent the level of GCC compatibility that Clang/LLVM provides.

system/include/compat/arm_neon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@
285285
# undef HEDLEY_EMSCRIPTEN_VERSION
286286
#endif
287287
#if defined(__EMSCRIPTEN__)
288+
#include <emscripten/version.h>
288289
# define HEDLEY_EMSCRIPTEN_VERSION HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
289290
#endif
290291

system/include/emscripten/emscripten.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "em_types.h"
2626
#include "em_js.h"
2727
#include "wget.h"
28+
#include "version.h"
2829

2930
#ifdef __cplusplus
3031
extern "C" {

tests/test_other.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4407,19 +4407,20 @@ def test_readdir_r_silly(self):
44074407
def test_emversion(self):
44084408
create_file('src.cpp', r'''
44094409
#include <stdio.h>
4410+
#include <emscripten/version.h>
44104411
int main() {
44114412
printf("major: %d\n", __EMSCRIPTEN_major__);
44124413
printf("minor: %d\n", __EMSCRIPTEN_minor__);
44134414
printf("tiny: %d\n", __EMSCRIPTEN_tiny__);
44144415
}
44154416
''')
4416-
self.run_process([EMXX, 'src.cpp'])
44174417
expected = '''\
44184418
major: %d
44194419
minor: %d
44204420
tiny: %d
44214421
''' % (shared.EMSCRIPTEN_VERSION_MAJOR, shared.EMSCRIPTEN_VERSION_MINOR, shared.EMSCRIPTEN_VERSION_TINY)
4422-
self.assertContained(expected, self.run_js('a.out.js'))
4422+
self.do_runf('src.cpp', expected)
4423+
self.do_runf('src.cpp', expected, emcc_args=['-sSTRICT'])
44234424

44244425
def test_libc_files_without_syscalls(self):
44254426
# a program which includes FS due to libc js library support, but has no syscalls,

tools/system_libs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import os
1111
import shutil
12+
import textwrap
1213
from enum import IntEnum, auto
1314
from glob import iglob
1415

@@ -1873,6 +1874,15 @@ def install_system_headers(stamp):
18731874
cmake_dest = shared.Cache.get_sysroot_dir('lib', 'cmake')
18741875
copytree_exist_ok(cmake_src, cmake_dest)
18751876

1877+
# Create a version header based on the emscripten-version.txt
1878+
version_file = os.path.join(shared.Cache.get_include_dir(), 'emscripten/version.h')
1879+
utils.write_file(version_file, textwrap.dedent(f'''\
1880+
/* Automatically generated by tools/system_libs.py */
1881+
#define __EMSCRIPTEN_major__ {shared.EMSCRIPTEN_VERSION_MAJOR}
1882+
#define __EMSCRIPTEN_minor__ {shared.EMSCRIPTEN_VERSION_MINOR}
1883+
#define __EMSCRIPTEN_tiny__ {shared.EMSCRIPTEN_VERSION_TINY}
1884+
'''))
1885+
18761886
# Create a stamp file that signal the the header have been installed
18771887
# Removing this file, or running `emcc --clear-cache` or running
18781888
# `./embuilder build sysroot --force` will cause the re-installation of

0 commit comments

Comments
 (0)