如何使用 autotools 在 libdir 的子目录中安装 lua 模块

How to install a lua module in a subdirectory of libdir with the autotools

我写了一个小 lua 模块,我使用 autotools 编译它,我想修改它的组织:

现在我在主目录中有这个:

autogen.sh
configure.ac
Makefile.am
src
    file1.c
    file2.c
    file3.c

autogen.sh

 #!/bin/sh
 echo "Running aclocal..." ; aclocal $ACLOCAL_FLAGS || exit 1
 echo "Running libtoolize..."; libtoolize --copy --automake || exit 1;
 echo "Running autoheader..." ; autoheader || exit 1
 echo "Running autoconf..." ; autoconf || exit 1
 echo "Running automake..." ; automake --add-missing --copy --gnu || exit 1
 ./configure "$@"

configure.ac

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([lua-clangc], [0.0.1], [cedlemo@gmx.com])
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC(clang gcc)
# Checks for libraries.
PKG_CHECK_MODULES([DEPS], [lua])
#LUA_VERSION = $(pkg-config --version)
#AC_SUBST([LUA_VERSION])
# Checks for header files.
AC_CHECK_HEADERS([string.h stdlib.h clang-c/Index.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AM_INIT_AUTOMAKE(1.13 dist-bzip2 foreign subdir-objects)
LT_PREREQ(2.4)
LT_INIT
AC_CONFIG_FILES([
  Makefile
])
AC_OUTPUT

Makefile.am

MAINTAINERCLEANFILES = \
Makefile.in aclocal.m4 config.h.in configure \
depcomp install-sh missing compile config.sub \
config.guess ltmain.sh compile

lib_LTLIBRARIES = src/clangc.la
src_clangc_la_SOURCES = src/clangc.c src/indexlib.c src/translationunitlib.c src/constants.c src/clangc_module_functions.c
src_clangc_la_LDFLAGS = -module -avoid-version -shared -llua -lm -lclang -fPIC 

我使用这个命令来编译和安装 lua 模块:

./autogen.sh --libdir=/usr/lib/lua/5.3
make
sudo make install

我的文件安装在 /usr/lib/lua/5.3

/usr/lib/lua/5.3/clangc.so
/usr/lib/lua/5.3/clangc.la

我只想使用

./autogen.sh --prefix=/usr

并且 configure.ac 脚本像这样构建 libdir:

libdir=prefix + /lib/lua/ + LUA_VERSION + /clangc/

(我已经有办法在configure.ac中获取LUA_VERSION)

我一如既往地回答自己:

我只需要像这样修改我的 configure.ac:

AC_PREREQ([2.69])
AC_INIT([lua-clangc], [0.0.1], [cedlemo@gmx.com])
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC(clang gcc)

# Checks for libraries.
PKG_CHECK_MODULES([DEPS], [lua])
LUA_VERSION=$(pkg-config --modversion lua | awk -F "." '{print "."}')
AC_SUBST([LUA_VERSION])

libdir=$prefix/lib/lua/$LUA_VERSION/clangc
# Checks for header files.
AC_CHECK_HEADERS([string.h stdlib.h clang-c/Index.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AM_INIT_AUTOMAKE(1.13 dist-bzip2 foreign subdir-objects)
LT_PREREQ(2.4)
LT_INIT
AC_CONFIG_FILES([
  Makefile
])
AC_OUTPUT

首先我必须找到当前的 lua 版本:

LUA_VERSION=$(pkg-config --modversion lua | awk -F "." '{print "."}')

在我的系统上:

  • pkg-config --modversion lua returns 5.3.1
  • awk -F "." '{print $1"."$2}' returns 5.3

然后行:

libdir=$prefix/lib/lua/$LUA_VERSION/clangc

施展所有魔法。

当我使用:

./autogen.sh --prefix=/usr

我的库安装在:

/usr/lib/lua/5.3/clangc