Package: liblept5
Version: 1.73-6
Severity: normal
Tags: patch

Dear maintainer,

liblept looks to be broken on big endian architectures.  This was
discovered by means of the OCRmyPDF test suite.  It's failing on
s390x,[1] the broken files are emitted at the stage where OCRmyPDF
invokes liblept code, and the broken files are highly suggestive of
endianness issues.

I believe the attached backported patch will fix the problem, though
I've only been able to confirm that it doesn't break building the
package.

(Many thanks to James R. Barlow, OCRmyPDF's upstream author, for
examining the broken files, and to Mattia Rizzolo for running the tests
on an s390x porterbox.)

[1] http://autopkgtest.ubuntu.com/packages/o/ocrmypdf/zesty/s390x

-- System Information:
Debian Release: stretch/sid
  APT prefers testing
  APT policy: (900, 'testing')
Architecture: i386 (i686)

Kernel: Linux 4.8.0-2-686-pae (SMP w/2 CPU cores)
Locale: LANG=en_GB.utf8, LC_CTYPE=en_GB.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages liblept5 depends on:
ii  libc6            2.24-8
ii  libgif7          5.1.4-0.4
ii  libjpeg62-turbo  1:1.5.1-2
ii  libopenjp2-7     2.1.2-1
ii  libpng16-16      1.6.26-6
ii  libtiff5         4.0.7-1
ii  libwebp6         0.5.1-4
ii  zlib1g           1:1.2.8.dfsg-2+b3

liblept5 recommends no packages.

liblept5 suggests no packages.

-- no debconf information

-- 
Sean Whitton
Description: Fix endian detection across all build methods
 Unfortunately Leptonica has been broken on big endian systems when
 built with autotools because endianness.h was never used.
 
 This commit ensures it is generated by Autotools, CMake, and the
 static Makefile, so it has therefore been included unconditionally in
 alltypes.h. This will break the native Visual Studio build but that
 hasn't been maintained in a long time. For the static Makefile, the
 detection should now work under any Make implementation, not just GNU.
 
 With the detected endianness now preserved in a header, it is no
 longer necessary to define L_LITTLE_ENDIAN or L_BIG_ENDIAN manually
 when building against Leptonica. This is a substantial improvement as
 forgetting to do so would have resulted in broken behaviour on big
 endian systems despite a complete lack of errors or warnings. Note
 that it will still respect your choice if you do decide to define
 these manually.
 
 Building universal binaries for OS X should theoretically work with
 all build methods but this hasn't been tested. Feedback would be
 appreciated.
Author: James Le Cuirot <ch...@gentoo.org>
Reviewed-by: Sean Whitton <spwhit...@spwhitton.name>
Origin: upstream, fd252ce0a17561b74f8cc02726601e5be121ac58
Forwarded: not-needed

---
diff --git a/.gitignore b/.gitignore
index 72aa855..4dc5b4b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,5 @@
 # build directories
 /build*
 /win*
+
+/src/endianness.h
diff --git a/endiantest.c b/endiantest.c
deleted file mode 100644
index e45f27f..0000000
--- a/endiantest.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/*====================================================================*
- -  Copyright (C) 2001 Leptonica.  All rights reserved.
- -
- -  Redistribution and use in source and binary forms, with or without
- -  modification, are permitted provided that the following conditions
- -  are met:
- -  1. Redistributions of source code must retain the above copyright
- -     notice, this list of conditions and the following disclaimer.
- -  2. Redistributions in binary form must reproduce the above
- -     copyright notice, this list of conditions and the following
- -     disclaimer in the documentation and/or other materials
- -     provided with the distribution.
- -
- -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
- -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *====================================================================*/
-
-/*
- *    endiantest.c
- *
- *    This test was contributed by Bill Janssen.  When used with the
- *    gnu compiler, it allows efficient computation of the endian
- *    flag as part of the normal compilation process.  As a result,
- *    it is not necessary to set this flag either manually or
- *    through the configure Makefile generator.
- */
-
-#include <stdio.h>
-
-int main()
-{
-/* fprintf(stderr, "doing the test\n"); */
-    long v = 0x04030201;
-    if (*((unsigned char *)(&v)) == 0x04)
-        printf("L_BIG_ENDIAN\n");
-    else
-        printf("L_LITTLE_ENDIAN\n");
-    return 0;
-}
-
-
diff --git a/prog/CMakeLists.txt b/prog/CMakeLists.txt
index b57913e..58c734d 100644
--- a/prog/CMakeLists.txt
+++ b/prog/CMakeLists.txt
@@ -104,7 +104,6 @@ add_prog_target(dwalineargen dwalineargen.c)
 add_prog_target(dwamorph1_reg dwamorph1_reg.c dwalinear.3.c dwalinearlow.3.c)
 add_prog_target(dwamorph2_reg dwamorph2_reg.c dwalinear.3.c dwalinearlow.3.c)
 add_prog_target(edgetest edgetest.c)
