|
| 1 | +## This CMake script parses a .ninja_log file (LOGFILE) and prints a list of |
| 2 | +## build/link times, sorted longest first. |
| 3 | +## |
| 4 | +## cmake -DLOGFILE=<.ninja_log file> \ |
| 5 | +## -P PrintNinjaBuildTimes.cmake |
| 6 | +## |
| 7 | +## If LOGFILE is omitted, the current directory's .ninja_log file is used. |
| 8 | +################################################################################ |
| 9 | + |
| 10 | +cmake_minimum_required(VERSION 3.15) |
| 11 | + |
| 12 | +# Prepend the string with "0" until the string length equals the specified width |
| 13 | +function(pad_string_with_zeros string_var width) |
| 14 | + set(local_string "${${string_var}}") |
| 15 | + string(LENGTH "${local_string}" size) |
| 16 | + while(size LESS width) |
| 17 | + string(PREPEND local_string "0") |
| 18 | + string(LENGTH "${local_string}" size) |
| 19 | + endwhile() |
| 20 | + set(${string_var} "${local_string}" PARENT_SCOPE) |
| 21 | +endfunction() |
| 22 | + |
| 23 | +################################################################################ |
| 24 | + |
| 25 | +if (NOT LOGFILE) |
| 26 | + set(LOGFILE ".ninja_log") |
| 27 | +endif() |
| 28 | + |
| 29 | +# Check if logfile exists |
| 30 | +if (NOT EXISTS "${LOGFILE}") |
| 31 | + message(FATAL_ERROR "LOGFILE does not exist ('${LOGFILE}').") |
| 32 | +endif() |
| 33 | + |
| 34 | +# Read the logfile and generate a map / keylist |
| 35 | +set(keys) |
| 36 | +file(STRINGS "${LOGFILE}" lines) |
| 37 | +foreach(line ${lines}) |
| 38 | + |
| 39 | + # Parse each build time |
| 40 | + string(REGEX MATCH |
| 41 | + "^([0-9]+)\t([0-9]+)\t[0-9]+\t([^\t]+)+\t[0-9a-fA-F]+$" _DUMMY "${line}") |
| 42 | + |
| 43 | + if (CMAKE_MATCH_COUNT EQUAL 3) |
| 44 | + set(start_ms ${CMAKE_MATCH_1}) |
| 45 | + set(end_ms ${CMAKE_MATCH_2}) |
| 46 | + set(command "${CMAKE_MATCH_3}") |
| 47 | + math(EXPR runtime_ms "${end_ms} - ${start_ms}") |
| 48 | + |
| 49 | + # Compute human readable time |
| 50 | + math(EXPR days "${runtime_ms} / (1000 * 60 * 60 * 24)") |
| 51 | + math(EXPR runtime_ms "${runtime_ms} - (${days} * 1000 * 60 * 60 * 24)") |
| 52 | + math(EXPR hours "${runtime_ms} / (1000 * 60 * 60)") |
| 53 | + math(EXPR runtime_ms "${runtime_ms} - (${hours} * 1000 * 60 * 60)") |
| 54 | + math(EXPR minutes "${runtime_ms} / (1000 * 60)") |
| 55 | + math(EXPR runtime_ms "${runtime_ms} - (${minutes} * 1000 * 60)") |
| 56 | + math(EXPR seconds "${runtime_ms} / 1000") |
| 57 | + math(EXPR milliseconds "${runtime_ms} - (${seconds} * 1000)") |
| 58 | + |
| 59 | + # Format time components |
| 60 | + pad_string_with_zeros(days 3) |
| 61 | + pad_string_with_zeros(hours 2) |
| 62 | + pad_string_with_zeros(minutes 2) |
| 63 | + pad_string_with_zeros(seconds 2) |
| 64 | + pad_string_with_zeros(milliseconds 3) |
| 65 | + |
| 66 | + # Construct table entry |
| 67 | + # Later values in the file for the same command overwrite earlier entries |
| 68 | + string(MAKE_C_IDENTIFIER "${command}" key) |
| 69 | + set(ENTRY_${key} |
| 70 | + "${days}d ${hours}h ${minutes}m ${seconds}s ${milliseconds}ms | ${command}" |
| 71 | + ) |
| 72 | + |
| 73 | + # Record the key: |
| 74 | + list(APPEND keys "${key}") |
| 75 | + endif() |
| 76 | +endforeach() |
| 77 | + |
| 78 | +list(REMOVE_DUPLICATES keys) |
| 79 | + |
| 80 | +# Build the entry list: |
| 81 | +set(entries) |
| 82 | +foreach(key ${keys}) |
| 83 | + list(APPEND entries "${ENTRY_${key}}") |
| 84 | +endforeach() |
| 85 | + |
| 86 | +if (NOT entries) |
| 87 | + message(FATAL_ERROR "LOGFILE contained no build entries ('${LOGFILE}').") |
| 88 | +endif() |
| 89 | + |
| 90 | +# Sort in descending order: |
| 91 | +list(SORT entries) |
| 92 | +list(REVERSE entries) |
| 93 | + |
| 94 | +# Dump table: |
| 95 | +message(STATUS "-----------------------+----------------------------") |
| 96 | +message(STATUS "Time | Command ") |
| 97 | +message(STATUS "-----------------------+----------------------------") |
| 98 | + |
| 99 | +foreach(entry ${entries}) |
| 100 | + message(STATUS ${entry}) |
| 101 | +endforeach() |
0 commit comments