make & conditional compilation

make & conditional compilation

Post by Paul D. Boy » Wed, 13 May 1998 04:00:00



I have a problem which probably trivial for some who knows 'make' well,
but it has me stumped.  I have written a program which can act as either
a CGI bin for a httpd server or a traditional Unix command line program
depending on a compile time directive being defined.  For CGI related
parsing, all of the function definitions and data structure declarations
are contained in files called cgi_util.c and cgi_util.h.  These are not
needed in the Unix command line version of the program.

I would like to know how to write a Makefile such that if -DUNIX_CMD_LINE
is defined in CFLAGS then the definition of the source files (SRCS,
see below) will not include cgi_util.c

For reference I have appended my Makefile at the end of the post.
Thank-you for any help or advice.

Paul

--

Director, X-ray Structural Facility |   phone: (919) 515-7362
Department of Chemistry - Box 8204  |   FAX:   (919) 515-5079
North Carolina State University     |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html
---------------------------Makefile-------------------------------
Here is my Makefile:
# Makefile for sinfo.  Default build is for CGI bin executable.
# If you would rather build a Unix command line version of the
# program define UNIX_CMD_LINE in the CFLAGS variable.

#Linux make variables:

#  Development flags (for gcc only)
CFLAGS = -g -ansi -pedantic  -Wall \
         -Wshadow -Wbad-function-cast -Wconversion \
         -Wstrict-prototypes -DUNIX_CMD_LINE

#Production flags for Linux
#CFLAGS = -O2

#Other platforms

#SGI CFLAGS.
#CFLAGS = -ansi -mips2 -O3

#DEC OSF/1 FLAGS
#CFLAGS = std1 -O3

#General definitions.

EXE     = sinfo
LIB_DIR = -L/usr/local/pgsql/lib
LIBS    = -lm -lpq

SRCS =   OutputFiles.c     ProcessTuples.c \
         SearchDatabase.c  WholeTableThread.c sinfo_util.c \
         sinfo.c           cgi_util.c

OBJS = $(SRCS:.c=.o)

$(EXE): $(OBJS)

.c.o:
        $(CC) -c $(CFLAGS) $<

clean:
        rm -f *.o $(EXE)

 
 
 

make & conditional compilation

Post by Paul A. Lande » Wed, 13 May 1998 04:00:00



Quote:> I would like to know how to write a Makefile such that if -DUNIX_CMD_LINE
> is defined in CFLAGS then the definition of the source files (SRCS,
> see below) will not include cgi_util.c

I know of a way to do it using the GNU make.  Since conditions are
sometimes different depending on which make you use, use GNU make if you
use my suggestion.

First put -DUNIX_CMD_LINE in a separate make var. like:

CMDLINE    = -DUNIX_CMD_LIN

Then append this on to the CFLAGS var:

CFLAGS = -g -ansi -pedantic  -Wall \
         -Wshadow -Wbad-function-cast -Wconversion \
         -Wstrict-prototypes
CFLAGS += $(CMDLINE)

Now only append cgi_util.c if the var CMDLINE is defined:

SRCS =   OutputFiles.c     ProcessTuples.c \
         SearchDatabase.c  WholeTableThread.c sinfo_util.c \
         sinfo.c

ifeq (,${CMDLINE})
# do nothing
else
# if it is defined, add it
SRCS    += cgi_util.c
endif

The ifeq is the condition in GNU make.  Now just comment out the make
variable CMDLINE when you don't want to define UNIX_CMD_LINE c
preprocessor directive and you don't want to include cgi_util.c in the
SRCS make var.

Hope this helps.

> Here is my Makefile:
> # Makefile for sinfo.  Default build is for CGI bin executable.
> # If you would rather build a Unix command line version of the
> # program define UNIX_CMD_LINE in the CFLAGS variable.
> #Linux make variables:
> #  Development flags (for gcc only)
> CFLAGS = -g -ansi -pedantic  -Wall \
>          -Wshadow -Wbad-function-cast -Wconversion \
>          -Wstrict-prototypes -DUNIX_CMD_LINE
> #Production flags for Linux
> #CFLAGS = -O2
> #Other platforms
> #SGI CFLAGS.
> #CFLAGS = -ansi -mips2 -O3
> #DEC OSF/1 FLAGS
> #CFLAGS = std1 -O3
> #General definitions.

> EXE     = sinfo
> LIB_DIR = -L/usr/local/pgsql/lib
> LIBS    = -lm -lpq
> SRCS =   OutputFiles.c     ProcessTuples.c \
>          SearchDatabase.c  WholeTableThread.c sinfo_util.c \
>          sinfo.c           cgi_util.c
> OBJS = $(SRCS:.c=.o)
> $(EXE): $(OBJS)

