# The olsr.org Optimized Link-State Routing daemon (olsrd)
#
# (c) by the OLSR project
#
# See our Git repository to find out who worked on this file
# and thus is a copyright holder on it.
#
# All rights reserved.
#
# 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 olsr.org, olsrd 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.
#
# Visit http://www.olsr.org for more information.
#
# If you find this software useful feel free to make a donation
# to the project. For more information see the website or contact
# the copyright holders.
#

#
# Steps to compile for Android:
#
# 1) Download and extract the Android NDK from
#    http://developer.android.com/sdk/ndk/index.html
#    (e.g. into to /opt, otherwise call 'make NDK_BASE=')
#
# 2) Compile olsrd with the make command:
#    make OS=android DEBUG=0 NDK_BASE=/opt/android-ndk-r10
#
# 3) Install olsrd on your local PC, e.g.
#    sudo make OS=android DEBUG=0 install_all
#    which creates a /data directory on your PC.
#
# 4) Change /data/local/etc/olsrd/olsrd.conf. You need
#    to adapt the 'Interfaces' line, e.g. to use
#    the eth0 on your android mobile. Also, the
#    LoadPlugin lines needs adaption, e.g. you
#    need "/data/local/lib/olsrd_txtinfo.so.1.1"
#
# 5) Copy all file from /data to your mobile, e.g.
#    by pushing the files with the 'adb' tool.
#

#
# LINUX SPECIFIC CONFIGURATION
#

LDCONFIG ?= /sbin/ldconfig

DESTDIR ?=
prefix ?=
sbindir ?= $(prefix)/bin
etcdir ?= $(prefix)/etc/olsrd
libdir ?= $(prefix)/lib
datarootdir ?= $(prefix)/usr/share

# the rest are defined in Makefile.inc, but we don't need to override defaults
SBINDIR = $(DESTDIR)$(sbindir)
ETCDIR = $(DESTDIR)$(etcdir)
LIBDIR = $(DESTDIR)$(libdir)
SHAREDIR = $(DESTDIR)$(datarootdir)

# there probably should be an Android log.c and misc.c to support
# Logcat, but this works for now
SRCS += $(sort $(wildcard src/unix/*.c src/linux/*.c))
HDRS += $(sort $(wildcard src/unix/*.h src/linux/*.h))

CPPFLAGS +=	-DOLSRD_GLOBAL_CONF_FILE=\"$(CFGFILE)\"
# bionic libc: missing declarations
CPPFLAGS +=	-DINET_ADDRSTRLEN=16
CPPFLAGS +=	-D'IPTOS_PREC(tos)=((tos)&0xe0)'
CPPFLAGS +=	-D'IPTOS_TOS(tos)=((tos)&0x1e)'
# Android NDK is missing some Linux headers and regex, we have them here:
CPPFLAGS += -I$(TOPDIR)/android

# Compilation flags from build/toolchains/arm-eabi-4.2.1/setup.mk
#  * removed -fstrict-aliasing since the code has a lot of type-punning
CPPFLAGS +=  \
	-march=armv5te -mtune=xscale \
	-msoft-float -fpic \
	-mthumb-interwork \
	-ffunction-sections \
	-funwind-tables \
	-fstack-protector \
	-fno-short-enums \
	-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ \
	-D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__
ifeq ($(DEBUG),0)
CPPFLAGS +=  \
	-fomit-frame-pointer \
	-funswitch-loops \
	-finline-limit=300
else
CPPFLAGS += \
	-fno-omit-frame-pointer
endif

ifdef OLSRD_PLUGIN
GENERATE_PIC = true
endif

LDFLAGS += -llog

ifdef GENERATE_PIC
CFLAGS +=	-fPIC
LDFLAGS +=	-fPIC
endif

PLUGIN_SONAME ?= $(PLUGIN_NAME)
PLUGIN_FULLNAME ?= $(PLUGIN_NAME).so.$(PLUGIN_VER)
INSTALL_LIB =	install -D -m 755 $(PLUGIN_FULLNAME) $(LIBDIR)/$(PLUGIN_FULLNAME); \
		$(LDCONFIG) -n $(LIBDIR)
UNINSTALL_LIB =	rm -f $(LIBDIR)/$(PLUGIN_FULLNAME); \
		$(LDCONFIG) -n $(LIBDIR)

# include compiler settings
include $(TOPDIR)/make/Makefile.android.compiler

ifeq ($(DEBUG),0)
STRIP = $(CROSS_COMPILE)strip
endif

# Android didn't add regex until android-8 so include this one
REGEX_LIB = $(TOPDIR)/android/regex/libregex.a
REGEX_CLEAN = regex_clean
$(REGEX_LIB):
	$(MAKECMDPREFIX)$(MAKE) MAKECMDPREFIX="$(MAKECMDPREFIX)" -C $(TOPDIR)/android/regex/ REGEX_LIB=$(REGEX_LIB) VERBOSE=$(VERBOSE) \
		NDK_TOOLCHAIN_BASE="$(NDK_TOOLCHAIN_BASE)" \
		NDK_PLATFORM_VERSION="$(NDK_PLATFORM_VERSION)" \
		NDK_SYSROOT="$(NDK_SYSROOT)" \
		CROSS_COMPILE="$(CROSS_COMPILE)" \
		CC="$(CC)" LD="$(LD)" AR="$(AR)" STRIP="$(STRIP)" \
		CPPFLAGS="$(CPPFLAGS)"

$(REGEX_CLEAN):
	$(MAKECMDPREFIX)$(MAKE) MAKECMDPREFIX="$(MAKECMDPREFIX)" -C $(TOPDIR)/android/regex/ clean REGEX_LIB=$(REGEX_LIB)

# Local Variables:
# mode: makefile
# End:
