Makefile conditional with automake/autoconf
Asked Answered
A

2

7

can anybody tell me if there is a way to insert a conditional block to Makefile.am so that it will be passed further to a Makfile created by autotools?

Here is an example:

ifeq "$(SOMEVAR)" ""
SOMEVAR="default_value"
endif

This seems to be a usual Makefile way of doing conditional things. Automake cuts endif line off and make fails eventually with a message like this:

Makefile:390: * missing `endif'. Stop.

any thoughts?

Attractant answered 23/11, 2010 at 13:45 Comment(0)
V
5

Since it's tagged as Autoconf also, I suggest putting the condition in configure.ac, if that is possible. Similar to so:

AM_CONDITIONAL([CONDITION_NAME], [test x"${SOMEVAR}" != x])

Then, your Makefile.am would contain

if CONDITION_NAME
<conditional code>
else
<else :)>
endif

Update

The problem has to do with

python setup.py --root=$(DESTDIR) --prefix=$(DESTDIR)$(prefix)

being called from somewhere. If DESTDIR is empty, the prefix may expand to a relative path, which is not what you want. You have confirmed it is being called from your Makefile.am. Then there's two things you can do.

  1. Change the above command to python setup.py --root=${DESTDIR}/// --prefix=${DESTDIR}///$(prefix). Triple slashes may be necessary since, AFAIK, POSIX allows for double slashes to have a special meaning, but not for three or more consecutive slashes.

  2. Change the above command to DESTDIR=${DESTDIR:-///} && python setup.py --root=${DESTDIR} --prefix=${DESTDIR}$(prefix)

It may be noteworthy that, in my opinion and limited understanding of the whole picture, none of that should be necessary. Since the original caller of configure was able to specify exactly which prefix he really wanted to use. If none is specified, Autoconf already defaults to an absolute path (/usr/local). So, I guess, I don't quite understand why you run into your problem.

Volsung answered 23/11, 2010 at 13:49 Comment(15)
Thanks! This is for configure. I'm talking about the conditional block during make (depending on environment settings). For example if you run make to be installed in a different location like this: "DESTDIR=/some/rpm/builds make install" so Makefile should check whether this variable is set and then generate a command option for installingAttractant
Could you elaborate more on what exectly you want to do? DESTDIR=/some/rpm/builds make install does what it does. Do you need to modify it or is extending it enough? It can be extended by both the install-exec-local and install-data-local targets. These might check whether DESTDIR is set or not.Volsung
Well perhaps i needed to be more specific on what i'm trying to do, sorry. The idea is the following. When someone did not provide that DESDDIR env variable, I have to write a conditional block like this ifeq "$(DESTDIR)" "" DESTDIR="/" endif install: python setup.py --root=$(DESTDIR) --prefix=$(DESTDIR)$(prefix) otherwise empty DESTDIR passed to --root option and python is trying to install to a directory starting without slashAttractant
of course I can do the whole thing in configure.ac the way you proposed but then this DESTDIR will be passed to configure script not to make. This is just another solution, isn't it?Attractant
Do you have control over the python setup.py ... command line? If so, I'd suggest putting three slashes (///) after $(DESTDIR), which should portably do what you want. (Double slashes may have a special meaning in POSIX, AFAIK. But three or more slashes collapse to one slash). If this Python call is in your Makefile.am and I understood the problem correctly now, I'd probably try that.Volsung
Similarly, if the python ... command is located in your Makefile.am, you could do this instead: DESTDIR="${DESTDIR:-/}" python setup.py ...Volsung
Yep! This is exactly what i wanted to do! Bravo!Attractant
The second approach with DESTDIR="${DESTDIR:-/}" seems to be nicer, but it doesn't work ... produces empty DESTDIR= in all cases.Attractant
Apologies. That should have been DESTDIR="${DESTDIR:-/}" && python ... (note that &&). Also, you must change $(DESTDIR) to ${DESTDIR} in the Python command.Volsung
same effect DESTDIR is just empty, so the command line looks like this when no DESTDIR is provided: DESTDIR="" && python setup.py install --root= --prefix=/normal/prefix and like this when destdir is provided: DESTDIR="" && python setup.py install --root=/destdir --prefix=/normal/prefixAttractant
That's odd. Could you please show the whole rule that exhibits this behaviour? make should invoke /bin/sh and have it execute the respective lines. Does the following /bin/sh command behave as expected? DESTDIR=${DESTDIR:-/} && echo python setup.py install --root=${DESTDIR} --prefix=${DESTDIR}/usr/localVolsung
Eventually I solved the problem other way around. I put a line above in Makefile.am: DESTDIR ?= / which means define a value if it's not set and then used it in python line. Have no idea why it didn't work with ${DESTDIR:-/} construction. I use gnu make and bash, can it be the case that it's not understood by my environment?Attractant
In the answer, the comment is made that $(DESTDIR)$(prefix) may expand to a relative path if DESTDIR is empty. That isn't true. Autoconf generated configure scripts require that prefix begin with a '/'.Unsecured
Lots of pointless workaround, but is there a way to make that automake emit regular if/else/endif into a makefile??Time
@PavelP See this other questionWhencesoever
S
0

I propose another approach I found accidentally in Is there a way to tell automake not to interpret part of the automakefile?. But unfortunately it does not work with ifeq .. else .. endif conditionals.

Skive answered 4/11, 2021 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.