# To choose target architecture, variable TARGET_TYPE has to be set to
# an according value:
# 	- Linux (default)
#	- ARM
#
# For example: cmake -DCMAKE_BUILD_TYPE=Debug -DTARGET_TYPE=Linux -DPLATFORM=FC
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
PROJECT (inkdemo)
# will work with lower versions, but I set to the most recent at time of creation
cmake_minimum_required(VERSION 3.25)

SET (PLATFORM FC)

# I cloned the SDK via git to a directory I created called PBSDK on my PC
SET (TOOLCHAIN_PATH "../../PBSDK/SDK_6.3.0/SDK-B288/usr")
SET (TOOLCHAIN_PREFIX "arm-obreey-linux-gnueabi")
SET (TOOLCHAIN_INSTALL "sysroot/usr")

ADD_DEFINITIONS(-DPLATFORM_FC)
#SET (CMAKE_VERBOSE_MAKEFILE ON)

# building for ARM
SET (TARGET_TYPE ARM)
SET (CMAKE_BUILD_TYPE Release)

# change install prefix
SET(CMAKE_INSTALL_PREFIX "${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}/${TOOLCHAIN_INSTALL}" CACHE PATH "Install path prefix" FORCE)

# let's add the compiler for ARM
SET (CMAKE_C_COMPILER ${CMAKE_CURRENT_SOURCE_DIR}/${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}-gcc)
SET (CMAKE_CXX_COMPILER ${CMAKE_CURRENT_SOURCE_DIR}/${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}-g++)
SET (CMAKE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}-g++)
SET (CMAKE_ARR ${CMAKE_CURRENT_SOURCE_DIR}/${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}-arr)
SET (CMAKE_STRIP ${CMAKE_CURRENT_SOURCE_DIR}/${TOOLCHAIN_PATH}/bin/${TOOLCHAIN_PREFIX}-strip)
SET (TARGET_INCLUDE "")
SET (TARGET_LIB pthread inkview freetype z libdl.so.2)


# set needed compilation flags
MESSAGE (STATUS "Build for ARM Release")
SET (CMAKE_C_FLAGS_RELEASE "-DNDEBUG -s -fsigned-char -Wall -O2 -Wno-format-y2k -fomit-frame-pointer")
SET (CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -s -fsigned-char -Wall -O2 -Wno-format-y2k -fomit-frame-pointer")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ldl -lpthread")


SET (SRC_LIST
	${CMAKE_SOURCE_DIR}/src/main.c)
	
# output executable is "inkdemo" - rename the outputted executable with a .app ext if not using the "copy to pocketbook" section below
ADD_EXECUTABLE (inkdemo
		${SRC_LIST}) 


INCLUDE_DIRECTORIES(${TARGET_INCLUDE})
TARGET_LINK_LIBRARIES (inkdemo ${TARGET_LIB})

# copying to pocketbook after build (change path if needed - or delete?)
# targetfile is the path to the pocketbook over wifi
SET (targetfile "/run/user/1000/gvfs/ftp:host=10.42.0.14/mnt/ext1/system/bin")
add_custom_command(TARGET inkdemo POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:inkdemo> ${targetfile}/sh_ivtool >/dev/null || :
)
# end of copy to pocketbook section

INSTALL (TARGETS inkdemo DESTINATION bin)

