#/
# @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 Fortran source files:
ifdef FORTRAN_COMPILER
	FC := $(FORTRAN_COMPILER)
else
	FC := gfortran
endif

# Define the command-line options when compiling Fortran files:
FFLAGS ?= \
	-std=f95 \
	-ffree-form \
	-O3 \
	-Wall \
	-Wextra \
	-Wimplicit-interface \
	-fno-underscoring \
	-pedantic

# 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

# 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 Fortran targets:
fortran_objects := sasum.o sasumsub.o

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

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


# TARGETS #

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

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

.PHONY: all


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

$(fortran_objects): %.o: %.f
	$(QUIET) $(FC) $(FFLAGS) $(fPIC) -c -o $@ $<


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

$(c_objects): %.o: %.c
	$(QUIET) $(CC) $(CFLAGS) $(fPIC) -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)" \
		-I ../include \
		-c \
		-o $@ \
		$<


# 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.o
	$(QUIET) $(LD) -shared $(LDFLAGS) $(fPIC) -o $@ $(fortran_objects) $(c_objects) $<


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

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

.PHONY: list


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

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

.PHONY: clean
