Skip to content

Commit

Permalink
Fix build scripts and add visionOS support
Browse files Browse the repository at this point in the history
Multiple fixes:
- building on Apple Silicon Macs - remove the assumptions that the build machine is always x86_64
- fix badly broken build.sh for Mac, especially with getopts for options & params
- extend build-ios.sh to allow building for iOS-derived platforms like visionOS

The expected iOS build invocation is:
build-ios.sh [clean] [release|debug] ${ARCH} ${PLATFORM}
where
ARCH = arm64|arm64e|x86_64
PLATFORM = iphoneos|iphonesimulator|xros|xrsimulator
  • Loading branch information
tkukielk committed May 25, 2024
1 parent feff7ef commit d60ff6d
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 77 deletions.
22 changes: 14 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ if(APPLE)
endif()
endif()

if((${IOS_PLAT} STREQUAL "iphoneos") OR (${IOS_PLAT} STREQUAL "iphonesimulator"))
if((${IOS_PLAT} STREQUAL "iphoneos") OR (${IOS_PLAT} STREQUAL "iphonesimulator") OR (${IOS_PLAT} STREQUAL "xros") OR (${IOS_PLAT} STREQUAL "xrsimulator"))
set(IOS_PLATFORM "${IOS_PLAT}")
else()
message(FATAL_ERROR "Unrecognized iOS platform '${IOS_PLAT}'")
endif()

if(${IOS_ARCH} STREQUAL "x86_64")
set(IOS_PLATFORM "iphonesimulator")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch x86_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch x86_64")
set(CMAKE_SYSTEM_PROCESSOR x86_64)
elseif(${IOS_ARCH} STREQUAL "arm64")
set(IOS_PLATFORM ${IOS_PLAT})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch arm64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64")
set(CMAKE_SYSTEM_PROCESSOR arm64)
elseif(${IOS_ARCH} STREQUAL "arm64e")
set(IOS_PLATFORM ${IOS_PLAT})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch arm64e")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64e")
set(CMAKE_SYSTEM_PROCESSOR arm64e)
Expand All @@ -78,11 +77,18 @@ if(APPLE)
message("-- PLATFORM: ${IOS_PLATFORM}")
else()
if(${MAC_ARCH} STREQUAL "x86_64")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch x86_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch x86_64")
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_OSX_ARCHITECTURES ${MAC_ARCH})
set(APPLE True)
elseif(${MAC_ARCH} STREQUAL "arm64")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch arm64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64")
set(CMAKE_SYSTEM_PROCESSOR arm64)
set(TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_OSX_ARCHITECTURES ${MAC_ARCH})
set(APPLE True)
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch x86_64 -arch arm64")
Expand All @@ -107,7 +113,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(WARN_FLAGS "/W4 /WX")
else()
# No -pedantic -Wno-extra-semi -Wno-gnu-zero-variadic-macro-arguments
set(WARN_FLAGS "-Wall -Werror -Wextra -Wno-unused-parameter")
set(WARN_FLAGS "-Wall -Werror -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable")
endif()

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
Expand Down Expand Up @@ -310,17 +316,17 @@ endif()
################################################################################################

if (BUILD_PACKAGE)
if (${CMAKE_PACKAGE_TYPE} STREQUAL "deb")
if ("${CMAKE_PACKAGE_TYPE}" STREQUAL "deb")
# FIXME: hardcode it for 64-bit Linux for now
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib/${CPACK_DEBIAN_ARCHITECTURE}-linux-gnu)
include(tools/MakeDeb.cmake)
endif()
if (${CMAKE_PACKAGE_TYPE} STREQUAL "rpm")
if ("${CMAKE_PACKAGE_TYPE}" STREQUAL "rpm")
# TODO: [MG] - fix path
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_SYSTEM_PROCESSOR}-linux-gnu)
include(tools/MakeRpm.cmake)
endif()
if (${CMAKE_PACKAGE_TYPE} STREQUAL "tgz")
if ("${CMAKE_PACKAGE_TYPE}" STREQUAL "tgz")
# TODO: [MG] - fix path... should we simply use /usr/local/lib without CPU?
# TODO: [MG] - Windows path is not ideal -- C:/Program Files (x86)/MSTelemetry/* - what should we use instead?
include(tools/MakeTgz.cmake)
Expand Down
96 changes: 63 additions & 33 deletions build-ios.sh
Original file line number Diff line number Diff line change
@@ -1,69 +1,99 @@
#!/bin/sh

# The expected iOS build invocation is:
# build-ios.sh [clean] [release|debug] ${ARCH} ${PLATFORM}
# where
# ARCH = arm64|arm64e|x86_64
# PLATFORM = iphoneos|iphonesimulator|xros|xrsimulator

