New module 'libunistring-optional'.
[gnulib.git] / m4 / libunistring-base.m4
diff --git a/m4/libunistring-base.m4 b/m4/libunistring-base.m4
new file mode 100644 (file)
index 0000000..355fe2f
--- /dev/null
@@ -0,0 +1,145 @@
+# libunistring-base.m4 serial 1
+dnl Copyright (C) 2010 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Paolo Bonzini and Bruno Haible.
+
+dnl gl_LIBUNISTRING_LIBSOURCE([VERSION], [SourceFile])
+dnl Declares that SourceFile should be compiled, unless we are linking
+dnl with libunistring and its version is >= the given VERSION.
+dnl SourceFile should be relative to the lib directory and end in '.c'.
+dnl This macro is to be used for public libunistring API, not for
+dnl undocumented API.
+dnl
+dnl You have to bump the VERSION argument to the next projected version
+dnl number each time you make a change that affects the behaviour of the
+dnl functions defined in SourceFile (even if SourceFile itself does not
+dnl change).
+
+AC_DEFUN([gl_LIBUNISTRING_LIBSOURCE],
+[
+  AC_REQUIRE([gl_LIBUNISTRING_LIB_PREPARE])
+  dnl Use the variables HAVE_LIBUNISTRING, LIBUNISTRING_VERSION from
+  dnl gl_LIBUNISTRING_CORE if that macro has been run.
+  if gl_LIBUNISTRING_VERSION_CMP([$1])
+  then
+    m4_foreach_w([gl_source_file], [$2],
+      [AC_LIBOBJ(m4_bpatsubst(m4_defn([gl_source_file]), [\.c$], []))
+      ])
+  fi
+])
+
+dnl gl_LIBUNISTRING_LIBHEADER([VERSION], [HeaderFile])
+dnl Declares that HeaderFile should be created, unless we are linking
+dnl with libunistring and its version is >= the given VERSION.
+dnl HeaderFile should be relative to the lib directory and end in '.h'.
+dnl Prepares for substituting LIBUNISTRING_HEADERFILE (to HeaderFile or empty).
+dnl
+dnl When we are linking with the already installed libunistring and its version
+dnl is < VERSION, we create HeaderFile here, because we may compile functions
+dnl (via gl_LIBUNISTRING_LIBSOURCE above) that are not contained in the
+dnl installed version.
+dnl When we are linking with the already installed libunistring and its version
+dnl is > VERSION, we don't create HeaderFile here: it could cause compilation
+dnl errors in other libunistring header files if some types are missing.
+dnl
+dnl You have to bump the VERSION argument to the next projected version
+dnl number each time you make a non-comment change to the HeaderFile.
+
+AC_DEFUN([gl_LIBUNISTRING_LIBHEADER],
+[
+  AC_REQUIRE([gl_LIBUNISTRING_LIB_PREPARE])
+  dnl Use the variables HAVE_LIBUNISTRING, LIBUNISTRING_VERSION from
+  dnl gl_LIBUNISTRING_CORE if that macro has been run.
+  if gl_LIBUNISTRING_VERSION_CMP([$1])
+  then
+    LIBUNISTRING_[]AS_TR_CPP([$2])='$2'
+  else
+    LIBUNISTRING_[]AS_TR_CPP([$2])=
+  fi
+  AC_SUBST([LIBUNISTRING_]AS_TR_CPP([$2]))
+])
+
+dnl Miscellaneous preparations/initializations.
+
+AC_DEFUN([gl_LIBUNISTRING_LIB_PREPARE],
+[
+  AC_REQUIRE([AC_PROG_AWK])
+
+dnl Sed expressions to extract the parts of a version number.
+changequote(,)
+gl_libunistring_sed_extract_major='/^[0-9]/{s/^\([0-9]*\).*/\1/p;q;}
+i\
+0
+q
+'
+gl_libunistring_sed_extract_minor='/^[0-9][0-9]*[.][0-9]/{s/^[0-9]*[.]\([0-9]*\).*/\1/p;q;}
+i\
+0
+q
+'
+gl_libunistring_sed_extract_subminor='/^[0-9][0-9]*[.][0-9][0-9]*[.][0-9]/{s/^[0-9]*[.][0-9]*[.]\([0-9]*\).*/\1/p;q;}
+i\
+0
+q
+'
+changequote([,])
+
+  if test "$HAVE_LIBUNISTRING" = yes; then
+    LIBUNISTRING_VERSION_MAJOR=`echo "$LIBUNISTRING_VERSION" | sed -n -e "$gl_libunistring_sed_extract_major"`
+    LIBUNISTRING_VERSION_MINOR=`echo "$LIBUNISTRING_VERSION" | sed -n -e "$gl_libunistring_sed_extract_minor"`
+    LIBUNISTRING_VERSION_SUBMINOR=`echo "$LIBUNISTRING_VERSION" | sed -n -e "$gl_libunistring_sed_extract_subminor"`
+  fi
+])
+
+dnl gl_LIBUNISTRING_VERSION_CMP([VERSION])
+dnl Expands to a shell statement that evaluates to true if LIBUNISTRING_VERSION
+dnl is less than the VERSION argument.
+dnl This is the unoptimized variant:
+AC_DEFUN([gl_LIBUNISTRING_VERSION_CMP_ORIG],
+[ { test "$HAVE_LIBUNISTRING" != yes \
+    || {
+         requested_version_major=`echo '$1' | sed -n -e "$gl_libunistring_sed_extract_major"`
+         requested_version_minor=`echo '$1' | sed -n -e "$gl_libunistring_sed_extract_minor"`
+         requested_version_subminor=`echo '$1' | sed -n -e "$gl_libunistring_sed_extract_subminor"`
+         test $LIBUNISTRING_VERSION_MAJOR -lt $requested_version_major \
+         || { test $LIBUNISTRING_VERSION_MAJOR -eq $requested_version_major \
+              && { test $LIBUNISTRING_VERSION_MINOR -lt $requested_version_minor \
+                   || { test $LIBUNISTRING_VERSION_MINOR -eq $requested_version_minor \
+                        && test $LIBUNISTRING_VERSION_SUBMINOR -lt $requested_version_subminor
+                      }
+                 }
+            }
+       }
+  }
+])
+dnl This is the optimized variant, that assumes the argument is a literal:
+AC_DEFUN([gl_LIBUNISTRING_VERSION_CMP],
+[ { test "$HAVE_LIBUNISTRING" != yes \
+    || {
+         m4_pushdef([requested_version_major],
+           [gl_LIBUNISTRING_ARG_OR_ZERO(m4_bpatsubst([$1], [^\([0-9]*\).*], [\1]), [])])
+         m4_pushdef([requested_version_minor],
+           [gl_LIBUNISTRING_ARG_OR_ZERO(m4_bpatsubst([$1], [^[0-9]*[.]\([0-9]*\).*], [\1]), [$1])])
+         m4_pushdef([requested_version_subminor],
+           [gl_LIBUNISTRING_ARG_OR_ZERO(m4_bpatsubst([$1], [^[0-9]*[.][0-9]*[.]\([0-9]*\).*], [\1]), [$1])])
+         test $LIBUNISTRING_VERSION_MAJOR -lt requested_version_major \
+         || { test $LIBUNISTRING_VERSION_MAJOR -eq requested_version_major \
+              && { test $LIBUNISTRING_VERSION_MINOR -lt requested_version_minor \
+                   || { test $LIBUNISTRING_VERSION_MINOR -eq requested_version_minor \
+                        && test $LIBUNISTRING_VERSION_SUBMINOR -lt requested_version_subminor
+                      }
+                 }
+            }
+         m4_popdef([requested_version_subminor])
+         m4_popdef([requested_version_minor])
+         m4_popdef([requested_version_major])
+       }
+  }
+])
+
+dnl gl_LIBUNISTRING_ARG_OR_ZERO([ARG], [ORIG]) expands to ARG if it is not the
+dnl same as ORIG, otherwise to 0.
+m4_define([gl_LIBUNISTRING_ARG_OR_ZERO], [m4_if([$1], [$2], [0], [$1])])