# make -f Makefile-64-s390x rebuild
#-------------------------------------------------------------------------
# Makefile-64-linux: makefile to build libBSF4ooRexx850.so-64-amd64
#
# ooRexx 5.x, 2005-06-07, 2009-09-08, 2017-01-08, 2022-09-21
#
# make -f Makefile-64-s390x [ 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: use JAVA_HOME, rename to *BSF4ooRexx850*
#

# ------------------------ Apache Version 2.0 license -------------------------
#    Copyright (C) 2001-2022 Rony G. Flatscher, 2016 Paul Dunkley
#
#    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

# JDK_BASE = /usr/lib/jvm/java-6-openjdk-amd64

# 2016-04-05
#JDK_BASE = /opt/ibm/java-s390x-80
# JDK_BASE = /opt/ibm/ibm-java-s390x-80
# use JAVA_HOME
JDK_BASE = $(JAVA_HOME)

#INC_PATH = -I. -I/usr/local/include -I$(JDK_BASE)/include -I$(JDK_BASE)/include/linux -I/usr/include
INC_PATH = -I.  -I./include -I/usr/local/include -I$(JDK_BASE)/include -I$(JDK_BASE)/include/linux -I/usr/include

#INC_PATH_ORX = -I/usr/include $(INC_PATH)
INC_PATH_ORX = -I/usr/lib64 -I/usr/include/rpcsvc $(INC_PATH)

#LIB_PATH = -L. -L/usr/local/lib64 -L/usr/lib64 -L/usr/lib
LIB_PATH = -L. -L/usr/local/lib64 -L/usr/local/lib -L/usr/lib64 -L/usr/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

BITNESS         = 64
ARCH            = s390x

#
# CFLAGS_ori     = -c -ggdb -D__cplusplus
CFLAGS		=  -fPIC -c -O3 -D__cplusplus -DUNIX -m$(BITNESS) -DBSF4REXX_$(BITNESS)_BIT
CFLAGS_ORX      = -DUSE_OREXX

# LINK_FLAGS      = -shared -rdynamic -lpthread $(JDK_BASE)/jre/lib/i386/client/libjvm.so -lc  -lm
# LINK_FLAGS      = --export-dynamic -shared -rdynamic -lpthread $(JDK_BASE)/jre/bin/classic/libjvm.so -lc  -lm
LINK_FLAGS      =  -shared -rdynamic -lpthread $(JDK_BASE)/jre/lib/s390x/j9vm/libjvm.so -lc  -lm

# rgf, 2017-01-26: "-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)

# 	$(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*


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

