Debian Codegraph @

config/external/wasm2c_sandbox_compiler/preprocess_wasm2c_config.py

raw
1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source 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/.
6
7 import itertools
8
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)
14
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@",
33 ]
34
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"
41
42 #define WABT_VERSION_STRING "Firefox-in-tree-version"
43
44 #define WABT_DEBUG 0
45
46 /* We don't require color printing of wasm2c errors on any platform */
47 #define HAVE_WIN32_VT100 0
48
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
63
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
75
76 /* Use internal Pico-SHA. Never use OpenSSL */
77 #define HAVE_OPENSSL_SHA_H 0
78
79 /* Whether snprintf is defined by stdio.h */
80 #define HAVE_SNPRINTF 1
81
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
99
100 #define WITH_EXCEPTIONS 0
101
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
109 """
110
111
112 def generate_config(output, config_h_in):
113 file_config_h_in = open(config_h_in)
114 lines = file_config_h_in.readlines()
115
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)]
119
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))
124
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 :]
128 )
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)
138 )
139 output.write(output_str)