# make -f Makefile-32-linux rebuild
#-------------------------------------------------------------------------
# Makefile-32-linux: makefile to build libBSF4ooRexx850.so-32-x86
#
# ooRexx 5.x, 2005-06-07, 2009-09-08, 2017-01-08, 2022-09-21
#
# make -f Makefile-32-linux [ 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
#
#    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
# JDK_BASE = /usr/lib/jvm/java-6-openjdk

# Mac-Virtualbox Ubuntut 11.04 desktop i386
# JDK_BASE = /home/wu/jdk_rpm_bin/tmp/jdk1.6.0_10
# JDK_BASE = /usr/lib/jvm/java-6-oracle
# JDK_BASE = /usr/lib/jvm/java-6-openjdk
# JDK_BASE = ./java_include/lin_include_6
JDK_BASE = $(JAVA_HOME)
# 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

BITNESS         = 32
ARCH            = x86

#
CFLAGS		= -fPIC -c -O3 -D__cplusplus -DUNIX -m$(BITNESS) -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) -m$(BITNESS) -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

