TOP_DIR=../..

PKGNAME = netcgi2-apache
# Keep the name in sync with the one on handler.c :
BUILD_EXTRA = mod_netcgi_apache.so

INCLUDES += $(INC_NETSTRING) $(INC_NETSYS) $(INC_NETCGI2)
REQUIRES += findlib

OBJECTS = netcgi_apache.cmo netcgi_modtpl.cmo
DOBJECTS = netcgi_apache.mli netcgi_modtpl.mli

ALL_EXTRA = mod_netcgi_apache.so
INSTALL_EXTRA = mod_netcgi_apache.so 500netcgi_apache.info

include $(TOP_DIR)/Makefile.rules

ifeq ($(APACHE_MAJOR),2)
APACHE_LDFLAGS_SHLIB += -lapr-1 -laprutil-1
endif

# mod_netcgi_apache.so: apache.lo handler.lo wrappers.lo netcgi_apache_mod.cmo
# 	$(OCAMLC) -verbose -custom -linkall -o $@ \
# 	  -cc "$(APXS) -c $(filter %.lo,$^)" \
# 	  dynlink.cma unix.cma $(filter-out %.lo,$^) \
# 	  -cclib '$(APACHE_CFLAGS) $(APACHE_LDFLAGS_SHLIB) $(APACHE_OCAMLLIBS)'
# $(patsubst %.o,.libs/%.o,$^) \

# %.o: %.c
# 	$(APACHE_CC) $(APACHE_CFLAGS) -I $(APACHE_OCAMLLIBDIR) -c $<

# -Wl,-bI:$(APACHE_LIBDIR)/httpd.exp
# mod_netcgi_apache.so: netcgi_apache_mod.cmo apache.o handler.o wrappers.o
# 	$(OCAMLC) -o $@ -verbose -linkall -custom \
# 	  -ccopt -Wl,--warn-unresolved-symbols \
# 	  dynlink.cma unix.cma $(patsubst %.o, ./.libs/%.o, $^) \
# 	  -cclib "$(APACHE_CFLAGS) $(APACHE_LDFLAGS_SHLIB) $(APACHE_OCAMLLIBS)"

MOD_OBJECTS := wrappers.o handler.o apache.o netcgi_apache_mod.o

# must be after the `include' for APACHE_MAJOR to be defined
ifeq ($(APACHE_MAJOR),2)
MOD_OBJECTS := $(patsubst %.o,%.lo,$(MOD_OBJECTS))
else
MOD_OBJECTS := $(MOD_OBJECTS)
endif

### Embed Caml code into the C code.
### Requires `caml_startup' instead of `caml_main' in handler.c
### Only works with OCaml >= 3.11 on plafforms where PIC differs from non-PIC
### See http://caml.inria.fr/mantis/view.php?id=3866
mod_netcgi_apache.so: $(MOD_OBJECTS)
	$(APXS) -c -o $@ $^ -L$(APACHE_OCAMLLIBDIR) -Wl,-R,$(APACHE_OCAMLLIBDIR) $(APACHE_OCAMLLIBS)
	test -f .libs/$@ && cp .libs/$@ .

netcgi_apache_mod.lo: netcgi_apache_mod.o
	echo "$@ generated by the rule for $^"
netcgi_apache_mod.o: netcgi_apache_mod.ml
	$(OCAMLC) -verbose -output-obj -linkall -o $@ -cc $(APXS) \
	  dynlink.cma unix.cma findlib.cma $^

%.o: %.c
	$(APXS) -I$(APACHE_OCAMLLIBDIR) -c $<
%.lo: %.c
	$(APXS) -I$(APACHE_OCAMLLIBDIR) -c $<

include depend
