# usage: make -f Makefile-all-macosx [all \ x86 | x86_64 | ppc | universal \ clean ]
# rgf, 2011-01-02, 2011-01-12, 2011-02-20, 2013-08-12, 2017-01-08

# to support earlier versions of MacOSX export MACOSX_DEPLOYMENT_TARGET=min_version
# before compiling (cf. "man g++"), e.g.:
#
#                  export MACOSX_DEPLOYMENT_TARGET=10.6

#/*----------------------------------------------------------------------------*/
#/*                                                                            */
#/* Copyright (c) 2008-2017 Rexx Language Association. All rights reserved.    */
#/*                                                                            */
#/* This program and the accompanying materials are made available under       */
#/* the terms of the Common Public License v1.0 which accompanies this         */
#/* distribution. A copy is also available at the following address:           */
#/* http://www.oorexx.org/license.html                                         */
#/*                                                                            */
#/* Redistribution and use in source and binary forms, with or                 */
#/* without modification, are permitted provided that the following            */
#/* conditions are met:                                                        */
#/*                                                                            */
#/* Redistributions of source code must retain the above copyright             */
#/* notice, this list of conditions and the following disclaimer.              */
#/* Redistributions in binary form must reproduce the above copyright          */
#/* notice, this list of conditions and the following disclaimer in            */
#/* the documentation and/or other materials provided with the distribution.   */
#/*                                                                            */
#/* Neither the name of Rexx Language Association nor the names                */
#/* of its contributors may be used to endorse or promote products             */
#/* derived from this software without specific prior written permission.      */
#/*                                                                            */
#/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS        */
#/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT          */
#/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS          */
#/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   */
#/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
#/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED   */
#/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,        */
#/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
#/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING    */
#/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS         */
#/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.               */
#/*                                                                            */
#/*----------------------------------------------------------------------------*/


# allow earlier versions of MacOSX to use the lib
# export MACOSX_DEPLOYMENT_TARGET=10.6
# echo $MACOSX_DEPLOYMENT_TARGET

# --------------------------------------------------------
# build path to use
BLD_PATH = ./tmp

# directory to copy generated lib to
TGT_LIB_DIR     = ./lib

# 32-bit target names
BSF_OBJ_ORX_32     = $(BLD_PATH)/BSF4ooRexx-32-x86.o
BSF_ORX_TARGET_32  = $(BLD_PATH)/libBSF4ooRexx.dylib-32-x86

# 64-bit target names
BSF_OBJ_ORX_64     = $(BLD_PATH)/BSF4ooRexx-64-x86_64.o
BSF_ORX_TARGET_64  = $(BLD_PATH)/libBSF4ooRexx.dylib-64-x86_64

# neutral target name
BSF4REXX   	   = $(BLD_PATH)/libBSF4ooRexx.dylib


# rgf, 2014-03-30, 2014-03-31
# INC_PATH = -I. -I/System/Library/Frameworks/JavaVM.framework/headers
# INC_PATH = -I. -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers
INC_PATH = -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers -I .

# old ooRexx 4.x installations (need different ones for ooRexx 5.0beta)
ORX_LIBPATH_32 = /opt/oldrexx/ooRexx-i386/lib
ORX_LIBPATH_64 = /opt/oldrexx/ooRexx-x86_64/lib/

# rgf, 2017-04-05: manually creating an ooRexx 5.0 "installation"
ORX_LIBPATH_64 = /opt/oldrexx/ooRexx-5-x86_64/lib/
ORX5_INCPATH_64 = -I/opt/oldrexx/ooRexx-5-x86_64/include/

ORX_LIBS_32 = $(ORX_LIBPATH_32)/librexx.dylib $(ORX_LIBPATH_32)/librexxapi.dylib
ORX_LIBS_64 = $(ORX_LIBPATH_64)/librexx.dylib $(ORX_LIBPATH_64)/librexxapi.dylib


INC_PATH_ALL = $(INC_PATH) -I/System/Library/Frameworks/ooRexx.framework/Versions/Current/Headers $(ORX5_INCPATH_64)

