libtiff: Merge branch 'do_not_format_tiffvers_h' into 'master'

From ac45771ea43551e1f66685908dc42c676bef8baf Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Sat, 10 Dec 2022 14:02:22 +0100
Subject: [PATCH 1/4] Exclude reformatting of tiffvers.h which breaks version
 detection for FindTIFF.cmake

---
 .pre-commit-config.yaml | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 27aba544..dbad088d 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -3,7 +3,11 @@ repos:
         rev: 'v15.0.4'
         hooks:
         -   id: clang-format
+            # tif_fax3sm.c is a generated file
+            # tiffvers.h as well, and its formatting is sensitive for
+            # FindTIFF.cmake to detect the version number
             exclude: >
               (?x)^(
-                libtiff/tif_fax3sm.c
+                libtiff/tif_fax3sm.c|
+                libtiff/tiffvers.h
               )

From 234971dfd8c2031c331acb5385a0bfd0b3bfc4bf Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sat, 10 Dec 2022 14:20:17 +0100
Subject: [PATCH 2/4] tiffvers.h: revert formatting

---
 libtiff/tiffvers.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libtiff/tiffvers.h b/libtiff/tiffvers.h
index ad21a413..ca958869 100644
--- a/libtiff/tiffvers.h
+++ b/libtiff/tiffvers.h
@@ -1,6 +1,4 @@
-#define TIFFLIB_VERSION_STR                                                    \
-    "LIBTIFF, Version 4.5.0\nCopyright (c) 1988-1996 Sam Leffler\nCopyright "  \
-    "(c) 1991-1996 Silicon Graphics, Inc."
+#define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.5.0\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
 /*
  * This define can be used in code that requires
  * compilation-related definitions specific to a

From 2e7640c966e5c3bf32455d2e6212b6a5be6603d8 Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sat, 10 Dec 2022 14:26:54 +0100
Subject: [PATCH 3/4] tiffvers.h: add TIFFLIB_MAJOR_VERSION,
 TIFFLIB_MINOR_VERSION, TIFFLIB_MICRO_VERSION defines

Also add a TIFFLIB_AT_LEAST() macro
---
 Makefile.am              |  2 +-
 RELEASE-DATE             |  2 +-
 libtiff/tiffvers.h       | 16 +++++++++++++++-
 libtiff/tiffvers.h.in    | 14 ++++++++++++++
 test/test_open_options.c | 25 +++++++++++++++++++++++++
 5 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 254ab7d9..2f07eec7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -65,7 +65,7 @@ SUBDIRS = port libtiff tools build contrib test doc
 release:
 	(rm -f $(top_srcdir)/RELEASE-DATE && echo $(LIBTIFF_RELEASE_DATE) > $(top_srcdir)/RELEASE-DATE)
 	(rm -f $(top_srcdir)/VERSION && echo $(LIBTIFF_VERSION) > $(top_srcdir)/VERSION)
-	(rm -f $(top_srcdir)/libtiff/tiffvers.h && sed 's,LIBTIFF_VERSION,$(LIBTIFF_VERSION),;s,LIBTIFF_RELEASE_DATE,$(LIBTIFF_RELEASE_DATE),' $(top_srcdir)/libtiff/tiffvers.h.in > $(top_srcdir)/libtiff/tiffvers.h)
+	(rm -f $(top_srcdir)/libtiff/tiffvers.h && sed 's,LIBTIFF_VERSION,$(LIBTIFF_VERSION),;s,LIBTIFF_RELEASE_DATE,$(LIBTIFF_RELEASE_DATE),;s,LIBTIFF_MAJOR_VERSION,$(LIBTIFF_MAJOR_VERSION),;s,LIBTIFF_MINOR_VERSION,$(LIBTIFF_MINOR_VERSION),;s,LIBTIFF_MICRO_VERSION,$(LIBTIFF_MICRO_VERSION),' $(top_srcdir)/libtiff/tiffvers.h.in > $(top_srcdir)/libtiff/tiffvers.h)
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libtiff-4.pc
diff --git a/RELEASE-DATE b/RELEASE-DATE
index 6f787fd2..039ae27f 100644
--- a/RELEASE-DATE
+++ b/RELEASE-DATE
@@ -1 +1 @@
-20221209
+20221210
diff --git a/libtiff/tiffvers.h b/libtiff/tiffvers.h
index ca958869..b59e3c51 100644
--- a/libtiff/tiffvers.h
+++ b/libtiff/tiffvers.h
@@ -6,4 +6,18 @@
  * version checking should be done based on the
  * string returned by TIFFGetVersion.
  */
