# Strict compilation, but suppress deprecation of POSIX and a couple of
# warnings that get generated by the system headers
PEDANTIC=/Wall /wd4555 /wd4221 /wd4210 /wd4152 /wd4204 /wd4127 /wd4996 /wd4820 /wd4255 /wd4668 /wd4201 /wd4232 /wd4710 /wd4214
CFLAGS=/nologo /MP /WX /Zi /Zo /Oi /MD /I $(MAKEDIR)\winbuild /I $(MAKEDIR) /I $(MAKEDIR)\thirdparty\jansson
LIBS=shlwapi.lib advapi32.lib dbghelp.lib
LINKER=link /nologo /out:$@ /debug /incremental:no /opt:ref /opt:icf /pdb:$(@:.exe=.pdb)

JSON_SRCS=\
	thirdparty\jansson\dump.c \
	thirdparty\jansson\error.c \
	thirdparty\jansson\hashtable.c \
	thirdparty\jansson\load.c \
	thirdparty\jansson\memory.c \
	thirdparty\jansson\pack_unpack.c \
	thirdparty\jansson\strbuffer.c \
	thirdparty\jansson\strconv.c \
	thirdparty\jansson\utf.c \
	thirdparty\jansson\value.c

SRCS=\
	$(JSON_SRCS) \
	winbuild\abort.c \
	winbuild\errmap.c \
	winbuild\pathmap.c \
	winbuild\stat.c \
	winbuild\realpath.c \
	winbuild\dir.c \
	winbuild\dirname.c \
	winbuild\asprintf.c \
	winbuild\hostname.c \
	thirdparty\wildmatch\wildmatch.c \
	thirdparty\libart\src\art.c \
	winbuild\time.c \
	winbuild\backtrace.c \
	winbuild\getopt_long.c \
	winbuild\posix_spawn.c \
	winbuild\pthread.c \
	argv.c \
	envp.c \
	ignore.c \
	spawn.c \
	opt.c \
	cfg.c \
	clockspec.c \
	checksock.c \
	fstype.c     \
	log.c        \
	json.c       \
	bser.c       \
	expflags.c   \
	hash.c       \
	ioprio.c     \
	opendir.c    \
	pending.c    \
	perf.c       \
	stream.c     \
	stream_win.c \
	stream_stdout.c \
	ht.c         \
	cmds\find.c     \
	cmds\info.c     \
	cmds\log.c      \
	cmds\query.c    \
	cmds\since.c    \
	cmds\reg.c      \
	cmds\state.c    \
	cmds\subscribe.c    \
	cmds\trigger.c  \
	cmds\watch.c    \
	cmds\debug.c    \
	query\base.c       \
	query\parse.c      \
	query\dirname.c    \
	query\eval.c       \
	query\glob.c       \
	query\type.c       \
	query\suffix.c     \
	query\match.c      \
	query\pcre.c       \
	query\name.c       \
	query\fieldlist.c  \
	query\intcompare.c  \
	query\since.c      \
	query\empty.c      \
	watcher\auto.c \
	watcher\win32.c \
	listener.c   \
	listener-user.c   \
	clientmode.c \
	main.c       \
	root\ageout.c       \
	root\crawler.c       \
	root\dir.c       \
	root\file.c       \
	root\init.c       \
	root\iothread.c       \
	root\lock.c       \
	root\notifythread.c       \
	root\poison.c       \
	root\reap.c       \
	root\resolve.c       \
	root\stat.c       \
	root\symlink.c       \
	root\sync.c       \
	root\threading.c       \
	root\vcs.c       \
	root\warnerr.c       \
	root\watchlist.c       \
	state.c      \
	string.c     \
	time.c

TEST_SRCS=\
	thirdparty\tap.c \
	thirdparty\wildmatch\wildmatch.c \
	winbuild\asprintf.c \
	winbuild\time.c \
	winbuild\pthread.c \
	winbuild\backtrace.c \
	winbuild\abort.c \
	winbuild\errmap.c \
	argv.c \
	$(JSON_SRCS)

TEST_DIR_SRCS=\
	tests\art_test.c \
	tests\argv.c \
	tests\bser.c \
	tests\ignore_test.c \
	tests\log_stub.c \
	tests\log.c

OBJS=$(SRCS:.c=.obj)
TEST_OBJS=$(TEST_SRCS:.c=.obj)
TEST_DIR_OBJS=$(TEST_DIR_SRCS:.c=.obj)
DEPS=$(SRCS:.c=.dep) $(TEST_SRCS:.c=.dep) $(TEST_DIR_SRCS:.c=.dep)
TESTS=tests\argv.exe tests\log.exe tests\bser.exe tests\wildmatch_test.exe \
			tests\art.exe tests\ignore.exe tests\pending.exe
