Skip to main content

Using CMake

CMake can be used to build more complex projects in build.sh or bld.bat scripts.

If you are using CMake, be sure to make it a build requirement in the build section. You may also need to include make or ninja depending on your platform and build tools. On Windows, nmake can also be used, but that is not explicitly included.

requirements:
build:
- cmake
- if: win
then: ninja
else: make

For CMake projects using the FindPython module, you can tell CMake which Python to use by passing -DPython_EXECUTABLE="$PYTHON" (macOS or Linux) or -DPython_EXECUTABLE="%PYTHON%" (Windows) as a command line option. Older CMake projects may require similar, but slightly different options.

tip

Don't forget that depending on which CMake module is used, you have to use a different option:

or if you are still on the deprecated FindPythonLibs: -DPYTHON_EXECUTABLE=....

Some optional, but useful CMake options:

  • -DCMAKE_BUILD_TYPE=Release Configure as release build. This is better done on the initial cmake call as some packages construct different build configurations depending on this flag.
  • -DCMAKE_INSTALL_PREFIX=$PREFIX Specify the install location.
  • -DCMAKE_INSTALL_LIBDIR=lib Libraries will land in $PREFIX/lib. Sometimes projects install into lib64 or similar directories, but on conda-forge we keep libraries in plain lib.
  • -DBUILD_SHARED_LIBS=ON Instruct CMake to build shared libraries instead of static ones.
  • -DCMAKE_FIND_FRAMEWORK=NEVER and -DCMAKE_FIND_APPBUNDLE=NEVER Prevent CMake from using system-wide macOS packages.
  • -G Ninja Build the project using ninja.
  • -B<directory> and -S<directory> Specify the build and source directories, respectively.
  • ${CMAKE_ARGS} Add variables defined by conda-forge internally. This is required to enable various conda-forge enhancements, like CUDA builds.
warning

As ${CMAKE_ARGS} is a space separated list of options, it needs to be passed without quoting. Quoting it ("${CMAKE_ARGS}") can lead to build errors as it makes the shell treat the contents of the variable as a single argument.

Here are some basic commands for you to get started. These are dependent on your source code layout and aren't intended to be used "as is".

cmake -S . -B build -G Ninja -DPython3_EXECUTABLE="$PYTHON" $CMAKE_ARGS
cmake --build build --config Release

See also the bld.bat example in the Particularities on Windows section.