# Makefile for Cygwin utilities
# Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.

# This file is part of Cygwin.

# This software is a copyrighted work licensed under the terms of the
# Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
# details.

SHELL:=@SHELL@

srcdir:=@srcdir@
VPATH:=@srcdir@
prefix:=@prefix@
exec_prefix:=@exec_prefix@

bindir:=@bindir@
etcdir:=$(exec_prefix)/etc
program_transform_name:=@program_transform_name@

override INSTALL:=@INSTALL@
override INSTALL_PROGRAM:=@INSTALL_PROGRAM@
override INSTALL_DATA:=@INSTALL_DATA@

EXEEXT:=@EXEEXT@
EXEEXT_FOR_BUILD:=@EXEEXT_FOR_BUILD@

CC:=@CC@
CC_FOR_TARGET:=$(CC)
CXX:=@CXX@
CXX_FOR_TARGET:=$(CXX)

CFLAGS:=@CFLAGS@
CXXFLAGS:=@CXXFLAGS@
override CXXFLAGS+=-fno-exceptions -fno-rtti -DHAVE_DECL_GETOPT=0

include $(srcdir)/../Makefile.common

LIBICONV:=@libiconv@
libbfd:=${shell $(CC) -B$(bupdir2)/bfd/ --print-file-name=libbfd.a}
libintl:=${shell $(CC) -B$(bupdir2)/intl/ --print-file-name=libintl.a}
build_dumper:=${shell test -r $(libbfd) -a -r $(libintl) -a -n "$(LIBICONV)" && echo 1}

libz:=${shell x=$$($(CC) -mno-cygwin --print-file-name=libz.a); cd $$(dirname $$x); dir=$$(pwd); case "$$dir" in *mingw*) echo $$dir/libz.a ;; esac}
zlib_h:=-include ${patsubst %/lib/mingw/libz.a,%/include/zlib.h,${patsubst %/lib/libz.a,%/include/zlib.h,$(libz)}}
zconf_h:=${patsubst %/zlib.h,%/zconf.h,$(zlib_h)}
ifeq "${libz}" ""
zlib_h:=
zconf_h:=
libz:=
endif

DUMPER_INCLUDES:=-I$(bupdir2)/bfd -I$(updir1)/include

libcygwin:=$(cygwin_build)/libcygwin.a
libuser32:=$(w32api_lib)/libuser32.a
libkernel32:=$(w32api_lib)/libkernel32.a
ALL_DEP_LDLIBS:=$(libcygwin) $(w32api_lib)/libnetapi32.a \
		$(w32api_lib)/libadvapi32.a $(w32api_lib)/libkernel32.a \
		$(w32api_lib)/libuser32.a

ALL_LDLIBS:=${patsubst $(w32api_lib)/lib%.a,-l%,\
	      ${filter-out $(libuser32),\
	       ${filter-out $(libkernel32),\
		${filter-out $(libcygwin), $(ALL_DEP_LDLIBS)}}}}

MINGW_LIB:=$(mingw_build)/libmingw32.a
DUMPER_LIB:=${libbfd} ${libintl} -L$(bupdir1)/libiberty $(LIBICONV) -liberty
MINGW_LDLIBS:=${filter-out $(libcygwin),$(ALL_LDLIBS) $(MINGW_LIB)}
MINGW_DEP_LDLIBS:=${ALL_DEP_LDLIBS} ${MINGW_LIB}
ALL_LDFLAGS:=-B$(newlib_build)/libc -B$(w32api_lib) $(LDFLAGS) $(ALL_LDLIBS)
DUMPER_LDFLAGS:=$(ALL_LDFLAGS) $(DUMPER_LIB)
MINGW_CXX:=${patsubst %/cygwin/include,%/mingw/include,${filter-out -I$(newlib_source)/%,$(COMPILE_CXX)}} -I$(updir)

PROGS:=	cygcheck.exe cygpath.exe getfacl.exe kill.exe mkgroup.exe \
	mkpasswd.exe mount.exe passwd.exe ps.exe regtool.exe setfacl.exe \
	ssp.exe strace.exe umount.exe ipcrm.exe ipcs.exe

CLEAN_PROGS:=$(PROGS)
ifndef build_dumper
PROGS:=warn_dumper $(PROGS)
else
PROGS+=dumper$(EXEEXT)
CLEAN_PROGS+=dumper.exe
endif

.SUFFIXES:
.NOEXPORT:

.PHONY: all install clean realclean warn_dumper

all: Makefile $(PROGS)

strace.exe: strace.o path.o $(MINGW_DEP_LDLIBS)
ifdef VERBOSE
	$(CXX) $(MINGW_CXXFLAGS) -o $@ ${wordlist 1,2,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS)
else
	@echo $(CXX) -o $@ ${wordlist 1,2,$^} ${filter-out -B%, $(MINGW_CXXFLAGS) $(MINGW_LDFLAGS)};\
	$(CXX) $(MINGW_CXXFLAGS) -o $@ ${wordlist 1,2,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS)
endif

cygcheck.exe: cygcheck.o path.o dump_setup.o $(MINGW_DEP_LDLIBS)
ifeq "$(libz)" ""
	@echo '*** Building cygcheck without package content checking due to missing mingw libz.a.'
