From b4882cb251931832de1e005aad64e5ccaed76cc9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABl=20Bonithon?= <gael@xfce.org>
Date: Mon, 7 Feb 2022 17:04:18 +0100
Subject: [PATCH] Make CR3 support optional

CR3 support adds significant weight to the build, for limited feature.
---
 configure.ac                 | 41 +++++++++++++++++++++++-------------
 lib/Makefile.am              | 25 ++++++++++++++++------
 lib/canon.cpp                |  6 ++++++
 lib/makernotedir.cpp         |  6 ++++++
 lib/rawfile.cpp              | 10 +++++++++
 test/missingcoefficients.cpp |  8 +++++++
 testsuite/testsuite.cpp      |  2 ++
 tools/dumputils.cpp          |  6 ++++++
 8 files changed, 82 insertions(+), 22 deletions(-)

diff --git a/configure.ac b/configure.ac
index 0430677..e82f7e3 100644
--- configure.ac
+++ configure.ac
@@ -238,22 +238,32 @@ AC_ARG_ENABLE(asan,[  --enable-asan    Turn on address sanitizer],[
 
 ])
 
-AC_CHECK_PROG(CARGO, [cargo], [yes], [no])
-AS_IF(test x$CARGO = xno,
-    AC_MSG_ERROR([cargo is required])
-)
-AC_CHECK_PROG(RUSTC, [rustc], [yes], [no])
-AS_IF(test x$RUSTC = xno,
-    AC_MSG_ERROR([rustc is required])
-)
-
-dnl Release build we do.
-if test x$debug = xtrue; then
-CARGO_TARGET_SUBDIR=debug
-else
-CARGO_TARGET_SUBDIR=release
+AC_ARG_ENABLE([cr3],
+              [AC_HELP_STRING([--disable-cr3], [disable CR3 support])],
+              [enable_cr3=no],
+              [enable_cr3=yes])
+
+HAVE_CR3=no
+if test x$enable_cr3 = xyes; then
+  AC_CHECK_PROG(CARGO, [cargo], [yes], [no])
+  if test x$CARGO = xyes; then
+    AC_CHECK_PROG(RUSTC, [rustc], [yes], [no])
+    if test x$RUSTC = xyes; then
+      HAVE_CR3=yes
+      AC_DEFINE(HAVE_CR3, 1, [Define to 1 to enable CR3 support])
+
+      dnl Release build we do.
+      if test x$debug = xtrue; then
+        CARGO_TARGET_SUBDIR=debug
+      else
+        CARGO_TARGET_SUBDIR=release
+      fi
+      AC_SUBST(CARGO_TARGET_SUBDIR)
+    fi
+  fi
 fi
-AC_SUBST(CARGO_TARGET_SUBDIR)
+
+AM_CONDITIONAL(BUILD_CR3_SUPPORT, test x$HAVE_CR3 = xyes)
 
 dnl we need to disable gdk pixbuf for distcheck
 DISTCHECK_CONFIGURE_FLAGS="--disable-gnome"
@@ -285,5 +295,6 @@ echo "
 Options:
 
   Gnome support:        ${HAVE_GNOME}
+  CR3 support:          ${HAVE_CR3}
   Testsuite booststrap: ${HAVE_CURL}
 "
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 65c9813..3eaaa90 100644
--- lib/Makefile.am
+++ lib/Makefile.am
@@ -1,6 +1,7 @@
 
 AM_CPPFLAGS = -I$(top_srcdir)/include @BOOST_CPPFLAGS@
 
+if BUILD_CR3_SUPPORT
 RUST_FILES = mp4/Cargo.lock \
 	mp4/Cargo.toml \
 	mp4/lib.rs \
@@ -14,6 +15,7 @@ RUST_FILES = mp4/Cargo.lock \
 	mp4/mp4parse/src/unstable.rs \
 	mp4/mp4parse/Cargo.toml \
 	$(NULL)
+endif
 
 EXTRA_DIST = libopenraw.sym io/testfile.tmp \
 	exif/exif-tags.pl \
@@ -31,8 +33,6 @@ check_PROGRAMS = teststream
 teststream_SOURCES = io/teststream.cpp
 teststream_LDADD = libopenraw_internals.la
 
-CLEANFILES = mp4/target/CACHEDIR.TAG
-
 noinst_HEADERS = \
 	mp4/mp4parse.h \
 	mp4/mp4parse_ffi.h \
