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