Source: dislocker
Version: 0.7.3+git20240607-2
Tags: patch upstream
User: [email protected]
Usertags: ftcbfs

dislocker fails to cross build from source, because it fails to detect
the PolarSSL version. This matter has a bit more depth than it looks on
the surface. The PolarSSL detection actually caters for the rename and
searches for mbedcrypto first, then falls back to mbedtls and then to
polarssl. Once it has found the library, it has two code paths. When
compiling natively, it compiles a program to output the
${library}_VERSION_STRING C macro and uses that. For cross builds, we
cannot run this, so instead it opts for grepping the header.
Unfortunately, it neither gets the macro name right nor the quoting of
the shell pipeline, so this approach fails miserably. The cross
compilation branch is broken, dead code.

Instead of guessing the version from the header, I propose using the
pkg-config mechanism. If you find_package(POLARSSL mbedcrypto) and there
actually is a mbedcrypto.pc installed to one of the search paths, this
will readily populate a POLARSSL_VERSION with no extra effort. Equipped
with this, I suggest trying the pkg_check_modules approach first and if
that happens to work (which should be the case on all recent systems),
then we skip the other detection mechanisms and only for old systems, we
keep the original approach. The custom version detection is then only
run when pkg_check_modules did not work. As a result, native compilation
would continue to work where it worked earlier and cross compilation
would work on systems using pkg-config. What do you think about this?

I'm attaching a patch to implement the proposed mechanism. It deletes
the dysfunctional, dead code formerly used for cross compilation. I note
that it emits a CMake warning when including FindPkgConfig, but I happen
to not know how to fix that. I hope you can take or forward this patch
moving us a little closer to cross compiling the Debian package.

The patch won't make it cross, because the Ruby detection also doesn't
work. I'd like to defer the Ruby part to a separate bug report, because
the PolarSSL aspect is non-trivial already.

Helmut
--- dislocker-0.7.3+git20240607.orig/cmake/FindPolarSSL.cmake
+++ dislocker-0.7.3+git20240607/cmake/FindPolarSSL.cmake
@@ -11,6 +11,7 @@
 # POLARSSL_INC_FOLDER
 # POLARSSL_REAL_NAME
 
+include(FindPkgConfig)
 include(FindPackageHandleStandardArgs)
 
 find_path(POLARSSL_INCLUDE_DIRS NAMES mbedtls/ssl.h HINTS /usr/local/include)
@@ -41,7 +42,12 @@
   endif()
 endif()
 
-find_package_handle_standard_args(POLARSSL REQUIRED_VARS POLARSSL_INCLUDE_DIRS POLARSSL_LIBRARIES)
+pkg_check_modules(POLARSSL mbedcrypto)
+if (POLARSSL_FOUND AND POLARSSL_LIBRARIES AND POLARSSL_INCLUDE_DIRS)
+  set(POLARSSL_VERSION_STRING "${POLARSSL_VERSION}")
+else()
+  find_package_handle_standard_args(POLARSSL VERSION_VAR POLARSSL_VERSION_STRING REQUIRED_VARS POLARSSL_INCLUDE_DIRS POLARSSL_LIBRARIES)
+endif()
 
 if( ${POLARSSL_LIBRARIES-NOTFOUND} )
   message(FATAL_ERROR "Failed to get info from PolarSSL library, check your PolarSSL installation")
@@ -62,7 +68,7 @@
   set(ENV{SDKROOT} "${envSdkRoot}")
 endif()
 
-if( NOT CMAKE_CROSSCOMPILING )
+if( NOT POLARSSL_VERSION_STRING )
   execute_process(
     COMMAND echo "#include <${POLARSSL_INC_FOLDER}/version.h>\n#include <stdio.h>\nint main(){printf(${POLARSSL_REAL_NAME}_VERSION_STRING);return 0;}"
     OUTPUT_FILE a.c
@@ -77,14 +83,6 @@
   execute_process(
     COMMAND ${CMAKE_COMMAND} -E remove a.c a.out
   )
-else()
-  execute_process(
-    COMMAND grep -w "MBEDTLS_VERSION_STRING" ${POLARSSL_INCLUDE_DIRS}/${POLARSSL_INC_FOLDER}/version.h
-    COMMAND sed -e "s@\s\+@ @g"
-    COMMAND cut -d\  -f3
-    COMMAND sed -e "s@\"@@g"
-    OUTPUT_VARIABLE POLARSSL_VERSION_STRING
-  )
 endif()
 
 message("PolarSSL/mbedTLS version: " ${POLARSSL_VERSION_STRING})

Reply via email to