#copyright (c) 2006-2012 Kazuki Iwamoto http://www.maid.org/ iwm@maid.org
#
#Permission is hereby granted, free of charge, to any person obtaining
#a copy of this software and associated documentation files (the
#"Software"), to deal in the Software without restriction, including
#without limitation the rights to use, copy, modify, merge, publish,
#distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so, subject to
#the following conditions:
#
#The above copyright notice and this permission notice shall be included
#in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MAJOR=0
MINOR=0
MICRO=7
VERSION=$(MAJOR).$(MINOR).$(MICRO)

!ifndef LINK
LINK=link
!endif
!ifndef MT
MT=mt
!endif

CLFLAGS=/c /D_MBCS /DNDEBUG /DWIN32 /D_WINDOWS /D_WIN32_WINNT=0x0500 /DWINVER=0x0500 /D_CRT_SECURE_NO_DEPRECATE /Fo$@ /MD /nologo /O2 /wd4819
LNFLAGS=/INCREMENTAL:NO /MACHINE:IX86 /NOLOGO /OUT:$@ /RELEASE
RCFLAGS=/c1252 /d_MBCS /dNDEBUG /dWIN32 /d_WINDOWS /d_WIN32_WINNT=0x0500 /dWINVER=0x0500 /fo$@ /l0
ASFLAGS=/c /Fo$@ /nologo /safeseh
LBFLAGS=/MACHINE:IX86 /NODEFAULTLIB:msvcrt.lib /NOLOGO /OUT:$@
ICFLAGS=/DEFAULTLIB:iwmcrt\iwmcrt.lib /NODEFAULTLIB:msvcrt.lib

all: iconv.dll intl.dll msgfmt.exe

msgfmt.exe: iwmcrt\iwmcrt.lib $*.res $*.obj
	$(LINK) $(LNFLAGS) $(ICFLAGS) $*.res $*.obj
	if exist $@.manifest $(MT) -manifest $@.manifest -outputresource:$@;#1

msgfmt.res: $*.rc iconvmlang.ico
	$(RC) $(RCFLAGS) /DMAJOR=$(MAJOR) /DMINOR=$(MINOR) /DMICRO=$(MICRO) /DVERSION=\"$(VERSION)\" $*.rc

msgfmt.obj: $*.c
	$(CC) $(CLFLAGS) $**

intl.dll: iwmcrt\iwmcrt.lib iconv.dll $*.res $*.obj
	$(LINK) $(LNFLAGS) $(ICFLAGS) /DLL \
		/EXPORT:bind_textdomain_codeset=libintl_bind_textdomain_codeset,@3 \
		/EXPORT:bindtextdomain=libintl_bindtextdomain,@4 \
		/EXPORT:dcgettext=libintl_dcgettext,@5 \
		/EXPORT:dcngettext=libintl_dcngettext,@6 \
		/EXPORT:dgettext=libintl_dgettext,@7 \
		/EXPORT:dngettext=libintl_dngettext,@8 \
		/EXPORT:gettext=libintl_gettext,@9 \
		/EXPORT:libintl_bind_textdomain_codeset,@10 \
		/EXPORT:libintl_bindtextdomain,@11 \
		/EXPORT:libintl_dcgettext,@12 \
		/EXPORT:libintl_dcngettext,@13 \
		/EXPORT:libintl_dgettext,@14 \
		/EXPORT:libintl_dngettext,@15 \
		/EXPORT:libintl_fprintf,@16 \
		/EXPORT:libintl_gettext,@17 \
		/EXPORT:libintl_ngettext,@18 \
		/EXPORT:libintl_printf,@19 \
		/EXPORT:libintl_set_relocation_prefix,@20 \
		/EXPORT:libintl_sprintf,@21 \
		/EXPORT:libintl_textdomain,@22 \
		/EXPORT:libintl_version,@23 \
		/EXPORT:libintl_vfprintf,@24 \
		/EXPORT:libintl_vprintf,@25 \
		/EXPORT:libintl_vsprintf,@26 \
		/EXPORT:ngettext=libintl_ngettext,@27 \
		/EXPORT:textdomain=libintl_textdomain,@28 iconv.lib kernel32.lib user32.lib $*.res $*.obj
	if exist $@.manifest $(MT) -manifest $@.manifest -outputresource:$@;#2

