Skip to content

Commit

Permalink
Merge pull request #1663 from bugsnag/PLAT-12250-build-xcframeworks
Browse files Browse the repository at this point in the history
Plat 12250 build xcframeworks
  • Loading branch information
kstenerud authored Jun 18, 2024
2 parents 56a3622 + 608fe47 commit 5f99305
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ build_ios_static: ## Build the static library target
build_carthage: ## Build the latest pushed commit with Carthage
@./scripts/build-carthage.sh

build_xcframework: ## Build as a multiplatform xcframework
@./scripts/build-xcframework.sh

build_swift: ## Build with Swift Package Manager
@swift build

Expand Down
121 changes: 121 additions & 0 deletions scripts/build-xcframework.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env bash
#
# Written by: Karl Stenerud
#
# Script to build all platforms, architectures, and simulators into a single xcframework per target.
#
# See https://developer.apple.com/documentation/xcode/creating-a-multi-platform-binary-framework-bundle
#

set -e -u -o pipefail



# -------------------------------------------------------------------
# Configuration
# -------------------------------------------------------------------

# Base name of the .xcodeproj directory
PROJECT_NAME=Bugsnag

# Targets that are suffixed with the platform (e.g. XYZ-iOS, XYZ-macOS etc)
NAMESPACED_TARGETS=( "Bugsnag" )

# Targets that are not namespaced
GENERIC_TARGETS=( )

# Platforms we are building for: iOS, macOS, tvOS, watchOS (in future: visionOS)
PLATFORMS=( "iOS" "macOS" "tvOS" "watchOS" )



# -------------------------------------------------------------------
# Script
# -------------------------------------------------------------------

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PROJECT_DIR=$( cd -- "$( dirname -- "${SCRIPT_DIR}" )" &> /dev/null && pwd )
XCFW_DIR="${PROJECT_DIR}/build/xcframeworks"
INTERMEDIATES_DIR="${XCFW_DIR}/intermediates"
PRODUCTS_DIR="${XCFW_DIR}/products"

rm -rf "${XCFW_DIR}"
mkdir -p "${INTERMEDIATES_DIR}"
mkdir -p "${PRODUCTS_DIR}"

##
# Build a target. This function tries to sort out whether the target is namespaced or not
# (i.e. if it's named XYZ or XYZ-iOS) so that it produces a consistently named output file
# (such as XYZ-iOS.framework, XYZ-iOS-simulator.framework).
#
# Args:
# 1: Name of compilation target
# 2: Name of platform to build for (iOS, macOS, etc)
#
# Return: A set of commandline argumets for the final framework build.
##
build_target() {
target=$1
platform=$2

framework_name=${target}
if [[ ${framework_name} == *${platform} ]]; then
# If our target is named "XYZ-iOS", remove the "-iOS" part from the framework name
framework_name="${framework_name%-${platform}}"
fi

destination="generic/platform=${platform}"
archive_path="${INTERMEDIATES_DIR}/${target}"
if [[ ${archive_path} != *${platform} ]]; then
# Make sure our archive path is namespaced with the platform
archive_path="${archive_path}-${platform}"
fi
# Note: Redirecting xcodebuild's stdout to stderr because we need stdout for ourselves.
xcodebuild archive -project ${PROJECT_DIR}/${PROJECT_NAME}.xcodeproj \
-scheme ${target} \
-configuration Release \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
-destination "${destination}" \
-archivePath "${archive_path}" 1>&2
echo "-archive ${archive_path}.xcarchive -framework ${framework_name}.framework"

if [ "${platform}" != "macOS" ]; then
# For non-macos targrets, build for the simulator as well.
destination="${destination} Simulator"
archive_path="${archive_path}-simulator"
xcodebuild archive -project ${PROJECT_DIR}/${PROJECT_NAME}.xcodeproj \
-scheme ${target} \
-configuration Release \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
-destination "${destination}" \
-archivePath "${archive_path}" 1>&2
echo "-archive ${archive_path}.xcarchive -framework ${framework_name}.framework"
fi
}

if [[ ${#NAMESPACED_TARGETS[@]} -gt 0 ]]; then
for target in "${NAMESPACED_TARGETS[@]}"; do
xcframework_args=""
for platform in "${PLATFORMS[@]}"; do
new_args=$( build_target ${target}-${platform} ${platform} )
xcframework_args="${xcframework_args} ${new_args}"
done
xcodebuild -create-xcframework ${xcframework_args} -output "${PRODUCTS_DIR}/${target}.xcframework"
done
fi

if [[ ${#GENERIC_TARGETS[@]} -gt 0 ]]; then
for target in "${GENERIC_TARGETS[@]}"; do
xcframework_args=""
for platform in "${PLATFORMS[@]}"; do
new_args=$( build_target ${target} ${platform} )
xcframework_args="${xcframework_args} ${new_args}"
done
xcodebuild -create-xcframework ${xcframework_args} -output "${PRODUCTS_DIR}/${target}.xcframework"
done
fi

echo
echo "** build-frameworks.sh: script completed successfully **"

0 comments on commit 5f99305

Please sign in to comment.