##
##  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@

# Used in build_config.rb to build mruby
NGX_MRUBY_CFLAGS+=@NGX_MRUBY_CFLAGS@
NGX_MRUBY_LDFLAGS+=@NGX_MRUBY_LDFLAGS@

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@

OPENSSL_VERSION=@OPENSSL_VERSION@
OPENSSL_MAKE_TARGET=@OPENSSL_MAKE_TARGET@
OPENSSL_CONFIG_ENABLE_TLSEXT=@OPENSSL_CONFIG_ENABLE_TLSEXT@

#   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_version

#
# mruby
#
build_mruby: $(MRUBY_BUILD_DEPEND_TARGETS)
	# Only for ngx_mruby developers getting source code from git.
	@if [ -f .mruby_version ]; then \
		built_version=`cat .mruby_version`; \
		current_version=`git log -1 --format="%H" mruby`; \
		if [ $$built_version != $$current_version ]; then \
			echo $$current_version > .mruby_version; \
			(cd $(MRUBY_ROOT) && rm -rf build); \
		fi; \
	elif [ -d .git ]; then \
		git log -1 --format="%H" mruby > .mruby_version; \
	fi
	(cd $(MRUBY_ROOT) && $(RAKE) MRUBY_CONFIG=$(NGX_MRUBY_ROOT)/build_config.rb NGX_MRUBY_CFLAGS="$(NGX_MRUBY_CFLAGS)" NGX_MRUBY_LDFLAGS="$(NGX_MRUBY_LDFLAGS)")

clean_mruby:
	rm -f .mruby_version	
	(cd $(MRUBY_ROOT) && $(RAKE) MRUBY_CONFIG=$(NGX_MRUBY_ROOT)/build_config.rb 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 `echo $(CFLAGS) | sed -e 's/-[^I][^ ]*//g'`\" >> ./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

#
# openssl
#
openssl: .openssl_build_done

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

.openssl_configure_done:
	(cd $(NGX_OPENSSL_SRC) \
	&& if [ -f Makefile ]; then $(MAKE) clean; fi \
	&& echo "Building OpenSSL version: $(OPENSSL_VERSION)" \
	&& ./config --prefix=$(NGX_OPENSSL_INSTALL_DIR) --shared zlib -fPIC $(OPENSSL_CONFIG_ENABLE_TLSEXT) $(NGX_OPENSSL_OPT) \
	&& $(MAKE) depend) \
	&& cp /dev/null .openssl_configure_done

format:
	./apply-clang-format

.PHONY: install
