# usage: make -f Makefile-all-macosx [all | x86_64 | arm64 | ppc | universal | clean ]
# rgf, 2011-01-02, 2011-01-12, 2011-02-20, 2013-08-12, 2017-01-08, 2021-07-19
#
#  2021-07-19: using "-target" (instead of "-arch") and version information in name of value, cf. x86_64 and arm64
#              ad otool, install_name_tool, cf. e.g. <https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling>
#
# rgf, 2017-08-17: about using "std++" (C++11) on clang, e.g. cf <https://stackoverflow.com/questions/19774778/when-is-it-necessary-to-use-use-the-flag-stdlib-libstdc>
#
#         target 10.9, add to g++ "-std=c++11" (or "-std=gnu++11"); (and before Maverick: "-stdlib=libc++"; to force gnu without c++-11 support use: "-stdlib=libstdc++")
#
# rgf, 2021-08-21: adding -rpath when librexx.4.dylib gets sought
# rgf, 2021-09-22: usage of RPATH was wrong, if multiple paths then one needs to supply multiple -rpath switches (one for each directory)
#                  using: @loader_path -> @loader_path/../lib -> /usr/local/lib-> /opt/oorexx/lib  -> /Library/Frameworks/ooRexx.framework/Libraries
# rgf, 20220921:   use JAVA_HOME, rename to *BSF4ooRexx850*
#

# to support earlier versions of MacOSX export MACOSX_DEPLOYMENT_TARGET=min_version
# before compiling (cf. "man g++"), e.g.:
#
#  MacOSX 10.9, "Mavericks", released: 2013-10-22
#                  export MACOSX_DEPLOYMENT_TARGET=10.9
#  MacOSX 10.6, "Snow Leopard", released: 2009-08-28
#                  export MACOSX_DEPLOYMENT_TARGET=10.6
#  MacOSX 10.11, "El Capitan", released: 2014
#                  export MACOSX_DEPLOYMENT_TARGET=10.11


#/*----------------------------------------------------------------------------*/
#/*                                                                            */
#/* Copyright (c) 2008-2022 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

# Mojave, 10.14, 2018-06-04
# export MACOSX_DEPLOYMENT_TARGET=10.14
# 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)/BSF4ooRexx850-32-x86.o
BSF_ORX_TARGET_32  = $(BLD_PATH)/libBSF4ooRexx850.dylib-32-x86

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

BSF_OBJ_ORX_ARM64    = $(BLD_PATH)/BSF4ooRexx850-64-arm64.o
BSF_ORX_TARGET_ARM64 = $(BLD_PATH)/libBSF4ooRexx850.dylib-64-arm64

# neutral target name
BSF4REXX   	   = $(BLD_PATH)/libBSF4ooRexx850.dylib
LIB_INSTALL_NAME   = /usr/local/lib/libBSF4ooRexx850.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 .

# INC_PATH = -I/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/include -I .

# JDK_HOME = /Library/Java/JavaVirtualMachines/bellsoft-jdk8u302-full.jdk
JDK_HOME = $(JAVA_HOME)


INC_PATH = -I ${JDK_HOME}/include -I ${JDK_HOME}/include/darwin -I .

# an arm64 Java for MacOS
JDK_HOME_ARM64 = /Users/rony/work/j8-zulu-fx-arm64/zulu8.54.0.21-ca-fx-jdk8.0.292-macosx_aarch64/zulu-8.jdk/Contents/Home
INC_PATH_ARM64 = -I ${JDK_HOME_ARM64}/include -I ${JDK_HOME_ARM64}/include/darwin -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, 20170122, ooRexx 5.0.0 (beta)
# ORX_LIBPATH_64 = /Library/Frameworks/ooRexx.framework/Libraries/
# rgf, 20210319
ORX_LIBPATH_64 = /usr/local/lib

# rgf, 2017-04-05: manually creating an ooRexx 5.0 "installation"
# rgf, 2020-08-21: removing this now misleading definitions
# ORX_LIBPATH_64 = /opt/oldrexx/ooRexx-5-x86_64/lib/
# ORX5_INCPATH_64 = -I/opt/oldrexx/ooRexx-5-x86_64/include/

# if BSF4ooRexx with ooRexx got installed
OOREXX_FRAMEWORK = /Library/Frameworks/ooRexx.framework/Versions/Current/
# make install will install to:
OOREXX_APP_INSTALL = /Users/rony/Applications/ooRexx5

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

# ORX_LIBS_64 = -L/usr/local/lib -L${OOREXX_FRAMEWORK}/Libraries -L${OOREXX_APP_INSTALL}999/lib -lrexx -lrexxapi


# assuming that ooRexx is universal or arm64
ORX_LIBS_64 = -L${OOREXX_APP_INSTALL}/lib   -lrexx -lrexxapi
INC_PATH_ALL = $(INC_PATH) -I$(OOREXX_APP_INSTALL)/include
INC_PATH_ALL_ARM64 = $(INC_PATH_ARM64) -I$(OOREXX_APP_INSTALL)/include

