<div>I just posted a short blog article demonstrating how to figure out the number of processors available for a "make -j" or a scripted ctest_build call (with the "Unix Makefiles" 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'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 "-j${PROCESSOR_COUNT}")</div><div>endif()</div><div> </div><div>...to enable parallel builds with "Unix Makefiles" and the ctest_build command.</div>
<div><br></div><div><br></div><div>Here'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 "/proc/cpuinfo")</div>
<div> if(EXISTS "${cpuinfo_file}")</div><div> file(STRINGS "${cpuinfo_file}" procs REGEX "^processor.: [0-9]+$")</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 "system_profiler")</div><div> if(cmd_sys_pro)</div><div> execute_process(COMMAND ${cmd_sys_pro} OUTPUT_VARIABLE info)</div>
<div> string(REGEX REPLACE "^.*Total Number Of Cores: ([0-9]+).*$" "\\1"</div><div> PROCESSOR_COUNT "${info}")</div><div> endif()</div><div> endif()</div><div> </div><div> # Windows:</div>
<div> if(WIN32)</div><div> set(PROCESSOR_COUNT "$ENV{NUMBER_OF_PROCESSORS}")</div><div> endif()</div><div>endif()</div><div><br></div></div>