Makefile conditionally includes

I am trying to write an application that needs ALSA or OSS headers. Basically, I want to pass the definition to the compiler if /etc/oss.conf does not exist, since this probably means that the soundcard.h header does not exist (feel free to correct me on this, I'm still new to working with OSS) . In the OSS documentation, you should use the include directive like this:

include /etc/oss.conf CFLAGS := -I$(OSSLIBDIR)/include/sys 

One problem. OSS support is optional, so I want to check if the header exists, and if so, pass the definition to the compiler. The problem is that AFAIK cannot check if the file exists outside the make rule of the file. Inside the rule, if I use the if statement, for some reason, trying to install CFLAGS does not change it:

 test: $(objects) @if [ -f ${OSS_CONFIG} ]; then \ . ${OSS_CONFIG}; \ CFLAGS+=" -I${OSSLIBDIR} -DUSE_OSS"; \ fi @echo ${CFLAGS} 

(The above only outputs the original CFLAGS value, even if ${OSS_CONFIG} exists.) This is of course extremely ugly, and I wonder if there is a cleaner way to do this. Or so I'm going to do it to trigger a worldwide catastrophic event related to the genocide of kittens?

Oh, and please don't tell me to use autoconf.

+9
c open-source makefile alsa
source share
2 answers

Suppose: use -include (then it will not warn + fail). I don't know if I can ever use this syntax for myself.

Another way to hack may be something like: DUMMY_VAR := $(shell ... ) to execute arbitrary code. I think it is even less likely to work.

Other than that, I do not think it is possible. When I recently studied a similar problem, I found that I could not get make to run arbitrary shell commands while creating the makefile.

+9
source share

Yes, there is a cleaner way. You can check for the existence of a file using the wildcard function, and then use ifeq / endif to install the corresponding CFLAGS . If you are AUDIO_CFLAGS related audio flags in something like AUDIO_CFLAGS then you can automatically include the correct set of flags in the Makefile.

It will look something like this:

 OSS_CONF_FILE := $(strip $(wildcard /etc/oss.conf)) ifeq ($OSS_CONF_FILE,) AUDIO_CFLAGS = "-I$(ALSALIBDIR) -DUSE_ALSA" else AUDIO_CFLAGS = "-I${OSSLIBDIR} -DUSE_OSS" endif sample_build_rule: $(CC) $(CFLAGS) $(AUDIO_CFLAGS) ... 
+9
source share

All Articles