For such scenarios, a special Makefile need to handle multiple access to the resource. The target employs lockfile command to create a lock variable and prevent other process to start the compilation. Once the lock is acquired, the makefile calls itself recursively, now with a flag (called PROTECTED) set. The rule will be checked again, but now it will actually build the library or file, if it not updated.
If it was updated, it won't compile again. This happen when a process check the file need to be built, but other process already get the lock. Once the lock is released, it won't need to build the process again.
The sample Makefile showed bellow try to build the out out_file from the source_file (what a lack of creativity!).
# This makefile that generate one resouce in paralellal mode.
# the out_file is the target. First, the makefile verify if it
# is running protected, by consulting a PROTECTED variable.
# If not, it will create a lock file, and call itself with
# protected flag on, once it aquired the lock. As it call the
# Makefile again, the rule will be checked if the resource
# has already been created. So, the shared resource between
# process running the makefile will only be created once!
#
MAKEFILE=./Makefile
ifeq ($(PROTECTED),YES)
else
all: out_file
@echo "Makefile $(MAKE_NUM) done!"
endif
out_file: source_file
ifeq ($(PROTECTED),YES)
@echo "Creating out_file"
@rm -f out_file
@sleep 3
@touch out_file
else
@lockfile make.lock
@echo "Enter Critical Section"
@$(MAKE) -f $(MAKEFILE) PROTECTED=YES
@echo "Leave Critical Section"
@rm -f make.lock
endif
