toeb / cmakepp

An Enhancement Suite for the CMake Build System
Other
433 stars 37 forks source link

Add progress seed to process_wait_all() idle callbacks #76

Closed Manu343726 closed 9 years ago

Manu343726 commented 9 years ago

This way it's easy to print progress status based on that seed. It's not a real progress value, but a seed to be used to generate output.

I'm using it to generate dynamic ellipsis output with \r on the boost-biicode scripts.

toeb commented 9 years ago

I'll show you a example to reach what you want without modifying process_wait_all:

  set(handles_list)

  set(script "
    foreach(i RANGE 0 10)
      execute_process(COMMAND \${CMAKE_COMMAND} -E sleep 1)
    endforeach()
  ")

  # create some scripts
  foreach(i RANGE 0 5)
    message("starting Task ${i}")
    ## start a process 
    ## since all args are passed on to `execute()`
    ## you specify callbacks for that specific process here
    ## (e.g. --success-callback, error-callback, --state-changed-callback)
    ## the resulting handles are appended to handles_list
    assign(handles_list[] = process_start_script("${script}" --success-callback "[](process_handle) message(FORMAT 'process #${i} succeeded (pid: {process_handle.pid})')")) 
  endforeach()

  ## this function creates a string containing status information
  function(progress_string value maximum ticks)
    math(EXPR multiplier "20/${maximum}")
    math(EXPR value "${value} * ${multiplier}")
    math(EXPR maximum "${maximum} * ${multiplier}")
    math(EXPR rest_count "${maximum} - ${value}")
    string_repeat("=" ${value})
    ans(status)
    string_repeat(" " ${rest_count})
    ans(rest)
    math(EXPR status_ticker "${ticks} % 5")
    string_repeat("." ${status_ticker})
    ans(status_ticker)
    return("[${status}${rest}]${status_ticker}          ")
  endfunction()

  ## this idlecallback displays updating status on the console
  ## it uses the ref 'ticks' to count the numbber of times the idle callback was called
  ref_set(ticks 0)
  function(my_idle_callback)
    ref_get(ticks)
    ans(ticks)
    math(EXPR ticks "${ticks} + 1")
    ref_set(ticks ${ticks})

    progress_string("${finished_count}" "${process_count}" "${ticks}")
    ans(status)
    echo_append("\r${status}")
  endfunction()

  ## process_wait_all has an `--idle-callback` which will be called while polling the process handles_list
  ## 
  process_wait_all(${handles_list} --idle-callback "[]()my_idle_callback({{ARGN}})")
toeb commented 9 years ago

so as you can see you can use ref_get() ref_set() to create you own idle callcount

toeb commented 9 years ago

I would not want to add the ticks variable to process_wait_all as it is unnecessary from a process_wait_all standpoint and can be emulated with ref_get and ref_set. however I might be renaming some inner variables of process_wait_all that you can access by scope inheritance