@@ -43,7 +43,6 @@ noinst_HEADERS = \
 	tiffepfile.hpp \
 	rawfilefactory.hpp \
 	cr2file.hpp \
-	cr3file.hpp \
 	neffile.hpp \
 	orffile.hpp \
 	arwfile.hpp \
@@ -64,7 +63,6 @@ noinst_HEADERS = \
 	ifd.hpp \
 	ifdfilecontainer.hpp \
 	ifddir.hpp ifdentry.hpp \
-	isomediacontainer.hpp \
 	orfcontainer.hpp \
 	rw2container.hpp \
 	mrwcontainer.hpp \
@@ -86,6 +84,19 @@ noinst_HEADERS = \
 lib_LTLIBRARIES = libopenraw.la
 noinst_LTLIBRARIES = libopenraw_internals.la
 
+libopenraw_internals_la_SOURCES =
+
+if BUILD_CR3_SUPPORT
+
+noinst_HEADERS += \
+	cr3file.hpp \
+	isomediacontainer.hpp
+libopenraw_internals_la_SOURCES += \
+	cr3file.cpp \
+	isomediacontainer.cpp
+
+CLEANFILES = mp4/target/CACHEDIR.TAG
+
 if DEBUG
 CARGO_RELEASE_ARGS=
 else
@@ -107,6 +118,8 @@ clean-local:
 	CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) cargo clean $(CARGO_VERBOSE) $(CARGO_RELEASE_ARGS)
 	rm -f $(CARGO_TARGET_DIR)/.rustc_info.json
 
+endif
+
 # The use of --all-static is necesary for the RUST_LIB
 libopenraw_la_LDFLAGS = \
 	-version-info @LIBOPENRAW_VERSION_INFO@ \
@@ -126,7 +139,7 @@ libopenraw_la_SOURCES = \
 	$(NULL)
 
 libopenraw_internals_la_DEPENDENCIES = $(RUST_LIB)
-libopenraw_internals_la_SOURCES = \
+libopenraw_internals_la_SOURCES += \
 	io/io.c io/posix_io.h \
 	io/posix_io.c io/posix_io.h \
 	io/stream.cpp io/stream.hpp \
@@ -152,7 +165,6 @@ libopenraw_internals_la_SOURCES = \
 	tiffepfile.cpp \
 	rawfilefactory.cpp \
 	cr2file.cpp \
-	cr3file.cpp \
 	neffile.cpp \
 	orffile.cpp \
 	arwfile.cpp \
@@ -168,7 +180,6 @@ libopenraw_internals_la_SOURCES = \
 	rawdata.cpp \
 	ifdfilecontainer.cpp \
 	ifddir.cpp ifdentry.cpp \
-	isomediacontainer.cpp \
 	makernotedir.hpp makernotedir.cpp \
 	rawcontainer.cpp \
 	orfcontainer.cpp \
diff --git a/lib/canon.cpp b/lib/canon.cpp
index c34f976..9f0257d 100644
--- lib/canon.cpp
+++ lib/canon.cpp
@@ -19,6 +19,10 @@
  * <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <array>
 
 #include "canon.hpp"
@@ -112,8 +116,10 @@ const ModelIdMap canon_modelid_map = {
     { 0x03980000, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_EOS_M100) },
     { 0x04180000, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_G1XMKIII) },
     { 0x80000432, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_2000D) },
+#ifdef HAVE_CR3
     { 0x80000422, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_3000D) },
     // CR3
+#endif
     { 0x00000412, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_EOS_M50) },
     { 0x80000424, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_EOS_R) },
     { 0x80000433, OR_MAKE_CANON_TYPEID(OR_TYPEID_CANON_EOS_RP) },
diff --git a/lib/makernotedir.cpp b/lib/makernotedir.cpp
index bef0242..7ebbb19 100644
--- lib/makernotedir.cpp
+++ lib/makernotedir.cpp
@@ -18,6 +18,10 @@
  * <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <fcntl.h>
 
 #include <string.h>
