# Generate Makefile code that can generate Makefile.dep
# Arguments:
#  /local/dep/CFLAGS   CFLAGS used for compiling
#  /local/dep/SRCS     list of c soures
#  /local/pcb/DEPDEPS  targets make dep should generate first

sortuniq /local/pcb/DEPDEPS_SORTED /local/pcb/DEPDEPS

print [@

### generate dependencies (requires gcc) ###
FORCE:

include Makefile.dep

DEPDEPS=@/local/pcb/DEPDEPS_SORTED@

dep: FORCE $(DEPDEPS)
	make -f Makefile.depgen PLUGDIR=$(PLUGDIR) LIBRND=$(LIBRND) LIBRND_PLUGDIR=$(LIBRND)/plugins SRC_3RD_DIR=$(SRC_3RD_DIR)
@]

append /local/dep/CFLAGS {-D_PCB_DEP_}
gsub /local/dep/CFLAGS {-I} {-isystem }
gsub /local/dep/CFLAGS {-isystem [.][.]} {-I ..}
gsub /local/dep/CFLAGS {-isystem [.]} {-I.}
sortuniq /local/dep/SRCS_SORTED /local/dep/SRCS

redir {Makefile.depgen}

print [@
# Generated by configure - DO NOT EDIT

LIBRND=../src/librnd

# Calculate dependencies, assuming all generated files present and
# SRC_3RD_DIR and PLUGDIR are set

all:
	echo "### Generated file, do not edit, run make dep ###" > Makefile.dep.tmp
	echo "" >> Makefile.dep.tmp
@]

foreach /local/c in /local/dep/SRCS_SORTED
	put /local/o /local/c
	sub {/local/o} {.c$} {.o}
	sub {/local/c} {.o$} {.c}
	switch /local/c
		case {/src_plugins/} end
		case {$(PLUGDIR)}
			print [@	gcc -MT @/local/o@ -MM @/local/c@ @/local/dep/CFLAGS@ @/local/pcb/DEPCFLAGS@ >> Makefile.dep.tmp
@]; end
		case {$(LIBRND_PLUGDIR)}
			print [@	gcc  -MT @/local/o@ -MM @/local/c@ @/local/dep/CFLAGS@ >> Makefile.dep.tmp
@]; end
		case {$(LIBRND)}
			print [@	gcc  -MT @/local/o@ -MM @/local/c@ @/local/dep/CFLAGS@ >> Makefile.dep.tmp
@]; end
		case {$(SRC_3RD_DIR)}
			print [@	gcc -MT @/local/o@ -MM @/local/c@ @/local/dep/CFLAGS@ >> Makefile.dep.tmp
@]; end
		case {../src_3rd/}
			print [@	gcc -MT @/local/o@ -MM @/local/c@ @/local/dep/CFLAGS@ >> Makefile.dep.tmp
@]; end

		default print [@	gcc -MM @/local/c@ @/local/dep/CFLAGS@ >> Makefile.dep.tmp
@]; end
	end
end

print [@
	sed "s^ librnd/^ $(LIBRND)/^g" < Makefile.dep.tmp > Makefile.dep && rm Makefile.dep.tmp
@]
