# This makefile wraps the Endless Sky SCons build instructions and supports the same targets
# and options it does, by simply passing them through to the SCons program. If you have Scons
# installed, this makefile will defer to your installed version. Otherwise, a local SCons package
# will be obtained from SourceForge and made available to the Endless Sky tooling.
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Davide Kirchner
# Copyright (c) 2020 Benjamin Hauch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# Build targets:
# 'dist'    - a 64-bit general-release version of Endless Sky (non-specific architecture, default target)
# 'release' - a 64-bit optimized (-O3 -march=native) version of Endless Sky for local consumption
# 'debug'   - a 64-bit version of Endless Sky with debugging symbols (-g)
# 'profile' - a 64-bit version of Endless Sky with debugging symbols and profiling hooks for gprof (-pg)
# 'clean'   - removes all compiled objects and binaries.
# 'clean-all' - runs clean, and also removes `scons-local`

# End-user configuration
# https://github.com/endless-sky/endless-sky/wiki/BuildInstructions
# 1. Obtain the 'dev64' zip archive from the Endless Sky wiki, unzip it, and
#    set DIR_ESLIB to its location
# 2. Set BIN_DIR to where you want the exe to be placed (it will ship in a
#    config-dependent subfolder, e.g. BIN_DIR\debug\endless-sky.exe)
# 3. Set BUILDDIR to where you would like the intermediate .o files to reside
# 4. Set SCONS_VERSION to the version of the SCons program you would like to use
#    if SCons is not found in PATH
# 5. Set PYTHON_BINARY to the name or path of a Python3 binary. Python v2 is not supported.
# 6. Set AR to the binary archive tooling that is capable of link-time optimization
#
DIR_ESLIB ?= C:\dev64
BIN_DIR ?= bin
BUILDDIR ?= build
SCONS_VERSION ?= 4.2.0
PYTHON_BINARY ?= py
AR ?= gcc-ar

# Note: Several tools provided by MinGW & MSYS are required.
# 1. `cURL` - command-line HTTP client, used to download the `scons-local` package from SourceForge
# 2. `tar` - command-line compression client, used to unzip the downloaded `scons-local` package

#######
# Turn off all implicit rules, because we want scons to do all the work.
# (Additional builtin rules can be disabled by passing -r on the invoking CLI.)
.SUFFIXES:
# Don't invoke the wrapper targets via multiple processes.
.NOTPARALLEL:

enable_lto ::= AR=$(AR)
es_search_dirs ::= DIR_ESLIB="$(DIR_ESLIB)"
# Common environment variables to pass to SCons, expanded immediately:
scons_env ::= $(es_search_dirs) $(enable_lto)
# Common options to pass, expanded in-recipe:
scons_opts = -Q $(patsubst -j,,$(filter -j%,$(MAKEFLAGS))) BUILDDIR=$(BUILDDIR) BIN_DIR=$(BIN_DIR)

# Configure target dependencies to invoke SCons
dist:
all: release debug
release debug dist build-tests test: get_scons

# Target-specific variable overrides
dist: BUILDDIR ::= "$(BUILDDIR)/pkgd"
dist: BIN_DIR ::= "$(BIN_DIR)/pkgd"

# Target-specific build recipes
dist:
	$(scons_env) $(scons_exe) $(scons_opts) mode=release
debug release:
	$(scons_env) $(scons_exe) $(scons_opts) mode=$@
build-tests test:
	$(scons_env) $(scons_exe) $(scons_opts) $@
profile:
	@echo "Profiling is currently only supported on linux"

.PHONY: all release debug dist profile test build-tests clean clean-all get_scons

needs_tarball ::= $(strip $(shell ls scons-local/scons.py 2>/dev/null || echo true))
ifeq ($(needs_tarball),true)
# Download / Unpack the tarball, and mark it as up-to-date (vs. the packaged-on date).
scons-local-%.tar.gz:
	@echo "No system installation of SCons, downloading version $(SCONS_VERSION)"
	curl -sSf -L http://sourceforge.net/projects/scons/files/scons-local/$(SCONS_VERSION)/$@ > $@
	@touch $@
scons-local: scons-local-$(SCONS_VERSION).tar.gz
	@echo "Unpacking tarball into $@"
	@mkdir -p $@
	@tar xzf $< --directory $@
	@touch $@
endif

# Conditional recipe to invoke the system's SCons, or obtain a substitute `scons-local` package.
native_scons ::= $(strip $(shell which scons 2>/dev/null))
ifeq ($(native_scons),)
scons_exe = $(PYTHON_BINARY) ./scons-local/scons.py
get_scons: scons-local; @echo "Using locally installed scons"
else
# (A system-installed SCons also implies that a python binary is located in the user's PATH.)
scons_exe ::= "$(native_scons)"
get_scons:
	@echo "Using system scons $(scons_exe)"
endif

clean:
	$(scons_exe) -c
clean-all: clean
	rm -rf scons-local
	rm -f scons-local-*.tar.gz
