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