#
# Module   : makefile
# Abstract : script for building libp2j.so/p2j.dll on multiple platforms
#
# Copyright (c) 2010-2013, Golden Code Development Corporation.
# ALL RIGHTS RESERVED. Use is subject to license terms.
#
#           Golden Code Development Corporation
#                      CONFIDENTIAL
#
# -#- -I- --Date-- ---------------------------------Description----------------------------------
# 001 LMR 20101123 Created initial version of this makefile to support 
#                  building libp2j.so on multiple platforms; for each 
#                  individidual OS a number of variables need to be defined,
#                  as explained in the file header.
# 002 CS  20130103 Implement OS-DIR support as an INPUT stream. Added filesys.c
#                  to the libp2j library construction
# 003 GES 20130430 Added support for memory management functions.
# 004 EVL 20130425 Adding Windows compilation stuffs for p2j.dll library.
# 005 EVL 20130618 Adding Windows registry access module.
# 006 EVL 20130723 Adding text console CHARVA replacenent.
# 007 EVL 20130731 Adding Windows module for console functions compilation.
# 008 GES 20131021 Rewrote to maximize common code and to do OS and CPU testing here instead of
#                  in the platform-independent Java build.  Also added new files to the build.
# 

# the following required variables are supplied as arguments to make:
#    SONAME:      the name of the shared library to build
#    SRCDIR:      the directory with the library source code
#    JRE_HOME:    the path to the java distribution

# calculate the operating system and save it in OS
ifndef OS
   OS := $(shell uname -s)
endif

ifeq ($(OS),Windows_NT)
   override OS=Windows
endif
ifeq (Windows,$(findstring Windows,$(OS)))
   override OS=Windows
endif

# calculate the CPU bitness and save it in ARCH
ifndef ARCH
   ifeq "$(OS)" "Windows"
      ifeq "$(PROCESSOR_ARCHITECTURE)" "AMD64"
         ARCH=64bit
      else
         ARCH=32bit
      endif
   else
      ARCH := $(shell uname -p)
   endif
endif

ifeq "$(ARCH)" "i386"
   override ARCH=32bit
endif
ifeq "$(ARCH)" "i486"
   override ARCH=32bit
endif
ifeq "$(ARCH)" "i586"
   override ARCH=32bit
endif
ifeq "$(ARCH)" "i686"
   override ARCH=32bit
endif
ifeq "$(ARCH)" "x86_64"
   override ARCH=64bit
endif
ifeq "$(ARCH)" "sparc"
   override ARCH=64bit
endif

# begin compile/link variable definitions
JNIHEADER=$(SRCDIR)/com_goldencode_p2j_util_LaunchManager.h $(SRCDIR)/com_goldencode_p2j_util_FileChecker.h $(SRCDIR)/com_goldencode_p2j_util_FileSystemDaemon.h $(SRCDIR)/com_goldencode_p2j_util_MemoryManager.h $(SRCDIR)/com_goldencode_p2j_util_LibraryManager.h
SOURCES=process.c filesys.c memory.c terminal.c library.c

# platform-specific definitions that are needed very early
ifeq "$(OS)" "Windows"
   JNIHEADER+=$(SRCDIR)/com_goldencode_p2j_util_Registry.h 
   SOURCES+=process_win.c filesys_win.c terminal_win.c library_win.c registry.c
   PLATFORM_DIR=win32
endif

ifeq "$(OS)" "Linux"
   SOURCES+=process_linux.c filesys_linux.c terminal_linux.c library_linux.c
   PLATFORM_DIR=linux
endif

ifeq "$(OS)" "SunOS"
   SOURCES+=process_linux.c filesys_linux.c terminal_linux.c library_linux.c
   PLATFORM_DIR=solaris
endif

# more compile/link variable definitions
CC=gcc
OBJEXT=o
INCLUDEDIRS=${JRE_HOME}/../include ${JRE_HOME}/../include/$(PLATFORM_DIR) $(SRCDIR)
override INCLUDES+=$(foreach d,$(INCLUDEDIRS),-I$(d))
override CFLAGS+=$(INCLUDES) -Wall
override LDFLAGS+=-static-libgcc -shared

# SRCFILE and OBJFILE are placeholders for the actual source/object files
COMPILECMD=$(CC) -c SRCFILE -o OBJFILE

# this will only be run once so we refer to the real files here:
LINKCMD=$(CC) -o $(SONAME) $(OBJS)
RMCMD=rm -fv

# linux section 
ifeq "$(OS)" "Linux"
   override CFLAGS+=-fpic

   # NCURSES library is a requirement in the project anyway so the C code 
   # calls functions in that interface directly instead of exec'ing command
   # line utilities for the same purpose (to avoid the hard requirement of
   # having extra utility programs installed in addition to P2J); this is 
   # the reason why libp2j depends on libncurses:
   override LDFLAGS+=-lncurses -ldl 
endif

# windows section
ifeq "$(OS)" "Windows"
   
   ifeq "$(ARCH)" "64bit"
      override CFLAGS+=-fpic
   endif

   # We do not use NCURSES for Windows target. All terminal functionality is
   # implemented using native Win x86 API:
   override LDFLAGS+=-lmsvcrt -lgdi32 -Wl,--subsystem,windows,--kill-at 

   override COMPILECMD+=-DWIN_TARGET
   override RMCMD=del
endif

# solaris section
ifeq "$(OS)" "SunOS"
   override CFLAGS+=-fpic -DSOLARIS

   # CURSES library instead of NCURSES (see linux section above)
   override LDFLAGS+=-lcurses -ldl

   ifeq "$(ARCH)" "64bit"
      override CFLAGS+=-m64
      override LDFLAGS+=-m64
   endif

endif

# final compile/link variable definitions
ifeq "$(ARCH)" "64bit"
   override COMPILECMD+=-DWORD_SIZE_64
else
   override COMPILECMD+=-DWORD_SIZE_32
endif

OBJS=$(SOURCES:.c=.$(OBJEXT))
REALCOMPILECMD:=$(patsubst SRCFILE,$$<,$(patsubst OBJFILE,$$@,$(COMPILECMD)))

# build rules
define compile
%.$(OBJEXT): $(SRCDIR)/%.c $(JNIHEADER)
	$(REALCOMPILECMD) $(CFLAGS)
endef

.PHONY: all clean

all: $(SONAME)

$(eval $(compile))

$(SONAME): $(OBJS)
	$(LINKCMD) $(LDFLAGS)

clean: 
	$(RMCMD) *.$(OBJEXT) $(SONAME)

