#
#
# Copyright 2016 Broadcom.
#
#  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.
#
#

#
# We need to set and export OPEN_ROOT to point to the
# root directory of the OpEN ADK installation, and then
# bring in the common.mak settings.
#
export OPEN_ROOT = ../..

export RUBY_H = ./ruby1.8.7
export SWIGVER=1.3.35

ifeq (1,$(BLD_L7_TARGETOS_LINUX_UB14))
export RUBY_H = ./ruby1.9.1
endif

ifeq (1,$(BLD_L7_TARGETOS_LINUX_UB16))
export RUBY_H = ./ruby2.2.0
endif

ifeq (1,$(BLD_L7_TARGETOS_LINUX_COS71))
export RUBY_H = ./ruby2.0.0
export SWIGVER=3.0.0
endif

include $(OPEN_ROOT)/swig/make/make.swig

#
# Simple rule for compiling our applications, no
# incremental steps.
#
all: OpEN.so
	$(NOOP)

openswig: OpEN.so
	$(NOOP)

OpEN.so: openwrap.i getwrappers.sh createstub.pl
	$(DBG) swig -DL7_NO_PRINTF_FMT=1 -w801 -ruby -o openwrapbase.c openwrap.i
	$(DBG) sh getwrappers.sh
	$(DBG) perl createstub.pl
	$(DBG) echo "void AppendWrappedFuncs();" > tmp.txt
	$(DBG) cat tmp.txt openwrapbase.c result.txt > openwraptmp.c
	$(DBG) sed "s/SWIG_RubyInitializeTrackings();/SWIG_RubyInitializeTrackings(); AppendWrappedFuncs();/" < openwraptmp.c > openwrap.c
	$(DBG) $(CROSS_COMPILE)$(CC) -I$(RUBY_H) -fPIC $(CFLAGS) $(CFLAGSEXTRA) -c openwrap.c
	$(DBG) $(CROSS_COMPILE)$(CC) -shared openwrap.o -o $@ $(LDFLAGS) $(LFLAGSEXTRA)

openwrap.i: genifile.pl
	$(DBG) perl genifile.pl

clean:
	$(DBG) rm -f *.o OpEN.so openwrap.c openwrapbase.c openwraptmp.c result.txt append.txt tmp.txt openwrap.i
