#/
# @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 := @
endif

# Determine the OS:
#
# [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
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 WebAssembly:
EMCCFLAGS ?= $(CFLAGS)

# Shared `emcc` flags:
EMCC_SHARED_FLAGS := \
	-s EXPORTED_FUNCTIONS="['_stdlib_base_inv']" \
	-s STRICT=1 \
	-s NO_FILESYSTEM=1 \
	-s ERROR_ON_UNDEFINED_SYMBOLS=1 \
	-s ERROR_ON_MISSING_LIBRARIES=1

# WebAssembly specific flags:
# TODO: Remove `-s LEGALIZE_JS_FFI=0`, as this setting is a hack to force WASM only mode (see https://github.com/kripken/emscripten/issues/5370).
EMCC_WASM_FLAGS := $(EMCC_SHARED_FLAGS) \
	-s SIDE_MODULE=1 \
	-s WASM=1 \
	-s LEGALIZE_JS_FFI=0

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

# Determine whether to generate [position independent code][1]:
#
# [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 := inv.o

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

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

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


# TARGETS #

# Default target.
#
# This target is the default target.

all: $(c_objects) $(addon_objects) addon.node wasm

.PHONY: all


# Compile C source.
#
# This target compiles C source files.

$(c_objects): %.o: %.c
	$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -I ../include -c -o $@ $<


# Compile add-ons.
#
# This target compiles C++ add-on source files.

$(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 $@ \
		$<


# Compile WebAssembly binaries.
#
# This target compiles WebAssembly binaries.

$(wasm_objects): %.wasm: %.c
	$(QUIET) $(EMCC) \
		$(EMCCFLAGS) \
		$(EMCC_WASM_FLAGS) \
		$(fPIC) \
		$(INCLUDE) \
		-I ../include \
		-o $*.js \
		$(SOURCE_FILES) \
		$< \
		$(LIBPATH) \
		$(LIBRARIES)
	$(QUIET) rm -f $*.js


# Generate WebAssembly text files.
#
# This target generates WebAssembly text files from WebAssembly binaries.

$(wat_objects): %.wat: %.wasm
	$(QUIET) $(WASM2WAT) \
		-o $*.wat \
		$<


# Generate add-on.
#
# This target generates a Node.js add-on by creating a shared object which can be linked to by other libraries and executables.

addon.node: $(addon_objects)
	$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(OBJECT_FILES) $(c_objects) $< $(LIBPATH) $(LIBRARIES)


# Generate WebAssembly.
#
# This target generates WebAssembly files.

wasm: clean-wasm $(wasm_objects) $(wat_objects)

.PHONY: wasm


# List libraries.
#
# This target lists compiled libraries.

list: $(c_objects)
	$(QUIET) for file in $^; do \
		echo \"$(this_dir)/$$file\"; \
	done

.PHONY: list


# Clean-up add-on.
#
# This target removes generated files for building an add-on.

clean-addon:
	$(QUIET) -rm -f *.o *.node

.PHONY: clean-addon


# Clean-up WebAssembly.
#
# This target removes generated files for compiling to WebAssembly.

clean-wasm:
	$(QUIET) -rm -f *.wasm *.wat

.PHONY: clean-wasm


# Perform clean-up.
#
# This target removes generated files.

clean: clean-addon

.PHONY: clean