@@ -48,7 +52,9 @@ MakerNoteDir::createMakerNote(off_t offset,
 
     // Canon MakerNote don't have an ID
     if (file_type == OR_RAWFILE_TYPE_CR2
+#ifdef HAVE_CR3
         || file_type == OR_RAWFILE_TYPE_CR3
+#endif
         || file_type == OR_RAWFILE_TYPE_CRW) {
 
         return std::make_shared<MakerNoteDir>(
diff --git a/lib/rawfile.cpp b/lib/rawfile.cpp
index 7df9de6..9b335ba 100644
--- lib/rawfile.cpp
+++ lib/rawfile.cpp
@@ -19,6 +19,10 @@
  * <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stddef.h>
 #include <stdint.h>
 
@@ -47,7 +51,9 @@
 
 #include "arwfile.hpp"
 #include "cr2file.hpp"
+#ifdef HAVE_CR3
 #include "cr3file.hpp"
+#endif
 #include "crwfile.hpp"
 #include "dngfile.hpp"
 #include "erffile.hpp"
@@ -133,9 +139,11 @@
         RawFileFactory::registerType(OR_RAWFILE_TYPE_RAF,
                                      std::bind(&Internals::RafFile::factory, _1),
                                      "raf");
+#ifdef HAVE_CR3
         RawFileFactory::registerType(OR_RAWFILE_TYPE_CR3,
                                      std::bind(&Internals::Cr3File::factory, _1),
                                      "cr3");
+#endif
     });
 }
 
@@ -289,10 +297,12 @@ RawFile::identifyIOBuffer(IO::Stream::Ptr& stream, RawFile::Type& _type)
         _type = OR_RAWFILE_TYPE_MRW;
         return OR_ERROR_NONE;
     }
+#ifdef HAVE_CR3
     if (len >= 12 && (memcmp(buff + 4, "ftypcrx ", 8) == 0)) {
         _type = OR_RAWFILE_TYPE_CR3;
         return OR_ERROR_NONE;
     }
+#endif
     if (len >= 14 && memcmp(buff, "II\x1a\0\0\0HEAPCCDR", 14) == 0) {
         _type = OR_RAWFILE_TYPE_CRW;
         return OR_ERROR_NONE;
diff --git a/test/missingcoefficients.cpp b/test/missingcoefficients.cpp
index e568761..9e08128 100644
--- test/missingcoefficients.cpp
+++ test/missingcoefficients.cpp
@@ -26,6 +26,10 @@
  * of the support of various cameras.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <iostream>
 #include <memory>
 
@@ -33,7 +37,9 @@
 #include "../lib/rawfile_private.hpp"
 #include "../lib/arwfile.hpp"
 #include "../lib/cr2file.hpp"
+#ifdef HAVE_CR3
 #include "../lib/cr3file.hpp"
+#endif
 #include "../lib/crwfile.hpp"
 #include "../lib/dngfile.hpp"
 #include "../lib/erffile.hpp"
@@ -84,7 +90,9 @@ int main(int, char**)
 {
   OpenRaw::Internals::audit_coefficients<ArwFile>();
   OpenRaw::Internals::audit_coefficients<Cr2File>();
+#ifdef HAVE_CR3
   OpenRaw::Internals::audit_coefficients<Cr3File>();
+#endif
   OpenRaw::Internals::audit_coefficients<CRWFile>();
   OpenRaw::Internals::audit_coefficients<DngFile>();
   OpenRaw::Internals::audit_coefficients<ERFFile>();
diff --git a/testsuite/testsuite.cpp b/testsuite/testsuite.cpp
index 2f4a46f..6360528 100644
--- testsuite/testsuite.cpp
+++ testsuite/testsuite.cpp
@@ -265,9 +265,11 @@ bool Test::testRawType(const std::string & result)
     case OR_RAWFILE_TYPE_CR2:
         RETURN_TEST(result == "CR2", result);
         break;
+#ifdef HAVE_CR3
     case OR_RAWFILE_TYPE_CR3:
         RETURN_TEST(result == "CR3", result);
         break;
+#endif
     case OR_RAWFILE_TYPE_CRW:
         RETURN_TEST(result == "CRW", result);
         break;
diff --git a/tools/dumputils.cpp b/tools/dumputils.cpp
index acc8f0d..dcf13f3 100644
--- tools/dumputils.cpp
+++ tools/dumputils.cpp
@@ -19,6 +19,10 @@
  * <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <boost/format.hpp>
 
 #include "dumputils.hpp"
@@ -31,9 +35,11 @@ std::string typeToString(or_rawfile_type t)
     case OR_RAWFILE_TYPE_CR2:
         return "Canon CR2";
         break;
+#ifdef HAVE_CR3
     case OR_RAWFILE_TYPE_CR3:
         return "Canon CR3";
         break;
+#endif
     case OR_RAWFILE_TYPE_CRW:
         return "Canon CRW";
         break;
