Skip to content

Commit a882aa5

Browse files
jared-marsauGitHub Enterprise
authored andcommitted
Jared/sign in embed script (apple#37)
To more easily integrate into developer CI systems, deferring code signing until the generated Xcode project is being built is ideal. To support this, I've updated the shell script that embeds libraries to also code sign binaries when embedding. Importantly, this final code sign step will use the same code signing identity that developers configure the generated Xcode project to use. This ensures that the code signing identity used for the rest of the app is also used to sign the native libraries copied into the generated Xcode project by Apple.Core. So now developers can: 1. Build and sign libraries when they build with build.py, if they choose. This allows down stream consumers of the plug-ins to validate binary origin. 2. Consume plug-in libraries in their projects without having to pay attention to code signing identity; all libraries will automatically be re-signed with the code signing identity used by Xcode when building the generated Xcode project.
1 parent f0911d8 commit a882aa5

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

build.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from datetime import datetime
1010
from pathlib import Path
1111

12-
from scripts.python.upi_cli_argument_options import PluginID, PlatformID, ConfigID, BuildActionID, CleanActionID
12+
from scripts.python.upi_cli_argument_options import PluginID, PlatformID, ConfigID, BuildActionID, CleanActionID, CodeSignActionID
1313
from scripts.python.upi_build_context import BuildContext
1414
from scripts.python.upi_utility import PromptColor, Printer
1515

1616
# Set a script version to track evolution
17-
build_script_version = "2.2.0"
17+
build_script_version = "2.2.1"
1818

1919
# -----------------
2020
# Prompt Formatting
@@ -74,8 +74,7 @@
7474
argument_parser.add_argument("-m", "--platforms", dest="platform_list", nargs='*', default=[PlatformID.ALL], help=f"Selects the desired platforms to target when building native libraries. Possible values are: {PlatformID.IOS}, {PlatformID.IOS_SIMULATOR}, {PlatformID.MACOS}, {PlatformID.TVOS}, {PlatformID.TVOS_SIMULATOR}, {PlatformID.VISIONOS}, {PlatformID.VISIONOS_SIMULATOR}, {PlatformID.SIMULATORS}, {PlatformID.DEVICES} or {PlatformID.ALL}. Default is: {PlatformID.ALL}")
7575
argument_parser.add_argument("-b", "--build-action", dest="build_actions", nargs='*', default=[BuildActionID.BUILD, BuildActionID.PACK], help=f"Sets the build actions for the selected plug-ins. Possible values are: {BuildActionID.BUILD}, {BuildActionID.PACK}, {BuildActionID.NONE} or {BuildActionID.ALL}. Defaults are: {BuildActionID.BUILD}, {BuildActionID.PACK}")
7676
argument_parser.add_argument("-bc","--build-config", dest="build_config", default=ConfigID.ALL, help=f"Sets the build configuration to compile. Possible values are: {ConfigID.RELEASE}, {ConfigID.DEBUG}, or {ConfigID.ALL} which builds all other configs. Default is: {ConfigID.ALL}")
77-
argument_parser.add_argument("-c", "--codesign-identity", dest="codesign_identity", default=str(), help=f"String which uniquely identifies your codesign identity, typically represented by a hash. Only applied if build actions include {BuildActionID.BUILD}")
78-
argument_parser.add_argument("-sc", "--skip-codesign", dest="skip_codesign", action="store_true", help=f"Skips codesign and all user prompts.")
77+
argument_parser.add_argument("-c", "--codesign-identity", dest="codesign_identity", default=str(), help=f"Signs compiled native libraries with provided code signing identity hash or prompts the user to select from a list of identities on the system when {CodeSignActionID.PROMPT} is passed.")
7978
argument_parser.add_argument("-u", "--unity-installation-root", dest="unity_installation_root", default="", help="Root path to search for Unity installations when building tests. Note: performs a full recursive search of the given directory.")
8079
argument_parser.add_argument("-o", "--output-path", dest="output_path", default=CTX.build_output_path, help=f"Build result path for final packages. Default: {CTX.build_output_path}")
8180
argument_parser.add_argument("-k", "--clean-action", dest="clean_actions", nargs='*', default=[CleanActionID.NONE], help=f"Sets the clean actions for the selected plug-ins. Possible values are: {CleanActionID.NATIVE}, {CleanActionID.PACKAGES}, {CleanActionID.TESTS}, {CleanActionID.NONE}, or {CleanActionID.ALL}. Defaults to no clean action.")
@@ -117,14 +116,11 @@ def Main():
117116
f"\n Clean Actions({Printer.Bold('-k')}): {CTX.printer.Context(' '.join(build_args.clean_actions))}"
118117
f"\n Force Clean({Printer.Bold('-f')}): {CTX.printer.Context('Yes (-f set)' if build_args.force_clean else 'No (-f not set)')}"
119118
f"\n Build Tests({Printer.Bold('-t')}): {CTX.printer.Context('Yes (-t set)' if build_args.build_tests else 'No (-t not set)')}"
120-
f"\n Skip Codesign({Printer.Bold('-sc')}): {CTX.printer.Context('Yes (-sc set)' if build_args.skip_codesign else 'No (-sc not set)')}")
119+
f"\n Codesigning Identity({Printer.Bold('-c')}): {CTX.printer.Context(build_args.codesign_identity if len(build_args.codesign_identity) > 0 else 'None supplied.')}")
121120

