Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改进 CMake 中 clock_gettime 的兼容性检查 #623

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

tayne3
Copy link
Contributor

@tayne3 tayne3 commented Sep 11, 2024

改进 CMake 中 clock_gettime 的兼容性检查

方案一:最小化改动

CMakeLists.txt 中增加以下内容

if (NOT HAVE_CLOCK_GETTIME)
    include(CheckLibraryExists)
    check_library_exists(rt clock_gettime "" HAVE_CLOCK_GETTIME_IN_RT)
    if (HAVE_CLOCK_GETTIME_IN_RT)
        set(HAVE_CLOCK_GETTIME ${HAVE_CLOCK_GETTIME_IN_RT})
    endif()
endif()

方案二:模块化改进

  1. 新增宏定义
    cmake/utils.cmake 中新增宏 project_check_library_function,用于检查特定库中的函数是否存在。该宏的定义如下:

    macro(project_check_library_function library function local)
        string(TOUPPER ${library} str1)
        string(TOUPPER ${function} str2)
        set(str3 HAVE_${str2}_IN_${str1})
        check_library_exists("${library}" "${function}" "${local}" ${str3})
        if (${str3})
            set(${str3} 1)
        else()
            set(${str3} 0)
        endif()
    endmacro()
  2. 更新 CMakeLists.txt
    CMakeLists.txt 中使用新定义的宏来检查 clock_gettime 函数是否存在于 rt 库中:

    project_check_library_function("rt" "clock_gettime" "")
  3. 更新 hconfig.h.in
    修改 hconfig.h.in 文件,以支持新的检查结果:

    #ifndef HAVE_CLOCK_GETTIME
    #define HAVE_CLOCK_GETTIME (@HAVE_CLOCK_GETTIME@ | @HAVE_CLOCK_GETTIME_IN_RT@)
    #endif

方案二更加遵循原cmake脚本的风格, 而当前提交的代码采用的是改动更小的方案一。

@ithewei ithewei merged commit 0cfc3c1 into ithewei:master Sep 12, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants