# New Makefile for SIMH 3.8
# Philipp Hachtmann
#


################################################################################

# Find out something about the system we build for.
# This process is ugly and should be revisited!

BUILD=linux

ifneq ($(WIN32),)
  BUILD=win32
endif

ifneq (,$(findstring solaris,$(OSTYPE)))
  BUILD=solaris
endif

ifneq (,$(findstring darwin,$(OSTYPE)))
  BUILD=darwin
endif


################################################################################

# Set build options according to what we found out.

ifeq ($(BUILD),win32)
  CC = gcc 
  LDFLAGS = -lm -lwsock32 -lwinmm
  CFLAGS=-std=c99 -U__STRICT_ANSI__ -O2 -I. -Wall
  EXEC_SUFFIX = .exe
endif 	

ifeq ($(BUILD),solaris)
  CC=gcc
  CFLAGS=-D_GNU_SOURCE
  LDFLAGS=-lm -lsocket -lnsl  -lrt -lpthread 
endif

ifeq ($(BUILD),darwin)
  CC=gcc
  CFLAGS = -D_GNU_SOURCE
  LDFLAGS=
endif

ifeq ($(BUILD),linux)
  CC=gcc
  CFLAGS = -D_GNU_SOURCE -std=c99 -U__STRICT_ANSI__ -g
  LDFLAGS= -lrt -lm
endif


################################################################################

# Add network definitions if desired

ifneq ($(USE_NETWORK),)
  CFLAGS+=-DUSE_NETWORK
  LDFLAGS+=-lwpcap -lpacket
endif


################################################################################

# Other stuff to add to CFLAGS and LDFLAGS

CFLAGS+=-I common
LDFLAGS+=


################################################################################

# Target directory where the executables go to.
# The directory is automatically created if it doesn't exist.
TARGET_DIR=bin

# This makes the target directory go away with "make clean":
CLEANSTUFF+=$(TARGET_DIR)


################################################################################
################################################################################

# This is the main section containing all the simulator dependent information.


# Names used by the template mechanism.
# They are used as part of the corresponding variable names 
# like "NOVA" in "NOVA_CFLAGS".
# You will have to add a name for every new simulator or build
# configuration.

SIM_NAMES=COMMON H316 NOVA ECLIPSE

#
# Global stuff. The special executable "nothing" will not be built.
#
COMMON_DIR=common
COMMON_MODULES=scp sim_console sim_fio sim_timer sim_sock sim_tmxr sim_ether sim_tape
COMMON_CFLAGS=
COMMON_EXEC=nothing


################################################################################
#
# Data General Nova
#
NOVA_DIR=nova
NOVA_EXEC=nova
NOVA_MODULES= nova_sys nova_cpu nova_dkp nova_dsk nova_lp nova_mta \
              nova_plt nova_pt nova_clk nova_tt nova_tt1 nova_qty


################################################################################
#
# Data General Eclipse
#

# Note: Here you see we define a ..._SRC_DIR and ..._OBJ_DIR. This is
#       done because Nova and Eclipse have the same modules compiled 
#       with different CFLAGS.
#       This is handled automatically by the generic rule template for
#       object files. But if the OBJ_DIR should be deleted
#       by "make clean", it must be added manually to the CLEANSTUFF list.

ECLIPSE_SRC_DIR=nova
ECLIPSE_OBJ_DIR=eclipse
CLEANSTUFF+=$(ECLIPSE_OBJ_DIR)

ECLIPSE_EXEC=eclipse
ECLIPSE_MODULES=eclipse_cpu eclipse_tt nova_sys nova_dkp nova_dsk nova_lp \
                nova_mta nova_plt nova_pt nova_clk nova_tt1 nova_qty  
ECLIPSE_CFLAGS=-DECLIPSE -DUSE_INT64


################################################################################

#
# Honeywell DDP-516/H316
#
H316_DIR=h316
H316_EXEC=h316
H316_MODULES = h316_stddev h316_lp h316_cpu h316_sys h316_mt h316_fhd h316_dp


################################################################################
################################################################################

# Here comes the weird generic stuff. Should be altered VERY carefully only.

# Default target
default: all


#################################
#
# The object file build rule template
#

define OBJECT_TEMPLATE

# If no ..._OBJ_DIR is specified, use ..._DIR

ifeq (,$$($(1)_OBJ_DIR))
$(1)_OBJ_DIR=$$($(1)_DIR)
endif

# If no ..._SRC_DIR is specified, use ..._DIR

ifeq (,$$($(1)_SRC_DIR))
  $(1)_SRC_DIR=$$($(1)_DIR)
endif

# Generate object file names with path (for example h316/h316_cpu.o)

$(1)_OBJS=$$(foreach obj, $$($(1)_MODULES), $$($(1)_OBJ_DIR)/$$(obj).o)

# Add the object files to the CLEANSTUFF list

CLEANSTUFF+=$$($(1)_OBJS)

# The build rule.
# At the moment, header file changes might cause too many files to be rebuilt. But
# that seems to be acceptable for the moment.

$$($(1)_OBJS) : $$($(1)_OBJ_DIR)/%.o : $$($(1)_SRC_DIR)/%.c $$($(1)_SRC_DIR)/*.h $$(COMMON_DIR)/*.h
	@echo CC $$@

# Create output directory if it doesn't exist yet.

	@if [ ! -d $$($(1)_OBJ_DIR) ]; then echo "MKDIR $$($(1)_OBJ_DIR)"; mkdir $$($(1)_OBJ_DIR); fi

ifneq (,$$(VERBOSE))
	@echo $$(CC) -c -o $$@ -I $$($(1)_DIR) $$(CFLAGS) $$($(1)_CFLAGS) $$<
endif

	@$$(CC) -c -o $$@ -I $$($(1)_DIR) $$(CFLAGS) $$($(1)_CFLAGS) $$<

endef


#################################
#
# The executable file build rule template
#

define PROGRAM_TEMPLATE

# Add the executable to the CLEANSTUFF list

CLEANSTUFF+=$$(TARGET_DIR)/$$($(1)_EXEC)$$(EXEC_SUFFIX)

# We won't build or mention the executable named "noting"!
ifneq (nothing,$$($(1)_EXEC))
EXEC_TARGETS+=$$($(1)_EXEC)
endif

# Generate a phony target with the simulator's name.
# Enables things like "make h316".
# On Windows this WILL NOT be "make h316.exe".

$$($(1)_EXEC): $$(TARGET_DIR)/$$($(1)_EXEC)$$(EXEC_SUFFIX)

.PHONY: $$($(1)_EXEC)

$$(TARGET_DIR)/$$($(1)_EXEC)$$(EXEC_SUFFIX): $$($(1)_OBJS) $$(COMMON_OBJS)
	@if [ ! -d $$(TARGET_DIR) ]; then echo "MKDIR $$(TARGET_DIR)";mkdir $$(TARGET_DIR);fi
	@echo LD $$@
ifneq (,$$(VERBOSE))
	@echo $$(CC) -o$$@ $$(LDFLAGS) $$($(1)_LDFLAGS) $$^
endif
	@$$(CC) -o$$@ $$(LDFLAGS) $$($(1)_LDFLAGS) $$^
endef


#################################
#
# Use the template with SIM_NAMES
#

$(foreach sn, $(SIM_NAMES),$(eval $(call OBJECT_TEMPLATE,$(sn))))
$(foreach sn, $(SIM_NAMES),$(eval $(call PROGRAM_TEMPLATE,$(sn))))


#################################
#
# The all target
#

all: $(EXEC_TARGETS)


#################################
#
# Output a little help
#

help:
	@echo Available targets:
	@echo
	@echo "all   - Build all simulators"
	@echo "clean - Delete all generated files and directories"
	@echo
	@echo Simulators can explicitely built by the following targets:
	@echo
	@echo $(EXEC_TARGETS)
	@echo
	@echo $(CLEANSTUFF)
	@echo
	@echo The actual CC calls can be made visible with specifying VERBOSE=something.
	@echo


#################################
#
# The clean rule.
#

clean:
	@echo CLEAN
	@rm -rf $(CLEANSTUFF)


#################################
#
# Specify phony targets
#

.PHONY: clean help all default


################################################################################
#
# This is the end of the game :-)
#
################################################################################




