# Onigmo Makefile for mingw32

product_name = onigmo

# Comment out the next line, if you don't want to use LTCG.
USE_LTCG = 1

# Enable the next line, if you want to use a .def file.
#DEFFILE = onig.def


# Get the default ARCH. (i686, x86-64, etc.)
ifndef ARCH
ARCH := $(shell $(CC) -dumpmachine | sed -e 's/-.*//' -e 's/_/-/' -e 's/^mingw32$$/i686/')
endif


CPPFLAGS =
CFLAGS = -O2 -Wall
LDFLAGS =
LOADLIBES =
ARLIB = ar
ARLIB_FLAGS = cr
ARDLL = gcc
ARDLL_FLAGS = -shared $(LINKFLAGS)
LINKFLAGS =

INSTALL = install -c
CP      = cp
RMDIR   = rm -rf
CC = gcc
RC = windres
DEFS = -DHAVE_CONFIG_H -DEXPORT

ifneq ($(ARCH),x86-64)
PYTHON = py -3.4-32
else
PYTHON = py -3
endif

WORKDIR = build_$(ARCH)

ifdef DEFFILE
# Use a .def file to export APIs.
CFLAGS = $(CFLAGS) -DONIG_EXTERN=extern
ARDLL_FLAGS = $(ARDLL_FLAGS) $(DEFFILE)
endif

subdirs =

libbase   = onigmo
libname   = $(WORKDIR)/lib$(libbase).a
dllname   = $(WORKDIR)/$(libbase).dll
dlllib    = $(WORKDIR)/lib$(libbase).dll.a

onigheaders  = onigmo.h regint.h regparse.h regenc.h st.h
posixheaders = onigmoposix.h
headers      = $(posixheaders) $(onigheaders)

onigobjs      = $(WORKDIR)/reggnu.o $(WORKDIR)/regerror.o \
		$(WORKDIR)/regparse.o $(WORKDIR)/regext.o \
		$(WORKDIR)/regcomp.o $(WORKDIR)/regexec.o \
		$(WORKDIR)/regenc.o $(WORKDIR)/regsyntax.o \
		$(WORKDIR)/regtrav.o $(WORKDIR)/regversion.o \
		$(WORKDIR)/st.o
posixobjs     = $(WORKDIR)/regposix.o $(WORKDIR)/regposerr.o
libobjs       = $(onigobjs) $(posixobjs)

jp_objs       = $(WORKDIR)/euc_jp.o $(WORKDIR)/shift_jis.o \
		$(WORKDIR)/windows_31j.o

iso_8859_objs = $(WORKDIR)/iso_8859_1.o  $(WORKDIR)/iso_8859_2.o \
		$(WORKDIR)/iso_8859_3.o  $(WORKDIR)/iso_8859_4.o \
		$(WORKDIR)/iso_8859_5.o  $(WORKDIR)/iso_8859_6.o \
		$(WORKDIR)/iso_8859_7.o  $(WORKDIR)/iso_8859_8.o \
		$(WORKDIR)/iso_8859_9.o  $(WORKDIR)/iso_8859_10.o \
		$(WORKDIR)/iso_8859_11.o $(WORKDIR)/iso_8859_13.o \
		$(WORKDIR)/iso_8859_14.o $(WORKDIR)/iso_8859_15.o \
		$(WORKDIR)/iso_8859_16.o

encobjs       = $(WORKDIR)/ascii.o $(WORKDIR)/utf_8.o \
		$(WORKDIR)/unicode.o \
		$(WORKDIR)/utf_16be.o $(WORKDIR)/utf_16le.o \
		$(WORKDIR)/utf_32be.o $(WORKDIR)/utf_32le.o \
		$(jp_objs) $(iso_8859_objs) \
		$(WORKDIR)/euc_tw.o $(WORKDIR)/euc_kr.o $(WORKDIR)/big5.o \
		$(WORKDIR)/gb18030.o \
		$(WORKDIR)/koi8_r.o $(WORKDIR)/koi8_u.o \
		$(WORKDIR)/windows_1250.o $(WORKDIR)/windows_1251.o \
		$(WORKDIR)/windows_1252.o $(WORKDIR)/windows_1253.o \
		$(WORKDIR)/windows_1254.o $(WORKDIR)/windows_1257.o

resobj        = $(WORKDIR)/onigmo.o

onigsources  = regerror.c regparse.c regext.c regcomp.c regexec.c regenc.c \
	       regsyntax.c regtrav.c regversion.c reggnu.c st.c
posixsources = regposix.c regposerr.c
libsources   = $(posixsources) $(onigsources)