122121
if len(build_args.unity_installation_root) > 0:
123122
print(f" Unity Installation Root({Printer.Bold('-u')}): {CTX.printer.Context(build_args.unity_installation_root)}")
124123

125-
if not build_args.skip_codesign:
126-
print(f" Codesigning Identity({Printer.Bold('-c')}): {CTX.printer.Context(build_args.codesign_identity if len(build_args.codesign_identity) > 0 else 'None supplied.')}")
127-
128124
if build_args.build_tests:
129125
print(f" Test Output Path({Printer.Bold('-to')}): {CTX.printer.Context(build_args.test_output_path)}")
130126

@@ -358,10 +354,11 @@ def Main():
358354
CTX.printer.MessageWithContext("Native library build using: ", f"Xcode {xcode_version} ({xcode_build_number})", "\n")
359355
CTX.printer.InfoMessage(f"If this is incorrect, please update your environment with {Printer.Bold('xcode-select')}. (Call \'{Printer.Bold('xcode-select -h')}\' from the command line for more info.)")
360356

361-
if build_args.skip_codesign:
362-
CTX.codesign_hash = ""
363-
else:
364-
CTX.codesign_hash = build_args.codesign_identity if len(build_args.codesign_identity) > 0 else toolchain.PromptForCodesignIdentity(CTX.printer)
357+
if len(build_args.codesign_identity) > 0:
358+
if build_args.codesign_identity == CodeSignActionID.PROMPT:
359+
CTX.codesign_hash = toolchain.PromptForCodesignIdentity(CTX.printer)
360+
else:
361+
CTX.codesign_hash = build_args.codesign_identity
365362

366363
CTX.printer.SectionHeading("Gather Unity Installation Info")
367364

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# CHANGELOG
22
All notable changes to this project will be documented in this file.
33

