# Makefile for Cygwin utilities

# 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.

srcdir:=/usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup/utils
target_builddir:=/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin
winsup_srcdir:=/usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup
configure_args='--disable-option-checking' '--prefix=/usr' '--with-newlib' '--enable-multilib' '--sysconfdir=/etc' '--sharedstatedir=/var' '--localstatedir=/var' '--program-transform-name=s,y,y,' '--with-target-subdir=x86_64-pc-cygwin' '--build=x86_64-pc-linux-gnu' '--host=x86_64-pc-cygwin' '--target=x86_64-pc-cygwin' 'build_alias=x86_64-pc-linux-gnu' 'host_alias=x86_64-pc-cygwin' 'target_alias=x86_64-pc-cygwin' 'CC=x86_64-pc-cygwin-cc -L/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/winsup/cygwin -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup/cygwin/include -B/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/ -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/targ-include -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/newlib/libc/include ' 'CFLAGS=-g -O2 ' 'LDFLAGS=' 'CPPFLAGS=' 'CXX=x86_64-pc-cygwin-c++ -L/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/winsup/cygwin -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup/cygwin/include -B/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/ -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/targ-include -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/newlib/libc/include ' 'CXXFLAGS=-g -O2 ' '--cache-file=.././config.cache'

export CC:=x86_64-pc-cygwin-cc -L/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/winsup/cygwin -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup/cygwin/include -B/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/ -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/targ-include -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/newlib/libc/include   
export CXX:=x86_64-pc-cygwin-c++ -L/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/winsup/cygwin -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup/cygwin/include -B/usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/ -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/targ-include -isystem /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/newlib/libc/include   

CFLAGS:=-g -O2 
CXXFLAGS:=-g -O2 
override CXXFLAGS+=-fno-exceptions -fno-rtti

include ${srcdir}/../Makefile.common

cygwin_build:=${target_builddir}/winsup/cygwin

cygwin_headers:=/usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/winsup/cygwin/include

# environment variables used by ccwrap
export CCWRAP_HEADERS:=. ${srcdir} $(call justdir,${cygwin_headers})
export CCWRAP_SYSTEM_HEADERS:=${cygwin_headers} /usr/src/testing/newlib-cygwin-2.11.1-1/build/x86_64-pc-cygwin/newlib/targ-include /usr/src/testing/newlib-cygwin-2.11.1-1/newlib-cygwin-2.11.1/newlib/libc/include
export CCWRAP_DIRAFTER_HEADERS:=/home/cygsimple/opt/usr/x86_64-pc-cygwin/include/w32api

WINDOWS_LIBDIR:=/home/cygsimple/opt/x86_64-pc-cygwin/lib/w32api

prefix:=/usr
exec_prefix:=${prefix}

bindir:=${exec_prefix}/bin
program_transform_name:=s,y,y,

override INSTALL:=/usr/bin/install -c
override INSTALL_PROGRAM:=/usr/bin/install -c
override INSTALL_DATA:=/usr/bin/install -c -m 644

EXEEXT:=.exe
EXEEXT_FOR_BUILD:=@EXEEXT_FOR_BUILD@

.PHONY: all install clean realclean warn_dumper warn_cygcheck_zlib

LDLIBS := -lnetapi32 -ladvapi32 -lkernel32 -luser32
CYGWIN_LDFLAGS := -static -Wl,--enable-auto-import -L${WINDOWS_LIBDIR} $(LDLIBS)
DEP_LDLIBS := $(cygwin_build)/libcygwin.a

MINGW_CXX      := x86_64-w64-mingw32-g++

# List all binaries to be linked in Cygwin mode.  Each binary on this list
# must have a corresponding .o of the same name.
CYGWIN_BINS := ${addsuffix .exe,cygpath gencat getconf getfacl ldd locale kill minidumper mkgroup \
        mkpasswd mount passwd pldd ps regtool setfacl setmetamode ssp tzset umount}

# List all binaries to be linked in MinGW mode.  Each binary on this list
# must have a corresponding .o of the same name.
MINGW_BINS := ${addsuffix .exe,cygcheck cygwin-console-helper ldh strace}

# List all objects to be compiled in MinGW mode.  Any object not on this
# list will will be compiled in Cygwin mode implicitly, so there is no
# need for a CYGWIN_OBJS.
MINGW_OBJS := bloda.o cygcheck.o dump_setup.o ldh.o path.o strace.o
MINGW_LDFLAGS:=-static

CYGCHECK_OBJS:=cygcheck.o bloda.o path.o dump_setup.o
ZLIB:=-lz

.PHONY: all
all:

# If a binary should link in any objects besides the .o with the same
# name as the binary, then list those here.
strace.exe: path.o
cygcheck.exe: cygcheck.o bloda.o path.o dump_setup.o

path-mount.o: path.cc
	${COMPILE.cc} -c -DFSTAB_ONLY -o $@ $<
