Open annidy opened 12 months ago
这是一个CMakeLists.txt文件,用于构建一个名为brotli的库。其中:
add_library(brotli INTERFACE)
声明了一个名为brotli的接口库include(ExternalProject)
引入了ExternalProject模块,用于下载和构建第三方库include(HPHPFunctions)
引入了HPHPFunctions模块,该模块提供了一个名为SET_HHVM_THIRD_PARTY_SOURCE_ARGS的函数SET_HHVM_THIRD_PARTY_SOURCE_ARGS()
函数用于设置第三方库的源代码参数,包括下载链接、哈希值等ExternalProject_Add()
函数用于添加一个名为bundled_brotli的ExternalProject,该项目会下载、构建并安装brotli库add_dependencies(brotli bundled_brotli)
声明了brotli库依赖于bundled_brotli项目ExternalProject_Get_Property(bundled_brotli INSTALL_DIR)
获取bundled_brotli项目的安装目录target_include_directories(brotli INTERFACE "${INSTALL_DIR}/include")
声明了brotli库的头文件目录,用于依赖该库的项目引用头文件target_link_libraries()
声明了brotli库的链接库,包括brotlidec-static、brotlienc-static和brotlicommon-static。对于大部分库,其实只需要简单的ExternalProject_Add即可。
创建thirdparty/glfw目录,添加一个CMakeLists.txt文件
include(ExternalProject)
ExternalProject_Add(GLFW_external
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.3.8
GIT_SHALLOW ON
UPDATE_COMMAND ""
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${STAGED_INSTALL_PREFIX}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DGLFW_BUILD_EXAMPLES=OFF
-DGLFW_BUILD_TESTS=OFF
-DGLFW_BUILD_DOCS=OFF
TEST_AFTER_INSTALL OFF
DOWNLOAD_NO_PROGRESS ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
)
它会check下来源代码,默认生成的target就是库作者定义的target。
剩下要做的是
add_subdirectory(thirdparty/glfw)
find_package(glfw3 REQUIRED CONFIG)
target_link_libraries(${PROJECT} PRIVATE glfw)
https://github.com/annidy/glfw_opengl_cmake_template/blob/main/src/CMakeLists.txt
使用 ExternalProject_Add 一般需要依赖库也支持CMake,并且遵循良好的定义。一种方法是我们自己来写这个cmakefile文件
option(GLFW_BUILD_DOCS OFF)
option(GLFW_BUILD_EXAMPLES OFF)
option(GLFW_BUILD_TESTS OFF)
option(GLFW_INSTALL OFF)
add_subdirectory(${CMAKE_SOURCE_DIR}/3rdparty/glfw)
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/3rdparty/glfw/include) set(GLFW_DEFINITIONS -DGLFW_INCLUDE_NONE)
如果源代码不支持cmake,但我们只使用头文件
```cmake
set(STB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/3rdparty/stb)
add_library(stb INTERFACE)
target_include_directories(stb
INTERFACE
${STB_INCLUDE_DIR})
include(cmake/glfw.cmake)
参考设计imrad
https://github.com/facebook/hhvm/blob/master/third-party/brotli/CMakeLists.txt