#/
# @license Apache-2.0
#
# Copyright (c) 2018 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/

# VARIABLES #

ifndef VERBOSE
	QUIET := @
else
	QUIET :=
endif

# Determine the OS ([1][1], [2][2]).
#
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
# [2]: http://stackoverflow.com/a/27776822/2225624
OS ?= $(shell uname)
ifneq (, $(findstring MINGW,$(OS)))
	OS := WINNT
else
ifneq (, $(findstring MSYS,$(OS)))
	OS := WINNT
else
ifneq (, $(findstring CYGWIN,$(OS)))
	OS := WINNT
else
ifneq (, $(findstring Windows_NT,$(OS)))
	OS := WINNT
endif
endif
endif
endif

# Determine the absolute path of the Makefile (see http://blog.jgc.org/2007/01/what-makefile-am-i-in.html):
this_dir := $(dir $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))

# Remove the trailing slash:
this_dir := $(patsubst %/,%,$(this_dir))

# Define the command for Node.js:
NODE ?= node

# Define the Node.js directory path.
#
# ## Notes
#
# 1. The directory is expected to contain include header files.
# 2. This only works on Mac and Linux.
NODE_DIR ?= $(shell $(NODE) -e 'console.log( path.resolve( process.execPath, "..", ".." ) );')

# Determine path to NAN header files:
INCLUDE_NAN ?= $(shell node -e 'require( "nan" );')

# Define the program used for compiling C source files:
ifdef C_COMPILER
	CC := $(C_COMPILER)
else
	CC := gcc
endif

# Define the command-line options when compiling C files:
CFLAGS ?= \
	-std=c99 \
	-O3 \
	-Wall \
	-pedantic

# Define the program used for compiling C++ source files:
ifdef CXX_COMPILER
	CXX := $(CXX_COMPILER)
else
	CXX := g++
endif

# Define the command-line options when compiling C++ files:
CXXFLAGS ?= \
	-std=c++11 \
	-O3 \
	-Wall \
	-pedantic

# Define the program used for compiling WebAssembly:
ifdef EMCC_COMPILER
	EMCC := $(EMCC_COMPILER)
else
	EMCC := emcc
endif

# Define the command-line options when compiling asm.js and WebAssembly:
EMCCFLAGS ?= $(CFLAGS)

# Shared `emcc` flags:
EMCC_SHARED_FLAGS := \
	--minify 0 \
	-s EXPORTED_FUNCTIONS="['_stdlib_strided_dabs','_malloc','_free']" \
	-s EXPORTED_RUNTIME_METHODS="['cwrap','setValue','getValue']" \
	-s STRICT=1 \
	-s NO_FILESYSTEM=1 \
	-s ERROR_ON_UNDEFINED_SYMBOLS=1 \
	-s ERROR_ON_MISSING_LIBRARIES=1  \
	-s NODEJS_CATCH_EXIT=0 \
	-s TOTAL_STACK=1024 \
	-s TOTAL_MEMORY=16777216 \
	-s ALLOW_MEMORY_GROWTH=0 \
	-s ABORTING_MALLOC=1

EMCC_ASM_FLAGS := $(EMCC_SHARED_FLAGS) \
	-s MODULARIZE=1 \
	-s ASM_JS=1 \
	--memory-init-file 0

EMCC_WASM_FLAGS := $(EMCC_SHARED_FLAGS) \
	-s MODULARIZE=1 \
	-s WASM=1 \
	-s BINARYEN_ASYNC_COMPILATION=0 \
	-s "BINARYEN_METHOD='native-wasm'"

# Define the program for generating WebAssembly text files:
WASM2WAT ?= wasm2wat

# Determine whether to generate position independent code ([1][1], [2][2]).
#
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
ifeq ($(OS), WINNT)
	fPIC ?=
else
	fPIC ?= -fPIC
endif

# Define the program for linking compiled files:
ifdef LINKER
	LD := $(LINKER)
else
	LD := g++
endif

# Define the command-line options when linking compiled files:
ifeq ($(OS), Darwin)
	LDFLAGS ?= \
		-undefined dynamic_lookup \
		-Wl,-no_pie \
		-Wl,-search_paths_first
else
	LDFLAGS ?=
endif

# List of external includes:
INCLUDE ?=

# List of external source files:
SOURCE_FILES ?=

# List of external object files:
OBJECT_FILES ?=

# List of libraries (e.g., `-lopenblas -lpthread`):
LIBRARIES ?=

# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
LIBPATH ?=

# List of C targets:
c_objects := dabs.o

# List of C++ add-on targets:
addon_objects := addon.o

# List of asm.js targets:
asm_objects := dabs.asm.js

# List of WebAssembly targets:
wasm_objects := dabs.wasm

# List of WebAssembly text file targets:
wat_objects := dabs.wat

# File containing pre-glue code:
pre_js := pre.js


# RULES #

#/
# Compiles source files.
#
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
# @param {string} [CXX_COMPILER] - C++ compiler (e.g., `gcc`)
# @param {string} [LINKER] - program for linking compiled files (e.g., `g++`)
# @param {string} [EMCC_COMPILER] - Emscripten compiler (e.g., `emcc`)
# @param {string} [CFLAGS] - C compiler options
# @param {string} [CXXFLAGS] - C++ compiler options
# @param {string} [LDFLAGS] - linker options
# @param {string} [EMCCFLAGS] - Emscripten compiler options
# @param {string} [EMCC_ASM_FLAGS] - Emscripten compiler options when compiling asm.js
# @param {string} [EMCC_WASM_FLAGS] - Emscripten compiler options when compiling WebAssembly
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
# @param {string} [SOURCE_FILES] - list of external source files
# @param {string} [OBJECT_FILES] - list of external object files
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
#
# @example
# make
#
# @example
# make all
#/
all: $(c_objects) $(addon_objects) addon.node wasm

