[CMake] cmake + ninja how to use several CPU cores?

Dave Milter davemilter at gmail.com
Sun Jul 28 12:03:23 EDT 2019


Hi,

I heard that ninja has great feature it allows build continue without
wainting full link.

So if you have library Lib and executable App,
source code of App may build in parallel with source code of Lib,
and sync only link stage. While other build systems force build of Lib,
and only then start build of any object files of App.

But is it possible to use this feature with cmake?

Here my cmake code to emulate bottleneck of our build:

```
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(imported)

set(EXTERN_LIB_FILE ${CMAKE_CURRENT_SOURCE_DIR}/extern_lib/libextern.so)
set(EXTERN_LIB_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/extern_lib/gen)
add_custom_command(OUTPUT  ${EXTERN_LIB_FILE}
                   COMMAND sh ./lib_generator.sh
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/extern_lib)
add_custom_target(extern_lib_target DEPENDS ${EXTERN_LIB_FILE})
add_library(extern_lib SHARED IMPORTED GLOBAL)
add_dependencies(extern_lib extern_lib_target)

set_target_properties(extern_lib
    PROPERTIES
    IMPORTED_LOCATION ${EXTERN_LIB_FILE}
    INTERFACE_INCLUDE_DIRECTORIES ${EXTERN_LIB_INCLUDES})
add_executable(app main.cpp)
target_link_libraries(app extern_lib)
```

we use not C/C++ library as part of our source tree,
and it builds some time,
so I expect that ninja can run "main.cpp" -> main.o compilation during
"extern_lib" build,
but this is not happens.

Is cmake has problem with add_custom_command and ninja,
or the whole idea is wrong, and cmake + ninja can not work in this way,
and I should use some other project generator instead of cmake for this?


More information about the CMake mailing list