# Central makefile for building the OS2/GCC C runtime
#
# InnoTek Systemberatung GmbH
#
# Copyright (c) 1994-1995 by Eberhard Mattes
# Copyright (c) 2003 InnoTek Systemberatung GmbH
#
# Author: Andrew Zabolotny <zap@cobra.ru>
# $Id: Makefile 204 2003-05-20 14:47:45Z bird $
#
# All Rights Reserved
#
# Requires GNU Make.
# In fact, this build system could be entitled "GNU Make Unleashed"
#
# For a brief description of how build system works please read build.txt

# Build type control variables. You can set them right from command line,
# e.g "make MODE=dbg"

# Build mode: opt (optimization), dbg (debug)
MODE = dbg
# Base output directory
OUT = out/
# Base installation directory
INS = out/$(MODE)/install/
# CPU type (pretend we are portable ;-)
CPU = 386
# The object file format to use for tools (emxomf, ld and friends)
TOOLFMT.dbg = aout
TOOLFMT.opt = omf
TOOLFMT = $(TOOLFMT.$(MODE))

# Use ash.exe which is quite fast (comparable to cmd.exe) but has more features
SHELL := ash.exe

# Actual output directory (referenced with $.)
. = $(OUT)$(MODE)/

# File name extensions
# Executable files -- xxx$E
E = .exe
# DLL files --- xxx$D
D = .dll
# Library files --- xxx$A
A = .$(if $(findstring omf,$(.TKIND)),lib,a)

# The C compiler
CC = gcc -c
# The C compiler flags
CFLAGS.INC = -Iinclude -Isrc/include
CFLAGS = -Wall -mstack-arg-probe -Zmt $(CFLAGS.INC) $(CFLAGS.$(MODE)) $(CFLAGS.KIND)
# The additional C compiler flags for different build modes
CFLAGS.opt = -s -O3 -fomit-frame-pointer
CFLAGS.dbg = -g
CFLAGS.aout =
CFLAGS.omf = -Zomf
CFLAGS.prof = -pg
# The object files are put in subdirectory objectformat-targetkind,
# we decompose object file name back and pick appropiate $CFLAGS
CFLAGS.KIND = $(foreach x,$(subst -, ,$(firstword $(subst /, ,$(subst $.,,$@)))),$(CFLAGS.$x))
# How to compile a .c file
DO.COMPILE.c = $(CC) $(strip $(CFLAGS) $1) -o $@ $< -I$(dir $<)
DO.COMPILE.s = $(call DO.COMPILE.c,-x assembler-with-cpp $1)

# The linker
LD = gcc
# Linker flags
LDFLAGS = -Zmt $(LDFLAGS.$(MODE)) $(LDFLAGS.KIND)
LDFLAGS.DLL = $(LDFLAGS) -Zdll
# Linker flags for different build modes
LDFLAGS.opt = -s
LDFLAGS.dbg = -g
LDFLAGS.aout =
LDFLAGS.omf = -Zomf -Zsys -Zsmall-conv -Zlinker /PM:VIO
LDFLAGS.prof = -pg
# Linker flags for different kinds of target
LDFLAGS.KIND = $(foreach x,$(subst -, ,$(firstword $(subst /, ,$(subst $.,,$@)))),$(LDFLAGS.$x))
# How to link a .exe file
DO.LINK.exe = $(LD) $(strip $(LDFLAGS) $(filter-out -l%,$1)) -o $@ $(^O) $(^LIB) $(filter -l%,$1)
# How to link a .dll file
DO.LINK.dll = $(LD) $(strip $(LDFLAGS.DLL) $(filter-out -l%,$1)) -o $@ $(^O) $(^DEF) $(^LIB) $(filter -l%,$1)

