From ea567e7a9d262b7089430cdc2b76914a408f5ffd Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Thu, 29 Feb 2024 08:11:02 +0300
Subject: [PATCH] CMake build support.
Based on original work by Anonymous Maarten and Sam Lantinga, with
a few bits from https://github.com/stohrendorf/libjpeg-cmake
---
CMakeLists.txt | 87 ++++++++++++++++++++
cmake/jconfig.h.in | 194 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 281 insertions(+)
create mode 100644 CMakeLists.txt
create mode 100644 cmake/jconfig.h.in
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..71ed317
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,87 @@
+cmake_minimum_required(VERSION 3.1...3.5)
+
+# extract version out of configure.ac
+file(READ "configure.ac" CONFIGURE_AC)
+string(REGEX MATCH "AC_INIT\\(\\[libjpeg\\], \\[([0-9.]+)\\]\\)" ver_found "${CONFIGURE_AC}")
+if(NOT ver_found)
+ message(FATAL_ERROR "Could not extract version from configure.ac")
+endif()
+set(LIBJPEG_VERSION "${CMAKE_MATCH_1}")
+
+project(jpeg LANGUAGES C VERSION ${LIBJPEG_VERSION})
+
+include(CheckSymbolExists)
+include(CheckCSourceCompiles)
+include(GNUInstallDirs)
+
+check_symbol_exists(setmode io.h USE_SETMODE)
+check_c_source_compiles("
+ int x[(char)0xff]; /* x[-1] if signed */
+ int main(void) {return 0;}" CHAR_IS_UNSIGNED
+)
+check_c_source_compiles("
+ int x[((signed char)0xff) >> 1]; /* x[-1] if signed */
+ int main(void) {return 0;}" RIGHT_SHIFT_IS_UNSIGNED
+)
+
+set(jpeg_headers
+ jerror.h
+ jmorecfg.h
+ jpeglib.h
+)
+set(jpeg_sources
+ jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c
+ jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c
+ jcomapi.c jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c
+ jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c
+ jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c
+ jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c
+ jfdctfst.c jfdctint.c jidctflt.c jquant1.c
+ jquant2.c jutils.c jmemmgr.c
+)
+
+if(MINGW)
+ set(CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION 1)
+ENDIF()
+if(MSVC)
+ set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
+ENDIF()
+
+add_library(jpeg ${jpeg_sources})
+add_library(JPEG::JPEG ALIAS jpeg)
+
+if(ANDROID)
+ target_sources(jpeg PRIVATE jmem-android.c)
+ # Android.mk does this for some reason:
+ target_compile_definitions(jpeg PRIVATE AVOID_TABLES)
+else()
+ target_sources(jpeg PRIVATE jmemansi.c)
+endif()
+target_sources(jpeg PRIVATE jidctint.c jidctfst.c)
+# FIXME : include asm for ARM
+# target_sources(jpeg PRIVATE jidctint.c jidctfst.S)
+
+set_property(TARGET jpeg PROPERTY DEFINE_SYMBOL JPEG_SHARED)
+
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/jconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/jconfig.h)
+list(APPEND jpeg_headers ${CMAKE_CURRENT_BINARY_DIR}/jconfig.h)
+target_include_directories(jpeg PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
+)
+
+set_target_properties(jpeg PROPERTIES
+ SOVERSION "${PROJECT_VERSION_MAJOR}"
+ VERSION "${PROJECT_VERSION}"
+)
+
+install(TARGETS jpeg
+ ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+ LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+ RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
+)
+
+install(FILES ${jpeg_headers}
+ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
+)
diff --git a/cmake/jconfig.h.in b/cmake/jconfig.h.in
new file mode 100644
index 0000000..a091f35
--- /dev/null
+++ b/cmake/jconfig.h.in
@@ -0,0 +1,194 @@
+/*
+ * jconfig.txt
+ *
+ * Copyright (C) 1991-1994, Thomas G. Lane.
+ * Modified 2009-2013 by Guido Vollbeding.
+ * This file is part of the Independent JPEG Group's software.
+ * For conditions of distribution and use, see the accompanying README file.
+ *
+ * This file documents the configuration options that are required to
+ * customize the JPEG software for a particular system.
+ *
+ * The actual configuration options for a particular installation are stored
+ * in jconfig.h. On many machines, jconfig.h can be generated automatically
+ * or copied from one of the "canned" jconfig files that we supply. But if
+ * you need to generate a jconfig.h file by hand, this file tells you how.
+ */
+
+
+/*
+ * These symbols indicate the properties of your machine or compiler.
+ * #define the symbol if yes, #undef it if no.
+ */
+
+/* Does your compiler support function prototypes?
+ * (If not, you also need to use ansi2knr, see install.txt)
+ *
+ * CMAKE PORT: ALWAYS DEFINE THIS.
+ */
+#define HAVE_PROTOTYPES
+
+/* Does your compiler support the declaration "unsigned char" ?
+ * How about "unsigned short" ?
+ *
+ * CMAKE PORT: ALWAYS DEFINE THESE.
+ */
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+
+/* Define "void" as "char" if your compiler doesn't know about type void.
+ * NOTE: be sure to define void such that "void *" represents the most general
+ * pointer type, e.g., that returned by malloc().
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #define void char */
+
+/* Define "const" as empty if your compiler doesn't know the "const" keyword.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #define const */
+
+/* Define this if an ordinary "char" type is unsigned.
+ * If you're not sure, leaving it undefined will work at some cost in speed.
+ * If you defined HAVE_UNSIGNED_CHAR then the speed difference is minimal.
+ */
+#cmakedefine CHAR_IS_UNSIGNED
+
+/* Define this if your system has an ANSI-conforming <stddef.h> file.
+ *
+ * CMAKE PORT: ALWAYS DEFINE THIS.
+ */
+#define HAVE_STDDEF_H
+
+/* Define this if your system has an ANSI-conforming <stdlib.h> file.
+ *
+ * CMAKE PORT: ALWAYS DEFINE THIS.
+ */
+#define HAVE_STDLIB_H
+
+/* Define this if your system does not have an ANSI/SysV <string.h>,
+ * but does have a BSD-style <strings.h>.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef NEED_BSD_STRINGS */
+
+/* Define this if your system does not provide typedef size_t in any of the
+ * ANSI-standard places (stddef.h, stdlib.h, or stdio.h), but places it in
+ * <sys/types.h> instead.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef NEED_SYS_TYPES_H */
+
+/* For 80x86 machines, you need to define NEED_FAR_POINTERS,
+ * unless you are using a large-data memory model or 80386 flat-memory mode.
+ * On less brain-damaged CPUs this symbol must not be defined.
+ * (Defining this symbol causes large data structures to be referenced through
+ * "far" pointers and to be allocated with a special version of malloc.)
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef NEED_FAR_POINTERS */
+
+/* Define this if your linker needs global names to be unique in less
+ * than the first 15 characters.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef NEED_SHORT_EXTERNAL_NAMES */
+
+/* Although a real ANSI C compiler can deal perfectly well with pointers to
+ * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
+ * and pseudo-ANSI compilers get confused. To keep one of these bozos happy,
+ * define INCOMPLETE_TYPES_BROKEN. This is not recommended unless you
+ * actually get "missing structure definition" warnings or errors while
+ * compiling the JPEG code.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef INCOMPLETE_TYPES_BROKEN */
+
+/* Define "boolean" as unsigned char, not enum, on Windows systems.
+ */
+#ifdef _WIN32
+#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
+typedef unsigned char boolean;
+#endif
+#ifndef FALSE /* in case these macros already exist */
+#define FALSE 0 /* values of boolean */
+#endif
+#ifndef TRUE
+#define TRUE 1
+#endif
+#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
+#endif
+
+
+/*
+ * The following options affect code selection within the JPEG library,
+ * but they don't need to be visible to applications using the library.
+ * To minimize application namespace pollution, the symbols won't be
+ * defined unless JPEG_INTERNALS has been defined.
+ */
+
+#ifdef JPEG_INTERNALS
+
+/* Define this if your compiler implements ">>" on signed values as a logical
+ * (unsigned) shift; leave it undefined if ">>" is a signed (arithmetic) shift,
+ * which is the normal and rational definition.
+ */
+#cmakedefine RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+
+/*
+ * The remaining options do not affect the JPEG library proper,
+ * but only the sample applications cjpeg/djpeg (see cjpeg.c, djpeg.c).
+ * Other applications can ignore these.
+ */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+/* These defines indicate which image (non-JPEG) file formats are allowed. */
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+/* Define this if you want to name both input and output files on the command
+ * line, rather than using stdout and optionally stdin. You MUST do this if
+ * your system can't cope with binary I/O to stdin/stdout. See comments at
+ * head of cjpeg.c or djpeg.c.
+ */
+#undef TWO_FILE_COMMANDLINE
+
+#cmakedefine USE_SETMODE /* Microsoft has setmode() */
+
+/* Define this if your system needs explicit cleanup of temporary files.
+ * This is crucial under MS-DOS, where the temporary "files" may be areas
+ * of extended memory; on most other systems it's not as important.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef NEED_SIGNAL_CATCHER */
+
+/* By default, we open image files with fopen(...,"rb") or fopen(...,"wb").
+ * This is necessary on systems that distinguish text files from binary files,
+ * and is harmless on most systems that don't. If you have one of the rare
+ * systems that complains about the "b" spec, define this symbol.
+ *
+ * CMAKE PORT: ALWAYS LEAVE THIS COMMENTED OUT.
+ */
+/* #undef DONT_USE_B_MODE */
+
+/* Define this if you want percent-done progress reports from cjpeg/djpeg.
+ */
+#undef PROGRESS_REPORT
+
+#endif /* JPEG_CJPEG_DJPEG */