# This makefile was originally generated by 'cbp2make' tool rev.147
# Edited by tehhowch
# Copyright: Public Domain

# Available targets:
# 'all'     - cleans & compiles both standard targets
# 'release' - a 64-bit optimized (-O2 -march=native) version of Endless Sky
# 'debug'   - a 64-bit version of Endless Sky with debugging symbols (-g)
# 'clean'   - removes all compiled object files and .exe files
# 'dist'    - a 64-bit general-release version of Endless Sky (non-specific architecture)

# Intended Usage
# CAPS VARIABLE -> override on command line or alter name in script
# lowercase variable -> should not need modification
#
#     avoid paths with spaces. (escape with \ if necessary)
#

# Preliminary 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 DIR_MINGW64 to the MinGW directory with a `bin` and `lib` subfolders
#    that has `libmingw32.a` and `libopengl32.a` in the lib subfolder
# 3. Set OUTPUT_DIR to where you want the exe to be placed (it will ship in a
#    config-dependent subfolder, e.g. OUTPUT_DIR\Debug\EndlessSky.exe)
# 4. Set SOURCE_DIR to where the games source files are located
# 5. Set OBJECT_DIR to where you would like the intermediate .o files to reside
#
DIR_ESLIB ?= C:\dev64
DIR_MINGW64 ?= C:\Program\ Files\mingw64\x86_64-w64-mingw32
OUTPUT_DIR ?= bin
SOURCE_DIR ?= source
OBJECT_DIR ?= obj

#######
# You should not need to modify the lines below.
ifdef build_dir
# The build directory is defined, so proceed to build the invoked target
exe := $(OUTPUT_DIR)\$(build_dir)\EndlessSky.exe

# Get all .cpp files in the source directory.
sources ::= $(sort $(wildcard $(SOURCE_DIR)/*.cpp))
# Compute needed objects by swapping the .cpp extension for .o, and swapping
# the basepath component (i.e. $(SOURCE_DIR)) for the build directory.
objects ::= $(addprefix $(OBJECT_DIR)\$(build_dir)\,$(notdir $(sources:.cpp=.o)))
# Include the target made from WinApp.rc
objects += $(OBJECT_DIR)\$(build_dir)\WinApp.o
depends ::= $(objects:.o=.d)

# Define non-default compilers and linkers
WINDRES = windres.exe
LD = g++.exe

# Compiler and linker flags
include_dir = -I$(DIR_ESLIB)\include
CXXFLAGS += -std=c++11 -Wall -MMD $(cxx_extra) $(include_dir)
# Require the linker uses WinMainCRTStartup as the entry point.
LDFLAGS += -mwindows -L$(DIR_ESLIB)\lib $(ldd_extra)
# Include the Windows Multimedia API.
LDLIBS += -lwinmm

archives ::= $(wildcard $(DIR_ESLIB)/lib/*.a) \
	$(DIR_MINGW64)\lib\libmingw32.a \
	$(DIR_MINGW64)\lib\libopengl32.a

# Default target (linker step)
$(exe): $(objects) | $(OUTPUT_DIR)\$(build_dir)
	$(LD) -o $@ $^ $(LDFLAGS) $(LDLIBS) -Wl,-\( $(archives) -Wl,-\)

# Include .o dependencies to ensure changing any of the .cpp's included headers
# triggers the .o recompilation (this must be defined after the desired exe definition).
-include $(depends)
# Pattern rule for compiling .cpps
$(OBJECT_DIR)\$(build_dir)\\%.o: $(SOURCE_DIR)\\%.cpp | $(OBJECT_DIR)\$(build_dir)
	$(CXX) -c $(CXXFLAGS) $< -o $@
# Pattern rule for compiling .rc
$(OBJECT_DIR)\$(build_dir)\WinApp.o: $(SOURCE_DIR)\WinApp.rc | $(OBJECT_DIR)\$(build_dir)
	$(WINDRES) --input-format rc --output-format coff $(include_dir) $< $@
# Pattern rule for main.cpp
$(OBJECT_DIR)\$(build_dir)\main.o: $(SOURCE_DIR)\main.cpp | $(OBJECT_DIR)\$(build_dir)
	$(CXX) -c $(CXXFLAGS) $< -o $@
# Rules for making the output and build directories
$(OBJECT_DIR):
	-@cmd /c 'if not exist $@ md $@'
$(OBJECT_DIR)\$(build_dir): | $(OBJECT_DIR)
	-@cmd /c 'if not exist $@ md $@'
$(OUTPUT_DIR):
	-@cmd /c 'if not exist $@ md $@'
$(OUTPUT_DIR)\$(build_dir): | $(OUTPUT_DIR)
	-@cmd /c 'if not exist $@ md $@'

else # build_dir not yet defined
# Define variables to be consumed by invoked make subprocess (which will have a target)

# Define targets as phony, i.e. invoke the subprocess no matter what. It will decide
# if there is anything to be done
.PHONY: default all release debug dist clean

# The default target invokes dist (e.g. portable)
default: dist
# make all invokes debug, release, and dist
all: debug release dist

# Define target-specific build directory for use by subprocess
debug: export build_dir := Debug
release: export build_dir := Win64
dist: export build_dir := pkgd

# Define compiler and linker flags for use by subprocess
debug: export cxx_extra := -g -DDEBUG
release: export cxx_extra := -O2 -march=native
dist: export cxx_extra := -O2
release dist: export ldd_extra := -Wl,-O2 -s

# Invoke the subprocess (-f flag is not sent in MAKEFLAGS)
debug release dist:
	$(MAKE) -$(MAKEFLAGS) -f .winmake
clean:
	cmd /c rd /S /Q $(OUTPUT_DIR)
	cmd /c rd /S /Q $(OBJECT_DIR)
endif # build_dir
