cmake: compilation statistics

前端 未结 3 1360
自闭症患者
自闭症患者 2021-02-02 11:19

I need to figure out which translation units need to be restructured to improve compile times, How do I get hold of the compilation time, using cmake, for my translation units ?

3条回答
  •  走了就别回头了
    2021-02-02 12:16

    Following properties could be used to time compiler and linker invocations:

    • RULE_LAUNCH_COMPILE
    • RULE_LAUNCH_CUSTOM
    • RULE_LAUNCH_LINK

    Those properties could be set globally, per directory and per target. That way you can only have a subset of your targets (say tests) to be impacted by this property. Also you can have different "launchers" for each target that also could be useful.

    Keep in mind, that using "time" directly is not portable, because this utility is not available on all platforms supported by CMake. However, CMake provides "time" functionality in its command-line tool mode. For example:

    # Set global property (all targets are impacted)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")
    # Set property for my_target only
    set_property(TARGET my_target PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E time")
    

    Example CMake output:

    [ 65%] Built target my_target
    [ 67%] Linking C executable my_target
    Elapsed time: 0 s. (time), 0.000672 s. (clock)
    

    Note, that as of CMake 3.4 only Makefile and Ninja generators support this properties.

    Also note, that as of CMake 3.4 cmake -E time has problems with spaces inside arguments. For example:

    cmake -E time cmake "-GUnix Makefiles"
    

    will be interpreted as:

    cmake -E time cmake "-GUnix" "Makefiles"
    

    I submitted patch that fixes this problem.

提交回复
热议问题