Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit CPU cores for ndkBuild with Cmake and Ninja

Before, when I was using ndkBuld for building native code on Android I was able to pass an argument to make to define a number of CPU cores to be used. If I wanted to utilize 4 cores I could add something like this

externalNativeBuild {
    ndkBuild {
        arguments "-j4", "APP_SHORT_COMMANDS=true"
        abiFilters "armeabi-v7a"
    }
}

Can somebody give an advice how can I do something similar with Cmake and Ninja? Is there some equivalent parameter for cmake configuration?

externalNativeBuild {
    cmake {
        arguments "-DANDROID_STL=c++_static"
        abiFilters getAbis()
    }
}

Thanks.

like image 783
bio007 Avatar asked Jan 27 '23 17:01

bio007


1 Answers

controlling ninja parallelism

Ninja also support the same parameter:

$ ninja --help
usage: ninja [options] [targets...]

[...]

options:
  [...]

  -j N     run N jobs in parallel [default=10, derived from CPUs available]

  [...]

controlling ninja parallelism differentiating compile and link jobs

Now, if you want more granularity. For example, if you would like to limit the number of simultaneous link jobs, or compile jobs, or both.

Starting with CMake 3.11, it is now possible to limit the number of compile and/or link jobs.

You could then configure your project with these options:

-DCMAKE_JOB_POOL_COMPILE:STRING=compile
-DCMAKE_JOB_POOL_LINK:STRING=link
'-DCMAKE_JOB_POOLS:STRING=compile=5;link=2'

Now, if your project end up spawning other child processed that are themselves building projects using ninja, you would have to:

  • use the fork of ninja that include Job Server support like it is done in make. Binaries are also available in the associated GitHub releases. See https://github.com/kitware/ninja#readme

  • make sure sub-project are also configured with the same -DCMAKE_JOB_ options

in the context of externalNativeBuild

This means you could try something like this:

externalNativeBuild {
    cmake {
        arguments "-DANDROID_STL=c++_static -DCMAKE_JOB_POOL_COMPILE:STRING=compile -DCMAKE_JOB_POOL_LINK:STRING=link '-DCMAKE_JOB_POOLS:STRING=compile=5;link=2'"
        abiFilters getAbis()
    }
}
like image 109
J-Christophe Avatar answered Jan 31 '23 20:01

J-Christophe