
# Copyright (C) Igor Sysoev
# Copyright (C) NGINX, Inc.

. auto/deps

echo "creating $NJS_MAKEFILE"

mkdir -p $NJS_BUILD_DIR/src
mkdir -p $NJS_BUILD_DIR/test


cat << END > $NJS_MAKEFILE

# This file is auto-generated by configure

NJS_CC = ${CC}
NJS_STATIC_LINK = ${AR} -r -c
NJS_LINK = ${CC} ${NJS_LD_OPT}
NJS_CFLAGS = ${NJS_CFLAGS} ${NJS_CC_OPT} ${CFLAGS}

default: $NJS_DEFAULT_TARGET
END


# The include paths list.

printf "NJS_LIB_INCS =" >> $NJS_MAKEFILE

for njs_inc in src $NJS_BUILD_DIR
do
	printf " -I$njs_inc" >> $NJS_MAKEFILE
done

echo >> $NJS_MAKEFILE
echo >> $NJS_MAKEFILE


# The njs object files list.

echo "NJS_LIB_OBJS = \\" >> $NJS_MAKEFILE

for njs_src in $NJS_LIB_SRCS
do
    fname=$(basename $njs_src)
    njs_obj="src/${fname%.c}.o"
    echo "    $NJS_BUILD_DIR/$njs_obj \\" >> $NJS_MAKEFILE
done

echo >> $NJS_MAKEFILE

# The njs static library.

cat << END >> $NJS_MAKEFILE

libnjs: $NJS_BUILD_DIR/libnjs.a

$NJS_BUILD_DIR/libnjs.a:   \\
	$NJS_BUILD_DIR/njs_auto_config.h \\
	\$(NJS_LIB_OBJS)

	\$(NJS_STATIC_LINK) $NJS_BUILD_DIR/libnjs.a \\
		\$(NJS_LIB_OBJS)

END


# object files.

for njs_src in $NJS_LIB_SRCS
do
    fname=$(basename $njs_src)
    njs_obj="src/${fname%.c}.o"
    njs_dep="src/${fname%.c}.dep"
    njs_dep_flags=`njs_gen_dep_flags $njs_dep $njs_obj`
    njs_dep_post=`njs_gen_dep_post $njs_dep $njs_obj`
    cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/$njs_obj: $njs_src
	\$(NJS_CC) -c \$(NJS_CFLAGS) $NJS_LIB_AUX_CFLAGS \\
		\$(NJS_LIB_INCS) -Injs \\
		-o $NJS_BUILD_DIR/$njs_obj \\
		$njs_dep_flags \\
		$njs_src
		$njs_dep_post

-include $NJS_BUILD_DIR/$njs_dep

END

done

# njs cli.

if [ $NJS_HAVE_READLINE = YES ]; then

cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/njs: \\
	$NJS_BUILD_DIR/libnjs.a \\
	src/njs_shell.c
	\$(NJS_LINK) -o $NJS_BUILD_DIR/njs \$(NJS_CFLAGS) \\
		$NJS_LIB_AUX_CFLAGS \$(NJS_LIB_INCS) -Injs \\
		src/njs_shell.c \\
		$NJS_BUILD_DIR/libnjs.a \\
		-lm $NJS_LIBS $NJS_LIB_AUX_LIBS $NJS_READLINE_LIB

END

else

cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/njs:
	@echo
	@echo " error: to make njs CLI \"readline\" library is required."
	@echo
	@exit 1

END

fi


# njs fuzzer.

cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/njs_process_script_fuzzer.o: \\
	src/njs_shell.c
	\$(NJS_CC) -c \$(CFLAGS) $NJS_LIB_AUX_CFLAGS \\
		\$(NJS_LIB_INCS) -Injs \\
		-DNJS_FUZZER_TARGET \\
		-o $NJS_BUILD_DIR/njs_process_script_fuzzer.o \\
		src/njs_shell.c

$NJS_BUILD_DIR/njs_process_script_fuzzer: \\
	$NJS_BUILD_DIR/libnjs.a \\
	$NJS_BUILD_DIR/njs_process_script_fuzzer.o
	\$(CXX) \$(CXXFLAGS) -o $NJS_BUILD_DIR/njs_process_script_fuzzer \\
		$NJS_LIB_AUX_CFLAGS \$(NJS_LIB_INCS) -Injs \\
		\$(LIB_FUZZING_ENGINE) \\
		$NJS_BUILD_DIR/njs_process_script_fuzzer.o \\
		$NJS_BUILD_DIR/libnjs.a \\
		-lm $NJS_LIBS $NJS_LIB_AUX_LIBS

END

# lib tests.

