########################################################################
#
# Copyright (c) 2009-2017, Secure Endpoints Inc.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# 
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# 

RELDIR=lib\hx509
intcflags=-I$(OBJ)
localcflags=-DASN1_LIB

!include ../../windows/NTMakefile.w32

gen_files_ocsp = $(OBJ)\asn1_ocsp_asn1.x

gen_files_pkcs10 = $(OBJ)\asn1_pkcs10_asn1.x

gen_files_crmf = $(OBJ)\asn1_crmf_asn1.x

libhx509_la_OBJS =			\
	$(OBJ)\ca.obj			\
	$(OBJ)\cert.obj			\
	$(OBJ)\cms.obj			\
	$(OBJ)\collector.obj		\
	$(OBJ)\crypto.obj		\
	$(OBJ)\crypto-ec.obj		\
	$(OBJ)\error.obj		\
	$(OBJ)\env.obj			\
	$(OBJ)\file.obj			\
	$(OBJ)\hx509_err.obj		\
	$(OBJ)\sel.obj			\
	$(OBJ)\sel-gram.obj		\
	$(OBJ)\sel-lex.obj		\
	$(OBJ)\keyset.obj		\
	$(OBJ)\ks_dir.obj		\
	$(OBJ)\ks_file.obj		\
	$(OBJ)\ks_mem.obj		\
	$(OBJ)\ks_null.obj		\
	$(OBJ)\ks_p11.obj		\
	$(OBJ)\ks_p12.obj		\
	$(OBJ)\ks_keychain.obj		\
	$(OBJ)\lock.obj			\
	$(OBJ)\name.obj			\
	$(OBJ)\peer.obj			\
	$(OBJ)\print.obj		\
	$(OBJ)\softp11.obj		\
	$(OBJ)\req.obj			\
	$(OBJ)\revoke.obj		\
	$(gen_files_ocsp:.x=.obj)	\
	$(gen_files_pkcs10:.x=.obj)

$(LIBHX509): $(libhx509_la_OBJS)
	$(LIBCON)

dist_libhx509_la_SOURCES =	\
	$(SRCDIR)\ca.c		\
	$(SRCDIR)\cert.c	\
	$(SRCDIR)\cms.c		\
	$(SRCDIR)\collector.c	\
	$(SRCDIR)\crypto.c	\
	$(SRCDIR)\crypto-ec.c	\
	$(SRCDIR)\doxygen.c	\
	$(SRCDIR)\error.c	\
	$(SRCDIR)\env.c		\
	$(SRCDIR)\file.c	\
	$(SRCDIR)\hx509.h	\
	$(SRCDIR)\hx_locl.h	\
	$(SRCDIR)\sel.c		\
	$(SRCDIR)\sel.h		\
	$(SRCDIR)\sel-gram.y	\
	$(SRCDIR)\sel-lex.l	\
	$(SRCDIR)\keyset.c	\
	$(SRCDIR)\ks_dir.c	\
	$(SRCDIR)\ks_file.c	\
	$(SRCDIR)\ks_mem.c	\
	$(SRCDIR)\ks_null.c	\
	$(SRCDIR)\ks_p11.c	\
	$(SRCDIR)\ks_p12.c	\
	$(SRCDIR)\ks_keychain.c \
	$(SRCDIR)\lock.c	\
	$(SRCDIR)\name.c	\
	$(SRCDIR)\peer.c	\
	$(SRCDIR)\print.c	\
	$(SRCDIR)\softp11.c	\
	$(SRCDIR)\ref\pkcs11.h	\
	$(SRCDIR)\req.c		\
	$(SRCDIR)\revoke.c

asn1_compile=$(BINDIR)\asn1_compile.exe

$(gen_files_ocsp:.x=.c): $$(@R).x

$(gen_files_pkcs10:.x=.c): $$(@R).x

$(gen_files_crmf:.x=.c): $$(@R).x

$(gen_files_ocsp) $(OBJ)\ocsp_asn1.hx: $(asn1_compile) ocsp.asn1
	cd $(OBJ)
	$(asn1_compile) --one-code-file \
		--preserve-binary=OCSPTBSRequest \
		--preserve-binary=OCSPResponseData \
		$(SRCDIR)\ocsp.asn1 ocsp_asn1 \
	|| ( $(RM) -f $(gen_files_ocsp) $(OBJ)\ocsp_asn1.h ; exit /b 1 )
	cd $(SRCDIR)

