Created
February 9, 2025 11:41
-
-
Save Vivaldi101/2417064c9d14c6503119b3dc3a278fb7 to your computer and use it in GitHub Desktop.
SwiftUI cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the minimum CMake version required | |
cmake_minimum_required(VERSION 3.15) | |
# Define your project name | |
project(MySwiftUIObjectiveCIntegration) | |
# Specify the Objective-C++ source files and Swift files you want to compile | |
set(SOURCES | |
main.mm # Your entry point Objective-C++ code (or app logic) | |
MyWrapper.mm # Wrapper file to interface between Objective-C++ and SwiftUI | |
MySwiftUIViewController.mm # Objective-C++ file that integrates SwiftUI via UIHostingController | |
) | |
# Specify the Swift source files | |
set(SWIFT_SOURCES | |
MySwiftUIView.swift # Your SwiftUI view (Swift code) | |
) | |
# Add an executable target | |
# This target will generate an executable application that can run on macOS or iOS | |
add_executable(MyApp ${SOURCES} ${SWIFT_SOURCES}) | |
# Set properties for the executable | |
set_target_properties(MyApp PROPERTIES | |
# Indicating this is a macOS or iOS app | |
MACOSX_BUNDLE YES # If you're targeting macOS, set this to YES (you can remove it if targeting iOS) | |
# Setting Swift version used in the project | |
XCODE_ATTRIBUTE_SWIFT_VERSION "5.0" # Ensure you use the correct version of Swift (adjust if necessary) | |
# Define the deployment target (macOS/iOS version) | |
XCODE_ATTRIBUTE_IOS_DEPLOYMENT_TARGET "13.0" # For iOS applications (adjust version based on your need) | |
XCODE_ATTRIBUTE_MACOSX_DEPLOYMENT_TARGET "10.15" # For macOS applications (adjust version if needed) | |
) | |
# Set the language for the target | |
# This tells CMake that we're working with both Objective-C++ and Swift files | |
set_target_properties(MyApp PROPERTIES | |
LANGUAGES CXX OBJCXX Swift | |
) | |
# Link necessary frameworks to the target | |
# These frameworks are necessary for the SwiftUI and UIKit integration | |
target_link_libraries(MyApp | |
"-framework Cocoa" # For macOS (removable for iOS targets) | |
"-framework SwiftUI" # Linking SwiftUI framework | |
"-framework UIKit" # For iOS (removable for macOS targets) | |
"-framework Foundation" # Foundation framework is commonly needed | |
) | |
# Set the system root and deployment target for macOS or iOS | |
if(APPLE) | |
# Specify the system root for iOS/macOS depending on your target platform | |
set(CMAKE_OSX_SYSROOT iphoneos) # Change to macosx if targeting macOS instead of iOS | |
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0") # Minimum version for iOS, adjust as needed | |
endif() | |
# Enable Swift language support in CMake | |
# CMake needs to know that Swift code is involved in the project | |
enable_language(Swift) | |
# Optionally, add more Swift compiler flags | |
# Here, we specify that we want to use Swift version 5 | |
set(SWIFT_FLAGS "-swift-version 5") # Optional, specify Swift version if necessary | |
# Optional: If you need to configure Swift build settings (e.g., optimizations), you can set them here | |
# For example, to enable optimizations or debugging symbols | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") # Enable optimizations for C++ | |
set(SWIFT_FLAGS "${SWIFT_FLAGS} -O2") # Enable optimizations for Swift code (if needed) | |
# Optional: To define custom build configurations (Debug/Release), use the following: | |
# set(CMAKE_BUILD_TYPE Debug) # Uncomment for Debug build, or use Release for release builds | |
# Optional: Handle any custom preprocessor definitions if needed (e.g., for debug/logging) | |
# add_definitions(-DDEBUG) # Uncomment to define the DEBUG flag | |
# Ensure that Swift files are properly compiled with necessary options | |
set_source_files_properties(${SWIFT_SOURCES} PROPERTIES | |
COMPILE_FLAGS "${SWIFT_FLAGS}" | |
) | |
# End of the CMake configuration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment