include VERSION

ifndef JAVA_HOME
$(error Set JAVA_HOME environment variable)
endif

libjdbc  := $(wildcard lib/jdbc-*.jar)

JAVA  := "$$JAVA_HOME/bin/java"
JAVAC := "$$JAVA_HOME/bin/javac" 
JAVAH := "$$JAVA_HOME/bin/javah"

TARGET := target
OSINFO_CLASS := org.sqlite.OSInfo
OSINFO_PROG := lib/org/sqlite/util/OSInfo.class

## building OSInfo.java 
#$(info compiling OSInfo.java)
#$(shell mkdir -p lib)
#$(shell $(JAVAC) src/main/java/org/sqlite/util/OSInfo.java -d lib)

OS_NAME := $(shell $(JAVA) -cp lib $(OSINFO_CLASS) --os)
OS_ARCH := $(shell $(JAVA) -cp lib $(OSINFO_CLASS) --arch)
LIB_FOLDER := $(shell $(JAVA) -cp lib $(OSINFO_CLASS))

# Windows uses different path separators, because they hate me
ifeq ($(OS_NAME),Windows)
	sep := ;
else
	sep := :
endif

sqlite := sqlite-$(version)

jni_h := $(shell find -L "$(JAVA_HOME)" -name jni.h)
ifneq ($(jni_h),)
jni_include := $(shell dirname "$(jni_h)")
endif

jni_md := $(shell find -L "$(JAVA_HOME)" -name jni_md.h)
ifneq ($(jni_md),)
jni_md_include := $(shell dirname "$(jni_md)")
endif


# os=Default is meant to be generic unix/linux

known_targets := Linux-x86 Linux-x86_64 Linux-arm Linux-armhf Mac-x86 Mac-x86_64 FreeBSD-x86_64 OpenBSD-x86_64 Windows-x86 Windows-x86_64 SunOS-sparcv9
target := $(OS_NAME)-$(OS_ARCH)

ifeq (,$(findstring $(strip $(target)),$(known_targets)))
  target := Default
endif

# cross-compilation toolchain prefix (e.g. "arm-linux-gnueabi-")
CROSS_PREFIX :=

Default_CC        := $(CROSS_PREFIX)gcc
Default_STRIP     := $(CROSS_PREFIX)strip
Default_CCFLAGS   := -I$(JAVA_HOME)/include -Ilib/inc_linux -Os -fPIC -fvisibility=hidden
Default_LINKFLAGS := -shared
Default_LIBNAME   := libsqlitejdbc.so
Default_SQLITE_FLAGS  := 

Linux-x86_CC        := $(CROSS_PREFIX)gcc
Linux-x86_STRIP     := $(CROSS_PREFIX)strip
Linux-x86_CCFLAGS   := -I$(JAVA_HOME)/include -Ilib/inc_linux -O2 -fPIC -m32 -fvisibility=hidden
Linux-x86_LINKFLAGS := -shared -static-libgcc
Linux-x86_LIBNAME   := libsqlitejdbc.so
Linux-x86_SQLITE_FLAGS  := 

Linux-x86_64_CC        := $(CROSS_PREFIX)gcc
Linux-x86_64_STRIP     := $(CROSS_PREFIX)strip
Linux-x86_64_CCFLAGS   := -Ilib/inc_linux -I$(JAVA_HOME)/include -O2 -fPIC -m64 -fvisibility=hidden
Linux-x86_64_LINKFLAGS := -shared -static-libgcc
Linux-x86_64_LIBNAME   := libsqlitejdbc.so
Linux-x86_64_SQLITE_FLAGS  := 

Linux-arm_CC          := $(CROSS_PREFIX)gcc
Linux-arm_STRIP       := $(CROSS_PREFIX)strip
Linux-arm_CCFLAGS     := -I$(JAVA_HOME)/include -Ilib/inc_linux -O2 -mfloat-abi=softfp -mfpu=vfp -fPIC -fvisibility=hidden
Linux-arm_LINKFLAGS   := -shared -static-libgcc
Linux-arm_LIBNAME     := libsqlitejdbc.so
Linux-arm_SQLITE_FLAGS  :=

Linux-armhf_CC        := $(CROSS_PREFIX)gcc
Linux-armhf_STRIP     := $(CROSS_PREFIX)strip
Linux-armhf_CCFLAGS   := -I$(JAVA_HOME)/include -Ilib/inc_linux -O2 -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden
Linux-armhf_LINKFLAGS := -shared -static-libgcc
Linux-armhf_LIBNAME   := libsqlitejdbc.so
Linux-armhf_SQLITE_FLAGS  := 

FreeBSD-x86_64_CC        := $(CROSS_PREFIX)cc
FreeBSD-x86_64_STRIP     := $(CROSS_PREFIX)strip
FreeBSD-x86_64_CCFLAGS   := -I$(JAVA_HOME)/include -Ilib/inc_linux -O2 -fPIC -fvisibility=hidden
FreeBSD-x86_64_LINKFLAGS := -shared
FreeBSD-x86_64_LIBNAME   := libsqlitejdbc.so
FreeBSD-x86_64_SQLITE_FLAGS  :=

OpenBSD-x86_64_CC        := $(CROSS_PREFIX)gcc
OpenBSD-x86_64_STRIP     := $(CROSS_PREFIX)strip
OpenBSD-x86_64_CCFLAGS   := -I$(JAVA_HOME)/include -Ilib/inc_linux -O2 -fPIC -fvisibility=hidden
OpenBSD-x86_64_LINKFLAGS := -shared
OpenBSD-x86_64_LIBNAME   := libsqlitejdbc.so
OpenBSD-x86_64_SQLITE_FLAGS  := 

SunOS-sparcv9_CC        := $(CROSS_PREFIX)gcc
SunOS-sparcv9_STRIP     := $(CROSS_PREFIX)strip
SunOS-sparcv9_CCFLAGS   := -I$(JAVA_HOME)/include -Ilib/inc_linux -O2 -fPIC -m64 -fvisibility=hidden
SunOS-sparcv9_LINKFLAGS := -shared -static-libgcc
SunOS-sparcv9_LIBNAME   := libsqlitejdbc.so
SunOS-sparcv9_SQLITE_FLAGS  := 

Mac-x86_CC        := gcc -arch i386
Mac-x86_STRIP     := strip -x
Mac-x86_CFLAGS    := -I$(JAVA_HOME)/include -O2 -fPIC -mmacosx-version-min=10.4 -fvisibility=hidden
Mac-x86_LINKFLAGS := -dynamiclib
Mac-x86_LIBNAME   := libsqlitejdbc.jnilib
Mac-x86_SQLITE_FLAGS  := -DSQLITE_ENABLE_LOCKING_STYLE=0

Mac-x86_64_CC        := gcc -arch $(OS_ARCH) 
Mac-x86_64_STRIP     := strip -x
MAC_SDK := /Developer/SDKs/MacOSX10.10.sdk
ifeq ($(wildcard MAC_SDK),)
	MAC_SDK := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk
endif
Mac-x86_64_CFLAGS    := -I$(MAC_SDK)/System/Library/Frameworks/JavaVM.framework/Headers -O2 -fPIC -mmacosx-version-min=10.5 -fvisibility=hidden
Mac-x86_64_LINKFLAGS := -dynamiclib 
Mac-x86_64_LIBNAME   := libsqlitejdbc.jnilib
Mac-x86_64_SQLITE_FLAGS  := 

Windows-x86_CC           := i686-w64-mingw32-gcc
Windows-x86_STRIP        := strip
Windows-x86_CCFLAGS      := -D_JNI_IMPLEMENTATION_ -Ilib/inc_win -O2
Windows-x86_LINKFLAGS    := -Wl,--kill-at -shared
Windows-x86_LIBNAME      := sqlitejdbc.dll
Windows-x86_SQLITE_FLAGS := 

Windows-x86_64_CC          := x86_64-w64-mingw32-gcc 
Windows-x86_64_STRIP        := x86_64-w64-mingw32-strip
Windows-x86_64_CCFLAGS     := -D_JNI_IMPLEMENTATION_ -Ilib/inc_win -O2
Windows-x86_64_LINKFLAGS    := -Wl,--kill-at -shared
Windows-x86_64_LIBNAME      := sqlitejdbc.dll
Windows-x86_64_SQLITE_FLAGS := 


CC        := $($(target)_CC)
STRIP     := $($(target)_STRIP)
CCFLAGS   := $($(target)_CCFLAGS) 
LINKFLAGS := $($(target)_LINKFLAGS) 
LIBNAME   := $($(target)_LIBNAME)
SQLITE_FLAGS := $($(target)_SQLITE_FLAGS)
SQLITE_AMAL_PREFIX = sqlite-amalgamation-$(shell ./amalgamation_version.sh $(version))
CCFLAGS := $(CCFLAGS) 
ifneq ($(jni_include),)
CCFLAGS := $(CCFLAGS) -I"$(jni_include)"
endif
ifneq ($(jni_md_include),)
CCFLAGS := $(CCFLAGS) -I"$(jni_md_include)"
endif

ifeq ($(OS_NAME),Windows)
    NATIVE32_DLL = win32
else ifeq ($(OS_NAME),Linux)
    ifeq ($(OS_ARCH),arm)
        NATIVE32_DLL = linux-arm
    else ifeq ($(OS_ARCH),armhf)
        NATIVE32_DLL = linux-armhf
    else
        NATIVE32_DLL = linux32
    endif
else ifeq ($(OS_NAME),Mac)
    NATIVE32_DLL = mac32
endif