endif
ifdef VERBOSE
	$(CXX) $(MINGW_CXXFLAGS) -o $@ ${wordlist 1,3,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS) $(libz)
else
	@echo $(CXX) -o $@ ${wordlist 1,3,$^} ${filter-out -B%, $(MINGW_CXXFLAGS) $(MINGW_LDFLAGS)} $(libz);\
	$(CXX) $(MINGW_CXXFLAGS) -o $@ ${wordlist 1,3,$^} -B$(mingw_build)/ $(MINGW_LDFLAGS) $(libz)
endif

dumper.o: dumper.cc dumper.h
ifdef VERBOSE
	${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $@ $(DUMPER_INCLUDES) ${firstword $^}
else
	@echo $(CXX) $c $(CFLAGS) $(DUMPER_INCLUDES) ... $(basename $@).cc;\
	${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $(@D)/$(basename $@)$o $(DUMPER_INCLUDES) $<
endif

module_info.o: module_info.cc
ifdef VERBOSE
	${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $@ $(DUMPER_INCLUDES) ${firstword $^}
else
	@echo $(CXX) $c $(CFLAGS) $(DUMPER_INCLUDES) ... $(basename $@).cc;\
	${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $(@D)/$(basename $@)$o $(DUMPER_INCLUDES) $<
endif

parse_pe.o: parse_pe.cc dumper.h
ifdef VERBOSE
	${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $@ $(DUMPER_INCLUDES) ${firstword $^}
else
	@echo $(CXX) $c $(CFLAGS) $(DUMPER_INCLUDES) ... $(basename $@).cc;\
	${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $(@D)/$(basename $@)$o $(DUMPER_INCLUDES) $<
endif

path.o: path.cc
ifdef VERBOSE
	$(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) $<
else
	@echo $(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) ... $^;\
	${MINGW_CXX} $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) $<
endif

dump_setup.o: dump_setup.cc
ifdef VERBOSE
	$(MINGW_CXX) $(zconf_h) $(zlib_h) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) $<
else
	@echo $(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) ... $^;\
	$(MINGW_CXX) $(zconf_h) $(zlib_h) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) $<
endif

cygcheck.o: cygcheck.cc
ifdef VERBOSE
	${MINGW_CXX} $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) -I$(updir) $<
else
	@echo $(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) ... $^;\
	${MINGW_CXX} $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) -I$(updir) $<
endif

strace.o: strace.cc
ifdef VERBOSE
	$(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) -I$(updir) $<
else
	@echo $(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) ... $^;\
	$(MINGW_CXX) $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) -I$(updir) $<
endif

kill.exe: kill.o $(bupdir1)/libiberty/strsignal.o
ifdef VERBOSE
	$(CXX) -o $@ $^ -B$(cygwin_build)/ $(ALL_LDFLAGS) $(KILL_LIB)
else
	@echo $(CXX) -o $@ $^ ${filter-out -B%, $(ALL_LDFLAGS)};\
	$(CXX) -o $@ $^ -B$(cygwin_build)/ $(ALL_LDFLAGS) $(KILL_LIB)
endif

clean:
	rm -f *.o $(CLEAN_PROGS)

realclean: clean
	rm -f  Makefile config.cache

install: all
	$(SHELL) $(updir1)/mkinstalldirs $(bindir) $(etcdir)
	for i in $(PROGS) ; do \
	  n=`echo $$i | sed '$(program_transform_name)'`; \
	  $(INSTALL_PROGRAM) $$i $(bindir)/$$n; \
	done

$(cygwin_build)/libcygwin.a: $(cygwin_build)/Makefile
	@$(MAKE) -C $(@D) $(@F)

$(mingw_build)/libmingw32.a: $(mingw_build)/Makefile
	@$(MAKE) -C $(@D) $(@F)

warn_dumper:
	@echo '*** Not building dumper.exe since some required libraries are'
	@echo '*** missing: libbfd.a and libintl.a.'
	@echo '*** If you need this program, check out the naked-bfd and naked-intl'
	@echo '*** sources from sources.redhat.com.  Then, configure and build these'
	@echo '*** libraries.  Otherwise, you can safely ignore this warning.'

dumper.exe: module_info.o parse_pe.o dumper.o $(ALL_DEP_LDLIBS)
ifdef VERBOSE
	$(CXX) -o $@ ${wordlist 1,3,$^} -B$(cygwin_build)/ $(DUMPER_LDFLAGS)
else
	@echo $(CXX) -o $@ ${wordlist 1,3,$^} ${filter-out -B%, $(DUMPER_LDFLAGS)};\
	$(CXX) -o $@ ${wordlist 1,3,$^} -B$(cygwin_build)/ $(DUMPER_LDFLAGS)
endif

%.exe: %.o $(ALL_DEP_LDLIBS)
ifdef VERBOSE
	$(CXX) -o $@ ${firstword $^} -B$(cygwin_build)/ $(ALL_LDFLAGS)
else
	@echo $(CXX) -o $@ ${firstword $^} ... ${filter-out -B%, $(ALL_LDFLAGS)};\
	$(CXX) -o $@ ${firstword $^} -B$(cygwin_build)/ $(ALL_LDFLAGS)
endif