# make -f Makefile-64-linux-arm rebuild
#-------------------------------------------------------------------------
# Makefile-64-linux-arm: makefile to build libBSF4ooRexx850.so-64-x86
#
# ooRexx 5.x, 2005-06-07, 2009-09-08, 2017-01-08, 2022-09-21
#
# make -f Makefile-64-linux-arm [ all | clean | clean_dll | rebuild ]
#-------------------------------------------------------------------------

# changes:
# 	20210823, rgf: define RPATH at link time to look up: $ORIGIN:$ORIGIN/../lib:/usr/local/lib64:/usr/lib64:/usr/local/lib:/usr/lib:/opt/ooRexx/lib
#       20220921, rgf: rename to *BSF4ooRexx850*
#

# ------------------------ Apache Version 2.0 license -------------------------
#    Copyright (C) 2001-2022 Rony G. Flatscher
#    Copyright (C) 2020-2021 Tony Dycks
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# -----------------------------------------------------------------------------


# -----------------------  Path Definitions  ----------------------------

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

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

# base directory of the Java JDK to use on this system

# >>> Revised to Use $JAVA_HOME Env Var -- T. Dycks -- November 17, 2020 <<<
# ==> To Use $JAVA_HOME Environment Variable ...
# To Set Value from Command Shell Prompt:
#   export JAVA_HOME=/path/to/jdk/base
# To Prepend to System PATH from Command Shell:
#   export PATH=$JAVA_HOME/bin:$PATH
# Statements Above can be added to the $HOME/.bashrc file to be set when Bash
# Command Shell is started; Some Distros also use $JDK_HOME as an environment variable
# Best Practice for JDK Version Differences
# Moved Previous Comment Up To Separate Line To Prevent Append of Space Characters to JDK_BASE
# Variable Assignement That Follows -- tfd July 19, 2021
JDK_BASE = $(JAVA_HOME)
# If Using $JDK_HOME Environment Variable; Uncomment Line Below & Comment Out $(JAVA_HOME) Line
#JDK_BASE = $(JDK_HOME)
# ---------------------------------------------------------------------------
# >>> Raspberry Pi 4 (aarch64) Architecture JDK 8 or 11 Settings <<<
# ==> Manjaro Linux; JDK 8 Setting for OpenJDK Pacman Package
#JDK_BASE = /usr/lib/jvm/java-8-openjdk
# ==> Ubuntu Linux Server; JDK 8 Setting for Ubuntu Debian Packages Install
#JDK_BASE = /usr/lib/jvm/java-8-openjdk-arm64
# ==> Raspberry Pi OS 64 Beta; JDK 8 Setting for Oracle Download Install
#JDK_BASE = /opt/jdk1.8.0_261 # Oracle JDK v1.8 Download
# ==> Raspberry Pi OS 64 Beta; OpenJDK 11 Debian Package Install
#JDK_BASE = /usr/lib/jvm/java-11-openjdk-arm64
# ==> CentOS/RHEL 7 64; JDK 8 Setting for RPM (yum) Packages Install
#JDK_BASE = /usr/lib/jvm/java-1.8.0
# ==> Fedora 33 64; JDK 8 Setting for RPM (yum or dnf) Package Install
#JDK_BASE = /usr/lib/jvm/java-1.8.0-openjdk
# >>> End of  Revisions for ARM aarch64 Distros -- T. Dycks -- November 17, 2020 <<<

# rgf, 2017-04-05: force it to use the locally stored Java include files
INC_PATH = -I. -I/usr/local/include -I$(JDK_BASE)/include -I$(JDK_BASE)/include/linux -I/usr/include

INC_PATH_ORX = -I/opt/ooRexx/include $(INC_PATH)

LIB_PATH = -L. -L/usr/local/lib -L/usr/lib -L/opt/ooRexx/lib


# --------------------------------------------------------
# delete command to use
del_cmd  = rm -f

# copy command to use
copy_cmd = cp

# source file to compile
BSF_SRC	= BSF4ooRexx.cc

#
# Compiler macros
#
# cc = gcc -fPIC
cc = gcc
link = gcc

# Revise BITNESS from 32 to 64 -- T. Dycks -- June 13, 2020
BITNESS         = 64
# ARCH            = x86
# tfd, 2021-08-25: Changed ARCH from "arm" to "arm64"
ARCH            = arm$(BITNESS)