-#define TIFFLIB_VERSION 20221209
+#define TIFFLIB_VERSION 20221210
+
+/* The following defines have been added in 4.5.0 */
+#define TIFFLIB_MAJOR_VERSION 4
+#define TIFFLIB_MINOR_VERSION 5
+#define TIFFLIB_MICRO_VERSION 0
+
+/* Macro added in 4.5.0. Returns TRUE if the current libtiff version is
+ * greater or equal to major.minor.micro
+ */
+#define TIFFLIB_AT_LEAST(major, minor, micro) \
+    (TIFFLIB_MAJOR_VERSION > (major) || \
+     (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION > (minor)) || \
+     (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION == (minor) && \
+      TIFFLIB_MICRO_VERSION >= (micro)))
diff --git a/libtiff/tiffvers.h.in b/libtiff/tiffvers.h.in
index 48ec0eb8..48fec9b1 100644
--- a/libtiff/tiffvers.h.in
+++ b/libtiff/tiffvers.h.in
@@ -7,3 +7,17 @@
  * string returned by TIFFGetVersion.
  */
 #define TIFFLIB_VERSION LIBTIFF_RELEASE_DATE
+
+/* The following defines have been added in 4.5.0 */
+#define TIFFLIB_MAJOR_VERSION LIBTIFF_MAJOR_VERSION
+#define TIFFLIB_MINOR_VERSION LIBTIFF_MINOR_VERSION
+#define TIFFLIB_MICRO_VERSION LIBTIFF_MICRO_VERSION
+
+/* Macro added in 4.5.0. Returns TRUE if the current libtiff version is
+ * greater or equal to major.minor.micro
+ */
+#define TIFFLIB_AT_LEAST(major, minor, micro) \
+    (TIFFLIB_MAJOR_VERSION > (major) || \
+     (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION > (minor)) || \
+     (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION == (minor) && \
+      TIFFLIB_MICRO_VERSION >= (micro)))
diff --git a/test/test_open_options.c b/test/test_open_options.c
index aef99e89..136adb37 100644
--- a/test/test_open_options.c
+++ b/test/test_open_options.c
@@ -43,6 +43,31 @@
 
 #define ERROR_STRING_SIZE 1024
 
+/* Test TIFFLIB_AT_LEAST() macro */
+#if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION,            \
+                      TIFFLIB_MICRO_VERSION)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+#if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION, 0)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+#if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, 0, 0)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+#if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION - 1, 0, 0)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+#if TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION + 1, 0, 0)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+#if TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION + 1, 0)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+#if TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION,             \
+                     TIFFLIB_MICRO_VERSION + 1)
+#error "TIFFLIB_AT_LEAST broken"
+#endif
+
 typedef struct MyErrorHandlerUserDataStruct
 {
     char *buffer;

From 23b315d10f907495aa7ec6e7b2f9d822ab6598fb Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sun, 11 Dec 2022 12:21:56 +0100
Subject: [PATCH 4/4] tiffvers.h.in: add clang-format off/on

---
 libtiff/tiffvers.h    | 7 +++++++
 libtiff/tiffvers.h.in | 7 +++++++
 2 files changed, 14 insertions(+)

diff --git a/libtiff/tiffvers.h b/libtiff/tiffvers.h
index b59e3c51..c90b5c1c 100644
--- a/libtiff/tiffvers.h
+++ b/libtiff/tiffvers.h
@@ -1,3 +1,8 @@
+/* clang-format off */
+
+/* clang-format disabled because FindTIFF.cmake is very sensitive to the
+ * formatting of below line being a single line.
+ */
 #define TIFFLIB_VERSION_STR "LIBTIFF, Version 4.5.0\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
 /*
  * This define can be used in code that requires
@@ -21,3 +26,5 @@
      (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION > (minor)) || \
      (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION == (minor) && \
       TIFFLIB_MICRO_VERSION >= (micro)))
+
+/* clang-format on */
diff --git a/libtiff/tiffvers.h.in b/libtiff/tiffvers.h.in
index 48fec9b1..b98851c0 100644
--- a/libtiff/tiffvers.h.in
+++ b/libtiff/tiffvers.h.in
@@ -1,3 +1,8 @@
+/* clang-format off */
+
+/* clang-format disabled because FindTIFF.cmake is very sensitive to the
+ * formatting of below line being a single line.
+ */
 #define TIFFLIB_VERSION_STR "LIBTIFF, Version LIBTIFF_VERSION\nCopyright (c) 1988-1996 Sam Leffler\nCopyright (c) 1991-1996 Silicon Graphics, Inc."
 /*
  * This define can be used in code that requires
@@ -21,3 +26,5 @@
      (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION > (minor)) || \
      (TIFFLIB_MAJOR_VERSION == (major) && TIFFLIB_MINOR_VERSION == (minor) && \
       TIFFLIB_MICRO_VERSION >= (micro)))
+
+/* clang-format on */