简体   繁体   English

使用 autotools 和 automake 在 /var/lib 中创建目录结构

[英]Create directory structure in /var/lib using autotools and automake

I'm using autotools on a C project that, after installation, needs a particular directory structure in /var/lib as follows:我在 C 项目上使用 autotools,在安装后,需要/var/lib中的特定目录结构如下:

/var/lib/my-project/
    data/
    configurations/
        local/
        extra/
    inputs/

I'm currently using the directive AS_MKDIR_P in configure.ac like so:我目前在configure.ac中使用指令AS_MKDIR_P ,如下所示:

AS_MKDIR_P(/var/lib/my-project/data)
AS_MKDIR_P(/var/lib/my-project/configurations/local)
AS_MKDIR_P(/var/lib/my-project/configurations/extra)
AS_MKDIR_P(/var/lib/my-project/inputs)

But it needs the configure script to be run with root permissions which I don't think is the way to go. I think the instructions to create this directory structure needs to be in Makefile.am , so that make install creates them rather than configure , but I have no idea how to do that.但它需要以 root 权限运行configure脚本,我认为这不是通往 go 的方式。我认为创建此目录结构的说明需要在Makefile.am中,以便make install创建它们而不是configure ,但我不知道该怎么做。

You really, really, really do not want to specify /var/lib/my-project .您真的、真的、真的不想指定/var/lib/my-project As the project maintainer, you have the right to specify relative paths, but the user may change DESTDIR or prefix.作为项目维护者,您有权指定相对路径,但用户可以更改 DESTDIR 或前缀。 If you ignore DESTDIR and prefix and just install your files in /var/lib without regard for the user's requests, then your package is broken .如果您忽略 DESTDIR 和前缀,只将文件安装在/var/lib中而不考虑用户的请求,那么您的 package 就坏了 It is not just slightly damaged, it is completely unusable.它不仅轻微损坏,而且完全无法使用。 The autotool packaging must not specify absolute paths; autotool 打包不能指定绝对路径; that is for downsteam packagers (ie, those that build *.rpm or *.deb or *.dmg or...).这是针对下游打包程序的(即,构建 *.rpm 或 *.deb 或 *.dmg 或...的打包程序)。 All you need to do is add something like this to Makefile.am:您需要做的就是将类似这样的内容添加到 Makefile.am:

configdir = $(pkgdatadir)/configurations
localdir  = $(configdir)/local
extradir  = $(configdir)/extra
inputdir  = $(pkgdatadir)/inputs
mydatadir = $(pkgdatadir)/data

config_DATA = cfg.txt
local_DATA = local.txt
extra_DATA = extra.txt
input_DATA = input.txt
mydata_DATA = data.txt

This will put input.txt in $(DESTDIR)$(pkgdatadir)/inputs , etc. If you want that final path to be /var/lib/my-project , then you can specify datadir appropriately at configure time.这会将input.txt放在$(DESTDIR)$(pkgdatadir)/inputs等中。如果您希望最终路径为/var/lib/my-project ,那么您可以在配置时适当地指定 datadir。 For example:例如:

$ CONFIG_SITE= ./configure --datadir=/var/lib > /dev/null

This will assign /var/lib to datadir , so that pkgdatadir will be /var/lib/my-project and a subsequent make install DESTDIR=/path/to/foo will put the files in /path/to/foo/var/lib/my-package/ .这会将/var/lib分配给datadir ,因此pkgdatadir将是/var/lib/my-project并且随后的make install DESTDIR=/path/to/foo会将文件放入/path/to/foo/var/lib/my-package/ It is essential that your auto-tooled package honor things like prefix (which for these files was essentially overridden here by the explicit assignment of datadir ) and DESTDIR.重要的是,您的自动工具 package 必须遵守prefix (对于这些文件,此处基本上已被datadir的显式分配覆盖)和 DESTDIR 之类的东西。 The appropriate time to specify paths like /var/lib is when you run the configure script.指定/var/lib等路径的适当时间是在运行配置脚本时。 For example, you can add the options to the configure script in your rpm spec file or in debian/rules, or in whatever file your package system uses.例如,您可以在 rpm 规范文件或 debian/rules 或 package 系统使用的任何文件中将选项添加到配置脚本。 The auto-tools provide a very flexible packaging system which can be easily used by many different packaging systems (unfortunately, the word "package" is highly overloaded.).自动工具提供了一个非常灵活的包装系统,可以很容易地被许多不同的包装系统使用(不幸的是,“包装”这个词被高度重载了。)。 Embrace that flexibility.拥抱这种灵活性。

According to autotools documentation ( here and here ), there are hooks that you can specify in Makefile.am that will run at specific times during the installation.根据 autotools 文档( 此处此处),您可以在Makefile.am中指定挂钩,这些挂钩将在安装期间的特定时间运行。 For my needs I will use install-exec-hook (or install-data-hook ) which will be run after all executables (or data) have been installed:根据我的需要,我将使用install-exec-hook (或install-data-hook ),它将在安装所有可执行文件(或数据)后运行:

install-exec-hook:
    $(MKDIR_P) /var/lib/my-project/data
    $(MKDIR_P) /var/lib/my-project/configurations/local
    $(MKDIR_P) /var/lib/my-project/configurations/extra
    $(MKDIR_P) /var/lib/my-project/inputs

MKDIR_P is a variable containing the command mkdir -p , or an equivalent to it if the system doesn't have mkdir . MKDIR_P是一个包含命令mkdir -p的变量,或者如果系统没有mkdir则等效于它。 To make it available in Makefile.am you have to use the macro AC_PROG_MKDIR_P in configure.ac .要使其在Makefile.am中可用,您必须在configure.ac中使用宏AC_PROG_MKDIR_P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM