Created
October 1, 2018 23:17
-
-
Save raffienficiaud/6fca46cdfc79f37395121dcf259f8fce to your computer and use it in GitHub Desktop.
A helper CMake file for iOS for specifying the CODESIGN identity and PROVISIONING profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================================ | |
# Copyright 2018, Raffi Enficiaud. | |
# All rights reserved. | |
# | |
# Distributed under the OSI-approved Boost Software License 1.0. See | |
# https://www.boost.org/users/license.html for details. | |
# | |
# ============================================================================ | |
# This file contains several utilities for iOS developments | |
# It should be included only for iOS | |
#.rst: | |
# .. command:: xcode_target_code_sign | |
# | |
# This function adds the necessary properties to a designated target in order | |
# to ensure its proper signing by Xcode. | |
# | |
# :: | |
# | |
# xcode_target_code_sign( | |
# TARGET target | |
# [PRODUCT_BUNDLE_IDENTIFIER identifier] | |
# [CODESIGN_IDENTITY signing_identity] | |
# [PROVISIONING_PROFILE provisioning_profile]) | |
# | |
# * ``TARGET`` the target to which we need to apply the code signing/provisioning. | |
# Calling this function on tqrget of ``STATIC_LIBRARY`` type has no effect. | |
# * ``PRODUCT_BUNDLE_IDENTIFIER`` indicates the bundle identifier. This value identifies the application | |
# with an ID that is **required** to match with the one provided by the ``CODESIGN_IDENTITY``. | |
# It modifies the property ``MACOSX_BUNDLE_GUI_IDENTIFIER`` | |
# * ``CODESIGN_IDENTITY``: indicates the identity to be used for signing. This usually involves | |
# the existence of a public/private key pair in the keychain to which Xcode has access. | |
# Defaults to ``CMAKE_XCODE_CODESIGN_IDENTITY`` if not provided. A warning is emitted | |
# by ``cmake`` if the finally computed ``CODESIGN_IDENTITY`` is empty when the target | |
# requires signing. | |
# * ``PROVISIONING_PROFILE`` the provisining profile for the target: this is applicable only | |
# to targets | |
# | |
# .. note:: | |
# | |
# Application signing is required for running an application on a real device. It /may/ | |
# be required for running applications on a simulator. | |
# Currently only ``XCODE`` generator is supported. | |
# | |
# .. todo:: | |
# | |
# Change to accept several targets | |
function(xcode_target_code_sign ) | |
if(NOT IOS) | |
message(FATAL_ERROR "Not compiling on iOS") | |
endif() | |
if(NOT XCODE) | |
message(FATAL_ERROR "Codesign is currently supported only on Xcode generators") | |
endif() | |
set(options) | |
set(oneValueArgs TARGET PRODUCT_BUNDLE_IDENTIFIER CODESIGN_IDENTITY PROVISIONING_PROFILE) | |
set(multiValueArgs) | |
set(prefix _xcode_target_code_sign) | |
cmake_parse_arguments(PARSE_ARGV 0 ${prefix} "${options}" "${oneValueArgs}" "${multiValueArgs}" ) | |
if("${${prefix}_TARGET}" STREQUAL "") | |
message(FATAL_ERROR "'TARGET' argument is required for xcode_target_code_sign") | |
endif() | |
if(NOT TARGET "${${prefix}_TARGET}") | |
message(FATAL_ERROR "Target '${${prefix}_TARGET}' cannot be found") | |
endif() | |
get_target_property(target_type "${${prefix}_TARGET}" TYPE) | |
# none of this applies to static libraries. | |
if("${target_type}" STREQUAL "STATIC_LIBRARY") | |
message(WARNING "Setting code sign for a static library is not supported") | |
return() | |
endif() | |
# signing identity | |
if("${${prefix}_CODESIGN_IDENTITY}" STREQUAL "") | |
if(NOT ("${CMAKE_XCODE_CODESIGN_IDENTITY}" STREQUAL "")) | |
set(${prefix}_CODESIGN_IDENTITY "${CMAKE_XCODE_CODESIGN_IDENTITY}") | |
elseif("${IOS_PLATFORM}" STREQUAL "OS") | |
message(WARNING "Signing is required for iOS devices. Please indicate the " | |
"signing identity with the variable CMAKE_XCODE_CODESIGN_IDENTITY" | |
"or in the variable 'CODE_SIGN_IDENTITY' of the generated XCode project settings") | |
endif() | |
endif() | |
if(NOT ("${${prefix}_CODESIGN_IDENTITY}" STREQUAL "")) | |
set_target_properties(${${prefix}_TARGET} | |
PROPERTIES | |
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "${${prefix}_CODESIGN_IDENTITY}" | |
XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED ON | |
) | |
endif() | |
# indicates the bundle ID | |
if(NOT ("${${prefix}_PRODUCT_BUNDLE_IDENTIFIER}" STREQUAL "")) | |
set_target_properties(${${prefix}_TARGET} | |
PROPERTIES | |
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "${${prefix}_PRODUCT_BUNDLE_IDENTIFIER}" | |
) | |
endif() | |
# provisioning profile / only for executables | |
if("${target_type}" STREQUAL "EXECUTABLE") | |
if("${${prefix}_PROVISIONING_PROFILE}" STREQUAL "") | |
if(NOT ("${CMAKE_XCODE_PROVISIONING_PROFILE}" STREQUAL "")) | |
set(${prefix}_PROVISIONING_PROFILE "${CMAKE_XCODE_PROVISIONING_PROFILE}") | |
elseif("${IOS_PLATFORM}" STREQUAL "OS") | |
message(WARNING "A provisioning profile is required for signing applications for iOS devices. " | |
"Please indicate the profile with the variable CMAKE_XCODE_PROVISIONING_PROFILE" | |
"or in the 'PROVISIONING_PROFILE_SPECIFIER' of the generated XCode project settings") | |
endif() | |
endif() | |
if(NOT ("${${prefix}_PROVISIONING_PROFILE}" STREQUAL "")) | |
set_target_properties("${${prefix}_TARGET}" | |
PROPERTIES | |
XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER "${${prefix}_PROVISIONING_PROFILE}" | |
) | |
endif() | |
endif() | |
# fixing the rpath stuff of the build | |
set_target_properties("${${prefix}_TARGET}" | |
PROPERTIES | |
BUILD_WITH_INSTALL_RPATH TRUE | |
INSTALL_RPATH "@executable_path/" | |
XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@executable_path/" | |
) | |
endfunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment