Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / config / external / wasm2c_sandbox_compiler / preprocess_wasm2c_config.py
blob3edb8f4e06f567d96f1f7e305937642f3b727b7e
1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Souce Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distibuted with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 import itertools
9 # The wasm2c source relies on CMAKE to generate a config file to be used to build the project
10 # Since we do not use cmake, this script automates the generation of a similar config suitable
11 # for firefox builds
12 # The script has a list of known variables it can replace and throws an error if it encounters a
13 # new variable (for instance when the in-tree source is updated)
15 # This python script knows how to replace the following variables normally configured by cmake for
16 # the wasm2c source
17 known_vars = [
18 '#cmakedefine WABT_VERSION_STRING "@WABT_VERSION_STRING@"',
19 "#cmakedefine WABT_DEBUG @WABT_DEBUG@",
20 "#cmakedefine01 HAVE_ALLOCA_H",
21 "#cmakedefine01 HAVE_UNISTD_H",
22 "#cmakedefine01 HAVE_SNPRINTF",
23 "#cmakedefine01 HAVE_SSIZE_T",
24 "#cmakedefine01 HAVE_STRCASECMP",
25 "#cmakedefine01 HAVE_WIN32_VT100",
26 "#cmakedefine01 WABT_BIG_ENDIAN",
27 "#cmakedefine01 HAVE_OPENSSL_SHA_H",
28 "#cmakedefine01 COMPILER_IS_CLANG",
29 "#cmakedefine01 COMPILER_IS_GNU",
30 "#cmakedefine01 COMPILER_IS_MSVC",
31 "#cmakedefine01 WITH_EXCEPTIONS",
32 "#define SIZEOF_SIZE_T @SIZEOF_SIZE_T@",
35 # The above variables are replaced with the code shown below
36 replaced_variables = """
37 // mozilla-config.h defines the following which is used
38 // - HAVE_ALLOCA_H
39 // - HAVE_UNISTD_H
40 #include "mozilla-config.h"
42 #define WABT_VERSION_STRING "Firefox-in-tree-version"
44 #define WABT_DEBUG 0
46 /* We don't require color printing of wasm2c errors on any platform */
47 #define HAVE_WIN32_VT100 0
49 #ifdef _WIN32
50 // Ignore whatever is set in mozilla-config.h wrt alloca because it is
51 // wrong when cross-compiling on Windows.
52 #undef HAVE_ALLOCA_H
53 // It is wrong when cross-compiling on Windows.
54 #undef HAVE_UNISTD_H
55 /* Whether ssize_t is defined by stddef.h */
56 #define HAVE_SSIZE_T 0
57 /* Whether strcasecmp is defined by strings.h */
58 #define HAVE_STRCASECMP 0
59 #else
60 #define HAVE_SSIZE_T 1
61 #define HAVE_STRCASECMP 1
62 #endif
64 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
65 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
66 # define WABT_BIG_ENDIAN 0
67 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
68 # define WABT_BIG_ENDIAN 1
69 # else
70 # error "Can't handle mixed-endian architectures"
71 # endif
72 #else
73 # error "Don't know how to determine endianness"
74 #endif
76 /* Use internal Pico-SHA. Never use OpenSSL */
77 #define HAVE_OPENSSL_SHA_H 0
79 /* Whether snprintf is defined by stdio.h */
80 #define HAVE_SNPRINTF 1
82 #if defined(_MSC_VER)
83 #define COMPILER_IS_GNU 0
84 #define COMPILER_IS_CLANG 0
85 #define COMPILER_IS_MSVC 1
86 #elif defined(__GNUC__)
87 #if defined(__clang__)
88 #define COMPILER_IS_GNU 0
89 #define COMPILER_IS_CLANG 1
90 #define COMPILER_IS_MSVC 0
91 #else
92 #define COMPILER_IS_GNU 1
93 #define COMPILER_IS_CLANG 0
94 #define COMPILER_IS_MSVC 0
95 #endif
96 #else
97 #error "Unknown compiler"
98 #endif
100 #define WITH_EXCEPTIONS 0
102 #if SIZE_MAX == 0xffffffffffffffff
103 #define SIZEOF_SIZE_T 8
104 #elif SIZE_MAX == 0xffffffff
105 #define SIZEOF_SIZE_T 4
106 #else
107 #error "Unknown size of size_t"
108 #endif
112 def generate_config(output, config_h_in):
113 file_config_h_in = open(config_h_in, "r")
114 lines = file_config_h_in.readlines()
116 # Remove the known cmake variables
117 for known_var in known_vars:
118 lines = [x for x in lines if not x.startswith(known_var)]
120 # Do a sanity check to make sure there are no unknown variables
121 remaining_vars = [x for x in lines if x.startswith("#cmakedefine") or "@" in x]
122 if len(remaining_vars) > 0:
123 raise BaseException("Unknown cmake variables: " + str(remaining_vars))
125 pos = lines.index("#define WABT_CONFIG_H_\n")
126 skipped = itertools.takewhile(
127 lambda x: not (x.strip()) or x.startswith("#include "), lines[pos + 1 :]
129 pos += len(list(skipped))
130 pre_include_lines = lines[0:pos]
131 post_include_lines = lines[pos:]
132 output_str = (
133 "".join(pre_include_lines)
134 + "\n"
135 + replaced_variables
136 + "\n"
137 + "".join(post_include_lines)
139 output.write(output_str)