for njs_src in $NJS_LIB_TEST_SRCS
do
    fname=$(basename $njs_src)
    njs_dep="test/${fname%.c}.dep"
    njs_bin="${fname%.c}"
    njs_dep_flags=`njs_gen_dep_flags $njs_dep $fname`
    njs_dep_post=`njs_gen_dep_post $njs_dep $fname`
    cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/$njs_bin: $njs_src \\
	$NJS_BUILD_DIR/libnjs.a
	\$(NJS_CC) -o $NJS_BUILD_DIR/$njs_bin \$(NJS_CFLAGS) \\
		\$(NJS_LIB_INCS) $njs_dep_flags \\
		$njs_src $NJS_BUILD_DIR/libnjs.a \\
		$njs_dep_post -lm

-include $NJS_BUILD_DIR/$njs_dep

END

done


# njs tests.

njs_src="src/test/njs_externals_test.c"
fname=$(basename $njs_src)
njs_externals_obj="test/${fname%.c}.o"
njs_dep="test/${fname%.c}.dep"
njs_dep_flags=`njs_gen_dep_flags $njs_dep $njs_externals_obj`
njs_dep_post=`njs_gen_dep_post $njs_dep $njs_externals_obj`

cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/$njs_externals_obj: $njs_src
	\$(NJS_CC) -c \$(NJS_CFLAGS) $NJS_LIB_AUX_CFLAGS \\
		\$(NJS_LIB_INCS) -Injs \\
		-o $NJS_BUILD_DIR/$njs_externals_obj \\
		$njs_dep_flags \\
		$njs_src
		$njs_dep_post

-include $NJS_BUILD_DIR/$njs_dep

END

for njs_src in $NJS_TEST_SRCS
do
    fname=$(basename $njs_src)
    njs_dep="test/${fname%.c}.dep"
    njs_bin="${fname%.c}"
    njs_dep_flags=`njs_gen_dep_flags $njs_dep $fname`
    njs_dep_post=`njs_gen_dep_post $njs_dep $fname`
    cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/$njs_bin: $njs_src \\
	$NJS_BUILD_DIR/libnjs.a \\
	$NJS_BUILD_DIR/$njs_externals_obj
	\$(NJS_CC) -o $NJS_BUILD_DIR/$njs_bin \$(NJS_CFLAGS) \\
		$NJS_LIB_AUX_CFLAGS \$(NJS_LIB_INCS) \\
		$njs_dep_flags \\
		$NJS_BUILD_DIR/$njs_externals_obj \\
		-Injs $njs_src $NJS_BUILD_DIR/libnjs.a \\
		-lm $NJS_LIBS $NJS_LIB_AUX_LIBS
		$njs_dep_post

-include $NJS_BUILD_DIR/$njs_dep

END

done

# main targets.

cat << END >> $NJS_MAKEFILE

$NJS_BUILD_DIR/njs_auto_config.h:
	@echo
	@echo " Please run ./configure before make"
	@echo
	@exit 1

all: $NJS_BUILD_DIR/njs_auto_config.h \\
    $NJS_DEFAULT_TARGET test lib_test benchmark

njs: $NJS_BUILD_DIR/njs_auto_config.h $NJS_BUILD_DIR/njs
njs_fuzzer: $NJS_BUILD_DIR/njs_auto_config.h \\
	$NJS_BUILD_DIR/njs_process_script_fuzzer

lib_test: $NJS_BUILD_DIR/njs_auto_config.h \\
	$NJS_BUILD_DIR/random_unit_test \\
	$NJS_BUILD_DIR/rbtree_unit_test \\
	$NJS_BUILD_DIR/lvlhsh_unit_test \\
	$NJS_BUILD_DIR/utf8_unit_test

	$NJS_BUILD_DIR/random_unit_test
	$NJS_BUILD_DIR/rbtree_unit_test
	$NJS_BUILD_DIR/lvlhsh_unit_test
	$NJS_BUILD_DIR/utf8_unit_test

unit_test: $NJS_BUILD_DIR/njs_auto_config.h \\
	$NJS_BUILD_DIR/njs_unit_test \\
	$NJS_BUILD_DIR/njs_interactive_test

	$NJS_BUILD_DIR/njs_unit_test
	$NJS_BUILD_DIR/njs_interactive_test

test: expect_test unit_test

benchmark: $NJS_BUILD_DIR/njs_auto_config.h \\
	$NJS_BUILD_DIR/njs_benchmark

	$NJS_BUILD_DIR/njs_benchmark

dist:
	NJS_VER=`grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/'`; \\
	rm -rf njs-\$\${NJS_VER} \\
	&& hg archive njs-\$\${NJS_VER}.tar.gz \\
		-p njs-\$\${NJS_VER} \\
		-X ".hg*" \\
	&& echo njs-\$\${NJS_VER}.tar.gz done
END


# Makefile.

cat << END > Makefile

# This file is auto-generated by configure

include $NJS_MAKEFILE

.PHONY: clean
clean:
	rm -rf $NJS_BUILD_DIR Makefile

END
