2

I have a configure script that generates a config.inc file containing some variable definitions and a makefile that imports those configurations using

include config.inc

The thing that bothers me is that if the user tries to run the makefile directly without first running configure they get an unhelpful error message:

makefile:2: config.inc: No such file or directory
make: *** No rule to make target 'config.inc'.  Stop.

Is there a way for me to produce a better error message, instructing the user to first run the configure script, without resorting to the autoconf strategy of generating the full makefile from inside configure?

hugomg
  • 68,213
  • 24
  • 160
  • 246

2 Answers2

5

Sure, no problem; just do something like this:

atarget:
        echo here is a target

ifeq ($(wildcard config.inc),)
  $(error Please run configure first!)
endif

another:
        echo here is another target

include config.inc

final:
        echo here is a final target

Note this is definitely specific to GNU make; there's no portable way to do this.

EDIT: the above example will work fine. If the file config.inc exists, then it will be included. If the file config.inc does not exist, then make will exit as it reads the makefile (as a result of the error function) and never get to the include line so there will be no obscure error about missing include files. That's what the original poster asked for.

EDIT2: Here's an example run:

$ cat Makefile
all:
        @echo hello world

ifeq ($(wildcard config.inc),)
  $(error Please run configure first!)
endif

include config.inc

$ touch config.inc

$ make
hello world

$ rm config.inc

$ make
Makefile:5: *** Please run configure first!.  Stop.
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I tried this, it doesn't work either unfortunately... If you put targets after the ifeq or the include, you get the error `commands commence before first target. Stop.` If you put them above, the same `No such file or directory` error occurs. – Alfredo Gimenez Apr 03 '15 at 01:41
  • I don't know what you mean by "targets". There are no targets in my example. The error you report suggests that you're indenting the `ifeq`, etc. with a TAB character or something: don't do that. Write it just like the example I gave. `ifeq` is a _makefile_ construct and should not be indented; the `ifeq` and `endif` start in column 1 and `$(error ...)` is either not indented or indented with a few spaces (not TAB), and put the entire thing before the `include` line. – MadScientist Apr 03 '15 at 11:53
  • Yes I see that. But at some point you want to add targets to your Makefile to use it. There is no way to put any targets in your Makefile and get the effect that OP originally asked for. I did *not* indent the `ifeq`, I copied it as-is and tried to add a target. – Alfredo Gimenez Apr 03 '15 at 19:49
  • I don't understand what you're saying. Where did you "try to add a target", and how? You can add targets anywhere you want. The `ifeq`, `wildcard`, and `error` functions are all run right away, as make is parsing the makefile. Make will stop parsing and exit as soon as it hits the error function, so it never even gets to the `include` line or anything past it. I'll modify my example. – MadScientist Apr 03 '15 at 23:57
  • Go ahead and try your own example as you posted. Invoking `make` when `config.inc` is not present fails with the message `Makefile:11: config.inc: No such file or directory` – Alfredo Gimenez Apr 04 '15 at 00:06
  • It works fine for me. I'll add an example. All I can assume is that you're using a version of GNU make which is too old to have the `error` function available: it was introduced in GNU make 3.78, which was released in 1999. – MadScientist Apr 04 '15 at 00:14
  • I'm using 3.81. I ran the example and still get `Makefile:11: config.inc: No such file or directory`. But since it works for your version I'll give it the deserved upvote. – Alfredo Gimenez Apr 04 '15 at 00:20
  • I've tried it with 3.81 and it works. Are you using OSX? Are you using the version of GNU make provided by Apple? I know for a fact that they've applied some questionable patches to their GNU make. Maybe this is another example. Other than that, or a simple typo, I can't explain the behavior you see. – MadScientist Apr 04 '15 at 00:41
  • RedHat linux, not sure if make has been patched on this particular system, but maybe someone else will run into the same issue and end up here :-P – Alfredo Gimenez Apr 04 '15 at 01:04
0

I gave up and decided to use autoconf and automake to handle my makefile-generation needs.

hugomg
  • 68,213
  • 24
  • 160
  • 246