if [ "$1" == "clean" ]; then
rm -f CMakeCache.txt *.cmake
rm -rf out
rm -rf .buildtools
# make clean
echo "build-ios.sh: cleaning previous build artifacts"
rm -f CMakeCache.txt *.cmake
rm -rf out
rm -rf .buildtools
# make clean
shift
fi

if [ "$1" == "release" ] || [ "$2" == "release" ]; then
BUILD_TYPE="Release"
else
BUILD_TYPE="Debug"
if [ "$1" == "release" ]; then
BUILD_TYPE="Release"
shift
elif [ "$1" == "debug" ]; then
BUILD_TYPE="Debug"
shift
fi

# Set Architecture: arm64, arm64e or x86_64
if [ "$2" == "arm64" ] || [ "$3" == "arm64" ]; then
IOS_ARCH="arm64"
elif [ "$2" == "arm64e" ] || [ "$3" == "arm64e" ]; then
IOS_ARCH="arm64e"
else
IOS_ARCH="x86_64"
if [ "$1" == "arm64" ]; then
IOS_ARCH="arm64"
shift
elif [ "$1" == "arm64e" ]; then
IOS_ARCH="arm64e"
shift
elif [ "$1" == "x86_64" ]; then
IOS_ARCH="x86_64"
shift
fi

# Set Platform: device or simulator
if [ "$2" == "device" ] || [ "$3" == "device" ] || [ "$4" == "device" ]; then
IOS_PLAT="iphoneos"
else
# the last param is expected to specify the platform name: iphoneos|iphonesimulator|xros|xrsimulator
# so if it is non-empty and it is not "device", we take it as a valid platform name
# otherwise we fall back to old iOS logic which only supported iphoneos|iphonesimulator
IOS_PLAT="iphonesimulator"
if [ -n "$1" ] && [ "$1" != "device" ]; then
IOS_PLAT="$1"
elif [ "$1" == "device" ]; then
IOS_PLAT="iphoneos"
fi

# Set target iOS minver
default_ios_target=10.0
if [ -z $IOS_DEPLOYMENT_TARGET ]; then
export IOS_DEPLOYMENT_TARGET=${default_ios_target}
export FORCE_RESET_DEPLOYMENT_TARGET=YES
else
export FORCE_RESET_DEPLOYMENT_TARGET=NO
echo "IOS_ARCH = $IOS_ARCH, IOS_PLAT = $IOS_PLAT, BUILD_TYPE = $BUILD_TYPE"

FORCE_RESET_DEPLOYMENT_TARGET=NO
DEPLOYMENT_TARGET=""

if [ "$IOS_PLAT" == "iphoneos" ] || [ "$IOS_PLAT" == "iphonesimulator" ]; then
SYS_NAME="iOS"
DEPLOYMENT_TARGET="$IOS_DEPLOYMENT_TARGET"
if [ -z "$DEPLOYMENT_TARGET" ]; then
DEPLOYMENT_TARGET="10.0"
FORCE_RESET_DEPLOYMENT_TARGET=YES
fi
elif [ "$IOS_PLAT" == "xros" ] || [ "$IOS_PLAT" == "xrsimulator" ]; then
SYS_NAME="visionOS"
DEPLOYMENT_TARGET="$XROS_DEPLOYMENT_TARGET"
if [ -z "$DEPLOYMENT_TARGET" ]; then
DEPLOYMENT_TARGET="1.0"
FORCE_RESET_DEPLOYMENT_TARGET=YES
fi
fi
echo "ios deployment target="$IOS_DEPLOYMENT_TARGET
echo "force reset deployment target="$FORCE_RESET_DEPLOYMENT_TARGET

echo "deployment target = $DEPLOYMENT_TARGET"
echo "force reset deployment target = $FORCE_RESET_DEPLOYMENT_TARGET"

# Install build tools and recent sqlite3
FILE=.buildtools
OS_NAME=`uname -a`
FILE=".buildtools"
if [ ! -f $FILE ]; then
tools/setup-buildtools-apple.sh ios
# Assume that the build tools have been successfully installed
echo > $FILE
# Assume that the build tools have been successfully installed
echo > $FILE
fi

if [ -f /usr/bin/gcc ]; then
echo "gcc version: `gcc --version`"
echo "gcc version: `gcc --version`"
fi

if [ -f /usr/bin/clang ]; then
echo "clang version: `clang --version`"
echo "clang version: `clang --version`"
fi

mkdir -p out
cd out

CMAKE_PACKAGE_TYPE=tgz