#rpath for searching librexx[api].4.dylib, get infos: otool -l tmp/libBSF4ooRexx850.dylib
# RPATH = '$$ORIGIN:$$ORIGIN/../lib:/usr/local/lib:/Library/Frameworks/ooRexx.framework/Libraries:/opt/ooRexx/lib'
# on MacOS: advice to use $loader_path
# RPATH = @loader_path:@loader_path/../lib:/usr/local/lib:/Library/Frameworks/ooRexx.framework/Libraries:/opt/oorexx/lib
# ALL_RPATH =-rpath @loader_path -rpath @loader_path/../lib -rpath /usr/local/lib -rpath /Library/Frameworks/ooRexx.framework/Libraries -rpath /opt/oorexx/lib
# on Linux the fallback directory is /opt/ooRexx/lib (note the capital "R"), therefore we use the same case on MacOS as well
ALL_RPATH =-rpath @loader_path -rpath @loader_path/../lib -rpath /usr/local/lib -rpath /opt/ooRexx/lib -rpath /Library/Frameworks/ooRexx.framework/Libraries


# INC_PATH_ALL = $(INC_PATH) -I/System/Library/Frameworks/ooRexx.framework/Versions/Current/Headers $(ORX5_INCPATH_64)
INC_PATH_ALL = $(INC_PATH) -I/Applications/ooRexx5/include -I$(OOREXX_APP_INSTALL)/include

CFLAGS = -fPIC
LFLAGS = -shared

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

# --------------------------------------------------- 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 -o$(BSF_OBJ_ORX_64) BSF4ooRexx.cc

#	g++ -v -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64) $(BSF_OBJ_ORX_64) $(ORX_LIBS_64) -framework JavaVM -arch x86_64
#	g++    -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64) $(BSF_OBJ_ORX_64) $(ORX_LIBS_64) -L${JDK_HOME}/jre/lib/libjvm.dylib -arch x86_64

#	g++    -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64)  $(BSF_OBJ_ORX_64) $(ORX_LIBS_64) -arch x86_64
# experimenting with '-target' macOS 10.12: "Sierra", released 2016-06-13
	g++    -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64)  $(BSF_OBJ_ORX_64) $(ORX_LIBS_64) ${ALL_RPATH} -target x86_64-apple-macos10.13

	install_name_tool -id $(LIB_INSTALL_NAME) $(BSF_ORX_TARGET_64)
	cp -p $(BSF_ORX_TARGET_64) $(BSF4REXX)
	cp -p $(BSF_ORX_TARGET_64) $(TGT_LIB_DIR)

# ---------------------------------------------------

arm64: BSF4ooRexx.cc

	g++ -c $(CFLAGS) $(INC_PATH_ALL_ARM64) -m64 -arch arm64 -DUSE_OREXX -DUNIX -DBSF4REXX_64_BIT -o$(BSF_OBJ_ORX_ARM64) BSF4ooRexx.cc

#	g++ -v -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_64) $(BSF_OBJ_ORX_64) $(ORX_LIBS_64) -framework JavaVM -arch x86_64

#	g++    -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_ARM64) $(BSF_OBJ_ORX_ARM64) $(ORX_LIBS_64) -arch arm64
# experimenting with '-target'
	g++    -dynamiclib $(LFLAGS) -o $(BSF_ORX_TARGET_ARM64) $(BSF_OBJ_ORX_ARM64) $(ORX_LIBS_64)  ${ALL_RPATH} -target arm64-apple-macos11

	install_name_tool -id $(LIB_INSTALL_NAME) $(BSF_ORX_TARGET_ARM64)
	cp -p $(BSF_ORX_TARGET_ARM64) $(BSF4REXX)
	cp -p $(BSF_ORX_TARGET_ARM64) $(TGT_LIB_DIR)


# ---------------------------------------------------
universal: BSF4ooRexx.cc x86_64 arm64
#	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 arm64 $(BSF_ORX_TARGET_ARM64)

#	cp -p $(BSF4REXX)-universal $(BSF4REXX)/Library/Java/JavaVirtualMachines/bellsoft-jdk8u302-full.jdk
	cp -p $(BSF4REXX)-universal $(TGT_LIB_DIR)

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


# --------------------------------------------------- outdated
ppc_outdated: 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 libBSF4ooRexx850-ppc.dylib BSF4ooRexx850-mac-ppc.o /opt/ooRexx-ppc/lib/ooRexx/librexx850.dylib /opt/ooRexx-ppc/lib/ooRexx/librexxapi850.dylib -framework JavaVM -arch ppc
# -------------------------------------------------------- outdated, needs 32-bit ooRexx installed currently
x86_outdated: 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) $(ORX_LIBS_32) -framework JavaVM -arch i386

	cp -p $(BSF_ORX_TARGET_32) $(BSF4REXX)
	cp -p $(BSF_ORX_TARGET_32) $(TGT_LIB_DIR)
# --------------------------------------------------- outdated
universal__outdated: 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)