CFLAGS = -fPIC
LFLAGS = -shared

# --------------------------------------------------------
all: x86 x86_64 universal

# -------------------------------------------------------- needs 32-bit ooRexx installed currently
x86: BSF4ooRexx.cc
#	g++ -c $(CFLAGS) $(INC_PATH_ALL) -m32 -arch i386 -DUSE_OREXX -DUNIX -DBSF4REXX_32_BIT -oBSF4ooRexx-mac-i386.o BSF4ooRexx.cc
	g++ -c $(CFLAGS) $(INC_PATH_ALL) -m32 -arch i386 -DUSE_OREXX -DUNIX -DBSF4REXX_32_BIT -o$(BSF_OBJ_ORX_32) BSF4ooRexx.cc

#	g++ -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_32) $(BSF_OBJ_ORX_32) $(LIB_2_REXX_32)/librexx.dylib $(LIB_2_REXX_32)/librexxapi.dylib -framework JavaVM -arch i386
	g++ -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_32) $(BSF_OBJ_ORX_32) $(ORX_LIBS_32) -framework JavaVM -arch i386

	cp -p $(BSF_ORX_TARGET_32) $(BSF4REXX)
	cp -p $(BSF_ORX_TARGET_32) $(TGT_LIB_DIR)

# --------------------------------------------------- needs 64-bit ooRexx installed currently
x86_64: BSF4ooRexx.cc
#	g++ -c $(CFLAGS) $(INC_PATH) $(INC_PATH_ALL) -m64 -arch x86_64 -DUSE_OREXX -DUNIX -DBSF4REXX_64_BIT -oBSF4ooRexx-mac-x86_64.o BSF4ooRexx.cc
	g++ -c $(CFLAGS) $(INC_PATH) $(INC_PATH_ALL) -m64 -arch x86_64 -DUSE_OREXX -DUNIX -DBSF4REXX_64_BIT -o$(BSF_OBJ_ORX_64) BSF4ooRexx.cc

# 	g++ -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64) $(BSF_OBJ_ORX_64) $(LIB_2_REXX)/librexx.dylib $(LIB_2_REXX)/librexxapi.dylib -framework JavaVM -arch x86_64
	g++ -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64) $(BSF_OBJ_ORX_64) $(ORX_LIBS_64) -framework JavaVM -arch x86_64

	cp -p $(BSF_ORX_TARGET_64) $(BSF4REXX)
	cp -p $(BSF_ORX_TARGET_64) $(TGT_LIB_DIR)

# ---------------------------------------------------
ppc: BSF4ooRexx.cc
	g++ -c $(CFLAGS) $(INC_PATH) -I/opt/ooRexx-x86_64/include -I/opt/ooRexx-ppc/include -m32 -arch ppc -DUSE_OREXX -DUNIX -DBSF4REXX_32_BIT -oBSF4ooRexx-mac-ppc.o BSF4ooRexx.cc

	g++ -dynamiclib $(LFLAGS) -o libBSF4ooRexx-ppc.dylib BSF4ooRexx-mac-ppc.o /opt/ooRexx-ppc/lib/ooRexx/librexx.dylib /opt/ooRexx-ppc/lib/ooRexx/librexxapi.dylib -framework JavaVM -arch ppc

# ---------------------------------------------------
universal: BSF4ooRexx.cc x86 x86_64
#	lipo -create -output libBSF4ooRexx.dylib -arch x86_64 libBSF4ooRexx-x86_64.dylib -arch i386 libBSF4ooRexx-i386.dylib -arch ppc libBSF4ooRexx-ppc.dylib
	lipo -create -output $(BSF4REXX)-universal -arch x86_64 $(BSF_ORX_TARGET_64) -arch i386 $(BSF_ORX_TARGET_32)


# ---------------------------------------------------
.PHONY: clean
clean:
	rm -f $(BLD_PATH)/*.dylib*
	rm -f $(BLD_PATH)/*.o