> .c.o:
>    $(CC) -c $(CFLAGS) $<
> clean:
>    rm -f *.o $(EXE)

--
Paul Landes


 
 
 

make & conditional compilation

Post by Nathan Dorfm » Wed, 13 May 1998 04:00:00



Quote:>I have a problem which probably trivial for some who knows 'make' well,
>but it has me stumped.  I have written a program which can act as either
>a CGI bin for a httpd server or a traditional Unix command line program
>depending on a compile time directive being defined.  For CGI related
>parsing, all of the function definitions and data structure declarations
>are contained in files called cgi_util.c and cgi_util.h.  These are not
>needed in the Unix command line version of the program.

>I would like to know how to write a Makefile such that if -DUNIX_CMD_LINE
>is defined in CFLAGS then the definition of the source files (SRCS,
>see below) will not include cgi_util.c

IMHO, it is better to have two separate make targets. You can a) easily
change the building environment based on what you're compiling and b)
the binaries then have two separate names, say foobar and foobar.cgi,
which let them coexist and won't confuse you on which is which.

>Director, X-ray Structural Facility |       phone: (919) 515-7362
>Department of Chemistry - Box 8204  |       FAX:   (919) 515-5079
>North Carolina State University     |
>Raleigh, NC, 27695-8204
>http://laue.chem.ncsu.edu/web/xray.welcome.html

 
 
 

make & conditional compilation

Post by Aaron Cra » Thu, 14 May 1998 04:00:00



Quote:> I have a problem which probably trivial for some who knows 'make' well,
> but it has me stumped.  I have written a program which can act as either
> a CGI bin for a httpd server or a traditional Unix command line program
> depending on a compile time directive being defined.  For CGI related
> parsing, all of the function definitions and data structure declarations
> are contained in files called cgi_util.c and cgi_util.h.  These are not
> needed in the Unix command line version of the program.

> I would like to know how to write a Makefile such that if -DUNIX_CMD_LINE
> is defined in CFLAGS then the definition of the source files (SRCS,
> see below) will not include cgi_util.c

My experience is that most `makefile' problems of this sort can easily be
solved by switching to GNU Autoconf and GNU Automake to manage the build
procedure.  In this instance, you can arrange for a `configure' script to
support a `--with-cgi' option; the Makefiles it generates will include or
exclude the necessary source files as appropriate.  Alternatively, you could
arrange for your Makefiles to have two targets, only one of which builds
with the cgi sources.  These packages are supplied with pretty good
documentation, including some comparatively complex examples.

Using Autoconf and Automake also has the advantage that it is comparatively
easy to do multi-platform operation.  I retro-fitted them onto a Linux-only
project of mine (about 5500 lines of code) in a few hours, and all of a
sudden the package was portable.

As with all GNU packages, they are available from
<URL:ftp://prep.ai.mit.edu/pub/gnu/> as well as numerous mirrors.

Regards.

--

% If I had a ) for every time Windows crashed, what would I have?
Too many )'s

 
 
 

1. YACC parser & conditional compilation directives

I'm writing a kind of assembler translator, the grammar is perfectly
suitable for Yacc, but my problem is the conditional compilation
directives. For example, in the following source first and third
sections must be bypassed:

DEFINE a,2

IF a==1
   ... Do one thing ...
ELSE
  .IF a==2
    ... Do another thing
  .ELSE
    ... Do yet another thing
  .ENDIF
ENDIF

etc.

(All directives start with dot)

Of course, I could use some kind of hack in lexer, that would recognize
IFs, .ELSEs, .ENDIFs etc. and act accordindly, but I want to know
if there is a _nice_ way of skipping the conditioned out source ?
Besides, the conditional expression may be quite complex and I would have
problems with its parsing, had I used the hack.

Vadim
[Yacc doesn't handle this kind of two-level grammar at all well.  You need
either a two-pass system or some gross lexical hacks. -John]
--


2. Red Hat Install locks up at CD-ROM question

3. Conditional compilation

4. citrix behind fbsd firewall

5. Conditional compilation in C?

6. pipes question

7. Sys V/BSD "ps" newbie g++ preprocessor conditional compilation?

8. Something Helpful

9. conditional compilation

10. make: conditional compilation/linking?

11. Assembler and Conditional Compilation (C Preprocessor)

12. conditional compilation

13. automake conditional compilation technicality