- CMake起点:cocos2d-x/CMakeLists.txt
# 设置module的查找目录 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/Modules/") include(BuildModules) # 调用过程 BuildModules() 复制代码
- 设置lib:cmake/Modules/BuildModules.cmake
# 设置lib变量... # xxhash if(USE_PREBUILT_LIBS OR USE_SOURCES_EXTERNAL) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/xxhash) # 设置_INCLUDE_DIRS、_LIBRARIES等变量 set(XXHASH_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/external/xxhash) set(XXHASH_LIBRARIES xxhash) else() cocos_find_package(xxhash xxhash REQUIRED) endif() # 开始真正的项目lib添加 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cocos) 复制代码
- 将lib引入到项目中:cocos2d-x/cocos/CMakeLists.txt
# 引入lib foreach(pkg XXHASH ... ) cocos_use_pkg(cocos2dInternal ${pkg}) endforeach() 复制代码
cocos_use_pkg
的实现在cmake/Modules/CocosBuildHelper.cmake
function(cocos_use_pkg target pkg) set(prefix ${pkg}) set(_include_dirs ${${prefix}_INCLUDE_DIRS}) # 将头文件目录加入到项目中 include_directories( ${_include_dirs} ) set(_library_dirs ${${prefix}_LIBRARY_DIRS}) # 将库文件目录加入到项目中 link_directories(${_library_dirs}) set(_libs ${${prefix}_LIBRARIES}) # 链接lib target_link_libraries(${target} ${_libs}) # 宏定义 set(_defs ${${prefix}_DEFINITIONS}) add_definitions(${_defs}) endfunction() 复制代码
与第2步设置_INCLUDE_DIRS
、_LIBRARIES
等变量前后呼应。