.PHONY: all

#/
# Compiles C source files.
#
# @private
# @param {string} CC - C compiler (e.g., `gcc`)
# @param {string} CFLAGS - C compiler options
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
#/
$(c_objects): %.o: %.c
	$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -I ../include -c -o $@ $<

#/
# Compiles Node.js native add-ons.
#
# @private
# @param {string} CXX - C++ compiler (e.g., `g++`)
# @param {string} CXXFLAGS - C++ compiler options
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/`)
# @param {string} INCLUDE_NAN - directory containing NAN header files
# @param {string} NODE_DIR - Node.js directory path
#/
$(addon_objects): %.o: %.cpp
	$(QUIET) $(CXX) \
		$(CXXFLAGS) \
		$(fPIC) \
		-I "$(NODE_DIR)/include/node" \
		-I "$(NODE_DIR)/src" \
		-I "$(NODE_DIR)/deps/uv/include" \
		-I "$(NODE_DIR)/deps/v8/include" \
		-I "$(INCLUDE_NAN)" \
		$(INCLUDE) \
		-I ../include \
		-c \
		-o $@ \
		$<

#/
# Compiles asm.js.
#
# @private
# @param {string} EMCC - Emscripten compiler (e.g., `emcc`)
# @param {string} EMCCFLAGS - Emscripten compiler options
# @param {string} EMCC_ASM_FLAGS - Emscripten compiler options when compiling asm.js
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/`)
# @param {string} SOURCE_FILES - list of source files
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
#/
$(asm_objects): %.asm.js: %.c
	$(QUIET) $(EMCC) \
		$(EMCCFLAGS) \
		$(EMCC_ASM_FLAGS) \
		$(fPIC) \
		--pre-js $(pre_js) \
		$(INCLUDE) \
		-I ../include \
		-o $@ \
		$(SOURCE_FILES) \
		$< \
		$(LIBPATH) \
		$(LIBRARIES)
	$(QUIET) sed -i "" '1 s/^/\/\* eslint-disable \*\//' $@

#/
# Compiles WebAssembly.
#
# @private
# @param {string} EMCC - Emscripten compiler (e.g., `emcc`)
# @param {string} EMCCFLAGS - Emscripten compiler options
# @param {string} EMCC_WASM_FLAGS - Emscripten compiler options when compiling WebAssembly
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop/`)
# @param {string} SOURCE_FILES - list of source files
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
#/
$(wasm_objects): %.wasm: %.c
	$(QUIET) $(EMCC) \
		$(EMCCFLAGS) \
		$(EMCC_WASM_FLAGS) \
		$(fPIC) \
		--pre-js $(pre_js) \
		$(INCLUDE) \
		-I ../include \
		-o $*.js \
		$(SOURCE_FILES) \
		$< \
		$(LIBPATH) \
		$(LIBRARIES)
	$(QUIET) mv $*.js $*.wasm.js
	$(QUIET) sed -i "" '1 s/^/\/\* eslint-disable \*\//' $*.wasm.js

#/
# Compiles WebAssembly text files.
#
# @private
# @param {string} WASM2WAT - program for converting from the WebAssembly binary format to the WebAssembly text format (e.g., `wasm2awat`)
#/
$(wat_objects): %.wat: %.wasm
	$(QUIET) $(WASM2WAT) \
		-o $*.wat \
		$<

#/
# Creates a Node.js add-on shared object which can be linked to by other libraries and executables.
#
# @param {string} [LINKER] - program for linking compiled files (e.g., `g++`)
# @param {string} [LDFLAGS] - linker flags
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [OBJECT_FILES] - list of external object files to link
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas`)
#
# @example
# make addon.node
#/
addon.node: $(addon_objects)
	$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(OBJECT_FILES) $(c_objects) $< $(LIBPATH) $(LIBRARIES)

#/
# Creates a WebAssembly artifacts.
#
# @param {string} [EMCC_COMPILER] - Emscripten compiler (e.g., `emcc`)
# @param {string} [EMCCFLAGS] - Emscripten compiler options
# @param {string} [EMCC_ASM_FLAGS] - Emscripten compiler options when compiling asm.js
# @param {string} [EMCC_WASM_FLAGS] - Emscripten compiler options when compiling WebAssembly
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/`)
# @param {string} [SOURCE_FILES] - list of source files
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas`)
#
# @example
# make wasm
#/
wasm: clean-wasm $(asm_objects) $(wasm_objects) $(wat_objects)

.PHONY: wasm

#/
# Lists compiled libraries.
#
# @example
# make list
#/
list: $(c_objects)
	$(QUIET) for file in $^; do \
		echo \"$(this_dir)/$$file\"; \
	done

.PHONY: list

#/
# Removes generated Node.js add-on files.
#
# @example
# make clean-addon
#/
clean-addon:
	$(QUIET) -rm -f *.o *.node

.PHONY: clean-addon

#/
# Removes generated WebAssembly artifacts.
#
# @example
# make clean-wasm
#/
clean-wasm:
	$(QUIET) -rm -f *.asm.js *.wasm.js *.wasm *.wat

.PHONY: clean-wasm

#/
# Removes generated files.
#
# @example
# make clean
#/
clean: clean-addon

.PHONY: clean
