b0b627
# HG changeset patch
b0b627
# User Rob Lemley <rob@thunderbird.net>
b0b627
# Date 1662996529 0
b0b627
#      Mon Sep 12 15:28:49 2022 +0000
b0b627
# Node ID c9e44c0a569253884961ad2e18fae23f5ed0f6dc
b0b627
# Parent  5dfb405f325609c62215f9d74e01dba029b84611
b0b627
Bug 1790446 - Add build script to preprocess CMake config.h templates. r=dandarnell
b0b627
b0b627
b0b627
b0b627
Right now config.h.in is rewritten when the RNP source is updated.
b0b627
This has caused problems when new lines are added to it.
b0b627
b0b627
Depends on D157151
b0b627
b0b627
Differential Revision: https://phabricator.services.mozilla.com/D157152
b0b627
b0b627
diff --git a/comm/python/rocbuild/process_cmake_define_files.py b/python/rocb/commuild/process_cmake_define_files.py
b0b627
new file mode 100644
b0b627
--- /dev/null
b0b627
+++ b/comm/python/rocbuild/process_cmake_define_files.py
b0b627
@@ -0,0 +1,103 @@
b0b627
+# This Source Code Form is subject to the terms of the Mozilla Public
b0b627
+# License, v. 2.0. If a copy of the MPL was not distributed with this
b0b627
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
b0b627
+
b0b627
+from __future__ import absolute_import, print_function, unicode_literals
b0b627
+
b0b627
+import argparse
b0b627
+import os
b0b627
+import re
b0b627
+import sys
b0b627
+from buildconfig import topsrcdir, topobjdir
b0b627
+from mozbuild.backend.configenvironment import PartialConfigEnvironment
b0b627
+
b0b627
+
b0b627
+def define_type(string):
b0b627
+    vals = string.split("=", 1)
b0b627
+    if len(vals) == 1:
b0b627
+        vals.append(1)
b0b627
+    elif vals[1].isdecimal():
b0b627
+        vals[1] = int(vals[1])
b0b627
+    return tuple(vals)
b0b627
+
b0b627
+
b0b627
+def process_cmake_define_file(output, input_file, extra_defines):
b0b627
+    """Creates the given config header. A config header is generated by
b0b627
+    taking the corresponding source file and replacing some #define/#undef
b0b627
+    occurences:
b0b627
+        "#undef NAME" is turned into "#define NAME VALUE"
b0b627
+        "#cmakedefine NAME" is turned into "#define NAME VALUE"
b0b627
+        "#define NAME" is unchanged
b0b627
+        "#define NAME ORIGINAL_VALUE" is turned into "#define NAME VALUE"
b0b627
+        "#undef UNKNOWN_NAME" is turned into "/* #undef UNKNOWN_NAME */"
b0b627
+        "#cmakedefine UNKNOWN_NAME" is turned into "/* #undef UNKNOWN_NAME */"
b0b627
+        Whitespaces are preserved.
b0b627
+    """
b0b627
+
b0b627
+    path = os.path.abspath(input_file)
b0b627
+
b0b627
+    config = PartialConfigEnvironment(topobjdir)
b0b627
+
b0b627
+    defines = dict(config.defines.iteritems())
b0b627
+    defines.update(extra_defines)
b0b627
+
b0b627
+    with open(path, "r") as input_file:
b0b627
+        r = re.compile(
b0b627
+            r'^\s*#\s*(?P<cmd>[a-z]+)(?:\s+(?P<name>\S+)(?:\s+(?P<value>("[^"]+"|\S+)))?)?',
b0b627
+            re.U,
b0b627
+        )
b0b627
+        for line in input_file:
b0b627
+            m = r.match(line)
b0b627
+            if m:
b0b627
+                cmd = m.group("cmd")
b0b627
+                name = m.group("name")
b0b627
+                value = m.group("value")
b0b627
+                if name:
b0b627
+                    if cmd == "define":
b0b627
+                        if value and name in defines:
b0b627
+                            line = (
b0b627
+                                line[: m.start("value")]
b0b627
+                                + str(defines[name])
b0b627
+                                + line[m.end("value") :]
b0b627
+                            )
b0b627
+                    elif cmd in ("undef", "cmakedefine"):
b0b627
+                        if name in defines:
b0b627
+                            line = (
b0b627
+                                line[: m.start("cmd")]
b0b627
+                                + "define"
b0b627
+                                + line[m.end("cmd") : m.end("name")]
b0b627
+                                + " "
b0b627
+                                + str(defines[name])
b0b627
+                                + line[m.end("name") :]
b0b627
+                            )
b0b627
+                        else:
b0b627
+                            line = (
b0b627
+                                "/* #undef "
b0b627
+                                + line[m.start("name") : m.end("name")]
b0b627
+                                + " */"
b0b627
+                                + line[m.end("name") :]
b0b627
+                            )
b0b627
+
b0b627
+            output.write(line)
b0b627
+
b0b627
+
b0b627
+def main(output, *argv):
b0b627
+    parser = argparse.ArgumentParser(description="Process define files.")
b0b627
+
b0b627
+    parser.add_argument("input", help="Input define file.")
b0b627
+    parser.add_argument(
b0b627
+        "-D",
b0b627
+        type=define_type,
b0b627
+        action="append",
b0b627
+        dest="extra_defines",
b0b627
+        default=[],
b0b627
+        help="Additional defines not set at configure time.",
b0b627
+    )
b0b627
+
b0b627
+    args = parser.parse_args(argv)
b0b627
+
b0b627
+    return process_cmake_define_file(output, args.input, args.extra_defines)
b0b627
+
b0b627
+
b0b627
+if __name__ == "__main__":
b0b627
+    sys.exit(main(*sys.argv))
b0b627
diff --git a/comm/third_party/rnp/moz.build b/third_party/rnp/moz.b/commuild
b0b627
--- a/comm/third_party/rnp/moz.build
b0b627
+++ b/comm/third_party/rnp/moz.build
b0b627
@@ -34,19 +34,27 @@ COMPILE_FLAGS["WARNINGS_CFLAGS"] += [
b0b627
 if CONFIG["CC_TYPE"] == "clang-cl":
b0b627
     CXXFLAGS += [
b0b627
         "/EHs",
b0b627
     ]
b0b627
 
b0b627
-DEFINES["_GNU_SOURCE"] = True
b0b627
-
b0b627
-DEFINES["HAVE_BZLIB_H"] = True
b0b627
-DEFINES["HAVE_ZLIB_H"] = True
b0b627
-DEFINES["MOZ_RNP_DIST_INFO"] = rnp_dist_info
b0b627
-
b0b627
-CONFIGURE_DEFINE_FILES += [
b0b627
+rnp_defines = {
b0b627
+    "HAVE_BZLIB_H": True,
b0b627
+    "HAVE_ZLIB_H": True,
b0b627
+    "CRYPTO_BACKEND_BOTAN": True,
b0b627
+    "ENABLE_AEAD": True,
b0b627
+    "ENABLE_TWOFISH": True,
b0b627
+    "ENABLE_BRAINPOOL": True,
b0b627
+}
b0b627
+GeneratedFile(
b0b627
     "src/lib/config.h",
b0b627
-]
b0b627
+    script="/comm/python/rocbuild/process_cmake_define_files.py",
b0b627
+    inputs=["src/lib/config.h.in"],
b0b627
+    flags=[
b0b627
+        "-D%s=%s" % (k, "1" if v is True else v)
b0b627
+        for k, v in rnp_defines.items()
b0b627
+    ],
b0b627
+)
b0b627
 
b0b627
 LOCAL_INCLUDES = [
b0b627
     "include",
b0b627
     "src",
b0b627
     "src/common",
b0b627
diff --git a/comm/third_party/rnpdefs.mozbuild b/third_party/rnpdefs.mozb/commuild
b0b627
--- a/comm/third_party/rnpdefs.mozbuild
b0b627
+++ b/comm/third_party/rnpdefs.mozbuild
b0b627
@@ -16,17 +16,10 @@ rnp_dist_info = "{} {} rnp".format(
b0b627
 COMPILE_FLAGS["OS_CFLAGS"] = []
b0b627
 COMPILE_FLAGS["OS_CXXFLAGS"] = []
b0b627
 COMPILE_FLAGS["OS_INCLUDES"] = []
b0b627
 COMPILE_FLAGS["CLANG_PLUGIN"] = []
b0b627
 
b0b627
-DEFINES["RNP_NO_DEPRECATED"] = True
b0b627
-DEFINES["CRYPTO_BACKEND_BOTAN"] = True
b0b627
-DEFINES["ENABLE_AEAD"] = True
b0b627
-DEFINES["ENABLE_TWOFISH"] = True
b0b627
-DEFINES["ENABLE_BRAINPOOL"] = True
b0b627
-
b0b627
-
b0b627
 if CONFIG["COMPILE_ENVIRONMENT"]:
b0b627
     COMPILE_FLAGS["MOZ_HARDENING_CFLAGS"] = []
b0b627
 
b0b627
 if CONFIG["CC_TYPE"] == "clang-cl":
b0b627
     CFLAGS += [