CMake: Detect platform MacOS. How?
Solved
Installation and Deployment
-
Hello all!
In my project need to be defined specified pieces of code for each of platform:
if(IOS) message(WARNING "iOS Build") elseif(ANDROID) message(WARNING "Android Build") elseif( (!!!) OSX|MAC|MACOSX ) message(WARNING "MacOS build") else() message(WARNING "Undefined build") endif()
The question is which keyword need to be used for detecting MacOS? Been testing: "MAC", "MACOS" and "MACOSX". Nothing working properly. For iOS and Android all is working properly.
-
Solution found. Issue closed.
There need to be added in CMake file something like this:if(IOS) if(${CMAKE_OSX_SYSROOT} MATCHES "iphonesimulator") if (${CMAKE_OSX_ARCHITECTURES} MATCHES "arm64") message("-- iOS Build for simulator arm64") elseif(${CMAKE_OSX_ARCHITECTURES} MATCHES "x86_64") message("-- iOS Build for simulator x86_64") endif() elseif(${CMAKE_OSX_SYSROOT} MATCHES "iphoneos") message("-- iOS Build for iphone") else() message(FATAL_ERROR"-- iOS undefined build") endif() elseif(ANDROID) if(${ANDROID_ABI} STREQUAL "x86_64") message("-- Android Build for x86_64") elseif(${ANDROID_ABI} STREQUAL "x86") message("-- Android Build for x86") elseif(${ANDROID_ABI} STREQUAL "armeabi-v7a") message("-- Android Build for armeabi-v7a") elseif(${ANDROID_ABI} STREQUAL "arm64-v8a") message("-- Android Build for arm64-v8a") else() message(FATAL_ERROR "-- Android undefined build") endif() elseif(${CMAKE_OSX_SYSROOT} MATCHES "\/MacOSX.platform\/Developer\/SDKs") message("-- MacOS Build") else() message(FATAL_ERROR "-- Undefined build") endif()
or use this:
if(IOS) if(${CMAKE_OSX_SYSROOT} MATCHES "iphonesimulator") if (${CMAKE_OSX_ARCHITECTURES} MATCHES "arm64") message("-- iOS Build for simulator arm64") elseif(${CMAKE_OSX_ARCHITECTURES} MATCHES "x86_64") message("-- iOS Build for simulator x86_64") endif() elseif(${CMAKE_OSX_SYSROOT} MATCHES "iphoneos") message("-- iOS Build for iphone") else() message(FATAL_ERROR"-- iOS undefined build") endif() elseif(ANDROID) if(${ANDROID_ABI} STREQUAL "x86_64") message("-- Android Build for x86_64") elseif(${ANDROID_ABI} STREQUAL "x86") message("-- Android Build for x86") elseif(${ANDROID_ABI} STREQUAL "armeabi-v7a") message("-- Android Build for armeabi-v7a") elseif(${ANDROID_ABI} STREQUAL "arm64-v8a") message("-- Android Build for arm64-v8a") else() message(FATAL_ERROR "-- Android undefined build") endif() elseif(APPLE) if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") message("-- MacOS Build for x86_64") elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64") message("-- MacOS Build for arm64") else() message(FATAL_ERROR "-- MacOS undefined build") endif() else() message(FATAL_ERROR "-- Undefined build") endif()