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