libsndfile icon indicating copy to clipboard operation
libsndfile copied to clipboard

CMake: `option()` must not be before project

Open SpaceIm opened this issue 3 years ago • 7 comments

Please revert #791, option() must not be before project()

SpaceIm avatar Sep 04 '22 18:09 SpaceIm

Hi @SpaceIm . This is not always true:

All vcpkg-affecting variables must be defined before the first project() directive, such as via the command line or set() statements.

https://vcpkg.io/en/docs/users/buildsystems/cmake-integration.html#vcpkg_manifest_features

evpobr avatar Sep 05 '22 05:09 evpobr

It works fine in the context of vcpkg maybe, but vcpkg shouldn't force you to put an option declaration before project(). Actually there is no reason to put this option() here.

This should work as well:

[...]
if(BUILD_TESTING)
  list(APPEND VCPKG_MANIFEST_FEATURES "tests")
endif()

if(BUILD_SAMPLES)
  list(APPEND VCPKG_MANIFEST_FEATURES "samples")
endif()

project(myapp)

option(BUILD_TESTING "Build tests" OFF)
option(BUILD_SAMPLES "Build samples" OFF)

Anyway why not defining these VCPKG_MANIFEST_FEATURES externally in your CI files? It's not a good idea to hardcode package managers specific stuff in your build files.

SpaceIm avatar Sep 05 '22 07:09 SpaceIm

list() commands are still above project().

evpobr avatar Sep 05 '22 07:09 evpobr

list() commands are still above project().

It's vcpkg specitic stuff, they want to catch that in their toolchain file I guess. But having package manager specific stuff in a CMakeLists is not a good idea, it should be packager manager agnostic.

SpaceIm avatar Sep 05 '22 07:09 SpaceIm

In fact, i can't find in the documentation where other commands are forbidden before calling project()?

https://cmake.org/cmake/help/latest/command/project.html

evpobr avatar Sep 05 '22 07:09 evpobr

Hi, I have the same problem as @SpaceIm. Because of #791 Conan package does not work. Also, I agree that CMakeLists should be package manager agnostic. The reason why the Conan package does not work is that the project() function loads a toolchain file that contains set() commands for setting options, but these options are already set.

jnytra avatar Oct 09 '23 12:10 jnytra