当前位置: 首页 > 工具软件 > Vala > 使用案例 >

MingW Autotools 编写Vala程序 之二

鲜于华容
2023-12-01

仔细看过了一些关于Vala的Autotool介绍,发现可以用GNU后来的vala.m4来简化Autotools过程,前提当然也是更新vala.m4的文件,整理后的configure.ac去掉了之前关于gtk的C开发方式的一些写法,请看下面:

AC_PREREQ([2.65])
AC_INIT([helloworld],[1.0],[example@gnome.org])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE

LT_INIT
AC_PROG_CC
AM_PROG_VALAC

pkg_modules="gtk+-2.0 >= 2.24.1
             glib-2.0 >= 2.24.1
             gobject-2.0 >= 2.24.1"

VALA_CHECK_MODULES(HELLOWORLD,[$pkg_modules])


AC_CONFIG_FILES([Makefile
		src/Makefile])

AC_OUTPUT

在新的vala.m4的文件中,使用了VALA_CHECK_MODULES的宏,可以轻易地得到以HELLOWORLD为前缀的三个FLAGS,如下

HELLOWORLD_CFLAGS = -mms-bitfields -Ic:/gtkmm/include/gtk-2.0 -Ic:/gtkmm/lib/gtk-2.0/include -Ic:/gtkmm/include/atk-1.0 -Ic:/gtkmm/include/cairo -Ic:/gtkmm/include/gdk-pixbuf-2.0 -Ic:/gtkmm/include/pango-1.0 -Ic:/gtkmm/include/glib-2.0 -Ic:/gtkmm/lib/glib-2.0/include -Ic:/gtkmm/include -Ic:/gtkmm/include/freetype2 -Ic:/gtkmm/include/libpng14  
HELLOWORLD_LIBS = -Lc:/gtkmm/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgmodule-2.0 -lgobject-2.0 -lgthread-2.0 -lglib-2.0 -lintl  
HELLOWORLD_VALAFLAGS =  --pkg gtk+-2.0 --pkg glib-2.0 --pkg gobject-2.0

在源代码中的Makefile.am中可以直接引用,看这个helloworld的例子,这几个变量可以通过autoconfig直接获得,进一步简化了Makefile.am的写法

AM_CPPFLAGS = $(HELLOWORLD_CFLAGS)

bin_PROGRAMS = helloworld

helloworld_SOURCES = main.vala
helloworld_LDADD = $(HELLOWORLD_LIBS)
helloworld_VALAFLAGS = $(HELLOWORLD_VALAFLAGS)

CLEANFILES = *.h *.c *.vapi *.stamp

最后附上最新的vala.m4文件

# Autoconf support for the Vala compiler

# Copyright (C) 2008-2014 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.

# Check whether the Vala compiler exists in $PATH.  If it is found, the
# variable VALAC is set pointing to its absolute path.  Otherwise, it is
# simply set to 'valac'.
# Optionally a minimum release number of the compiler can be requested.
# If the ACTION-IF-FOUND parameter is given, it will be run if a proper
# Vala compiler is found.
# Similarly, if the ACTION-IF-FOUND is given, it will be run if no proper
# Vala compiler is found.  It defaults to simply print a warning about the
# situation, but otherwise proceeding with the configuration.
#
# AM_PROG_VALAC([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
# --------------------------------------------------------------------------
AC_DEFUN([AM_PROG_VALAC],
  [AC_PATH_PROG([VALAC], [valac], [valac])
   AS_IF([test "$VALAC" != valac && test -n "$1"],
      [AC_MSG_CHECKING([whether $VALAC is at least version $1])
       am__vala_version=`$VALAC --version | sed 's/Vala  *//'`
       AS_VERSION_COMPARE([$1], ["$am__vala_version"],
         [AC_MSG_RESULT([yes])],
         [AC_MSG_RESULT([yes])],
         [AC_MSG_RESULT([no])
          VALAC=valac])])
    if test "$VALAC" = valac; then
      m4_default([$3],
        [AC_MSG_WARN([no proper vala compiler found])
         AC_MSG_WARN([you will not be able to compile vala source files])])
    else
      m4_default([$2], [:])
    fi])
# _VALA_CHECK_COMPILE_WITH_ARGS(ARGS, [ACTION-IF-TRUE],
#   [ACTION-IF-FALSE])
# --------------------------------------
# Check that Vala compile with ARGS.
#
AC_DEFUN([_VALA_CHECK_COMPILE_WITH_ARGS],
[AC_REQUIRE([AM_PROG_VALAC])[]dnl

  cat <<_ACEOF >conftest.vala
void main(){}
_ACEOF

  AS_IF([vala_error=`$VALAC $1 -q --cc="${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS}" -o conftest$ac_exeext conftest.vala 2>&1`],
        [$2], [$3])
])

])# _VALA_CHECK_COMPILE_WITH_ARGS