#
# CFLAGS		= -fPIC -c -O3 -D__cplusplus -DUNIX -m$(BITNESS) -DBSF4REXX_$(BITNESS)_BIT
CFLAGS          = -fPIC -c -O3 -D__cplusplus -DUNIX -DBSF4REXX_$(BITNESS)_BIT
CFLAGS_ORX      = -DUSE_OREXX
# TODO: 20170108, rgf: -ldl really needed ?
LINK_FLAGS      = -shared -rdynamic -lpthread -lc  -lm -ldl

# rgf, 2017-01-25: "-lrexxutil" not needed
# LINK_ORX_FLAGS  = -lrexx -lrexxapi -lrexxutil
LINK_ORX_FLAGS  = -lrexx -lrexxapi

# rgf, 20210823: RPATH will be used to find the ooRexx libraries if libBSF4ooRexx.so was loaded by Java (and needs to load librexx[api].so)
#     in order to get $ORIGIN processed as such it needs to be written as $$'ORIGIN' to have substitions and escapes applied to yield $ORIGIN at link time!
#     in order on newer linkers to create RPATH and not RUNPATH one needs to use -Wl,--disable-new-dtags :(
#     note: -rpath is claimed to also work for libraries (so no need to use -rpath-link which is not shown in the dynamic section anyway)
#           to see effect of linking use: readelf -d path/to/binary
# RPATH           = $$'ORIGIN':$$'ORIGIN'/../lib:/usr/local/lib64:/usr/lib64:/usr/local/lib:/usr/lib:/opt/ooRexx/lib
RPATH           = $$'ORIGIN':$$'ORIGIN'/../lib:/usr/local/lib:/usr/lib:/opt/ooRexx/lib

BSF_SONAME      = libBSF4ooRexx850.so
BSF_OBJ_ORX     = $(BLD_PATH)/BSF4ooRexx850-$(BITNESS)-$(ARCH).o
BSF_ORX_TARGET  = $(BLD_PATH)/$(BSF_SONAME)-$(BITNESS)-$(ARCH)
BSF4REXX	= $(BLD_PATH)/$(BSF_SONAME)

# *************************************************************************** #
# Rules									      #
# *************************************************************************** #
# .cc.o:
#	  $(cc) $(CFLAGS) -o$*.o $(INC_PATH) $(LIB_PATH) $*.cc

#---------------------------------------------------------- Generate library

orexx :                $(BSF_ORX_TARGET)
#---------------------------------------------------------- END section

# Object Rexx ----------------------------------------------
$(BSF_ORX_TARGET) : $(BSF_SRC)
	echo
	echo Building $@ ...
	echo

	$(cc) $(CFLAGS) $(CFLAGS_ORX) -o$(BSF_OBJ_ORX) $(INC_PATH) $(INC_PATH_ORX) $(LIB_PATH) $(BSF_SRC)

# rgf, 2017-01-26: "-m32" a.k.a. "-m$(BITNESS)" was needed to successfully link a 32-bit .o file on a 64-bit system
#       $(link) -m$(BITNESS) -Wl,-soname,$(BSF_SONAME) -o $@ $(BSF_OBJ_ORX) $(LIB_PATH) $(LINK_FLAGS) $(LINK_ORX_FLAGS)

#	$(link) -Wl,-soname,$(BSF_SONAME)                                     -o $@ $(BSF_OBJ_ORX) $(LIB_PATH) $(LINK_FLAGS) $(LINK_ORX_FLAGS)

# newer gcc/ld: have to add "--disable-new-dtags"
	$(link) -Wl,--disable-new-dtags,-soname,$(BSF_SONAME),-rpath,$(RPATH) -o $@ $(BSF_OBJ_ORX) $(LIB_PATH) $(LINK_FLAGS) $(LINK_ORX_FLAGS)

	$(copy_cmd) $(BSF_ORX_TARGET) $(BSF4REXX)
	$(copy_cmd) $(BSF_ORX_TARGET) $(TGT_LIB_DIR)


#---------------------------------------------------------- END section
# builds all
rebuild: clean clean_dll orexx

#---------------------------------------------------------- END section
# just clean up the mess, just leave the dll
clean:
	- $(del_cmd) $(BLD_PATH)/*.o

#---------------------------------------------------------- END section
clean_dll:
	- $(del_cmd) $(BLD_PATH)/*.so*


# -------------------------------------------------------------------
# builds all
all: clean orexx