cmake_cmd="cmake -DCMAKE_OSX_SYSROOT=$IOS_PLAT -DCMAKE_SYSTEM_NAME=iOS -DCMAKE_IOS_ARCH_ABI=$IOS_ARCH -DCMAKE_OSX_DEPLOYMENT_TARGET=$IOS_DEPLOYMENT_TARGET -DBUILD_IOS=YES -DIOS_ARCH=$IOS_ARCH -DIOS_PLAT=$IOS_PLAT -DIOS_DEPLOYMENT_TARGET=$IOS_DEPLOYMENT_TARGET -DBUILD_UNIT_TESTS=YES -DBUILD_FUNC_TESTS=YES -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_PACKAGE_TYPE=$CMAKE_PACKAGE_TYPE -DFORCE_RESET_DEPLOYMENT_TARGET=$FORCE_RESET_DEPLOYMENT_TARGET $CMAKE_OPTS .."
echo $cmake_cmd
cmake_cmd="cmake -DCMAKE_OSX_SYSROOT=$IOS_PLAT -DCMAKE_SYSTEM_NAME=$SYS_NAME -DCMAKE_IOS_ARCH_ABI=$IOS_ARCH -DCMAKE_OSX_DEPLOYMENT_TARGET=$DEPLOYMENT_TARGET -DBUILD_IOS=YES -DIOS_ARCH=$IOS_ARCH -DIOS_PLAT=$IOS_PLAT -DIOS_DEPLOYMENT_TARGET=$DEPLOYMENT_TARGET -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_PACKAGE_TYPE=$CMAKE_PACKAGE_TYPE -DFORCE_RESET_DEPLOYMENT_TARGET=$FORCE_RESET_DEPLOYMENT_TARGET $CMAKE_OPTS .."
echo "${cmake_cmd}"
eval $cmake_cmd

make
Expand Down
103 changes: 67 additions & 36 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
#!/bin/bash

usage()
{
echo "Usage: build.sh [clean] [arm64|x86_64|universal] [CUSTOM_CMAKE_CXX_FLAGS=x] [noroot] [release] [-h|-?] [-l (static|shared)] [-D CMAKE_OPTION] [-v]"
echo " "
echo "options: "
echo " "
echo "Positional options (1st three arguments): "
echo "[clean] - perform clean build "
echo "[arm64|x86_64|universal] - Apple platform build type. Not applicable to other OS. "
echo "[CUSTOM_CMAKE_CXX_FLAGS] - custom CXX compiler flags "
echo "[noroot] - custom CXX compiler flags "
echo "[release] - build for Release "
echo " "
echo "Additional parameters: "
echo " -h | -? - this help. "
echo " -l [static|shared] - build static (default) or shared library. "
echo " -D [CMAKE_OPTION] - additional options to pass to cmake. Could be multiple. "
echo " -v - increase build verbosity (reserved for future use) "
echo " "
echo "Environment variables: "
echo "CMAKE_OPTS - any additional cmake options. "
echo "GIT_PULL_TOKEN - authorization token for Microsoft-proprietary modules. "
echo "MACOSX_DEPLOYMENT_TARGET - optional parameter for setting macosx deployment target "
echo "Plus any other environment variables respected by CMake build system. "
exit 0
}

export PATH=/usr/local/bin:$PATH

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
Expand All @@ -8,87 +35,91 @@ cd $DIR

export NOROOT=$NOROOT

if [ "$1" == "clean" ]; then
PARAM1="$1"
PARAM2="$2"
PARAM3="$3"

if [ "$PARAM1" == "clean" ]; then
echo "Cleaning previous build artifacts"
rm -f CMakeCache.txt *.cmake
rm -rf out
rm -rf .buildtools
# make clean
shift
fi

if [ "$1" == "noroot" ] || [ "$2" == "noroot" ] || [ "$3" == "noroot" ]; then
if [ "$PARAM1" == "noroot" ] || [ "$PARAM2" == "noroot" ] || [ "$PARAM3" == "noroot" ]; then
export NOROOT=true
echo "NOROOT = true"
shift
fi

if [ "$1" == "release" ] || [ "$2" == "release" ] || [ "$3" == "release" ]; then
if [ "$PARAM1" == "release" ] || [ "$PARAM2" == "release" ] || [ "$PARAM3" == "release" ]; then
BUILD_TYPE="Release"
echo "BUILD_TYPE = Release"
shift
elif [ "$PARAM1" == "debug" ] || [ "$PARAM2" == "debug" ] || [ "$PARAM3" == "debug" ]; then
BUILD_TYPE="Debug"
echo "BUILD_TYPE = Debug"
shift
else
BUILD_TYPE="Debug"
echo "Assuming default BUILD_TYPE = Debug"
fi

