From CMake setup 'make' to use '-j'

2019-02-12 17:45发布

问题:

I want my CMake project to be built by make -j N, whenever I call make from the terminal. I don't want to set -j option manually every time.

For that, I set CMAKE_MAKE_PROGRAM variable to the specific command line. I use the ProcessorCount() function, which gives the number of procesors to perform build in parallel.

When I do make, I do not see any speed up. However if I do make -j N, then it is built definitely faster.

Would you please help me on this issue? (I am developing this on Linux.)

Here is the snippet of the code that I use in CMakeList.txt:

include(ProcessorCount)
ProcessorCount(N)
message("number of processors: "  ${N})
if(NOT N EQUAL 0)
  set(CTEST_BUILD_FLAGS -j${N})
  set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${N})
  set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j ${N}")      
endif()
message("cmake make program" ${CMAKE_MAKE_PROGRAM})

Thank you very much.

回答1:

Via setting CMAKE_MAKE_PROGRAM variable you want to affect on build process. But:

  1. This variable affects only on build via cmake --build, not on native tool (make) call:

    The CMAKE_MAKE_PROGRAM variable is set for use by project code. The value is also used by the cmake(1) --build and ctest(1) --build-and-test tools to launch the native build process.

  2. This variable should be CACHE one. It is used in such way by make-like generators:

    These generators store CMAKE_MAKE_PROGRAM in the CMake cache so that it may be edited by the user.

    That is, you need to set this variable with

    set(CMAKE_MAKE_PROGRAM <program> CACHE PATH "Path to build tool" FORCE)
    
  3. This variable should refer to an executable itself, not to a program with arguments:

    The value may be the full path to an executable or just the tool name if it is expected to be in the PATH.

    That is, value "make -j 2" cannot be used for that variable (splitting arguments as list

    set(CMAKE_MAKE_PROGRAM make -j 2 CACHE PATH "Path to build tool" FORCE)
    

    wouldn't help too).

In summary, you may redefine behavior of cmake --build call with setting CMAKE_MAKE_PROGRAM variable to the script, which calls make with parallel options. But you may not affect on behavior for direct make call.



回答2:

In case you want to speed up the build you can run multiple make processes in parallel but not cmake. To perform every build with predefined number of parallel processes you can define this in MAKEFLAGS.

Set MAKEFLAGS in your environment script, e.g. ~/.bashrc as you want:

export MAKEFLAGS=-j8

On Linux the following sets MAKEFLAGS to the number of CPU - 1: (Keep one CPU free for other tasks while build) and is useful in environments with dynamic ressources, e.g. VMware:

export MAKEFLAGS=-j$(($(grep -c ^processor /proc/cpuinfo) - 0))


回答3:

You may set the env variable MAKEFLAGS using this command

export MAKEFLAGS=-j$(nproc)


回答4:

Can this method be used for calling multiple functions? like function1,2,3 in parallel rather sequential, any other ideas appreciated.

example:

 if(FLAG1)
#some variables/options settings
    function1(${OPTIONS})
    function2(${OPTIONS})
    function3(${OPTIONS})
     ....
endif()

function(fucntion1)
#some variables/options parsing and passed to command.
      execute_process(COMMAND COMMAND1
                    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
                    RESULT_VARIABLE res
                    TIMEOUT 60)
endfunction()
function(fucntion2)
#some variables/options parsing and passed to command.
      execute_process(COMMAND COMMAND2
                    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
                    RESULT_VARIABLE res
                    TIMEOUT 60)
endfunction()