Blob Blame History Raw
# HG changeset patch
# User Rob Lemley <rob@thunderbird.net>
# Date 1662996529 0
#      Mon Sep 12 15:28:49 2022 +0000
# Node ID c9e44c0a569253884961ad2e18fae23f5ed0f6dc
# Parent  5dfb405f325609c62215f9d74e01dba029b84611
Bug 1790446 - Add build script to preprocess CMake config.h templates. r=dandarnell



Right now config.h.in is rewritten when the RNP source is updated.
This has caused problems when new lines are added to it.

Depends on D157151

Differential Revision: https://phabricator.services.mozilla.com/D157152

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