-add_prog_target(endiantest endiantest.c)
 add_prog_target(enhance_reg enhance_reg.c)
 add_prog_target(equal_reg equal_reg.c)
 add_prog_target(expand_reg expand_reg.c)
diff --git a/prog/Makefile.am b/prog/Makefile.am
index 1969d84..a1f6108 100644
--- a/prog/Makefile.am
+++ b/prog/Makefile.am
@@ -1,6 +1,6 @@
 AUTOMAKE_OPTIONS = parallel-tests
 AM_CFLAGS = $(DEBUG_FLAGS)
-AM_CPPFLAGS = -I$(top_srcdir)/src
+AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_builddir)/src
 LDADD = $(top_builddir)/src/liblept.la $(LIBM)
  
 INSTALL_PROGS = convertfilestopdf convertfilestops \
diff --git a/prog/endiantest.c b/prog/endiantest.c
deleted file mode 100644
index e45f27f..0000000
--- a/prog/endiantest.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/*====================================================================*
- -  Copyright (C) 2001 Leptonica.  All rights reserved.
- -
- -  Redistribution and use in source and binary forms, with or without
- -  modification, are permitted provided that the following conditions
- -  are met:
- -  1. Redistributions of source code must retain the above copyright
- -     notice, this list of conditions and the following disclaimer.
- -  2. Redistributions in binary form must reproduce the above
- -     copyright notice, this list of conditions and the following
- -     disclaimer in the documentation and/or other materials
- -     provided with the distribution.
- -
- -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
- -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *====================================================================*/
-
-/*
- *    endiantest.c
- *
- *    This test was contributed by Bill Janssen.  When used with the
- *    gnu compiler, it allows efficient computation of the endian
- *    flag as part of the normal compilation process.  As a result,
- *    it is not necessary to set this flag either manually or
- *    through the configure Makefile generator.
- */
-
-#include <stdio.h>
-
-int main()
-{
-/* fprintf(stderr, "doing the test\n"); */
-    long v = 0x04030201;
-    if (*((unsigned char *)(&v)) == 0x04)
-        printf("L_BIG_ENDIAN\n");
-    else
-        printf("L_LITTLE_ENDIAN\n");
-    return 0;
-}
-
-
diff --git a/prog/makefile.static b/prog/makefile.static
index 9740db0..7cc93bf 100644
--- a/prog/makefile.static
+++ b/prog/makefile.static
@@ -60,16 +60,6 @@
 #          or
 #     (b)  have the *.so files (or links to them) in this directory
 #
-#   Using gnu make, this should compile properly on a bigendian
-#   machine (e.g., Mac).  Otherwise, you can explicitly set the endianness,
-#   using either of these
-#      CPPFLAGS =    $(INCLUDES) -DL_LITTLE_ENDIAN -D_STANDARD_C_
-#      CPPFLAGS =    $(INCLUDES) -DL_BIG_ENDIAN -D_STANDARD_C_
-#
-#   To use the non-POSIX-compliant GNU functions fmemopen() and open_memstream(),
-#   remove -D_STANDARD_C_ from CPPFLAGS:
-#      CPPFLAGS =    $(INCLUDES) -D$(ENDIANNESS)
-#
 #   On cygwin, remove -fPIC from CC.  I believe that these files --
 #   convertfilestops.c, jbcorrelation.c, jbrankhaus.c, maketile.c,
 #   and viewertest.c -- which use posix directory functions, will
@@ -115,15 +105,9 @@ INCLUDES =	-I$(IMAGELIB_INCL) -I/usr/X11R6/include $(EXTRAINCLUDES)
 
 CFLAGS =	$(OPTIMIZE) $(OPTIONS)
 
-# Test for processor endianness (valid with gnu make only)
-ENDIANNESS := $(shell $(CC) -o endiantest endiantest.c; ./endiantest; rm -f endiantest)
-
-# Conditional compilation (depending on processor endian-ness)
 # Use -D_STANDARD_C to stub out the non-posix GNU functions fmemopen()
 # and open_memstream()
-CPPFLAGS =      $(INCLUDES) -D$(ENDIANNESS)
-#CPPFLAGS =      $(INCLUDES) -DL_LITTLE_ENDIAN
-#CPPFLAGS =      $(INCLUDES) -DL_BIG_ENDIAN
+CPPFLAGS = $(INCLUDES)
 
 LDFLAGS +=	-L$(LIB_DIR) -L/usr/X11R6/lib -L/usr/lib $(EXTRALIBS)
 #LDFLAGS +=	-L$(LIB_DIR) -L/usr/X11R6/lib -L/usr/local/lib -L/usr/lib $(EXTRALIBS)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 52797dc..7a2e47d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -82,6 +82,17 @@ set(leptonica_hdr
     stringcode.h sudoku.h watershed.h
 )
 
