Eular
Eular

Reputation: 1807

makefile same executable with different dependencies

I'm trying to write a makefile that creates the same executable (EXE) but for different purposes, (The *.o files in OBJ_DIR are built with a separate rule, not shown here)

FILE1 = ... some list of files ...
FILE2 = ... different list of files...

myexe1: $(patsubst %, $(OBJ_DIR)/%.o, $(FILE1))| $(BIN_DIR)
    $(F90) -o $(EXE) $^ $(FFLAGS)

myexe2: $(patsubst %, $(OBJ_DIR)/%.o, $(FILE2))| $(BIN_DIR)
    $(F90) -o $(EXE) $^ $(FFLAGS)

I can call the make with make myexe1 and make myexe2 and build the EXE.
But as the EXE is not myexe1 or myexe2, the build is triggered every time the make myexe1/myexe2 is called. But what I want, is when myexe1 is called it should check if EXE is updated and then it should trigger the build. I can make myexe1 a phony target to achieve this partially, like

FILE1 = ... some list of files ...
FILE2 = ... different list of files...

myexe1 : $(EXE)

$(EXE): $(patsubst %, $(OBJ_DIR)/%.o, $(FILE1))| $(BIN_DIR)
    $(F90) -o $(EXE) $^ $(FFLAGS)

But then I can not run the myexe2 anymore. Is it possible that when myexe1 or myexe2 is called it should check if the EXE and the associated object files are updated, and only then it should trigger the build process?

Upvotes: 0

Views: 39

Answers (1)

MadScientist
MadScientist

Reputation: 100876

This is an odd requirement. But I recommend you build the unique target, then copy it to the common target as a side-effect:

FILE1 = ... some list of files ...
FILE2 = ... different list of files...

myexe1: $(patsubst %, $(OBJ_DIR)/%.o, $(FILE1))| $(BIN_DIR)
        $(F90) -o $@ $^ $(FFLAGS)
        cp $@ $(EXE)

myexe2: $(patsubst %, $(OBJ_DIR)/%.o, $(FILE2))| $(BIN_DIR)
        $(F90) -o $@ $^ $(FFLAGS)
        cp $@ $(EXE)

Now whichever one of myexe1 or myexe2 was last built, will be the one in the $(EXE).

Upvotes: 2

Related Questions