##
##  Makefile -- Build procedure for ngx_mruby for nginx module
##	  MATSUMOTO, Ryosuke
##

#
# build configuration
#
NUM_THREADS?= 1
MAKEFLAGS=-j $(NUM_THREADS)
RAKE=rake -j $(NUM_THREADS)

NGX_MRUBY_ROOT=$(shell pwd)
NGX_SRC_ROOT=@NGX_SRC_ROOT@
BUILD_DIR=@BUILD_DIR@

ifeq ($(wildcard $(NGX_SRC_ROOT)/auto/configure),)
  NGX_CONFIGURE = ./configure
else
  # development branch does not have configure script in top of directory.
  NGX_CONFIGURE = ./auto/configure
endif

MRUBY_ROOT=@MRUBY_ROOT@
NDK_ROOT=@NDK_ROOT@

# libmruby.flags.mak contains MRUBY_* variables. It is created while building mruby.
MRUBY_MAK_FILE := $(MRUBY_ROOT)/build/host/lib/libmruby.flags.mak
ifneq ($(wildcard $(MRUBY_MAK_FILE)),) 
include $(MRUBY_MAK_FILE)
endif
MRUBY_LIBDIR=@MRUBY_LIBDIR@ # comes from configure
MRUBY_BUILD_DEPEND_TARGETS=@MRUBY_BUILD_DEPEND_TARGETS@
NGX_MRUBY_CFLAGS+=@NGX_MRUBY_CFLAGS@ # Use it in build_config.rb to build mruby

NGX_OPENSSL_SRC=@NGX_OPENSSL_SRC@
NGX_OPENSSL_OPT=@NGX_OPENSSL_OPT@
NGX_OPENSSL_INSTALL_DIR=@NGX_OPENSSL_INSTALL_DIR@
NGX_CONFIG_OPT=@NGX_CONFIG_OPT@
NGX_CFLAGS=@NGX_CFLAGS@
NGX_LDFLAGS=@NGX_LDFLAGS@
NGX_LIBS=@NGX_LIBS@
NGX_CONFIG_ADD_MODULE_OPT_NAME=@NGX_CONFIG_ADD_MODULE_OPT_NAME@
NGX_MAKEFILE_DEPENDS_TARGET=@NGX_MAKEFILE_DEPENDS_TARGET@
BUILD_DYNAMIC_MODULE=@BUILD_DYNAMIC_MODULE@ # DON'T USE THIS. ONLY FOR COMPAT TARGETS

#   flags
CFLAGS = $(NGX_CFLAGS) $(MRUBY_CFLAGS) -I/usr/local/include
LDFLAGS = $(NGX_LDFLAGS) $(MRUBY_LDFLAGS) -L/usr/local/lib
LIBS = $(NGX_LIBS) $(MRUBY_LIBS) $(MRUBY_LDFLAGS_BEFORE_LIBS) -L$(MRUBY_LIBDIR) -lmruby

#
# targets
#
all: build_ngx_mruby

install:
	cd $(NGX_SRC_ROOT) && $(MAKE) install

clean:
	-rm -rf mrbgems_config mrbgems_config_dynamic

clobber: clean_mruby clean
	-rm -rf tmp autom4te.cache config config.status config.log $(BUILD_DIR) .openssl_build_done .openssl_configure_done

#
# mruby
#
build_mruby: $(MRUBY_BUILD_DEPEND_TARGETS)
	cd $(MRUBY_ROOT) && $(RAKE) MRUBY_CONFIG=$(NGX_MRUBY_ROOT)/build_config.rb NGX_MRUBY_CFLAGS="$(NGX_MRUBY_CFLAGS)"

clean_mruby:
	cd $(MRUBY_ROOT) && $(RAKE) deep_clean && rm -rf build

$(MRUBY_MAK_FILE): build_mruby

#
#   nginx
#
build_ngx_mruby: build_mruby $(NGX_SRC_ROOT)/objs/Makefile
	cd $(NGX_SRC_ROOT) && $(MAKE)

$(NGX_SRC_ROOT)/objs/Makefile: $(NGX_MAKEFILE_DEPENDS_TARGET)
	cd $(NGX_SRC_ROOT) \
	&& $(NGX_CONFIGURE) $(NGX_CONFIG_ADD_MODULE_OPT_NAME)=$(NGX_MRUBY_ROOT) --add-module=$(NDK_ROOT) $(NGX_CONFIG_OPT)

# libmruby.flags.mak is generated or updated, so invoke make command to reevaluate MRUBY_* variables in it.
mrbgems_config: $(MRUBY_MAK_FILE)
	$(MAKE) generate_gems_config

mrbgems_config_dynamic: $(MRUBY_MAK_FILE)
	$(MAKE) generate_gems_config_dynamic

generate_gems_config:
	@echo CORE_LIBS=\"\$$CORE_LIBS $(LDFLAGS) $(LIBS)\" > ./mrbgems_config
	@echo CORE_INCS=\"\$$CORE_INCS $(CFLAGS)\" >> ./mrbgems_config
	@echo LINK_DEPS=\"\$$LINK_DEPS $(MRUBY_LIBMRUBY_PATH)\" >> ./mrbgems_config

generate_gems_config_dynamic:
	@echo ngx_module_libs=\"\$(LDFLAGS) \$(LIBS)\" > ./mrbgems_config_dynamic
	@echo CORE_LIBS=\"\$$CORE_LIBS \$(LDFLAGS) \$(LIBS)\" >> ./mrbgems_config_dynamic

# for backward compatibility
make_ngx_mruby:
	cd $(NGX_SRC_ROOT) && $(MAKE)
	@echo "Warning: make_ngx_mruby target will be removed. Use build_ngx_mruby instead."

ngx_mruby: 
ifdef BUILD_DYNAMIC_MODULE
	@echo "Error: You must rerun configure without --enable-dynamic" && false
else
	$(MAKE) build_ngx_mruby
	@echo "Warning: ngx_mruby target will be removed. Use build_ngx_mruby without BUILD_DYNAMIC_MODULE instead."
endif

ngx_mruby_dynamic:
ifdef BUILD_DYNAMIC_MODULE
	$(MAKE) build_ngx_mruby
	@echo "Warning: ngx_mruby_dynamic target will be removed. Use build_ngx_mruby with BUILD_DYNAMIC_MODULE instead."
else
	@echo "Error: You must rerun configure with --enable-dynamic" && false
endif

#
# openssl
#
openssl: .openssl_build_done

.openssl_build_done: .openssl_configure_done
	(cd $(NGX_OPENSSL_SRC) \
	&& $(MAKE) build_all install_sw) \
	&& cp /dev/null .openssl_build_done

.openssl_configure_done:
	(cd $(NGX_OPENSSL_SRC) \
	&& if [ -f Makefile ]; then $(MAKE) clean; fi \
	&& ./config --prefix=$(NGX_OPENSSL_INSTALL_DIR) --shared zlib -fPIC enable-tlsext $(NGX_OPENSSL_OPT) \
	&& $(MAKE) depend) \
	&& cp /dev/null .openssl_configure_done

format:
	./apply-clang-format

.PHONY: install
