CMake test for processor feature

后端 未结 2 2011
独厮守ぢ
独厮守ぢ 2021-01-24 14:04

I am wondering if it is possible for CMake to run tests like one might run with a configure script. Specifically I want to test if the system I am compiling on has support for

相关标签:
2条回答
  • 2021-01-24 14:40

    Selecting line 19 exactly makes this brittle. On my desktop (Linux 4.20 on i7-6700k), that line is

    wp              : yes
    

    Instead use grep's pattern-matching ability to check for the flags\t\t: line.

    grep -l '^flags[[:space:]]*:.*rdtscp' /proc/cpuinfo prints the filename and exits with success after the first match. Or prints nothing and exists with failure status if it doesn't find a match.

    I don't know CMake, but based on the other answer presumably you'd use

    execute_process(COMMAND grep -l '^flags[[:space:]]*:.*rdtscp' /proc/cpuinfo
        OUTPUT_VARIABLE OUT)
    

    The simpler version of this is just grep -l rdtscp /proc/cpuinfo, but requiring a match in the flags : line will prevent any possible false-positive. (To be even more belt-and-suspenders, you could require space or end of line before/after, maybe with PCREgrep for zero-width assertions. In case some future feature flag like XYZrdtscpABC that can be present without RDTSCP support becomes a thing in the future. Or like broken_rdtscp). Or we could just assume that rdtscp is never at the end of the a line and look for ^flags.*:.* rdtscp.

    Using -l gets grep to exit after the first match, in case you were using head/tail as an optimization to avoid processing more lines on massively multi-core systems like Xeon Phi? It will still read the whole file if there's no match for rdtscp, but probably any massively-multi-core system will have RDTSCP. And grep is very fast anyway.

    0 讨论(0)
  • 2021-01-24 14:52
    execute_process(COMMAND cat /proc/cpuinfo
        COMMAND head -n 19
        COMMAND tail -1
        COMMAND grep -c rdtscp
        OUTPUT_VARIABLE OUT)
    
    0 讨论(0)
提交回复
热议问题