From: Nick Bowler Date: Mon, 9 Jun 2014 23:46:34 +0000 (-0400) Subject: Add libpng detection macro. X-Git-Url: https://git.draconx.ca/gitweb/dxcommon.git/commitdiff_plain/da205696048844a53e536cfe179b09b02b9f3f18 Add libpng detection macro. I've been carrying around library detection in individual projects for too long. This is a from-scratch rewrite of the core probing code but the actual libpng test is basically the same as before. The idea is to better leverage M4 macros to reduce duplication, so it should be easy to add more libraries in the future. --- diff --git a/m4/cmdout.m4 b/m4/cmdout.m4 new file mode 100644 index 0000000..5aa3925 --- /dev/null +++ b/m4/cmdout.m4 @@ -0,0 +1,39 @@ +dnl Copyright © 2014 Nick Bowler +dnl +dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2. +dnl This is free software: you are free to do what the fuck you want to. +dnl There is NO WARRANTY, to the extent permitted by law. + +dnl DX_COMMAND_OUTPUT(variable, command, [action-if-ok], [action-if-failed]) +dnl +dnl Helper to capture standard output of a shell command. If the command +dnl is successful then variable is assigned with its output (with newlines +dnl converted to spaces), and action-if-ok is performed. Otherwise, if +dnl the command is unsuccessful, then variable is not changed, and +dnl action-if-failed is performed. +AC_DEFUN([DX_COMMAND_OUTPUT], [AC_REQUIRE([_DX_COMMAND_OUTPUT_SETUP])dnl +AS_IF([$2 >conftest.out 2>&AS_MESSAGE_LOG_FD], + [m4_do([dx_fn_cmdout_collect_output $1], + [m4_ifnblank([$3], [m4_newline([$3])])])], + [$4]) +rm -f conftest.out +]) + +AC_DEFUN_ONCE([_DX_COMMAND_OUTPUT_SETUP], [m4_divert_push([INIT_PREPARE])dnl +# Helper function to store the contents of conftest.out into a shell variable. +dx_fn_cmdout_collect_output () { + AS_UNSET([$][1]) + exec 3@;])])]) diff --git a/m4/libpng.m4 b/m4/libpng.m4 new file mode 100644 index 0000000..0f0af0b --- /dev/null +++ b/m4/libpng.m4 @@ -0,0 +1,62 @@ +dnl Copyright © 2009-2010, 2014 Nick Bowler +dnl +dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2. +dnl This is free software: you are free to do what the fuck you want to. +dnl There is NO WARRANTY, to the extent permitted by law. + +dnl DX_LIB_LIBPNG([min-version], [action-if-ok], [acton-if-failed]) +dnl +dnl Search for a suitable libpng. If min-version is non-empty, the test will +dnl attempt to reject a libpng installation which is older than the specified +dnl version number (as determined by the libpng header file). +dnl +dnl If a working library is found, appropriate compiler and linker flags +dnl are stored in LIBPNG_CFLAGS and LIBPNG_LIBS, respectively, and the +dnl action-if-ok is performed (may be empty). If no such library is found, +dnl then action-if-failed is performed if non-empty; otherwise configure will +dnl stop with a fatal error. +AC_DEFUN([DX_LIB_LIBPNG], [dnl +DX_LIB_SETUP([libpng])dnl +DX_LIB_PKGCONFIG_FLAGS([libpng]) +AC_MSG_CHECKING([for libpng[]m4_ifnblank([$1], [ at least version $1])]) +DX_LIB_SEARCH_LINK([libpng], + [m4_apply([_DX_LIB_LIBPNG_TEST], m4_split([$1], [\.]))], + [ + [[$LIBPNG_CFLAGS], [$LIBPNG_LIBS]], + [[$dx_cv_libpng_pkg_cflags], [$dx_cv_libpng_pkg_libs], + [test x"$dx_cv_libpng_pkg_found" = x"yes"]], + [[], [-lpng]], + ]) +AS_IF([test x"$dx_cv_libpng_lib_found" = x"yes"], [$2], + [m4_default([$3], [AC_MSG_FAILURE([dnl +libpng[]m4_ifnblank([$1], [ version $1 or newer]) is required. The latest +version may be found at . +m4_newline([DX_LIB_USERFLAG_BLURB([libpng])]) +m4_newline([DX_LIB_PKGCONFIG_BLURB([libpng])]) +])])])]) + +dnl Internal test program for libpng. Check that the version numbers in the +dnl header are sufficient and that some important functions are defined. +m4_define([_DX_LIB_LIBPNG_TEST], [AC_LANG_PROGRAM([dnl +#include +m4_ifnblank([$1], [dnl +#if PNG_LIBPNG_VER_MAJOR < $1 +DX_LIB_COMPILE_ERROR([major version insufficient]) +m4_ifnblank([$2], [dnl +#elif PNG_LIBPNG_VER_MAJOR == $1 +# if PNG_LIBPNG_VER_MINOR < $2 +DX_LIB_COMPILE_ERROR([minor version insufficient]) +m4_ifnblank([$3], [dnl +# elif PNG_LIBPNG_VER_MINOR == $2 +# if PNG_LIBPNG_VER_RELEASE < $3 +DX_LIB_COMPILE_ERROR([release version insufficient]) +# endif +])dnl +# endif +])dnl +#endif +])], [dnl +png_structp png = png_create_read_struct("", 0, 0, 0); +png_infop info = png_create_info_struct(png); +png_destroy_read_struct(&png, &info, 0); +])]) diff --git a/m4/pkg.m4 b/m4/pkg.m4 new file mode 100644 index 0000000..9fd99a9 --- /dev/null +++ b/m4/pkg.m4 @@ -0,0 +1,22 @@ +dnl Copyright © 2014 Nick Bowler +dnl +dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2. +dnl This is free software: you are free to do what the fuck you want to. +dnl There is NO WARRANTY, to the extent permitted by law. + +m4_pattern_forbid([^PKG_PROG_PKG_CONFIG$]) + +dnl DX_PKG_CONFIG(shell-variable, arguments, +dnl [action-if-ok], [action-if-failed], +dnl [action-if-not-available]) +dnl +dnl Execute pkg-config with the given arguments, and store the output in +dnl the specified shell variable. If successful, execute action-if-ok. If +dnl unsuccessful, execute action-if-failed. If pkg-config is not available, +dnl execute action-if-not-available. +AC_DEFUN([DX_PKG_CONFIG], [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl +AS_UNSET([$1]) +AS_IF([test x"$PKG_CONFIG" = x""], + [m4_default([$5], [AS_SET_STATUS([2])])], + [DX_COMMAND_OUTPUT([$1], [$PKG_CONFIG --print-errors $2], + [$3], [m4_default([$4], [AS_SET_STATUS([$?])])])])])