$(gen_files_pkcs10) $(OBJ)\pkcs10_asn1.hx: $(asn1_compile) pkcs10.asn1
	cd $(OBJ)
	$(asn1_compile) --one-code-file \
		--preserve-binary=CertificationRequestInfo \
		$(SRCDIR)\pkcs10.asn1 pkcs10_asn1 \
	|| ( $(RM) -f $(gen_files_pkcs10) $(OBJ)\pkcs10_asn1.h ; exit /b 1 )
	cd $(SRCDIR)

$(gen_files_crmf) $(OBJ)\crmf_asn1.hx: $(asn1_compile) crmf.asn1
	cd $(OBJ)
	$(asn1_compile) --one-code-file $(SRCDIR)\crmf.asn1 crmf_asn1 \
	|| ( $(RM) -f $(gen_files_crmf) $(OBJ)\crmf_asn1.h ; exit /b 1 )
	cd $(SRCDIR)

INCFILES=			    \
	$(INCDIR)\hx509.h	    \
	$(INCDIR)\hx509-protos.h    \
	$(INCDIR)\hx509-private.h   \
	$(INCDIR)\hx509_err.h	    \
	$(INCDIR)\ocsp_asn1.h	    \
	$(INCDIR)\pkcs10_asn1.h	    \
	$(INCDIR)\crmf_asn1.h	    \
	$(OBJ)\ocsp_asn1-priv.h	    \
	$(OBJ)\pkcs10_asn1-priv.h   \
	$(OBJ)\crmf_asn1-priv.h

hxtool.c: $(OBJ)\hxtool-commands.h

SLC=$(BINDIR)\slc.exe

$(OBJ)\hxtool-commands.c $(OBJ)\hxtool-commands.h: hxtool-commands.in $(SLC)
	cd $(OBJ)
	$(CP) $(SRCDIR)\hxtool-commands.in $(OBJ)\hxtool-commands.in
	$(SLC) hxtool-commands.in
	cd $(SRCDIR)

$(BINDIR)\hxtool.exe: $(OBJ)\tool\hxtool.obj $(OBJ)\tool\hxtool-commands.obj $(LIBHEIMDAL) $(OBJ)\hxtool-version.res
	$(EXECONLINK) $(LIBHEIMDAL) $(LIBROKEN) $(LIBSL) $(LIBVERS) $(LIBCOMERR) $(LIB_openssl_crypto)
	$(EXEPREP)

$(OBJ)\hx509-protos.h:
	cd $(OBJ)
	$(PERL) $(SRCDIR)\..\..\cf\make-proto.pl -R "^(_|^C)" -E HX509_LIB -q -P remove -o hx509-protos.h $(dist_libhx509_la_SOURCES) || $(RM) -f hx509-protos.h
	cd $(SRCDIR)

$(OBJ)\hx509-private.h:
	cd $(OBJ)
	$(PERL) $(SRCDIR)\..\..\cf\make-proto.pl -q -P remove -p hx509-private.h $(dist_libhx509_la_SOURCES) || $(RM) -f hx509-private.h
	cd $(SRCDIR)

$(OBJ)\hx509_err.c $(OBJ)\hx509_err.h: hx509_err.et
	cd $(OBJ)
	$(BINDIR)\compile_et.exe $(SRCDIR)\hx509_err.et
	cd $(SRCDIR)

$(OBJ)\sel-gram.obj: $(OBJ)\sel-gram.c
	$(C2OBJ) -I$(SRCDIR)

$(OBJ)\sel-lex.obj: $(OBJ)\sel-lex.c
	$(C2OBJ) -I$(SRCDIR) -I$(OBJ) -DYY_NO_UNISTD_H

$(OBJ)\sel-gram.c: sel-gram.y
	$(YACC) -o $@ --defines=$(OBJ)\sel-gram.h sel-gram.y

$(OBJ)\sel-lex.c: sel-lex.l
	$(LEX) -P_hx509_sel_yy -o$@ sel-lex.l

all:: $(INCFILES) $(LIBHX509)

prep:: mktooldir

mktooldir:
!  if !exist($(OBJ)\tool)
	$(MKDIR) $(OBJ)\tool
!  endif

all-tools:: $(BINDIR)\hxtool.exe

clean::
	-$(RM) $(BINDIR)\hxtool.*
	-$(RM) $(OBJ)\tool\*.*

{}.c{$(OBJ)\tool}.obj::
	$(C2OBJ_C) /Fd$(OBJ)\tool\ /Fo$(OBJ)\tool\ $(MPOPT) /UASN1_LIB $<

{$(OBJ)}.c{$(OBJ)\tool}.obj::
	$(C2OBJ_C) /Fd$(OBJ)\tool\ /Fo$(OBJ)\tool\ $(MPOPT) /UASN1_LIB $<