encdir       = enc
distfiles    = README COPYING HISTORY \
		Makefile.in configure.in config.h.in configure \
		$(headers) $(libsources) \
		test.rb testconv.rb
testc        = testc
testcu       = testu
testp        = testp

makeargs = $(MFLAGS) CPPFLAGS='$(CPPFLAGS)' CFLAGS='$(CFLAGS)' CC='$(CC)'

.SUFFIXES:
.SUFFIXES: .o .c .h .ps .dvi .info .texinfo .res .rc

$(WORKDIR)/%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) -Iwin32 -I. -o $@ -c $<

$(WORKDIR)/%.o: $(encdir)/%.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) -Iwin32 -I. -I$(encdir)/unicode -o $@ -c $<

$(WORKDIR)/%.o: win32/%.rc
	$(RC) -O coff -I. $< $@

# targets
default: all

setup:


all: lib dll

lib: $(libname)
dll: $(dllname)

$(libname): $(WORKDIR) $(libobjs) $(encobjs)
	$(ARLIB) $(ARLIB_FLAGS) $@ $(libobjs) $(encobjs)

$(dllname): $(WORKDIR) $(libobjs) $(encobjs) $(resobj) $(DEFFILE)
	$(ARDLL) $(libobjs) $(encobjs) $(resobj) -o $@ $(ARDLL_FLAGS) -Wl,--out-implib,$(dlllib)

$(WORKDIR):
	-mkdir $(WORKDIR)

$(WORKDIR)/regparse.o:  regparse.c $(onigheaders) win32/config.h st.h
$(WORKDIR)/regext.o:    regext.c   $(onigheaders) win32/config.h
$(WORKDIR)/regtrav.o:   regtrav.c  $(onigheaders) win32/config.h
$(WORKDIR)/regcomp.o:   regcomp.c  $(onigheaders) win32/config.h
$(WORKDIR)/regexec.o:   regexec.c  regint.h regenc.h onigmo.h win32/config.h
$(WORKDIR)/reggnu.o:    reggnu.c   regint.h regenc.h onigmo.h win32/config.h onigmognu.h
$(WORKDIR)/regerror.o:  regerror.c regint.h regenc.h onigmo.h win32/config.h
$(WORKDIR)/regenc.o:    regenc.c   regint.h regenc.h onigmo.h win32/config.h
$(WORKDIR)/regsyntax.o: regsyntax.c regint.h regenc.h onigmo.h win32/config.h
$(WORKDIR)/regversion.o: regversion.c onigmo.h win32/config.h
$(WORKDIR)/regposix.o:  regposix.c $(posixheaders) onigmo.h win32/config.h
$(WORKDIR)/regposerr.o: regposerr.c $(posixheaders) win32/config.h
$(WORKDIR)/st.o:        st.c regint.h onigmo.h win32/config.h st.h

$(WORKDIR)/ascii.o:      $(encdir)/ascii.c regenc.h win32/config.h
$(WORKDIR)/unicode.o:    $(encdir)/unicode.c regint.h regenc.h win32/config.h $(encdir)/unicode/casefold.h $(encdir)/unicode/name2ctype.h
$(WORKDIR)/utf_8.o:      $(encdir)/utf_8.c regenc.h win32/config.h
$(WORKDIR)/utf_16be.o:   $(encdir)/utf_16be.c regenc.h win32/config.h
$(WORKDIR)/utf_16le.o:   $(encdir)/utf_16le.c regenc.h win32/config.h
$(WORKDIR)/utf_32be.o:   $(encdir)/utf_32be.c regenc.h win32/config.h
$(WORKDIR)/utf_32le.o:   $(encdir)/utf_32le.c regenc.h win32/config.h
$(WORKDIR)/euc_jp.o:     $(encdir)/euc_jp.c regenc.h win32/config.h $(encdir)/jis/props.h
$(WORKDIR)/euc_tw.o:     $(encdir)/euc_tw.c regenc.h win32/config.h
$(WORKDIR)/euc_kr.o:     $(encdir)/euc_kr.c regenc.h win32/config.h
$(WORKDIR)/shift_jis.o:  $(encdir)/shift_jis.c regenc.h win32/config.h $(encdir)/jis/props.h
$(WORKDIR)/windows_31j.o: $(encdir)/windows_31j.c $(encdir)/shift_jis.c regenc.h win32/config.h $(encdir)/jis/props.h
$(WORKDIR)/iso_8859_1.o:  $(encdir)/iso_8859_1.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_2.o:  $(encdir)/iso_8859_2.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_3.o:  $(encdir)/iso_8859_3.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_4.o:  $(encdir)/iso_8859_4.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_5.o:  $(encdir)/iso_8859_5.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_6.o:  $(encdir)/iso_8859_6.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_7.o:  $(encdir)/iso_8859_7.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_8.o:  $(encdir)/iso_8859_8.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_9.o:  $(encdir)/iso_8859_9.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_10.o: $(encdir)/iso_8859_10.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_11.o: $(encdir)/iso_8859_11.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_13.o: $(encdir)/iso_8859_13.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_14.o: $(encdir)/iso_8859_14.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_15.o: $(encdir)/iso_8859_15.c regenc.h win32/config.h
$(WORKDIR)/iso_8859_16.o: $(encdir)/iso_8859_16.c regenc.h win32/config.h
#$(WORKDIR)/koi8.o:       $(encdir)/koi8.c   regenc.h win32/config.h
$(WORKDIR)/koi8_r.o:     $(encdir)/koi8_r.c regenc.h win32/config.h
$(WORKDIR)/koi8_u.o:     $(encdir)/koi8_u.c regenc.h win32/config.h
$(WORKDIR)/windows_1250.o: $(encdir)/windows_1250.c regenc.h win32/config.h
$(WORKDIR)/windows_1251.o: $(encdir)/windows_1251.c regenc.h win32/config.h
$(WORKDIR)/windows_1252.o: $(encdir)/windows_1252.c regenc.h win32/config.h
$(WORKDIR)/windows_1253.o: $(encdir)/windows_1253.c regenc.h win32/config.h
$(WORKDIR)/windows_1254.o: $(encdir)/windows_1254.c regenc.h win32/config.h
$(WORKDIR)/windows_1257.o: $(encdir)/windows_1257.c regenc.h win32/config.h
$(WORKDIR)/big5.o:       $(encdir)/big5.c   regenc.h win32/config.h
$(WORKDIR)/gb18030.o:    $(encdir)/gb18030.c   regenc.h win32/config.h

$(WORKDIR)/onigmo.o: win32/onigmo.rc onigmo.h


# Tests
test: all ctest utest ptest pytest

# C library test
ctest: $(WORKDIR)/$(testc)
	$(WORKDIR)/$(testc)

# C library test (Unicode)
utest: $(WORKDIR)/$(testcu)
	$(WORKDIR)/$(testcu)

# POSIX C library test
ptest: $(WORKDIR)/$(testp)
	$(WORKDIR)/$(testp)

# Python test
pytest:
	cd $(WORKDIR) && $(PYTHON) ../testpy.py EUC-JP
	cd $(WORKDIR) && $(PYTHON) ../testpy.py SJIS
	cd $(WORKDIR) && $(PYTHON) ../testpy.py UTF-8
	cd $(WORKDIR) && $(PYTHON) ../testpy.py UTF-16LE
	cd $(WORKDIR) && $(PYTHON) ../testpy.py UTF-16BE
	cd $(WORKDIR) && $(PYTHON) ../testpy.py UTF-32LE
	cd $(WORKDIR) && $(PYTHON) ../testpy.py UTF-32BE


$(WORKDIR)/$(testc): win32/$(testc).c $(libname)
	$(CC) $(CFLAGS) -o $(WORKDIR)/$(testc) -DONIG_EXTERN=extern win32/$(testc).c -I. $(libname)

$(WORKDIR)/$(testp): win32/$(testc).c $(dlllib)
	$(CC) $(CFLAGS) -DPOSIX_TEST -o $(WORKDIR)/$(testp) win32/$(testc).c -I. $(dlllib)

#$(testc)u.c: test.rb testconvu.rb
#	ruby -Ke testconvu.rb test.rb > $@

$(WORKDIR)/$(testcu): $(testcu).c $(libname)
	$(CC) $(CFLAGS) -o $(WORKDIR)/$(testcu) -DONIG_EXTERN=extern $(testcu).c -I. $(libname)

$(libbase).def: win32/makedef.py onigmo.h regenc.h onigmognu.h onigmoposix.h
	$(PYTHON) win32/makedef.py > $@

clean:
	-$(RMDIR) $(WORKDIR)


samples: all
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/simple  sample\simple.c  $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/posix   sample\posix.c   $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/names   sample\names.c   $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/listcap sample\listcap.c $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/sql     sample\sql.c     $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/encode  sample\encode.c  $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/syntax  sample\syntax.c  $(dlllib)
	$(CC) $(CFLAGS) -I. -o $(WORKDIR)/crnl    sample\crnl.c    $(dlllib)
