# VPC MASTER MAKEFILE



SHELL:=/bin/bash
# to control parallelism, set the MAKE_JOBS environment variable
ifeq ($(strip $(MAKE_JOBS)),)
	ifeq ($(shell uname),Darwin)
		CPUS:=$(shell /usr/sbin/sysctl -n hw.ncpu)
	endif
	ifeq ($(shell uname),Linux)
		CPUS:=$(shell grep processor /proc/cpuinfo | wc -l)
	endif
	MAKE_JOBS:=$(CPUS)
endif

ifeq ($(strip $(MAKE_JOBS)),)
	MAKE_JOBS:=8
endif

# All projects (default target)
all: 
	@$(MAKE) -f $(lastword $(MAKEFILE_LIST)) -j$(MAKE_JOBS) all-targets

all-targets : server_hl2mp client_hl2mp tier1 vgui_controls mathlib 


# Individual projects + dependencies

server_hl2mp : tier1 mathlib 
	@echo "Building: server_hl2mp"
	@+cd game/server && $(MAKE) -f server_osx32_hl2mp.mak $(CLEANPARAM)

client_hl2mp : tier1 vgui_controls mathlib 
	@echo "Building: client_hl2mp"
	@+cd game/client && $(MAKE) -f client_osx32_hl2mp.mak $(CLEANPARAM)

tier1 : 
	@echo "Building: tier1"
	@+cd tier1 && $(MAKE) -f tier1_osx32.mak $(CLEANPARAM)

vgui_controls : tier1 
	@echo "Building: vgui_controls"
	@+cd vgui2/vgui_controls && $(MAKE) -f vgui_controls_osx32.mak $(CLEANPARAM)

mathlib : tier1 
	@echo "Building: mathlib"
	@+cd mathlib && $(MAKE) -f mathlib_osx32.mak $(CLEANPARAM)

# this is a bit over-inclusive, but the alternative (actually adding each referenced c/cpp/h file to
# the tags file) seems like more work than it's worth.  feel free to fix that up if it bugs you. 
TAGS:
	@rm -f TAGS
	@find game/server -name '*.cpp' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find game/server -name '*.h' -print0 | xargs -0 etags --language=c++ --declarations --ignore-indentation --append
	@find game/server -name '*.c' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find game/client -name '*.cpp' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find game/client -name '*.h' -print0 | xargs -0 etags --language=c++ --declarations --ignore-indentation --append
	@find game/client -name '*.c' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find tier1 -name '*.cpp' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find tier1 -name '*.h' -print0 | xargs -0 etags --language=c++ --declarations --ignore-indentation --append
	@find tier1 -name '*.c' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find vgui2/vgui_controls -name '*.cpp' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find vgui2/vgui_controls -name '*.h' -print0 | xargs -0 etags --language=c++ --declarations --ignore-indentation --append
	@find vgui2/vgui_controls -name '*.c' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find mathlib -name '*.cpp' -print0 | xargs -0 etags --declarations --ignore-indentation --append
	@find mathlib -name '*.h' -print0 | xargs -0 etags --language=c++ --declarations --ignore-indentation --append
	@find mathlib -name '*.c' -print0 | xargs -0 etags --declarations --ignore-indentation --append



# Mark all the projects as phony or else make will see the directories by the same name and think certain targets 

.PHONY: TAGS showtargets regen showregen clean cleantargets cleanandremove relink server_hl2mp client_hl2mp tier1 vgui_controls mathlib 



# The standard clean command to clean it all out.

clean: 
	@$(MAKE) -f $(lastword $(MAKEFILE_LIST)) -j$(MAKE_JOBS) all-targets CLEANPARAM=clean



# clean targets, so we re-link next time.

cleantargets: 
	@$(MAKE) -f $(lastword $(MAKEFILE_LIST)) -j$(MAKE_JOBS) all-targets CLEANPARAM=cleantargets



# p4 edit and remove targets, so we get an entirely clean build.

cleanandremove: 
	@$(MAKE) -f $(lastword $(MAKEFILE_LIST)) -j$(MAKE_JOBS) all-targets CLEANPARAM=cleanandremove



#relink

relink: cleantargets 
	@$(MAKE) -f $(lastword $(MAKEFILE_LIST)) -j$(MAKE_JOBS) all-targets



# Here's a command to list out all the targets


showtargets: 
	@echo '-------------------' && \
	echo '----- TARGETS -----' && \
	echo '-------------------' && \
	echo 'clean' && \
	echo 'server_hl2mp' && \
	echo 'client_hl2mp' && \
	echo 'tier1' && \
	echo 'vgui_controls' && \
	echo 'mathlib'