4+
## [3.1.2] - 2024-04-11
5+
### Added
6+
- *Embed Apple Plug-In Libraries* script injected into the Unity-generated Xcode project will now sign native libraries using the same codesign identity configured for that Xcode project.
7+
- This simplifies the codesign workflow substantially, now libraries won't need to be signed when running the build script.
8+
49
## [3.1.1] - 2024-04-08
510
### Added
611
- Adding support for tracking of Apple Unity plug-ins without native libraries.

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Editor/AppleBuild.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ public static void ProcessExportPlistOptions(AppleBuildProfile appleBuildProfile
375375
/// <returns></returns>
376376
private static string GenerateEmbedNativeLibraryShellScript(string projectRelativeNativeLibraryRoot)
377377
{
378-
string embedNativeLibraryShellScript = "# Apple Unity Plug-in Embed libraries shell script\n"
378+
string embedNativeLibraryShellScript = "# Apple Unity Plug-in Sign & Embed libraries shell script\n"
379379
+ "# Copyright © 2024 Apple, Inc. All rights reserved.\n"
380380
+ "# This script is added to the generated Xcode project by the Apple.Core plug-in.\n"
381-
+ "# Please see AppleNativeLibraryUtility.cs in the Apple.Core plug-in for more information.\n"
381+
+ "# Please see AppleBuild.cs in the Apple.Core plug-in for more information.\n"
382382
+ "dstFrameworkFolder=\"$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH\"\n"
383383
+ "dstBundleFolder=\"$BUILT_PRODUCTS_DIR/$PLUGINS_FOLDER_PATH\"\n"
384384
+ $"APPLE_PLUGIN_LIBRARY_ROOT=\"$PROJECT_DIR/{projectRelativeNativeLibraryRoot}\"\n"
@@ -393,13 +393,21 @@ private static string GenerateEmbedNativeLibraryShellScript(string projectRelati
393393
+ " echo \" Embedding Apple plug-in framework $filename\"\n"
394394
+ " echo \" Source: $item\"\n"
395395
+ " echo \" Destination: $dstFrameworkFolder/$filename\"\n"
396+
+ " if [ ! -z \"$EXPANDED_CODE_SIGN_IDENTITY\" ]; then\n"
397+
+ " echo \" Code signing identity: $EXPANDED_CODE_SIGN_IDENTITY\"\n"
398+
+ " codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --timestamp\\=none --generate-entitlement-der $item\n"
399+
+ " fi\n"
396400
+ " ditto $item \"$dstFrameworkFolder/$filename\"\n"
397401
+ " break\n"
398402
+ " elif [[ $item = *'.bundle' ]]; then\n"
399403
+ " filename=$(basename $item)\n"
400404
+ " echo \" Embedding Apple plug-in bundle $filename\"\n"
401405
+ " echo \" Source: $item\"\n"
402406
+ " echo \" Destination: $dstBundleFolder/$filename\"\n"
407+
+ " if [ ! -z \"$EXPANDED_CODE_SIGN_IDENTITY\" ]; then\n"
408+
+ " echo \" Code signing identity: $EXPANDED_CODE_SIGN_IDENTITY\"\n"
409+
+ " codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --timestamp\\=none --generate-entitlement-der $item\n"
410+
+ " fi\n"
403411
+ " ditto $item \"$dstBundleFolder/$filename\"\n"
404412
+ " break\n"
405413
+ " fi\n"
@@ -413,10 +421,10 @@ private static string GenerateEmbedNativeLibraryShellScript(string projectRelati
413421
+ " exit 1\n"
414422
+ "fi";
415423

416-
string debugEmbedNativeLibraryShellScript = "# Apple Unity Plug-in Embed libraries shell script\n"
424+
string debugEmbedNativeLibraryShellScript = "# Apple Unity Plug-in Sign & Embed libraries shell script\n"
417425
+ "# Copyright © 2024 Apple, Inc. All rights reserved.\n"
418426
+ "# This script is added to the generated Xcode project by the Apple.Core plug-in.\n"
419-
+ "# Please see AppleNativeLibraryUtility.cs in the Apple.Core plug-in for more information.\n"
427+
+ "# Please see AppleBuild.cs in the Apple.Core plug-in for more information.\n"
420428
+ "echo \"Debug Apple Unity Plug-in Embed libraries shell script: enhanced output\"\n"
421429
+ "echo \"***********************************************************************\"\n"
422430
+ "dstFrameworkFolder=\"$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH\"\n"
@@ -442,13 +450,21 @@ private static string GenerateEmbedNativeLibraryShellScript(string projectRelati
442450
+ " echo \" Embedding Apple plug-in framework $filename\"\n"
443451
+ " echo \" Source: $item\"\n"
444452
+ " echo \" Destination: $dstFrameworkFolder/$filename\"\n"
453+
+ " if [ ! -z \"$EXPANDED_CODE_SIGN_IDENTITY\" ]; then\n"
454+
+ " echo \" Code signing identity: $EXPANDED_CODE_SIGN_IDENTITY\"\n"
455+
+ " codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --timestamp\\=none --generate-entitlement-der $item\n"
456+
+ " fi\n"
445457
+ " ditto $item \"$dstFrameworkFolder/$filename\"\n"
446458
+ " break\n"
447459
+ " elif [[ $item = *'.bundle' ]]; then\n"
448460
+ " filename=$(basename $item)\n"
449461
+ " echo \" Embedding Apple plug-in bundle $filename\"\n"
450462
+ " echo \" Source: $item\"\n"
451463
+ " echo \" Destination: $dstBundleFolder/$filename\"\n"
464+
+ " if [ ! -z \"$EXPANDED_CODE_SIGN_IDENTITY\" ]; then\n"
465+
+ " echo \" Code signing identity: $EXPANDED_CODE_SIGN_IDENTITY\"\n"
466+
+ " codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --timestamp\\=none --generate-entitlement-der $item\n"
467+
+ " fi\n"
452468
+ " ditto $item \"$dstBundleFolder/$filename\"\n"
453469
+ " break\n"
454470
+ " fi\n"

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.apple.unityplugin.core",
33
"displayName": "Apple.Core",
44
"description": "Provides project settings, post-build automation tools, and other shared functionality for Apple Unity Plug-ins.",
5-
"version": "3.1.1",
5+
"version": "3.1.2",
66
"unity": "2022.3",
77
"keywords": [
88
"apple"

scripts/python/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CHANGELOG
2+
All notable changes to build.py and related python scripts will be noted here.
3+
4+
## [2.2.1] - 2024-04-11
5+
### Updated
6+
- Script has been updated to not sign native libraries by default now that Apple.Core has been updated to handle this step.
7+
- Passing no codesign identity hash is no longer an issue
8+
- Libraries can still be signed using the `-c` flag along with a passed string representing the codesign identity hash
9+
- The string `prompt` may be passed to `-c` to instigate the script's codesign workflow which will ask the user to select from a list of codesign identities on the system

scripts/python/upi_cli_argument_options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ class CleanActionID:
7171

7272
# Performs all clean actions for the selected plug-ins
7373
ALL = "all"
74+
75+
# Code sign options (-c, --codesign-identity)
76+
class CodeSignActionID:
77+
# In general the -c flag takes a codesign identity hash as an argument, but when this argument is provided the script will enable the code signing identity selection workflow
78+
PROMPT = "prompt"

scripts/python/upi_toolchain.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,20 @@ def Codesign(printer : Printer, signable_object_path : str, hash : str, logWithC
121121

122122
# Method prompts the user before identifying a codesign identity to use for signing newly compiled native plug-in libraries
123123
def PromptForCodesignIdentity(printer : Printer) -> str:
124-
printer.WarningMessage("No codesign identity provided.", "\n")
125-
printer.InfoMessage("Recent versions of Unity require that native plug-in libraries are codesigned or they will not be loaded by the Editor or Player runtime.")
126-
printer.Message(f"For more information about code signing, please see: {Printer.Bold('https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html')}", printer.Indent(1))
127-
128124
if not utility.BooleanPrompt(printer, "Would you like the script to code sign the compiled native plug-in libraries?"):
129-
printer.Message("User opted out of code signing. Compiled libraries will not be signed and may not be loaded by Unity.")
125+
printer.Message("User opted out of code signing.")
126+
printer.Message(f"For more information about code signing, please see: {Printer.Bold('https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html')}", printer.Indent(1))
130127
return ""
131128

132129
codesign_identities = GetCodesignIdentities()
133130
if len(codesign_identities) == 0:
134-
printer.WarningMessage("No codesign identities found. Compiled libraries will not be signed and may not be loaded by Unity.")
131+
printer.WarningMessage("No codesign identities found.")
132+
printer.Message(f"For more information about code signing, please see: {Printer.Bold('https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html')}", printer.Indent(1))
135133
return ""
136134

137135
elif len(codesign_identities) == 1:
138136
id_hash, id_name = list(codesign_identities.items())[0]
137+
printer.InfoMessage("Only one codesign identity found.")
139138
printer.MessageWithContext("Using codesign identity: ", f"{id_hash} {id_name}", "\n")
140139
return id_hash
141140

0 commit comments

Comments
 (0)