<div>I just posted a short blog article demonstrating how to figure out the number of processors available for a &quot;make -j&quot; or a scripted ctest_build call (with the &quot;Unix Makefiles&quot; generator) on Linux, Mac and Windows. Please let me know if you have any ideas for how it might be improved or extended.</div>
<div><br></div><a href="http://www.kitware.com/blog/home/post/63">http://www.kitware.com/blog/home/post/63</a><div><br></div><div><br></div><div>Cheers,</div><div>David</div><div><br></div><div><br></div><div>Blog text copied here, too, for search-ability on the mailing list archives:</div>
<div><div>==============================================================================</div><div>At the end of this script snippet, the CMake variable PROCESSOR_COUNT has a value appropriate for passing to make&#39;s -j for parallel builds.</div>
<div> </div><div>When used in a ctest -S script, you can call...</div><div><br></div><div>if(PROCESSOR_COUNT)</div><div>  set(CTEST_BUILD_FLAGS &quot;-j${PROCESSOR_COUNT}&quot;)</div><div>endif()</div><div> </div><div>...to enable parallel builds with &quot;Unix Makefiles&quot; and the ctest_build command.</div>
<div><br></div><div><br></div><div>Here&#39;s the snippet:</div><div>if(NOT DEFINED PROCESSOR_COUNT)</div><div>  # Unknown:</div><div>  set(PROCESSOR_COUNT 0)</div><div> </div><div>  # Linux:</div><div>  set(cpuinfo_file &quot;/proc/cpuinfo&quot;)</div>
<div>  if(EXISTS &quot;${cpuinfo_file}&quot;)</div><div>    file(STRINGS &quot;${cpuinfo_file}&quot; procs REGEX &quot;^processor.: [0-9]+$&quot;)</div><div>    list(LENGTH procs PROCESSOR_COUNT)</div><div>  endif()</div>
<div> </div><div>  # Mac:</div><div>  if(APPLE)</div><div>    find_program(cmd_sys_pro &quot;system_profiler&quot;)</div><div>    if(cmd_sys_pro)</div><div>      execute_process(COMMAND ${cmd_sys_pro} OUTPUT_VARIABLE info)</div>
<div>      string(REGEX REPLACE &quot;^.*Total Number Of Cores: ([0-9]+).*$&quot; &quot;\\1&quot;</div><div>        PROCESSOR_COUNT &quot;${info}&quot;)</div><div>    endif()</div><div>  endif()</div><div> </div><div>  # Windows:</div>
<div>  if(WIN32)</div><div>    set(PROCESSOR_COUNT &quot;$ENV{NUMBER_OF_PROCESSORS}&quot;)</div><div>  endif()</div><div>endif()</div><div><br></div></div>