intl.res: $*.rc
	$(RC) $(RCFLAGS) /DMAJOR=$(MAJOR) /DMINOR=$(MINOR) /DMICRO=$(MICRO) /DVERSION=\"$(VERSION)\" $**

intl.obj: $*.c iconv.h
	$(CC) $(CLFLAGS) $*.c

iconv.dll: iwmcrt\iwmcrt.lib $*.res $*.obj
	$(LINK) $(LNFLAGS) $(ICFLAGS) /DLL \
		/EXPORT:_libiconv_version,@1 \
		/EXPORT:iconv_canonicalize,@2 \
		/EXPORT:libiconv,@3 \
		/EXPORT:libiconv_close,@4 \
		/EXPORT:libiconv_open,@5 \
		/EXPORT:libiconv_set_relocation_prefix,@6 \
		/EXPORT:libiconvctl,@7 \
		/EXPORT:libiconvlist,@8 kernel32.lib ole32.lib oleaut32.lib user32.lib $*.res $*.obj
	if exist $@.manifest $(MT) -manifest $@.manifest -outputresource:$@;#2

iconv.res: $*.rc
	$(RC) $(RCFLAGS) /DMAJOR=$(MAJOR) /DMINOR=$(MINOR) /DMICRO=$(MICRO) /DVERSION=\"$(VERSION)\" $**

iconv.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt_OBJECTS= \
	iwmcrt\alldiv.obj \
	iwmcrt\alldvrm.obj \
	iwmcrt\allmul.obj \
	iwmcrt\alloca.obj \
	iwmcrt\alloca16.obj \
	iwmcrt\alloca8.obj \
	iwmcrt\allrem.obj \
	iwmcrt\allshl.obj \
	iwmcrt\allshr.obj \
	iwmcrt\aulldiv.obj \
	iwmcrt\aulldvrm.obj \
	iwmcrt\aullrem.obj \
	iwmcrt\aullshr.obj \
	iwmcrt\chkstk.obj \
	iwmcrt\atexit.obj \
	iwmcrt\dllmain.obj \
	iwmcrt\dllmaincrtstartup.obj \
	iwmcrt\fstat32.obj \
	iwmcrt\fstat32i64.obj \
	iwmcrt\fstat64i32.obj \
	iwmcrt\iwmcrt.obj \
	iwmcrt\maincrtstartup.obj \
	iwmcrt\onexit.obj \
	iwmcrt\security.obj \
	iwmcrt\stat32.obj \
	iwmcrt\stat32i64.obj \
	iwmcrt\stat64i32.obj \
	iwmcrt\tlstable.obj \
	iwmcrt\winmaincrtstartup.obj \
	iwmcrt\wmaincrtstartup.obj \
	iwmcrt\wstat32.obj \
	iwmcrt\wstat32i64.obj \
	iwmcrt\wstat64i32.obj \
	iwmcrt\wwinmaincrtstartup.obj \
	iwmcrt\tlsarray.obj

iwmcrt\iwmcrt.lib: $*.def $(iwmcrt_OBJECTS)
	lib $(LBFLAGS) /DEF:$*.def
	lib $(LBFLAGS) $@ $(iwmcrt_OBJECTS)

iwmcrt\iwmcrt.def: iwmcrt\iwmcrt-makedef.exe
	iwmcrt\iwmcrt-makedef.exe > $@