# emxbind tool - you can depend on EMXBIND.BUILT :-)
EMXBIND = $(if $(wildcard $(EMXBIND.BUILT)),$(EMXBIND.BUILT),emxbind$E)
EMXBIND.BUILT = $.$(TOOLFMT)/emxbind$E
# emxbind flags
EMXBINDFLAGS = -q $(EMXBINDFLAGS.$(MODE))
EMXBINDFLAGS.opt = -s
EMXBINDFLAGS.dbg =
# Flags for emxbind
DO.EMXBIND = $(EMXBIND) $(EMXBINDFLAGS) -o $@ $1

# The macro assembler
ASM = ml -c
ASMFLAGS = -Cp -W3 -WX -VM -nologo
# How to compile an .asm file
DO.COMPILE.asm = $(ASM) $(ASMFLAGS) $1 -Fo$@ $<

# The tool to create an archive
AR = $(if $(findstring .lib,$@),emxomf)ar
ARFLAGS = crs
DO.LIBRARY = $(call RM,$@); $(AR) $(ARFLAGS)$1 $@ $(^O)

# The tool to extract exports from object files and archives
EMXEXP = $(if $(wildcard $(EMXEXP.BUILT)),$(EMXEXP.BUILT),emxexp$E)
EMXEXP.BUILT = $./$(TOOLFMT)/emxexp$E

# The tool to create import libraries
IMPLIB = $(if $(wildcard $(IMPLIB.BUILT)),$(IMPLIB.BUILT),emximp$E)
IMPLIB.BUILT = $./$(TOOLFMT)/emximp$E
IMPLIBFLAGS.prof = -m
IMPLIBFLAGS.KIND = $(foreach x,$(subst -, ,$(firstword $(subst /, ,$(subst $.,,$@)))),$(IMPLIBFLAGS.$x))
DO.IMPLIB = $(IMPLIB) -o $@ $(strip $1 $(IMPLIBFLAGS.KIND)) $(^I)\
  $(if $(^O),$(NL)$(AR) rs $@ $(^O))

# How to create dependencies
MAKEDEP = makedep
MAKEDEPFLAGS = $(CFLAGS.INC)
DO.DEPS = $(MAKEDEP) $(MAKEDEPFLAGS) \
  -I$(subst $(SPACE), -I,$(sort $(dir $^))) -p$(@D)/ -c -S -f$@ $^

# How to convert an a.out file to the OMF format
EMXOMF = $(if $(wildcard $(EMXOMF.BUILT)),$(EMXOMF.BUILT),emxomf$E)
EMXOMF.BUILT = $./$(TOOLFMT)/emxomf$E
DO.AOUT2OMF = $(EMXOMF) $(strip $1 -o) $@ $<

# How to filter just the object files from $^
^O = $(filter %.o,$^)
# Just the .imp files from $^
^I = $(filter %.imp,$^)
# How to filter just the .def files from $^
^DEF = $(filter %.def,$^)
# How to filter the libraries from $^
^LIB = $(filter %.$(if $(findstring omf,$(.TKIND)),lib,a),$^)

# A newline
define NL


endef
# Opening and closing brackets (for use inside variable expansions)
OB := (
CB := )
COMMA := ,
SPACE := $(EMPTY) $(EMPTY)
# Text output separator (cannot begin with '-', echo thinks its a switch)
SEP := ==========================================================================

# How to remove one or more files without questions
RM = rm -f $1
# How to remove one or more directories without questions
RMDIR = rm -rf $1
# How to copy several files to a directory
CP = cp $1 $2
# Miscelaneous tools
MKDIR = mkdir.exe -p $1
# Re-build the original string including the ',' between args. Also escape
# dollars since otherwise ash would expand them.
ECHOIZE = $(subst $$,\$$,$1$(strip $(subst $(SPACE)$(COMMA),$(COMMA),$(foreach x,2 3 4 5 6 7 8 9,$(if $($x),$(COMMA) $($x))))))
# How to output a text string (with appended newline)
ECHO = echo "$(call ECHOIZE,$1,$2,$3,$4,$5,$6,$7,$8,$9)"
# Same but append the text to a file (given with first argument)
FECHO = echo "$(call ECHOIZE,$2,$3,$4,$5,$6,$7,$8,$9)" >> "$1"
# How to replace the source file extension with a .o extension
OBJEXT = $(patsubst %.s,%.o,$(patsubst %.asm,%.o,$(patsubst %.c,%.o,$1)))
# Compute object file path given source file path (except the $. prefix)
OBJFILE = $(addprefix $(.TKIND.DIR),$(call OBJEXT,$1))