COLLECTDEPS=$(CC) $(CFLAGS) /Zs /showIncludes /EHsc $< | cscript /nologo //E:jscript winbuild\depends.js --stdin

.c.dep:
	@echo DEPEND $<
	@$(CC) $(CFLAGS) /Zs /showIncludes /EHsc $< > $@

{}.c{}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:.\ /c $<
	$(COLLECTDEPS) .

{tests}.c{tests}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:tests\ /c $<
	$(COLLECTDEPS) tests

{winbuild}.c{winbuild}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:winbuild\ /c $<
	$(COLLECTDEPS) winbuild

{cmds}.c{cmds}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:cmds\ /c $<
	$(COLLECTDEPS) cmds

{query}.c{query}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:query\ /c $<
	$(COLLECTDEPS) query

{watcher}.c{watcher}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:watcher\ /c $<
	$(COLLECTDEPS) watcher

{root}.c{root}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) $(PEDANTIC) /Fo:root\ /c $<
	$(COLLECTDEPS) root

{thirdparty\wildmatch}.c{thirdparty\wildmatch}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) /Fo:thirdparty\wildmatch\ /c $<
	$(COLLECTDEPS) thirdparty\wildmatch

{thirdparty\libart\src}.c{thirdparty\libart\src}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) /Fo:thirdparty\libart\src\ /c $<
	$(COLLECTDEPS) thirdparty\libart\src

{thirdparty\jansson}.c{thirdparty\jansson}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) /Fo:thirdparty\jansson\ /c $<
	$(COLLECTDEPS) thirdparty\jansson

{thirdparty}.c{thirdparty}.obj::
	@echo CC $<
	$(CC) $(CFLAGS) /Fo:thirdparty\ /c $<
	$(COLLECTDEPS) thirdparty

all: remove-config-h watchman.exe build-tests depend susres.exe

remove-config-h:
	-del config.h watchman

# problems with vcvarsall.bat? http://stackoverflow.com/a/10558328/149111
py-build:
	cd python && python ./setup.py clean build_py -c -d . build_ext -i

py-integration: py-build
	python -u runtests.py

# helper for suspending/resuming a target process
susres.exe: winbuild\susres.c
	$(CC) /Fesusres.exe winbuild\susres.c

integration: build-tests susres.exe py-integration

build-tests: $(TESTS)

watchman.exe: $(OBJS)
	@echo LINK $@
	$(LINKER) $(OBJS) $(LIBS)

tests\argv.exe: tests\argv.obj $(TEST_OBJS) \
	tests\log_stub.obj \
	argv.obj \
	string.obj \
	hash.obj \
	log.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

tests\art.exe: tests\art_test.obj $(TEST_OBJS) \
  	log.obj \
	tests\log_stub.obj \
	thirdparty\libart\src\art.obj \
	string.obj \
	hash.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

tests\ignore.exe: tests\ignore_test.obj $(TEST_OBJS) \
		hash.obj \
		ht.obj \
		ignore.obj \
		string.obj \
		log.obj \
		tests\log_stub.obj \
		thirdparty\libart\src\art.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

tests\pending.exe: tests\pending_test.obj $(TEST_OBJS) \
		hash.obj \
		ht.obj \
		ignore.obj \
		pending.obj \
		expflags.obj \
		opendir.obj \
		cfg.obj \
		time.obj \
		string.obj \
		stream_win.obj \
		log.obj \
		tests\log_stub.obj \
		thirdparty\libart\src\art.obj \
		winbuild\pathmap.obj \
		winbuild\stat.obj \
		winbuild\realpath.obj \
		winbuild\dir.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

tests\bser.exe: tests\bser.obj bser.obj $(TEST_OBJS) \
	tests\log_stub.obj  \
	string.obj \
	hash.obj \
	log.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

tests\log.exe: tests\log.obj log.obj $(TEST_OBJS) \
	string.obj \
	hash.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

tests\wildmatch_test.exe: tests\wildmatch_test.obj $(TEST_OBJS) \
	tests\log_stub.obj  \
	string.obj \
	hash.obj \
	log.obj
	@echo LINK $@
	$(LINKER) $** $(LIBS)

.windeps: $(DEPS)
	@cscript /nologo //E:jscript winbuild\depends.js

depend: .windeps

dist:
	-mkdir Watchman
	copy watchman.exe Watchman
	copy watchman.pdb Watchman
	copy LICENSE Watchman\LICENSE.txt
	copy README.markdown Watchman

clean:
	-del $(OBJS) $(TEST_OBJS) $(TEST_DIR_OBJS) *.exe

distclean: clean
	-del $(DEPS) .windeps

!IF EXIST(.windeps)
!INCLUDE .windeps
!ENDIF