if [ "$1" == "arm64" ] || [ "$2" == "arm64" ] || [ "$3" == "arm64" ]; then
if [ "$PARAM1" == "arm64" ] || [ "$PARAM2" == "arm64" ] || [ "$PARAM3" == "arm64" ]; then
MAC_ARCH="arm64"
elif [ "$1" == "universal" ] || [ "$2" == "universal" ] || [ "$3" == "universal" ]; then
echo "MAC_ARCH = arm64"
shift
elif [ "$PARAM1" == "universal" ] || [ "$PARAM2" == "universal" ] || [ "$PARAM3" == "universal" ]; then
MAC_ARCH="universal"
echo "MAC_ARCH = universal"
shift
elif [ "$PARAM1" == "x86_64" ] || [ "$PARAM2" == "x86_64" ] || [ "$PARAM3" == "x86_64" ]; then
MAC_ARCH="x86_64"
echo "MAC_ARCH = x86_64"
shift
else
MAC_ARCH="x86_64"
echo "Assuming default MAC_ARCH = x86_64"
fi

CUSTOM_CMAKE_CXX_FLAG=""
if [[ $1 == CUSTOM_BUILD_FLAGS* ]] || [[ $2 == CUSTOM_BUILD_FLAGS* ]] || [[ $3 == CUSTOM_BUILD_FLAGS* ]]; then
if [[ $1 == CUSTOM_BUILD_FLAGS* ]]; then
if [[ $PARAM1 == CUSTOM_BUILD_FLAGS* ]] || [[ $PARAM2 == CUSTOM_BUILD_FLAGS* ]] || [[ $PARAM3 == CUSTOM_BUILD_FLAGS* ]]; then
if [[ $PARAM1 == CUSTOM_BUILD_FLAGS* ]]; then
CUSTOM_CMAKE_CXX_FLAG="\"${1:19:999}\""
elif [[ $2 == CUSTOM_BUILD_FLAGS* ]]; then
elif [[ $PARAM2 == CUSTOM_BUILD_FLAGS* ]]; then
CUSTOM_CMAKE_CXX_FLAG="\"${2:19:999}\""
elif [[ $3 == CUSTOM_BUILD_FLAGS* ]]; then
elif [[ $PARAM3 == CUSTOM_BUILD_FLAGS* ]]; then
CUSTOM_CMAKE_CXX_FLAG="\"${3:19:999}\""
fi
echo "custom build flags="$CUSTOM_CMAKE_CXX_FLAG
shift
echo "custom build flags = $CUSTOM_CMAKE_CXX_FLAG"
fi

LINK_TYPE=
CMAKE_OPTS="${CMAKE_OPTS:--DBUILD_SHARED_LIBS=OFF}"
while getopts "h?vl:D:" opt; do
case "$opt" in
h|\?)
echo "Usage: build.sh [clean] [arm64|universal] [CUSTOM_CMAKE_CXX_FLAGS=x] [noroot] [release] [-h|-?] [-l (static|shared)] [-D CMAKE_OPTION] [-v]"
echo " "
echo "options: "
echo " "
echo "Positional options (1st three arguments): "
echo "[clean] - perform clean build "
echo "[arm64|universal] - Apple platform build type. Not applicable to other OS. "
echo "[CUSTOM_CMAKE_CXX_FLAGS] - custom CXX compiler flags "
echo "[noroot] - custom CXX compiler flags "
echo "[release] - build for Release "
echo " "
echo "Additional parameters: "
echo " -h | -? - this help. "
echo " -l [static|shared] - build static (default) or shared library. "
echo " -D [CMAKE_OPTION] - additional options to pass to cmake. Could be multiple. "
echo " -v - increase build verbosity (reserved for future use) "
echo " "
echo "Environment variables: "
echo "CMAKE_OPTS - any additional cmake options. "
echo "GIT_PULL_TOKEN - authorization token for Microsoft-proprietary modules. "
echo "MACOSX_DEPLOYMENT_TARGET - optional parameter for setting macosx deployment target "
echo "Plus any other environment variables respected by CMake build system. "
exit 0
h|\?) usage
;;
:) echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 0
;;
v) verbose=1
;;
D) CMAKE_OPTS="$CMAKE_OPTS -D$OPTARG"
D) CMAKE_OPTS="${CMAKE_OPTS} -D${OPTARG}"
;;
l) LINK_TYPE=$OPTARG
;;
esac
done
shift $((OPTIND -1))

echo "CMAKE_OPTS from caller: $CMAKE_OPTS"

if [ "$LINK_TYPE" == "shared" ]; then
CMAKE_OPTS="$CMAKE_OPTS -DBUILD_SHARED_LIBS=ON"
CMAKE_OPTS="${CMAKE_OPTS} -DBUILD_SHARED_LIBS=ON"
fi

# Set target MacOS minver
Expand Down

0 comments on commit d60ff6d

Please sign in to comment.