#------------ Variables appended by submakefiles ------------
# The list of available modules
MODULES :=
# The help text for module list
DO.HELP.MODULES :=
# The help about public makefile variables
DO.HELP.VARS := $(call ECHO,    MODE={dbg|opt} - choose between debug and optimized build modes.)$(NL)
DO.HELP.VARS += $(call ECHO,    OBJF={omf|aout} - build object files in omf or a.out format.)$(NL)
# The commands to install everything we have built
DO.INSTALL :=
# The list of install directories
INSDIRS :=
# The list of work directories needeed for building all targets
TARGDIRS :=
# Build rules (_@_ replaced by name of generated makefile)
RULES :=
# The list of dependency files
TARGDEPEND :=

.PHONY: default help all clean install cleandep cleandepend dep depend depdone
.SUFFIXES:
.SUFFIXES: .c .cpp .asm .s .o .exe .dll .a .lib .obj

# Default target
default: help

#------------ Submakefiles ------------
SUBMAK := version.smak $(wildcard src/*/*.smak)

# Include all submakefiles
-include $(SUBMAK)

# Sort and remove duplicate install directories
TARGDIRS := $(sort $(TARGDIRS))
INSDIRS := $(sort $(INSDIRS))

#------------ Global targets ------------
help:
	@$(call ECHO,$(SEP))
	@$(call ECHO,Welcome to $(PACKAGE) build system!)
	@$(call ECHO,$(COPYRIGHT))
	@$(call ECHO,To build something, type 'make {target} {vars}', where {target} is one of:)
	@$(call ECHO,    all - build all available modules)
	@$(call ECHO,    {module-name} - build just a particular module)
	@$(call ECHO,    clean - remove all generated files (remove all built files))
	@$(call ECHO,    install - generate a installation tree in $(INS))
	@$(call ECHO,    dep - generate dependency files for all changed targets)
	@$(call ECHO,    cleandep - remove all dependency file)
	@$(call ECHO,$(SEP))
	@$(call ECHO,There are a number of variables than can be set in the make)
	@$(call ECHO,command line to control various aspects of compilation:)
	@$(DO.HELP.VARS)
	@$(call ECHO,$(SEP))
	@$(call ECHO,The following modules are included in this package:)
	@$(DO.HELP.MODULES)
	@$(call ECHO,$(SEP))

all: $(MODULES)

clean:
	$(call RMDIR,$(OUT))

cleandep cleandepend:
	$(call RM,$(TARGDEPEND))

dep depend:
	@$(MAKE) --no-print-directory BUILD_DEPS=1 depdone

depdone:
	@$(call ECHO,Dependency files succesfully updated)

install: all $(INSDIRS)
	$(DO.INSTALL)

$. $(INSDIRS) $(TARGDIRS):
	$(call MKDIR,$@)

# bird: add rule for forcibly re-generating the rules.
rules:
	@$(call RM,$(OUT)genrules.smak)
	$(MAKE) $(OUT)genrules.smak

$(OUT)genrules.smak: $(SUBMAK) # Makefile $(wildcard *.smak)
	@$(call MKDIR,$(OUT))
	@$(call ECHO,Please wait, rebuilding make rules ...)
	@$(call RM,$@)
	@$(call FECHO,$@,# Autogenerated file -- DO NOT EDIT!)
	@$(subst _@,$@,$(RULES))

# The general a.out -> OMF conversion rule for object files
$.omf/%.obj: $.aout/%.o
	$(call DO.AOUT2OMF)

# The general a.out -> OMF conversion rule for libraries
$.omf%.lib: $.aout%.a
	$(call DO.AOUT2OMF)

-include $(OUT)genrules.smak
