project(casablanca-ios NONE)
cmake_minimum_required(VERSION 3.9)

enable_testing()

set(LIB_CPPREST libcpprest.a)
set(LIB_CPPREST_LIB_DIR "${CMAKE_CURRENT_BINARY_DIR}/lib")

if (CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET)
  set (ENV{CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET} ${CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET})
endif()

set(TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/ios-cmake/ios.toolchain.cmake")

set(SIM_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/build.i386" CACHE INTERNAL "")
set(SIM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../Release" CACHE INTERNAL "")

set(SIM64_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/build.x86_64" CACHE INTERNAL "")
set(SIM64_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../Release" CACHE INTERNAL "")

set(ARM_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/build.arm" CACHE INTERNAL "")
set(ARM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../Release" CACHE INTERNAL "")

if (DISABLE_BITCODE)
  set (ENABLE_BITCODE_ARG -DENABLE_BITCODE=OFF)
endif()

if (INCLUDE_32BIT)
  set (IOS_PLATFORM_VALUE OS)
else()
  set (IOS_PLATFORM_VALUE OS64)
endif()

if (DEPLOYMENT_TARGET)
  set (DEPLOYMENT_TARGET -DIOS_DEPLOYMENT_TARGET=${DEPLOYMENT_TARGET})
endif()

add_test(NAME ios_runner
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../Release/tests/common/testrunner/ios
    COMMAND xcodebuild test -project ios_runner.xcodeproj -configuration=${CMAKE_BUILD_TYPE} -scheme ios_runner -destination "platform=iOS Simulator,name=iPhone 6" LIBRARY_SEARCH_PATH=${SIM64_BINARY_DIR}
  )

if (INCLUDE_32BIT)
set (SIM_BINARY_LIB ${SIM_BINARY_DIR}/Binaries/${CMAKE_BUILD_TYPE}/${LIB_CPPREST})
file(MAKE_DIRECTORY ${SIM_BINARY_DIR})
execute_process(WORKING_DIRECTORY ${SIM_BINARY_DIR}
  COMMAND ${CMAKE_COMMAND}
    -GXcode
    -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE}
    -DIOS_PLATFORM=SIMULATOR
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    "${DEPLOYMENT_TARGET}"
    "${SIM_SOURCE_DIR}"
)
else()
set (SIM_BINARY_LIB "")
endif()

file(MAKE_DIRECTORY ${SIM64_BINARY_DIR})
execute_process(WORKING_DIRECTORY ${SIM64_BINARY_DIR}
  COMMAND ${CMAKE_COMMAND}
    -GXcode
    -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE}
    -DIOS_PLATFORM=SIMULATOR64
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    "${DEPLOYMENT_TARGET}"
    "${SIM64_SOURCE_DIR}"
)

file(MAKE_DIRECTORY ${ARM_BINARY_DIR})
execute_process(WORKING_DIRECTORY ${ARM_BINARY_DIR}
  COMMAND ${CMAKE_COMMAND}
    -GXcode
    -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE}
    -DIOS_PLATFORM=${IOS_PLATFORM_VALUE}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    "${DEPLOYMENT_TARGET}"
    "${ENABLE_BITCODE_ARG}"    
    "${ARM_SOURCE_DIR}"
    )

if (INCLUDE_32BIT)
set (SIM_TARGET sim)
## Simulator i386 version
add_custom_target(sim
  COMMAND ${CMAKE_COMMAND}
    --build ${SIM_BINARY_DIR}
    --config ${CMAKE_BUILD_TYPE}
    COMMENT "Building for i386 (simulator)"
VERBATIM
)
else()
set (SIM_TARGET "")
endif()

## Simulator x86_64 version
add_custom_target(sim64
  COMMAND ${CMAKE_COMMAND}
    --build ${SIM64_BINARY_DIR}
    --config ${CMAKE_BUILD_TYPE}
    COMMENT "Building for x86_64 (simulator)"
VERBATIM
)

## ARM version
add_custom_target(arm
  COMMAND ${CMAKE_COMMAND}
    --build ${ARM_BINARY_DIR}
    --config ${CMAKE_BUILD_TYPE}
  COMMENT "Building for arm"
  VERBATIM
)

add_custom_command(
  OUTPUT ${LIB_CPPREST_LIB_DIR}/${LIB_CPPREST}
  COMMAND mkdir -p "${LIB_CPPREST_LIB_DIR}"
  COMMAND lipo -create
    -output "${LIB_CPPREST_LIB_DIR}/${LIB_CPPREST}"
    ${SIM_BINARY_LIB}
    ${SIM64_BINARY_DIR}/Binaries/${CMAKE_BUILD_TYPE}/${LIB_CPPREST}
    ${ARM_BINARY_DIR}/Binaries/${CMAKE_BUILD_TYPE}/${LIB_CPPREST}
  COMMAND cp -R "${CMAKE_CURRENT_SOURCE_DIR}/../Release/include" "${CMAKE_CURRENT_BINARY_DIR}"
  DEPENDS
    ${SIM_TARGET}
    sim64
    arm
    ${SIM_BINARY_LIB}
    "${SIM64_BINARY_DIR}/Binaries/${CMAKE_BUILD_TYPE}/${LIB_CPPREST}"
    "${ARM_BINARY_DIR}/Binaries/${CMAKE_BUILD_TYPE}/${LIB_CPPREST}"
  VERBATIM
)

add_custom_target(cpprest ALL DEPENDS "${LIB_CPPREST_LIB_DIR}/${LIB_CPPREST}")