# VALA_CHECK_PACKAGES(PKGS, [ACTION-IF-FOUND],
#   [ACTION-IF-NOT-FOUND])
# --------------------------------------
# Check that PKGS Vala bindings are installed and usable.
#
AC_DEFUN([VALA_CHECK_PACKAGES],
[
  unset vala_pkgs
  unset vala_bindings
  ac_save_ifs="$IFS"; unset IFS
  for vala_pkg in $(echo "$1"); do
      vala_pkgs="${vala_pkgs:+$vala_pkgs }--pkg $vala_pkg"
      vala_bindings="${vala_bindings:+$vala_bindings }$vala_pkg"
  done
  IFS="$ac_save_ifs"
  AC_MSG_CHECKING([for $vala_bindings vala bindings])
  _VALA_CHECK_COMPILE_WITH_ARGS([$vala_pkgs],
    [vala_pkg_exists=yes],
    [vala_pkg_exists=no])

AS_IF([test x${vala_pkg_exists} = xno],[
  ifelse([$3], , [AC_MSG_ERROR([]dnl
[Package requirements were not met: $1

$vala_error

Consider adjusting the XDG_DATA_DIRS environment variable if you
installed bindings in a non-standard prefix.
])],
  [AC_MSG_RESULT([no])
$3])],[
  AC_MSG_RESULT([yes])
  ifelse([$2], , :, [$2])[]dnl
])

])# VALA_CHECK_PACKAGES


# Check for Vala bindings for a package, as well as the pkg-config
# CFLAGS and LIBS for the package. The arguments here work the
# same as those for PKG_CHECK_MODULES, which is called internally.
# As a result, the _CFLAGS, _LIBS, and _VALAFLAGS variables will
# all be declared, rather than only _VALAFLAGS.
#
# VALA_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
# [ACTION-IF-NOT-FOUND])
# --------------------------------------------------------------
AC_DEFUN([VALA_CHECK_MODULES],
[
		AC_REQUIRE([AM_PROG_VALAC])dnl
		AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
        AC_REQUIRE([_VALA_CHECK_COMPILE_WITH_ARGS])dnl
		AC_ARG_VAR([$1][_VALAFLAGS], [Vala compiler flags for $1])dnl

        VALA_MODULES="`echo $2 | sed -e 's/ [[=<>]]\+ [[0-9.]]\+//g'`"
        for MODULE in $VALA_MODULES; do
            $1[]_VALAFLAGS="$[]$1[]_VALAFLAGS --pkg $MODULE"
        done

        PKG_CHECK_MODULES([$1], [$2], [$3], [$4])

        pkg_failed=no
		AC_MSG_CHECKING([for $1 vala modules])

        _VALA_CHECK_COMPILE_WITH_ARGS([$1][_VALAFLAGS],
                                      [pkg_failed=yes],
                                      [pkg_failed=no])

		if test $pkg_failed = yes; then
		   AC_MSG_RESULT([no])
		   m4_default([$4], [AC_MSG_ERROR(
							[Package requirements ($2) were not met.])dnl
		   ])
		else
			AC_MSG_RESULT([yes])
			m4_default([$3], [:])
		fi[]dnl
])

# Check whether the Vala API Generator exists in `PATH'. If it is found,
# the variable VAPIGEN is set. Optionally a minimum release number of the
# generator can be requested.
#
# VALA_PROG_VAPIGEN([MINIMUM-VERSION])
# ------------------------------------
AC_DEFUN([VALA_PROG_VAPIGEN],
[AC_PATH_PROG([VAPIGEN], [vapigen], [])
  AS_IF([test -z "$VAPIGEN"],
    [AC_MSG_WARN([No Vala API Generator found. You will not be able to generate .vapi files.])],
    [AS_IF([test -n "$1"],
        [AC_MSG_CHECKING([$VAPIGEN is at least version $1])
         am__vapigen_version=`$VAPIGEN --version | sed 's/Vala API Generator  *//'`
         AS_VERSION_COMPARE([$1], ["$am__vapigen_version"],
           [AC_MSG_RESULT([yes])],
           [AC_MSG_RESULT([yes])],
           [AC_MSG_RESULT([no])
            AC_MSG_ERROR([Vala API Generator $1 not found.])])])])
])


 类似资料: