blob: 4b2825e072e7f84b7535ba9e16c0bcc6e9cf15a3 [file] [log] [blame]
include(CMakeParseArguments) # CMake 2.8 - 3.4 compatibility
set( _MY_DIR ${CMAKE_CURRENT_LIST_DIR})
# This function will use (and build if asked to) a static library target called 'gamesdk'.
# The location of the library is set according to your ANDROID_NDK_REVISION
# and ANDROID_PLATFORM, unless you explicitly set ANDROID_NDK_VERSION and/or
# ANDROID_SDK_VERSION arguments.
#
# All supported arguments are:
# PACKAGE_DIR: where the packaged version of the library is (relative to the caller working directory).
# This parameter is mandatory.
# ROOT_DIR: where the gamesdk directory is located (relative to the caller working directory), when building from sources.
# This must be specified if DO_LOCAL_BUILD is set
# GEN_TASK: the gradle task to build the package, when building from sources.
# This must be specified if DO_LOCAL_BUILD is set.
# DO_LOCAL_BUILD: whether to add a custom build command to build the gamesdk (ON/OFF).
# default value: OFF
# ANDROID_NDK_VERSION: override the version number for the NDK (major.minor.patch).
# It's recommended to use `ndkVersion` in your build.gradle file if you want to use a specific NDK.
# default value: derived from ANDROID_NDK_REVISION.
# ANDROID_API_LEVEL: override the android API level.
# It's recommended to change the SDK version in your build.gradle to use a specific SDK.
# default value: derived from ANDROID_PLATFORM.
# BUILD_TYPE: type of Game SDK build libraries to use. Can be "Release" or "Debug".
# default value: Release
function(add_gamesdk_target)
set(options DO_LOCAL_BUILD)
set(oneValueArgs GEN_TASK PACKAGE_DIR ROOT_DIR ANDROID_NDK_VERSION ANDROID_API_LEVEL BUILD_TYPE)
cmake_parse_arguments(GAMESDK "${options}" "${oneValueArgs}" "" ${ARGN} )
# Make sanity checks to avoid hard to debug errors at compile/link time.
if(NOT DEFINED GAMESDK_PACKAGE_DIR)
message(FATAL_ERROR "You must specify PACKAGE_DIR when calling add_gamesdk_target. It should be the folder where gamesdk is extracted (or where packages should be built, if compiling from sources).")
endif()
get_filename_component(INCLUDE_FULL_PATH "${GAMESDK_PACKAGE_DIR}/include/" REALPATH)
if (NOT GAMESDK_DO_LOCAL_BUILD AND NOT EXISTS ${INCLUDE_FULL_PATH})
message(FATAL_ERROR "Can't find the gamesdk includes in ${INCLUDE_FULL_PATH}. Are you sure you properly set up the path to the Game SDK using GAMESDK_PACKAGE_DIR?")
endif()
if (GAMESDK_DO_LOCAL_BUILD AND NOT DEFINED GAMESDK_GEN_TASK)
message(FATAL_ERROR "You specified DO_LOCAL_BUILD to build the game sdk from sources, but did not specified the Gradle task to run with GEN_TASK.")
endif()
if(GAMESDK_DO_LOCAL_BUILD AND NOT DEFINED GAMESDK_ROOT_DIR)
message(FATAL_ERROR "You specified DO_LOCAL_BUILD to build the game sdk from sources, but did not specified the gamesdk root folder with ROOT_DIR (used to run Gradle).")
endif()
if(NOT DEFINED GAMESDK_BUILD_TYPE)
set(GAMESDK_BUILD_TYPE "${CMAKE_BUILD_TYPE}")
endif()
# Infer Android SDK/NDK and STL versions
if (NOT DEFINED GAMESDK_ANDROID_NDK_VERSION)
set(GAMESDK_ANDROID_NDK_VERSION ${ANDROID_NDK_REVISION})
endif()
string(REGEX REPLACE "^([^.]+).*" "\\1" GAMESDK_ANDROID_NDK_MAJOR_VERSION ${GAMESDK_ANDROID_NDK_VERSION} ) # Get NDK major version
if (NOT DEFINED GAMESDK_ANDROID_API_LEVEL)
string(REGEX REPLACE "^android-([^.]+)" "\\1" GAMESDK_ANDROID_API_LEVEL ${ANDROID_PLATFORM} )
endif()
string(REPLACE "+" "p" GAMESDK_ANDROID_STL ${ANDROID_STL}) # Game SDK build names use a sanitized STL name (c++ => cpp)
# Set up the "gamesdk" library
set(BUILD_NAME ${ANDROID_ABI}_API${GAMESDK_ANDROID_API_LEVEL}_NDK${GAMESDK_ANDROID_NDK_MAJOR_VERSION}_${GAMESDK_ANDROID_STL}_${GAMESDK_BUILD_TYPE})
set(GAMESDK_LIB_DIR "${GAMESDK_PACKAGE_DIR}/libs/${BUILD_NAME}")
include_directories( "${GAMESDK_PACKAGE_DIR}/include" ) # Games SDK Public Includes
get_filename_component(DEP_LIB "${GAMESDK_LIB_DIR}/libgamesdk.a" REALPATH)
add_library(gamesdk STATIC IMPORTED GLOBAL)
if(GAMESDK_DO_LOCAL_BUILD)
# Get the absolute path for the root dir, otherwise it can't be used as a working directory for commands.
get_filename_component(GAMESDK_ROOT_DIR "${GAMESDK_ROOT_DIR}" REALPATH)
# If building from a project containing local.properties, generated by Android Studio with
# the local Android SDK and NDK paths, copy it to gamesdk to allow it to build with the local
# toolchain.
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../local.properties")
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../local.properties
DESTINATION ${GAMESDK_ROOT_DIR})
endif()
# Build Game SDK (Gradle will use local.properties to find the Android SDK/NDK,
# or the environment variables if no local.properties - i.e: if compiling from command line).
if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
file(TO_NATIVE_PATH "${GAMESDK_ROOT_DIR}/gradlew.bat" GAMESDK_GRADLE_BIN)
else()
file(TO_NATIVE_PATH "${GAMESDK_ROOT_DIR}/gradlew" GAMESDK_GRADLE_BIN)
endif()
add_custom_command(
OUTPUT
${DEP_LIB}
COMMAND
${CMAKE_COMMAND} -E env ANDROID_NDK_REVISION=${GAMESDK_ANDROID_NDK_VERSION} ${GAMESDK_GRADLE_BIN} ${GAMESDK_GEN_TASK} -PGAMESDK_ANDROID_API_LEVEL=${GAMESDK_ANDROID_API_LEVEL} -PGAMESDK_BUILD_TYPE=${GAMESDK_BUILD_TYPE}
VERBATIM
WORKING_DIRECTORY
"${GAMESDK_ROOT_DIR}"
)
add_custom_target(gamesdk_lib DEPENDS ${DEP_LIB})
add_dependencies(gamesdk gamesdk_lib)
else()
# Sanity check that the library file exists
if(NOT EXISTS ${DEP_LIB})
message(FATAL_ERROR "Can't find the gamesdk library in ${DEP_LIB}. Are you sure you are using a supported Android SDK/NDK and STL variant?")
endif()
endif()
# Set the gamesdk target to use the library (see https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Exporting-and-Importing-Targets)
set_target_properties(gamesdk PROPERTIES IMPORTED_LOCATION ${DEP_LIB})
endfunction()
# Use this function in addition to add_gamesdk_target, to integrate GameSDK
# sources to your project - allowing the IDE to provide autocompletions and
# debugging.
function(add_gamesdk_sources)
add_subdirectory("${_MY_DIR}/../src" "../../../../out")
endfunction()