Mike
Mike

Reputation: 49473

Is it possible to have gnu make check for a files existence?

Is it possible to get make to check for a file before linking?

I have a makefile system with a top level Makefile that calls into other subdirectories and issues make in them.
The goal of my system is:

  1. build the source
    1. Stop the build on the parent directory and any current child directory if there are any compilation errors.
  2. link the executables
    1. Display an error during the linking stage if there is a failure due to a missing archive file. Note: Only the build at the current subdirectory level should show the error and bounce out, but the overall procedure should continue and move onto the next child directory.
    2. Display an error during the linking stage if there is a failure due to an undefined symbol in an existing archive file

So right now I have my child Makefile doing a "if build fails, then parent fail, if link fails, then parent continue" sort of thing by:

#build the source code
    $(CC) -o $@ -c $<

#link the executable
    -$(CC) $^ -o $@ $(LIB)  #the - allows the parent to continue even if this fails

And this works, however this will allow any link error to go through, I only want to allow the parent to continue if the archive $(LIB) doesn't exist.


To clarify: Given the following directory structure:

C
├── Makefile
└── childdir
    ├── a.c      // This source file uses functions from idontexist.a
    └── Makefile

The top level Makefile is:

.PHONEY: all

all:    
    @echo "Start the build!"      # I want to see this always
    $(MAKE) --directory=childdir  # then if this works, or fails because the 
                                  # .a is missing
    @echo "Do more stuff!"        # then I want to see this   

The Makefile at childdir/ is:

LIB=idontexist.a  #This doesn't exist and that's fine

EXE=target

SRC=a.c
OBJS=$(patsubst %.c,%.o,$(SRC))

%.o : %.c
    $(CC) -o $@ -c $<    #If this fails, I want the ENTIRE build to fail, that's
                           # good, I want that.
.PHONEY: all

all: $(EXE)

$(EXE):$(OBJS)
    -$(CC) $^ -o $@ $(LIB)  #If this fails, because $(LIB) is missing
                             # I don't really care, if it's because we can't find 
                             # some symbol AND the file DOES exist, that's a problem

Upvotes: 0

Views: 197

Answers (1)

MadScientist
MadScientist

Reputation: 101041

You can use:

LIB = $(wildcard idontexist.a)

which will expand to the filename if it exists, or empty if it doesn't.

Upvotes: 0

Related Questions