+test_big_endian(BIG_ENDIAN)
+
+if(BIG_ENDIAN)
+  set(ENDIANNESS L_BIG_ENDIAN)
+else()
+  set(ENDIANNESS L_LITTLE_ENDIAN)
+endif()
+
+set(APPLE_UNIVERSAL_BUILD "defined (__APPLE_CC__)")
+configure_file(endianness.h.in endianness.h @ONLY)
+
 add_library                     (leptonica ${LIBRARY_TYPE} ${leptonica_src} ${leptonica_hdr})
 set_target_properties           (leptonica PROPERTIES OUTPUT_NAME lept${VERSION_MAJOR}${VERSION_MINOR})
 set_target_properties           (leptonica PROPERTIES DEBUG_OUTPUT_NAME lept${VERSION_MAJOR}${VERSION_MINOR}d)
diff --git a/src/Makefile.am b/src/Makefile.am
index a9aaed6..4acdbc2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -77,7 +77,6 @@ noinst_PROGRAMS = xtractprotos
 LDADD = liblept.la
 
 EXTRA_DIST = arrayaccess.h.vc                                   \
- endiantest.c endianness.h.dist	                                \
  hmttemplate1.txt hmttemplate2.txt                              \
  leptonica-license.txt		                                \
  morphtemplate1.txt morphtemplate2.txt                          \
@@ -87,5 +86,3 @@ allheaders: $(liblept_la_SOURCES)
 	@test -x xtractprotos || echo "First run 'make xtractprotos'"
 	./xtractprotos -prestring=LEPT_DLL -protos=inline $(liblept_la_SOURCES)
 
-dist-hook: 
-	cp "$(distdir)"/endianness.h{.dist,}
diff --git a/src/alltypes.h b/src/alltypes.h
index b9512a0..a84c0bf 100644
--- a/src/alltypes.h
+++ b/src/alltypes.h
@@ -33,6 +33,7 @@
 #include <stdarg.h>
 
     /* General and configuration defs */
+#include "endianness.h"
 #include "environ.h"
 
     /* Generic and non-image-specific containers */
diff --git a/src/endianness.h b/src/endianness.h
deleted file mode 100644
index e9eaba9..0000000
--- a/src/endianness.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#if !defined (L_BIG_ENDIAN) && !defined (L_LITTLE_ENDIAN)
-# if 0
-#  ifdef __BIG_ENDIAN__
-#   define L_BIG_ENDIAN
-#  else
-#   define L_LITTLE_ENDIAN
-#  endif
-# else
-#  define L_LITTLE_ENDIAN
-# endif
-#endif
diff --git a/src/endianness.h.dist b/src/endianness.h.dist
deleted file mode 100644
index f9c2bf7..0000000
--- a/src/endianness.h.dist
+++ /dev/null
@@ -1,4 +0,0 @@
-#if !defined (L_BIG_ENDIAN) && !defined (L_LITTLE_ENDIAN)
-# define L_LITTLE_ENDIAN
-/* # define L_BIG_ENDIAN */
-#endif
diff --git a/src/endiantest.c b/src/endiantest.c
deleted file mode 100644
index e45f27f..0000000
--- a/src/endiantest.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/*====================================================================*
- -  Copyright (C) 2001 Leptonica.  All rights reserved.
- -
- -  Redistribution and use in source and binary forms, with or without
- -  modification, are permitted provided that the following conditions
- -  are met:
- -  1. Redistributions of source code must retain the above copyright
- -     notice, this list of conditions and the following disclaimer.
- -  2. Redistributions in binary form must reproduce the above
- -     copyright notice, this list of conditions and the following
- -     disclaimer in the documentation and/or other materials
- -     provided with the distribution.
- -
- -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
- -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *====================================================================*/
-
-/*
- *    endiantest.c
- *
- *    This test was contributed by Bill Janssen.  When used with the
- *    gnu compiler, it allows efficient computation of the endian
- *    flag as part of the normal compilation process.  As a result,
- *    it is not necessary to set this flag either manually or
- *    through the configure Makefile generator.
- */
-
-#include <stdio.h>
-
-int main()
-{
-/* fprintf(stderr, "doing the test\n"); */
-    long v = 0x04030201;
-    if (*((unsigned char *)(&v)) == 0x04)
-        printf("L_BIG_ENDIAN\n");
-    else
-        printf("L_LITTLE_ENDIAN\n");
-    return 0;
-}
-
-
diff --git a/src/makefile.static b/src/makefile.static
index 5d1f419..308959f 100644
--- a/src/makefile.static
+++ b/src/makefile.static
@@ -40,14 +40,6 @@
 #   To remove object files in src: make clean
 #   To remove object files and executables in prog: make clean
 #
