Home > Software engineering >  Create directory structure in /var/lib using autotools and automake
Create directory structure in /var/lib using autotools and automake

Time:04-16

I'm using autotools on a C project that, after installation, needs a particular directory structure in /var/lib as follows:

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

I'm currently using the directive AS_MKDIR_P in configure.ac like so:

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.

CodePudding user response:

You really, really, really do not want to specify /var/lib/my-project. As the project maintainer, you have the right to specify relative paths, but the user may change DESTDIR or prefix. 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. It is not just slightly damaged, it is completely unusable. The autotool packaging must not specify absolute paths; that is for downsteam packagers (ie, those that build *.rpm or *.deb or *.dmg or ...). All you need to do is add something like this to 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. 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/. 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. The appropriate time to specify paths like /var/lib is when you run the configure script. 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. 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.

CodePudding user response:

According to autotools documentations (here and here), there are hooks that you can specify in Makefile.am that will run at specific times during the installation. For my needs I will use install-exec-hook which will be run after all executables have been installed:

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
  • Related