Skip to content

Commit 4a0f035

Browse files
committed
Enable building for Apple Frameworks
1 parent c317892 commit 4a0f035

File tree

10 files changed

+253
-13
lines changed

10 files changed

+253
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ then build and install USD into `/path/to/my_usd_install_dir`.
141141
> python USD/build_scripts/build_usd.py /path/to/my_usd_install_dir
142142
```
143143

144+
Additionally you can provide the `--build-apple-framework` flag to create a framework output, for easier integration
145+
into an Xcode application. Framework support is currently experimental, and will always build monolithic.
146+
144147
###### iOS
145148

146149
When building from a macOS system, you can cross compile for iOS based platforms.
@@ -150,6 +153,7 @@ Additionally, they will not support Python bindings or command line tools.
150153

151154
To build for iOS, add the `--build-target iOS` parameter.
152155

156+
iOS builds default to building as a framework.
153157

154158
##### Windows:
155159

build_scripts/apple_utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,27 @@ def GetDevelopmentTeamID():
217217
except Exception as ex:
218218
raise Exception("No development team found with exception " + ex)
219219

220-
def CodesignFiles(files):
221-
codeSignID = GetCodeSignID()
220+
def CodesignFiles(files, codeSignID=None):
221+
if not codeSignID:
222+
codeSignID = GetCodeSignID()
222223

223224
for f in files:
224225
subprocess.call(['codesign', '-f', '-s', '{codesignid}'
225226
.format(codesignid=codeSignID), f],
226227
stdout=devout, stderr=devout)
227228

228229

229-
def Codesign(install_path, verbose_output=False):
230+
def Codesign(context, verbose_output=False):
230231
if not MacOS():
231232
return False
232233
if verbose_output:
233234
global devout
234235
devout = sys.stdout
235236

236-
files = ExtractFilesRecursive(install_path,
237+
files = ExtractFilesRecursive(context.usdInstDir,
237238
(lambda file: '.so' in file or '.dylib' in file))
238-
CodesignFiles(files)
239+
CodesignFiles(files, context.macOSCodesign)
240+
239241

240242
def CreateUniversalBinaries(context, libNames, x86Dir, armDir):
241243
if not MacOS():
@@ -281,4 +283,5 @@ def ConfigureCMakeExtraArgs(context, args:List[str]) -> List[str]:
281283

282284
if system_name:
283285
args.append(f"-DCMAKE_SYSTEM_NAME={system_name}")
284-
return args
286+
287+
return args

build_scripts/build_usd.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,11 @@ def InstallUSD(context, force, buildArgs):
17891789
'-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH',
17901790
'-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH'])
17911791

1792+
if MacOS():
1793+
extraArgs.append(f"-DPXR_BUILD_APPLE_FRAMEWORK={'ON' if context.buildAppleFramework else 'OFF'}")
1794+
if context.macOSCodesign:
1795+
extraArgs.append(f"-DPXR_APPLE_CODESIGN_IDENTITY={context.macOSCodesign}")
1796+
17921797
# Make sure to use boost installed by the build script and not any
17931798
# system installed boost
17941799
extraArgs.append('-DBoost_NO_BOOST_CMAKE=On')
@@ -1903,6 +1908,12 @@ def InstallUSD(context, force, buildArgs):
19031908
help=("Build target for macOS cross compilation. "
19041909
"(default: {})".format(
19051910
apple_utils.GetBuildTargetDefault())))
1911+
subgroup = group.add_mutually_exclusive_group()
1912+
subgroup.add_argument("--build-apple-framework", dest="build_apple_framework", action="store_true",
1913+
help="Build USD as an Apple Framework (Default if using build)")
1914+
subgroup.add_argument("--no-build-apple-framework", dest="no_build_apple_framework", action="store_true",
1915+
help="Do not build USD as an Apple Framework (Default if macOS)")
1916+
19061917
if apple_utils.IsHostArm():
19071918
# Intel Homebrew stores packages in /usr/local which unfortunately can
19081919
# be where a lot of other things are too. So we only add this flag on arm macs.
@@ -1938,6 +1949,7 @@ def InstallUSD(context, force, buildArgs):
19381949
default=codesignDefault, action="store_true",
19391950
help=("Enable code signing for macOS builds "
19401951
"(defaults to enabled on Apple Silicon)"))
1952+
group.add_argument("--codesign-id", dest="macos_codesign_id", type=str)
19411953

19421954
if Linux():
19431955
group.add_argument("--use-cxx11-abi", type=int, choices=[0, 1],
@@ -2216,17 +2228,25 @@ def __init__(self, args):
22162228
self.ignorePaths = args.ignore_paths or []
22172229
self.buildTarget = None
22182230
self.macOSCodesign = ""
2231+
self.buildAppleFramework = False
22192232
# Build target and code signing
22202233
if MacOS():
22212234
self.buildTarget = args.build_target
22222235
apple_utils.SetTarget(self, self.buildTarget)
22232236

2224-
self.macOSCodesign = \
2225-
(args.macos_codesign if hasattr(args, "macos_codesign")
2226-
else False)
2237+
if args.macos_codesign:
2238+
self.macOSCodesign = args.macos_codesign_id or apple_utils.GetCodeSignID()
22272239
if apple_utils.IsHostArm() and args.ignore_homebrew:
22282240
self.ignorePaths.append("/opt/homebrew")
22292241

2242+
self.buildAppleFramework = ((args.build_apple_framework
2243+
or self.buildTarget in apple_utils.EMBEDDED_PLATFORMS)
2244+
and not args.no_build_apple_framework)
2245+
if self.buildAppleFramework:
2246+
self.buildShared = False
2247+
self.buildMonolithic = True
2248+
2249+
22302250
coreOnly = self.buildTarget in apple_utils.EMBEDDED_PLATFORMS
22312251

22322252
self.useCXX11ABI = \
@@ -2239,10 +2259,10 @@ def __init__(self, args):
22392259

22402260
# Optional components
22412261
self.buildTests = args.build_tests
2242-
self.buildPython = args.build_python and not coreOnly
2262+
self.buildPython = args.build_python and not coreOnly and not self.buildAppleFramework
22432263
self.buildExamples = args.build_examples
22442264
self.buildTutorials = args.build_tutorials
2245-
self.buildTools = args.build_tools and not coreOnly
2265+
self.buildTools = args.build_tools and not coreOnly and not self.buildAppleFramework
22462266

22472267
# - Documentation
22482268
self.buildDocs = args.build_docs or args.build_python_docs
@@ -2675,8 +2695,9 @@ def FormatBuildArguments(buildArgs):
26752695
])
26762696

26772697
if MacOS():
2678-
if context.macOSCodesign:
2679-
apple_utils.Codesign(context.usdInstDir, verbosity > 1)
2698+
# We don't need to codesign when building a framework because it's handled during framework creation
2699+
if context.macOSCodesign and not context.buildAppleFramework:
2700+
apple_utils.Codesign(context, verbosity > 1)
26802701

26812702
printInstructions = any([context.buildPython, context.buildTools, context.buildPrman])
26822703
if printInstructions:
@@ -2699,3 +2720,12 @@ def FormatBuildArguments(buildArgs):
26992720
if context.buildPrman:
27002721
Print("See documentation at http://openusd.org/docs/RenderMan-USD-Imaging-Plugin.html "
27012722
"for setting up the RenderMan plugin.\n")
2723+
2724+
if context.buildAppleFramework:
2725+
Print("""
2726+
Added the following framework to your Xcode Project, (recommended as Embed Without Signing):
2727+
OpenUSD.framework
2728+
2729+
Set the following compiler argument, to find the headers:
2730+
SYSTEM_HEADER_SEARCH_PATHS=$(SRCROOT)/$(TARGET_NAME)/OpenUSD.framework/Headers
2731+
""")

cmake/defaults/CXXDefaults.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,8 @@ if (PXR_PREFER_SAFETY_OVER_SPEED)
106106
else()
107107
set(PXR_PREFER_SAFETY_OVER_SPEED "0")
108108
endif()
109+
110+
# Set that Apple Framework is being build
111+
if (PXR_BUILD_APPLE_FRAMEWORK)
112+
_add_define("PXR_BUILD_APPLE_FRAMEWORK")
113+
endif()

cmake/defaults/Options.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ option(PXR_PREFER_SAFETY_OVER_SPEED
5555
ON)
5656

5757
if(APPLE)
58+
set(PXR_APPLE_CODESIGN_IDENTITY "-" CACHE STRING "The Codesigning identity needed to sign compiled objects")
5859
# Cross Compilation detection as defined in CMake docs
5960
# Required to be handled here so it can configure options later on
6061
# https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-ios-tvos-visionos-or-watchos
@@ -74,6 +75,16 @@ if(APPLE)
7475
set(PXR_BUILD_IMAGING OFF)
7576
endif ()
7677
endif ()
78+
79+
option(PXR_BUILD_APPLE_FRAMEWORK "Builds an Apple Framework." APPLE_EMBEDDED)
80+
set(PXR_APPLE_FRAMEWORK_NAME "OpenUSD" CACHE STRING "Name to provide Apple Framework build")
81+
set(PXR_APPLE_IDENTIFIER_DOMAIN "org.openusd" CACHE STRING "Name to provide Apple Framework build")
82+
if (${PXR_BUILD_APPLE_FRAMEWORK})
83+
if(${PXR_BUILD_USD_TOOLS})
84+
MESSAGE(STATUS "Setting PXR_BUILD_USD_TOOLS=OFF because PXR_BUILD_APPLE_FRAMEWORK is enabled.")
85+
set(PXR_BUILD_USD_TOOLS OFF)
86+
endif()
87+
endif()
7788
endif()
7889

7990

@@ -145,6 +156,12 @@ set(PXR_LIB_PREFIX ""
145156

146157
option(BUILD_SHARED_LIBS "Build shared libraries." ON)
147158
option(PXR_BUILD_MONOLITHIC "Build a monolithic library." OFF)
159+
if (${PXR_BUILD_APPLE_FRAMEWORK})
160+
set(BUILD_SHARED_LIBS OFF)
161+
set(PXR_BUILD_MONOLITHIC ON)
162+
MESSAGE(STATUS "Setting PXR_BUILD_MONOLITHIC=ON for Framework build")
163+
endif ()
164+
148165
set(PXR_MONOLITHIC_IMPORT ""
149166
CACHE
150167
STRING

cmake/macros/Public.cmake

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,12 @@ function(pxr_toplevel_epilogue)
11171117
# Setup the plugins in the top epilogue to ensure that everybody has had a
11181118
# chance to update PXR_EXTRA_PLUGINS with their plugin paths.
11191119
pxr_setup_plugins()
1120+
1121+
# Build
1122+
if (PXR_BUILD_APPLE_FRAMEWORK)
1123+
pxr_create_apple_framework()
1124+
endif ()
1125+
11201126
endfunction() # pxr_toplevel_epilogue
11211127

11221128
function(pxr_monolithic_epilogue)
@@ -1305,3 +1311,26 @@ function(pxr_build_python_documentation)
13051311
")
13061312

13071313
endfunction() # pxr_build_python_documentation
1314+
1315+
function(pxr_create_apple_framework)
1316+
# CMake can have a lot of different boolean representations, that need to be narrowed down
1317+
if (APPLE_EMBEDDED)
1318+
set(EMBEDDED_BUILD "true")
1319+
else()
1320+
set(EMBEDDED_BUILD "false")
1321+
endif()
1322+
1323+
_get_library_prefix(LIB_PREFIX)
1324+
if(TARGET usd_ms)
1325+
set(FRAMEWORK_ROOT_LIBRARY_NAME "${LIB_PREFIX}usd_ms.dylib")
1326+
else()
1327+
set(FRAMEWORK_ROOT_LIBRARY_NAME "${LIB_PREFIX}usd.dylib")
1328+
endif()
1329+
1330+
# Install the Info.plist and shell script
1331+
configure_file(cmake/resources/Info.plist.in "${PROJECT_BINARY_DIR}/Info.plist" @ONLY)
1332+
configure_file(cmake/resources/AppleFrameworkBuild.zsh.in "${PROJECT_BINARY_DIR}/AppleFrameworkBuild.zsh" @ONLY)
1333+
1334+
# Run the shell script for the primary configuration
1335+
install(CODE "execute_process(COMMAND zsh ${PROJECT_BINARY_DIR}/AppleFrameworkBuild.zsh )")
1336+
endfunction() # pxr_create_apple_framework
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env zsh
2+
3+
# Creates an Apple framework for the given platform type
4+
# documentation: https://developer.apple.com/documentation/bundleresources/placing_content_in_a_bundle
5+
echo "⌛️ Creating @PXR_APPLE_FRAMEWORK_NAME@ ..."
6+
7+
# Variables are substituted by CMake
8+
CMAKE_INSTALL_PREFIX="@CMAKE_INSTALL_PREFIX@"
9+
PROJECT_BINARY_DIR="@PROJECT_BINARY_DIR@"
10+
11+
FRAMEWORK_NAME="@PXR_APPLE_FRAMEWORK_NAME@"
12+
FRAMEWORK_DIR="${CMAKE_INSTALL_PREFIX}/${FRAMEWORK_NAME}.framework"
13+
FRAMEWORK_HEADERS_DIR="${FRAMEWORK_DIR}/Headers"
14+
FRAMEWORK_LIBRARIES_DIR="${FRAMEWORK_DIR}/Libraries"
15+
FRAMEWORK_PLUGIN_DIR="${FRAMEWORK_LIBRARIES_DIR}/usd"
16+
FRAMEWORK_ROOT_LIBRARY_NAME="@FRAMEWORK_ROOT_LIBRARY_NAME@"
17+
EMBEDDED_BUILD=@EMBEDDED_BUILD@
18+
FRAMEWORK_RESOURCES_DIR="${FRAMEWORK_DIR}"
19+
MATERIALX_SOURCE_LIBRARIES="${CMAKE_INSTALL_PREFIX}/libraries/"
20+
BUNDLE_IDENTIFIER="@PXR_APPLE_IDENTIFIER_DOMAIN@.@PXR_APPLE_FRAMEWORK_NAME@"
21+
CODESIGN_ID="@PXR_APPLE_CODESIGN_IDENTITY@"
22+
OLD_RC_PATH="${CMAKE_INSTALL_PREFIX}/lib"
23+
24+
function fix_linkage() {
25+
readonly file=${1:?"A file path must be specified."}
26+
readonly prepend="${FRAMEWORK_NAME}.framework/Libraries"
27+
filename=$(basename ${file})
28+
# First, change the install name. This corresponds to LC_ID_DYLIB.
29+
install_name_tool -id "@rpath/${prepend}/${filename}" ${file}
30+
31+
parts=("${(@f)$(otool -l ${file})}")
32+
for line in ${parts}; do
33+
dylib_name=""
34+
[[ $line =~ ' *name @rpath/(.*\.dylib)' ]] && dylib_name=$match[1]
35+
if [ -n "${dylib_name}" ]; then
36+
install_name_tool -change "@rpath/${dylib_name}" "@rpath/${prepend}/${dylib_name}" "${file}"
37+
fi
38+
if [[ $line == *"${OLD_RC_PATH}"* ]]; then
39+
install_name_tool -delete_rpath ${OLD_RC_PATH} ${file}
40+
fi
41+
done
42+
43+
codesign -f -s ${CODESIGN_ID} ${file}
44+
}
45+
46+
if [ "$EMBEDDED_BUILD" = false ];then
47+
PLIST_ROOT="${FRAMEWORK_DIR}/Versions/A/Resources/"
48+
fi
49+
50+
# Remove the existing directory if it exists
51+
if [ -d ${FRAMEWORK_DIR} ]; then
52+
echo "Removing existing framework";
53+
rm -Rf ${FRAMEWORK_DIR};
54+
fi
55+
56+
# Create the parent directory
57+
echo "Creating directories..."
58+
mkdir -p ${FRAMEWORK_DIR}
59+
mkdir -p ${PLIST_ROOT}
60+
61+
# Copy the plist over
62+
echo "Copying files into ${FRAMEWORK_DIR}"
63+
ditto "${PROJECT_BINARY_DIR}/Info.plist" "${PLIST_ROOT}/Info.plist"
64+
65+
# Copy the primary directories over
66+
ditto "${CMAKE_INSTALL_PREFIX}/include/" ${FRAMEWORK_HEADERS_DIR}
67+
ditto "${CMAKE_INSTALL_PREFIX}/lib/" ${FRAMEWORK_LIBRARIES_DIR}
68+
ditto "${CMAKE_INSTALL_PREFIX}/plugin/usd/" ${FRAMEWORK_PLUGIN_DIR}
69+
70+
71+
# Remove any so files because boost generates them for iPhone
72+
rm -rf ${FRAMEWORK_LIBRARIES_DIR}/cmake
73+
for file in ${FRAMEWORK_LIBRARIES_DIR}/**/libboost*.so*; do
74+
rm -f ${file}
75+
done
76+
77+
# Remove any static archive files as well
78+
for file in ${FRAMEWORK_LIBRARIES_DIR}/**/*.a; do
79+
rm -f ${file}
80+
done
81+
82+
83+
# Copy the MaterialX libraries if they exist
84+
if [ -d "${MATERIALX_SOURCE_LIBRARIES}" ]; then
85+
ditto ${MATERIALX_SOURCE_LIBRARIES} "${FRAMEWORK_LIBRARIES_DIR}/materialx/"
86+
fi
87+
88+
89+
echo "Correcting linkage on libraries..."
90+
# The root file needs to be a binary that matches the framework name
91+
mv "${FRAMEWORK_LIBRARIES_DIR}/${FRAMEWORK_ROOT_LIBRARY_NAME}" "${FRAMEWORK_DIR}/${FRAMEWORK_NAME}"
92+
(cd ${FRAMEWORK_LIBRARIES_DIR} && ln -s "../${FRAMEWORK_NAME}" ${FRAMEWORK_ROOT_LIBRARY_NAME})
93+
fix_linkage "${FRAMEWORK_DIR}/${FRAMEWORK_NAME}"
94+
install_name_tool -id "@rpath/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${FRAMEWORK_DIR}/${FRAMEWORK_NAME}"
95+
install_name_tool -change "@rpath/${FRAMEWORK_NAME}.framework/Libraries/${FRAMEWORK_NAME}" "@rpath/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${FRAMEWORK_DIR}/${FRAMEWORK_NAME}"
96+
97+
# Do Dylib fixing here
98+
# This finds all dylibs, but (.) skips linked files
99+
for file in ${FRAMEWORK_LIBRARIES_DIR}/**/*.dylib(.); do
100+
fix_linkage ${file}
101+
done
102+
103+
# Sign the final framework
104+
echo "Codesigning the framework..."
105+
codesign --force --sign ${CODESIGN_ID} ${FRAMEWORK_DIR} --generate-entitlement-der --identifier ${BUNDLE_IDENTIFIER}
106+
107+
echo "✅ Finished creating framework at ${FRAMEWORK_DIR}"

cmake/resources/Info.plist.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>@PXR_APPLE_FRAMEWORK_NAME@</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>@PXR_APPLE_IDENTIFIER_DOMAIN@.@PXR_APPLE_FRAMEWORK_NAME@</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>@PXR_APPLE_FRAMEWORK_NAME@</string>
15+
<key>CFBundlePackageType</key>
16+
<string>FMWK</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>@PXR_MAJOR_VERSION@.@PXR_MINOR_VERSION@.@PXR_PATCH_VERSION@</string>
19+
<key>CFBundleVersion</key>
20+
<string>@PXR_MAJOR_VERSION@.@PXR_MINOR_VERSION@.@PXR_PATCH_VERSION@</string>
21+
<key>CSResourcesFileMapped</key>
22+
<true />
23+
</dict>
24+
</plist>

pxr/base/plug/initConfig.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ ARCH_CONSTRUCTOR(Plug_InitConfig, 2, void)
110110
_AppendPathList(&result, buildLocation, binaryPath);
111111
_AppendPathList(&result, pluginBuildLocation, binaryPath);
112112

113+
#ifdef PXR_BUILD_APPLE_FRAMEWORK
114+
_AppendPathList(&result, "Libraries/usd", binaryPath);
115+
#endif
116+
113117
#ifdef PXR_INSTALL_LOCATION
114118
_AppendPathList(&result, installLocation, binaryPath);
115119
#endif // PXR_INSTALL_LOCATION

0 commit comments

Comments
 (0)