Building Lines Game for MacOS and iOS

To build my app for iOS I tried to run CMake from the command line without QT Creator:

cmake -S /Users/admin/dev/repos/examples/src/LinesGame/LinesGameQt -B . -DCMAKE_GENERATOR:STRING=Xcode \
  -DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=./.qtc/package-manager/auto-setup.cmake -DQT_QMAKE_EXECUTABLE:FILEPATH=/Users/admin/dev/libs/QT6/release/iOS/bin/qmake6 \
  -DCMAKE_PREFIX_PATH:PATH=/Users/admin/dev/libs/QT6/release/iOS \
  -DCMAKE_C_COMPILER:FILEPATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang \
  -DCMAKE_CXX_COMPILER:FILEPATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ \
  -DCMAKE_TOOLCHAIN_FILE:FILEPATH=/Users/admin/dev/libs/QT6/release/iOS/lib/cmake/Qt6/qt.toolchain.cmake \
  -DCMAKE_OSX_ARCHITECTURES:STRING=arm64 -DCMAKE_OSX_SYSROOT:STRING=iphoneos -DCMAKE_CXX_FLAGS_INIT:STRING= \
  -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM:STRING=XXXXXXX

but it requires .qtc to be copied into build directory before running CMake, but it is not clear where is it:

admin@son Qt Creator % find . -iname ".qtc"
admin@son Qt Creator % cd Applications
admin@son Applications % find . -iname ".qtc"
admin@son Applications % find . -iname "package-manager"
admin@son Applications % cd ~/dev/libs/QT6
admin@son QT6 % find . -iname ".qtc"
admin@son QT6 % find . -iname "package-manager"

Another idea was to add the following prior to project in my CMake:

cmake_minimum_required(VERSION 3.21.3)

if (APPLE)
    # The variable CMAKE_OSX_DEPLOYMENT_TARGET must initialized as a cache variable prior to the first project() command
    # Your code won't work the variable IOS cannot be used before the project call. It will always be false.
    # You must use the CMAKE_SYSTEM_NAME instead
    message("CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
    # Probably QT Creator specifies -DCMAKE_SYSTEM_NAME=iOS in the command line, but I am not sure.
    if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
        #Does not take an effect, the workaround is setting XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET.
        set(CMAKE_OSX_DEPLOYMENT_TARGET "14.0" CACHE STRING "Minimum iOS deployment version")
    else()
        # Build for Apple Silicon.
        if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
            set(CMAKE_OSX_ARCHITECTURES arm64 x86_64)
        endif()
        set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0" CACHE STRING "Minimum MacOS deployment version")
    endif()
    message("CMAKE_OSX_DEPLOYMENT_TARGET has been set to ${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif()

project(LinesGameQt LANGUAGES CXX)

but it does not work well enough because CMAKE_SYSTEM_NAME is empty with a clean build and is defined as iOS when I restart QT Creator.

The next possible alternative is defining CMAKE_OSX_DEPLOYMENT_TARGET and CMAKE_OSX_ARCHITECTURES directly in QT Creator settings that are the following by default for iOS:

For iOS production builds I specify CMAKE_OSX_DEPLOYMENT_TARGET=14.0 and CMAKE_OSX_ARCHITECTURES=arm64 and CMake generates the following in XCode project file:

D8BA168A2DF84337BA721D22 /* Release */ = {
	isa = XCBuildConfiguration;
	buildSettings = {
		ARCHS = arm64;
		DEVELOPMENT_TEAM = XXXXXXX;
		IPHONEOS_DEPLOYMENT_TARGET = 14.0;
		SDKROOT = iphoneos;
		SWIFT_COMPILATION_MODE = wholemodule;
		SYMROOT = /Users/admin/dev/repos/examples/src/LinesGame/LinesGameQt/build/Qt_6_7_0_iOS_Release/build;
	};
	name = Release;
};

For MacOS production builds I specify CMAKE_OSX_DEPLOYMENT_TARGET=11.0 and CMAKE_OSX_ARCHITECTURES=arm64;x86_64 to build for Apple Silicon and CMake generates the following in XCode project file:

AC829B0DBACA487F8FFF4819 /* Release */ = {
	isa = XCBuildConfiguration;
	buildSettings = {
		ARCHS = "arm64 x86_64";
		MACOSX_DEPLOYMENT_TARGET = 11.0;
		SDKROOT = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk;
		SWIFT_COMPILATION_MODE = wholemodule;
		SYMROOT = /Users/admin/dev/repos/examples/src/LinesGame/LinesGameQt/build/Desktop_x86_darwin_generic_mach_o_64bit/build;
	};
	name = Release;

For MacOS debug builds and for iOS Simulator I specify CMAKE_OSX_ARCHITECTURES=x86_64.

Leave a Reply

Your email address will not be published. Required fields are marked *