iwmcrt\alldiv.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\alldvrm.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\allmul.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\alloca.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\alloca16.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\alloca8.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\allrem.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\allshl.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\allshr.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\aulldiv.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\aulldvrm.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\aullrem.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\aullshr.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\chkstk.obj: $*.asm
	$(AS) $(ASFLAGS) $**

iwmcrt\atexit.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\dllmain.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\dllmaincrtstartup.obj: $*.c iwmcrt\iwmcrt.h
	$(CC) $(CLFLAGS) $*.c

iwmcrt\fstat32.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\fstat32i64.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\fstat64i32.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\iwmcrt.obj: $*.c $*.h
	$(CC) $(CLFLAGS) $*.c

iwmcrt\maincrtstartup.obj: $*.c iwmcrt\iwmcrt.h iwmcrt\main.c
	$(CC) $(CLFLAGS) $*.c

iwmcrt\onexit.obj: $*.c iwmcrt\iwmcrt.h
	$(CC) $(CLFLAGS) $*.c

iwmcrt\security.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\stat32.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\stat32i64.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\stat64i32.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\tlstable.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\winmaincrtstartup.obj: $*.c iwmcrt\iwmcrt.h iwmcrt\main.c
	$(CC) $(CLFLAGS) $*.c

iwmcrt\wmaincrtstartup.obj: $*.c iwmcrt\iwmcrt.h iwmcrt\main.c
	$(CC) $(CLFLAGS) $*.c

iwmcrt\wstat32.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\wstat32i64.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\wstat64i32.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\wwinmaincrtstartup.obj: $*.c iwmcrt\iwmcrt.h iwmcrt\main.c
	$(CC) $(CLFLAGS) $*.c

iwmcrt\tlsarray.obj: iwmcrt\iwmcrt-tlsarray.exe
	iwmcrt\iwmcrt-tlsarray.exe > $@

iwmcrt\iwmcrt-makedef.exe: $*.res $*.obj
	$(LINK) $(LNFLAGS) $**
	if exist $@.manifest $(MT) -manifest $@.manifest -outputresource:$@;#1

iwmcrt\iwmcrt-makedef.res: $*.rc iwmcrt\iwmcrt.ico
	$(RC) $(RCFLAGS) /DMAJOR=0 /DMINOR=0 /DMICRO=0 /DVERSION=\"0.0.0\" $*.rc

iwmcrt\iwmcrt-makedef.obj: $*.c
	$(CC) $(CLFLAGS) $**

iwmcrt\iwmcrt-tlsarray.exe: $*.res $*.obj
	$(LINK) $(LNFLAGS) $**
	if exist $@.manifest $(MT) -manifest $@.manifest -outputresource:$@;#1

iwmcrt\iwmcrt-tlsarray.res: $*.rc iwmcrt\iwmcrt.ico
	$(RC) $(RCFLAGS) /DMAJOR=0 /DMINOR=0 /DMICRO=0 /DVERSION=\"0.0.0\" $*.rc

iwmcrt\iwmcrt-tlsarray.obj: $*.c
	$(CC) $(CLFLAGS) $**