mount.exe: path-mount.o

.PHONY: tzmap
tzmap:
	${srcdir}/tzmap-from-unicode.org > ${srcdir}/$@.h

tzmap.h:
	[ -f "${srcdir}/tzmap.h" ] || ${srcdir}/tzmap-from-unicode.org > ${srcdir}/$@

tzset.o: tzmap.h

# Provide any necessary per-target variable overrides.

cygcheck.exe: MINGW_LDFLAGS += ${ZLIB} -lwininet -lpsapi -lntdll
cygcheck.exe: ${CYGCHECK_OBJS}

cygpath.o: CXXFLAGS += -fno-threadsafe-statics
cygpath.exe: CYGWIN_LDFLAGS += -lcygwin -luserenv -lntdll
ps.exe: CYGWIN_LDFLAGS += -lcygwin -lpsapi -lntdll
strace.exe: MINGW_LDFLAGS += -lntdll

ldd.exe:CYGWIN_LDFLAGS += -lpsapi
pldd.exe: CYGWIN_LDFLAGS += -lpsapi
minidumper.exe: CYGWIN_LDFLAGS += -ldbghelp

ldh.exe: MINGW_LDFLAGS += -nostdlib -lkernel32

# Check for dumper's requirements and enable it if found.
libbfd   := $(call libname,libbfd.a)
build_dumper := $(shell test -r "$(libbfd)" && echo 1)

ifdef build_dumper
CYGWIN_BINS += dumper.exe
dumper.o module_info.o parse_pe.o: CXXFLAGS += -I$(top_srcdir)/include
dumper.o parse_pe.o: dumper.h
dumper.exe: module_info.o parse_pe.o
dumper.exe: CYGWIN_LDFLAGS += -lpsapi -lbfd -lintl -liconv -liberty ${ZLIB}
else
all: warn_dumper
endif

all: Makefile $(CYGWIN_BINS) $(MINGW_BINS)

# test harness support (note: the "MINGW_BINS +=" should come after the
# "all:" above so that the testsuite is not run for "make" but only
# "make check".)
MINGW_BINS += testsuite.exe
MINGW_OBJS += path-testsuite.o testsuite.o
testsuite.exe: path-testsuite.o
path-testsuite.cc: path.cc ; @test -L $@ || ln -sf ${filter %.cc,$^} $@
path-testsuite.o: MINGW_CXXFLAGS += -DTESTSUITE
# this is necessary because this .c lives in the build dir instead of src
path-testsuite.o: MINGW_CXX := ${patsubst -I.,-I$(utils_source),$(MINGW_CXX)}
path-testsuite.cc path.cc testsuite.cc: testsuite.h
check: testsuite.exe ; $(<D)/$(<F)

# the rest of this file contains generic rules

# how to compile a MinGW object
${MINGW_OBJS}: override CXX:=${MINGW_CXX}
${MINGW_OBJS}: CCWRAP_HEADERS:=${srcdir}
${MINGW_OBJS}: CCWRAP_SYSTEM_HEADERS:=
# ${MINGW_OBJS}: CCWRAP_DIRAFTER_HEADERS:=
$(MINGW_OBJS): %.o: %.cc
	c++wrap -c -o $@ ${CXXFLAGS} $(MINGW_CXXFLAGS) $<

# how to link a MinGW binary
$(MINGW_BINS): %.exe: %.o
	$(MINGW_CXX) $(MINGW_CXXFLAGS) -o $@ ${filter %.o,$^} $(MINGW_LDFLAGS)

# how to link a Cygwin binary
$(CYGWIN_BINS): %.exe: %.o
	${CXX} -o $@ $(filter %.o,$^) -B${cygwin_build}/ ${LDFLAGS} ${CYGWIN_LDFLAGS}

# note: how to compile a Cygwin object is covered by the pattern rule in Makefile.common

# these dependencies ensure that the required in-tree libs are built first
$(MINGW_BINS): $(DEP_LDLIBS)
$(CYGWIN_BINS): $(DEP_LDLIBS)

cygcheck.o cygpath.o module_info.o path.o ps.o regtool.o strace.o: loadlib.h

.PHONY: clean
clean:
	rm -f *.o $(CYGWIN_BINS) $(MINGW_BINS) path-testsuite.cc testsuite.exe

.PHONY: realclean
realclean: clean
	rm -f Makefile config.cache

.PHONY: install
install: all
	/bin/mkdir -p ${DESTDIR}${bindir}
	for i in $(CYGWIN_BINS) ${filter-out testsuite.exe,$(MINGW_BINS)} ; do \
	  n=`echo $$i | sed '$(program_transform_name)'`; \
	  $(INSTALL_PROGRAM) $$i $(DESTDIR)$(bindir)/$$n; \
	done

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

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

Makefile: Makefile.in config.status
	/bin/sh ./config.status
