CC = @CC@
CFLAGS_OPT = -O2 -Wall -fPIC -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wuninitialized -fstack-protector-all -fcommon 
CFLAGS_OPT += ${CPPFLAGS} @RPC_CPPFLAGS@
LD_FLAGS = -lpthread @c_ssl_package@ @RPC_LDFLAGS@ @LDFLAGS@
mem_prot_opt_caml=-ccopt -Wl,-z,relro,-z,now -ccopt -fstack-protector
mem_prot_opt=-Wl,-z,relro,-z,now

c_include_dirs = -I@srcdir@ -I$(bindings_dir) -I@OCAMLLIB@
include_dirs = -I $(build_bindings_dir) -I $(build_rpc_dir)
caml_link_flags = -cclib -lcamlidl -cclib -L@OCAMLLIB@
bindings_dir = @top_srcdir@/src/bindings-pkcs11
rpc_dir = @top_srcdir@/src/rpc-pkcs11
build_bindings_dir = ../bindings-pkcs11
build_rpc_dir = ../rpc-pkcs11
libdir = @libdir@
prefix = ${DESTDIR}@prefix@


CLIENTLIBS = @lib_names@

ifeq ($(CUSTOM_SONAME),"")
        CUSTOM_SONAME=libp11client.so.0
endif

camlrpccompileclient = ocamlfind ocamlopt @ocaml_options@ -verbose -pp "camlp4o pa_macro.cmo @caml_client_ssl_define@ -D@socket_type@ -DSOCKET_PATH=\\\"@socket_path@\\\" @caml_client_ssl_files@ @caml_client_ssl_ca_file@ @caml_client_ssl_cert_file@ @caml_client_ssl_privkey_file@ @caml_client_ssl_server@" -package "str,rpc" @caml_client_ssl_package@ $(include_dirs) -o client -c @srcdir@/client.ml
camlrpccompilestubs = cp @srcdir@/modwrap.c modwrap_$(1).c && $(CC) $(2) -D@socket_type@ -DCAMLRPC -DLIBNAME=$(1) @libname_file@ -c modwrap_$(1).c @srcdir@/modwrap_camlrpc.c $(bindings_dir)/pkcs11_stubs.c $(c_include_dirs) $(CFLAGS_OPT) && rm modwrap_$(1).c
camlrpccompilelib = ocamlfind ocamlopt @ocaml_options@ -verbose $(2) $(mem_prot_opt_caml) -package "str,rpc" @caml_client_ssl_package@ -linkpkg -output-obj -o libp11client$(1).so pkcs11_stubs.o $(build_bindings_dir)/pkcs11_functions.o  modwrap_$(1).o modwrap_camlrpc.o $(build_bindings_dir)/pkcs11.cmx $(build_rpc_dir)/pkcs11_rpclib.cmxa client.cmx $(caml_link_flags)

crpccompilestubs = cp @srcdir@/modwrap.c modwrap_$(1).c && $(CC) $(2) @rpc_mt_define@ @c_ssl_define@ @c_gnutls_define@ -D@socket_type@ -DSOCKET_PATH=@socket_path@ -DLIBNAME=$(1) @libname_file@ @c_client_ssl_files@ @c_client_ssl_ca_file@ @c_client_ssl_cert_file@ @c_client_ssl_privkey_file@ @c_client_ssl_server@ -DCRPC -c @srcdir@/pkcs11_rpc_xdr.c @srcdir@/pkcs11_rpc_clnt.c modwrap_$(1).c @srcdir@/modwrap_crpc.c @srcdir@/modwrap_crpc_ssl.c $(c_include_dirs) $(CFLAGS_OPT) && rm modwrap_$(1).c
crpccompilelib = $(CC) $(2) $(mem_prot_opt) -shared -Wl,-soname,$(CUSTOM_SONAME) -fPIC -o libp11client$(1).so pkcs11_rpc_xdr.o pkcs11_rpc_clnt.o modwrap_$(1).o modwrap_crpc.o modwrap_crpc_ssl.o $(LD_FLAGS)

all :	@c_rpc_gen@ @linux_c_rpc_patch@ @client_to_compile@

client:
	$(call camlrpccompileclient)

crpc:
	$(foreach lib,$(CLIENTLIBS),$(call crpccompilestubs,$(lib));)
	$(foreach lib,$(CLIENTLIBS),$(call crpccompilelib,$(lib));)

camlrpc: client
	@$(foreach lib,$(CLIENTLIBS),$(call camlrpccompilestubs,$(lib));)
	@$(foreach lib,$(CLIENTLIBS),$(call camlrpccompilelib,$(lib));)

crpc_debug:
	@$(foreach lib,$(CLIENTLIBS),$(call crpccompilestubs,$(lib),-DDEBUG -g);)
	@$(foreach lib,$(CLIENTLIBS),$(call crpccompilelib,$(lib));)

camlrpc_debug: client
	$(foreach lib,$(CLIENTLIBS),$(call camlrpccompilestubs,$(lib),-DDEBUG -g);)
	$(foreach lib,$(CLIENTLIBS),$(call camlrpccompilelib,$(lib),-ccopt -DDEBUG -ccopt -g);)

rpc:
	cp $(rpc_dir)/pkcs11_rpc.x ./
	#Generate header
	rpcgen -h -N -M pkcs11_rpc.x > @srcdir@/pkcs11_rpc.h
	#Generate xdr helpers
	rpcgen -c -N -M pkcs11_rpc.x > @srcdir@/pkcs11_rpc_xdr.c
	#Generate client stubs
	rpcgen -l -N -M pkcs11_rpc.x > @srcdir@/pkcs11_rpc_clnt.c
	@rm pkcs11_rpc.x

linux_c_rpc_patch:
	#Patch generated pkcs_rpc_xrc.c to remove useless buf (Linux specific)
	spatch --no-show-diff --sp-file @srcdir@/pkcs11_rpc_xdr.cocci @srcdir@/pkcs11_rpc_xdr.c --in-place

debug:  @c_rpc_gen@ @linux_c_rpc_patch@ @client_to_compile_debug@

install_lib = echo "Installing libp11client$(1).so to $(DESTDIR)$(libdir)/libp11client$(1).so" && install -D libp11client$(1).so $(DESTDIR)$(libdir)/libp11client$(1).so
uninstall_lib = echo "Uninstalling $(DESTDIR)$(libdir)/libp11client$(1).so" && rm $(DESTDIR)$(libdir)/libp11client$(1).so

install:
	@$(foreach lib,$(CLIENTLIBS),$(call install_lib,$(lib));)

uninstall:
	@$(foreach lib,$(CLIENTLIBS),$(call uninstall_lib,$(lib));)

clean_rpc:
	# Remove rpc generated files
	@rm -f @c_rpc_clean@

clean:
	@rm -f *.cmi *.cmx *.o *.cmo *.cmxa *.cma *.so *.a *~