clean:
	if exist iconv.dll del /q iconv.dll
	if exist iconv.dll.manifest del /q iconv.dll.manifest
	if exist iconv.exp del /q iconv.exp
	if exist iconv.lib del /q iconv.lib
	if exist iconv.obj del /q iconv.obj
	if exist iconv.res del /q iconv.res
	if exist intl.dll del /q intl.dll
	if exist intl.dll.manifest del /q intl.dll.manifest
	if exist intl.exp del /q intl.exp
	if exist intl.lib del /q intl.lib
	if exist intl.obj del /q intl.obj
	if exist intl.res del /q intl.res
	if exist msgfmt.exe del /q msgfmt.exe
	if exist msgfmt.exe.manifest del /q msgfmt.exe.manifest
	if exist msgfmt.obj del /q msgfmt.obj
	if exist msgfmt.res del /q msgfmt.res
	if exist iwmcrt\iwmcrt.def del /q iwmcrt\iwmcrt.def
	if exist iwmcrt\iwmcrt.exp del /q iwmcrt\iwmcrt.exp
	if exist iwmcrt\iwmcrt.lib del /q iwmcrt\iwmcrt.lib
	if exist iwmcrt\iwmcrt-makedef.exe del /q iwmcrt\iwmcrt-makedef.exe
	if exist iwmcrt\iwmcrt-makedef.exe.manifest del /q iwmcrt\iwmcrt-makedef.exe.manifest
	if exist iwmcrt\iwmcrt-makedef.obj del /q iwmcrt\iwmcrt-makedef.obj
	if exist iwmcrt\iwmcrt-makedef.res del /q iwmcrt\iwmcrt-makedef.res
	if exist iwmcrt\iwmcrt-tlsarray.exe del /q iwmcrt\iwmcrt-tlsarray.exe
	if exist iwmcrt\iwmcrt-tlsarray.exe.manifest del /q iwmcrt\iwmcrt-tlsarray.exe.manifest
	if exist iwmcrt\iwmcrt-tlsarray.obj del /q iwmcrt\iwmcrt-tlsarray.obj
	if exist iwmcrt\iwmcrt-tlsarray.res del /q iwmcrt\iwmcrt-tlsarray.res
	for %f in ($(iwmcrt_OBJECTS)) do if exist %f del /q %f

dist:
	mkdir iconvmlang-$(VERSION)
	copy /y AUTHORS iconvmlang-$(VERSION)
	copy /y COPYING iconvmlang-$(VERSION)
	copy /y Makefile iconvmlang-$(VERSION)
	copy /y iconv.dll iconvmlang-$(VERSION)
	copy /y iconv.lib iconvmlang-$(VERSION)
	copy /y iconv.c iconvmlang-$(VERSION)
	copy /y iconv.rc iconvmlang-$(VERSION)
	copy /y iconv.h iconvmlang-$(VERSION)
	copy /y intl.dll iconvmlang-$(VERSION)
	copy /y intl.lib iconvmlang-$(VERSION)
	copy /y intl.c iconvmlang-$(VERSION)
	copy /y intl.rc iconvmlang-$(VERSION)
	copy /y libintl.h iconvmlang-$(VERSION)
	copy /y msgfmt.exe iconvmlang-$(VERSION)
	copy /y msgfmt.c iconvmlang-$(VERSION)
	copy /y msgfmt.rc iconvmlang-$(VERSION)
	copy /y iconvmlang.ico iconvmlang-$(VERSION)
	mkdir iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt.ico iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\alldiv.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\alldvrm.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\allmul.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\alloca.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\alloca8.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\alloca16.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\allrem.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\allshl.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\allshr.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\aulldiv.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\aulldvrm.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\aullrem.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\aullshr.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\chkstk.asm iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\atexit.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\dllmain.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\dllmaincrtstartup.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\fstat32.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\fstat32i64.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\fstat64i32.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt.h iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\main.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\maincrtstartup.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\onexit.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\security.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\stat32.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\stat32i64.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\stat64i32.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\tlstable.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\winmaincrtstartup.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\wmaincrtstartup.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\wstat32.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\wstat32i64.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\wstat64i32.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\wwinmaincrtstartup.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt-makedef.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt-makedef.rc iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt-tlsarray.c iconvmlang-$(VERSION)\iwmcrt
	copy /y iwmcrt\iwmcrt-tlsarray.rc iconvmlang-$(VERSION)\iwmcrt
	7za a -mx9 -so iconvmlang-$(VERSION).tar iconvmlang-$(VERSION) | 7za a -mx9 -si iconvmlang-$(VERSION).tar.gz
    7za a -mx9 iconvmlang-$(VERSION).zip iconvmlang-$(VERSION)
	rmdir /q /s iconvmlang-$(VERSION)