-#   Customization for endianness of machine hardware:
-#   When using the gnu compiler, endianness is automatically
-#   determined and set properly.  Otherwise, set the $CPPFLAGS variable:
-#       On little-endian machines (e.g., i386, x86-64):
-#          CPPFLAGS = $(INCLUDES) -DL_LITTLE_ENDIAN
-#       On big-endian machines (e.g., Mac Power PC, Sun Sparc):
-#          CPPFLAGS = $(INCLUDES) -DL_BIG_ENDIAN
-#
 #   Customization for I/O with external libraries (jpeg, png, tiff, webp, gif):
 #   Set flags in environ.h.  The default is to have libjpeg, libpng,
 #   libtiff and libz, but not libgif.
@@ -76,6 +68,7 @@ TEST		= test
 MKDIR		= mkdir -p
 LIBRARIAN	= ar cq
 RANLIB		= ranlib
+SED		= sed
 
 # Libraries are built into a binary tree determined by the environmental
 # variable  BINARY_BASE_DIR
@@ -117,13 +110,11 @@ CC =		gcc -ansi -D_BSD_SOURCE -DANSI -fPIC
 #CC =		gcc -ansi -DNO_CONSOLE_IO -D_BSD_SOURCE -DANSI -fPIC
 #CC =		gcc -ansi -D_BSD_SOURCE -DANSI
 
-#   Test for processor endianness (valid with gnu make only)
-ENDIANNESS := $(shell $(CC) -o endiantest endiantest.c; ./endiantest; rm -f endiantest)
-
-# Conditional compilation (depending on processor endianness)
-CPPFLAGS =      $(INCLUDES) -D$(ENDIANNESS)
-#CPPFLAGS =	$(INCLUDES) -DL_LITTLE_ENDIAN
-#CPPFLAGS =	$(INCLUDES) -DL_BIG_ENDIAN
+#   Test for processor endianness
+#   This version (using the definition of __BYTE_ORDER in endian.h)
+#   was provided by rofl0r.  It has been modified to return L_LITTLE_ENDIAN
+#   if the file endian.h does not exist, which is the situation on mingw.
+ENDIANNESS = $(shell echo __BYTE_ORDER | $(CC) -include endian.h -E - | grep -q 4321 && echo L_BIG_ENDIAN || echo L_LITTLE_ENDIAN)
 
 #   Shared library linker options
 SONAME_OPTION = -Wl,-h,
@@ -140,6 +131,7 @@ endif
 
 
 OPTIONS =
+CPPFLAGS =		$(INCLUDES)
 CFLAGS =		$(OPTIMIZE) $(OPTIONS)
 LIBRARIAN_SHARED =	gcc -shared
 
@@ -345,15 +337,18 @@ $(LIB_SHARED)/$(LEPTLIB_SHARED):	$(LEPTLIB_C:%.c=$(OBJ_SHARED)/%.o)
 
 #   Rules for compiling source
 
-$(OBJ_NODEBUG)/%.o:	%.c  $(LEPTLIB_H)
+endianness.h:	endianness.h.in
+		$(SED) -e 's/@APPLE_UNIVERSAL_BUILD@/defined (__APPLE_CC__)/g' -e 's/@ENDIANNESS@/$(ENDIANNESS)/g' endianness.h.in > endianness.h
+
+$(OBJ_NODEBUG)/%.o:	%.c  $(LEPTLIB_H) endianness.h
 		@$(TEST) -d $(OBJ_NODEBUG) || $(MKDIR) $(OBJ_NODEBUG)
 		$(COMPILE.c) -o $@ $<
 
-$(OBJ_DEBUG)/%.o:	%.c  $(LEPTLIB_H)
+$(OBJ_DEBUG)/%.o:	%.c  $(LEPTLIB_H) endianness.h
 		@$(TEST) -d $(OBJ_DEBUG) || $(MKDIR) $(OBJ_DEBUG)
 		$(COMPILE.c) -o $@ $<
 
-$(OBJ_SHARED)/%.o:	%.c  $(LEPTLIB_H)
+$(OBJ_SHARED)/%.o:	%.c  $(LEPTLIB_H) endianness.h
 		@$(TEST) -d $(OBJ_SHARED) || $(MKDIR) $(OBJ_SHARED)
 		$(COMPILE.c) -o $@ $<
 
@@ -380,7 +375,8 @@ clean:
 			$(LIB_NODEBUG)/*.a $(LIB_DEBUG)/*.a \
 			$(LIB_SHARED)/*.so $(LIB_SHARED)/*.so.? \
 			$(LIB_SHARED)/*.so.?.* \
-			xtractprotos.o xtractprotos
+			xtractprotos.o xtractprotos \
+			endianness.h
 
 ###########################################################
 

Attachment: signature.asc
Description: PGP signature

Reply via email to