前言
在mac电脑上,可以通过手动创建XCode工程的方式,来实现iOS lib的编译,但这种方式存在一个很明显的缺点,在需要指定编译文件的情况下,比较麻烦~CMake编译脚本完美的解决了这个问题,可以指定特定的文件参与编译
安装Xcode 和 CMake
xcode https://developer.apple.com/xcode/download
cmake OS X版本https://cmake.org/download
默认情况 cmake 命令行可能用不了,需要命令行中输入以下指令
export PATH=/Applications/CMake.app/Contents/bin/:$PATH
具体可参考 CMake Mac下安装
准备 iOS 工具链
ios.toolchain.cmake https://github.com/leetal/ios-cmake/releases
README文件中有工具链的使用方式
编写CMakeLists
ios.toolchain.cmake 工具链Demo中提供的CMakeLists,可以根据实际情况做下修改。
cmake_minimum_required (VERSION 3.2)
project (example-ios C CXX)
enable_testing()
MESSAGE( STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS} )
# Add some sanitary checks that the toolchain is actually working!
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(kqueue sys/event.h HAVE_KQUEUE)
if(NOT HAVE_KQUEUE)
message(STATUS "kqueue NOT found!")
else()
message(STATUS "kqueue found!")
endif()
find_library(APPKIT_LIBRARY AppKit)
if (NOT APPKIT_LIBRARY)
message(STATUS "AppKit.framework NOT found!")
else()
message(STATUS "AppKit.framework found! ${APPKIT_LIBRARY}")
endif()
find_library(UIKIT_LIBRARY UIKit)
if (NOT UIKIT_LIBRARY)
message(STATUS "UIKit.framework NOT found!")
else()
message(STATUS "UIKit.framework found! ${UIKIT_LIBRARY}")
endif()
# Hook up XCTest for the supported plaforms (all but WatchOS)
if(NOT PLATFORM MATCHES ".*WATCHOS.*")
# Use the standard find_package, broken between 3.14.0 and 3.14.4 at least for XCtest...
find_package(XCTest)
# Fallback: Try to find XCtest as host package via toochain macro (should always work)
find_host_package(XCTest REQUIRED)
endif()
# Includes
include_directories(${example-ios_SOURCE_DIR})
# Make sure try_compile() works
include(CheckTypeSize)
check_type_size(time_t SIZEOF_TIME_T)
# Source files
set(SOURCES
HelloWorld.cpp
HelloWorldIOS.mm
)
# Headers
set(HEADERS
HelloWorld.hpp
HelloWorldIOS.h
)
# Library
if(BUILD_SHARED)
add_library (example SHARED ${SOURCES} ${HEADERS})
target_compile_definitions(example PUBLIC IS_BUILDING_SHARED)
message(STATUS "Building shared version...")
else()
add_library (example STATIC ${SOURCES} ${HEADERS})
message(STATUS "Building static version...")
endif()
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX ${example-ios_SOURCE_DIR}/../example-app/example-lib CACHE PATH "Install path" FORCE)
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
# Executable
if(PLATFORM MATCHES "MAC.*")
set(APP_NAME TestApp)
add_executable (${APP_NAME} MACOSX_BUNDLE main.cpp)
set_target_properties(${APP_NAME} PROPERTIES
BUNDLE True
MACOSX_BUNDLE_GUI_IDENTIFIER leetal.com.helloworld
MACOSX_BUNDLE_BUNDLE_NAME helloworld
MACOSX_BUNDLE_BUNDLE_VERSION "0.1"
MACOSX_BUNDLE_SHORT_VERSION_STRING "0.1"
)
# Link the library with the executable
target_link_libraries(${APP_NAME} example)
endif()
# Debug symbols set in XCode project
set_xcode_property(example GCC_GENERATE_DEBUGGING_SYMBOLS YES "All")
# Installation
if(PLATFORM MATCHES "MAC.*")
install(TARGETS ${APP_NAME}
BUNDLE DESTINATION . COMPONENT Runtime
RUNTIME DESTINATION bin COMPONENT Runtime
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
# Note Mac specific extension .app
set(APPS "\${CMAKE_INSTALL_PREFIX}/${APP_NAME}.app")
# Directories to look for dependencies
set(DIRS ${CMAKE_BINARY_DIR})
install(CODE "include(BundleUtilities)
fixup_bundle(\"${APPS}\" \"\" \"${DIRS}\")")
set(CPACK_GENERATOR "DRAGNDROP")
include(CPack)
else()
install(TARGETS example
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
endif()
install (FILES ${HEADERS} DESTINATION include)
编译
cd example/example-lib
mkdir build
cd build
cmake .. -G Xcode -DCMAKE_TOOLCHAIN_FILE=../../ios.toolchain.cmake -DPLATFORM=OS64
cmake --build . --config Release
cmake .. -G Xcode -DCMAKE_TOOLCHAIN_FILE=../../ios.toolchain.cmake -DPLATFORM=OS64
指令指定了CMakeLists、工具链的位置及编译架构,会生成一个XCode工程。其中.. 表示CMakeLists文件在当前目录的上一个文件夹中,../表示上一个文件夹,../../表示上两个文件夹
网友评论