Skip to content

Commit

Permalink
Add error if missing CMAKE_BUILD_TYPE when invoking CMake (#11294)
Browse files Browse the repository at this point in the history
* add error if missing build type in CMake

* update test

* fix tesst

* use macro

* update test

* wip

* fix test

* minor changes

* fix tests and force cmake version

* minor changes

* wip

* green
  • Loading branch information
czoido committed May 23, 2022
1 parent 98d1954 commit db343fe
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
2 changes: 2 additions & 0 deletions conan/tools/cmake/cmakedeps/templates/config.py
Expand Up @@ -60,6 +60,8 @@ def template(self):
include(${CMAKE_CURRENT_LIST_DIR}/{{ targets_include_file }})
include(CMakeFindDependencyMacro)
check_build_type_defined()
foreach(_DEPENDENCY {{ '${' + pkg_name + '_FIND_DEPENDENCY_NAMES' + '}' }} )
# Check that we have not already called a find_package with the transitive dependency
if(NOT {{ '${_DEPENDENCY}' }}_FOUND)
Expand Down
10 changes: 10 additions & 0 deletions conan/tools/cmake/cmakedeps/templates/macros.py
Expand Up @@ -88,4 +88,14 @@ def template(self):
set(${out_libraries} ${_out_libraries} PARENT_SCOPE)
set(${out_libraries_target} ${_out_libraries_target} PARENT_SCOPE)
endfunction()
macro(check_build_type_defined)
# Check that the -DCMAKE_BUILD_TYPE argument is always present
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT isMultiConfig AND NOT CMAKE_BUILD_TYPE)
message(FATAL_ERROR "Please, set the CMAKE_BUILD_TYPE variable when calling to CMake "
"adding the '-DCMAKE_BUILD_TYPE=<build_type>' argument.")
endif()
endmacro()
""")
Expand Up @@ -72,7 +72,7 @@ def generate(self):
clean_first=True)
client.run("install . -pr:h=default -pr:b=default")
# The compilation works, so it finds the doxygen without transitive failures
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake")
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release")

# Assert there is no zlib target
assert "Target declared 'zlib::zlib'" not in client.out
Expand Down
Expand Up @@ -378,3 +378,65 @@ def package_info(self):
data = os.path.join("consumer/build/generators/mylib-release-x86_64-data.cmake")
contents = client.load(data)
assert 'set(ZLIB_FIND_MODE "")' in contents


@pytest.mark.tool_cmake(version="3.19")
def test_error_missing_build_type():
# https://github.com/conan-io/conan/issues/11168
client = TestClient()

client.run("new hello/1.0 -m=cmake_lib")
client.run("create . -tf=None")

conanfile = textwrap.dedent("""
[requires]
hello/1.0
[generators]
CMakeDeps
CMakeToolchain
""")

main = textwrap.dedent("""
#include <hello.h>
int main() {hello();return 0;}
""")

cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(app)
find_package(hello REQUIRED)
add_executable(app)
target_link_libraries(app hello::hello)
target_sources(app PRIVATE main.cpp)
""")

if platform.system() != "Windows":
client.save({
"conanfile.txt": conanfile,
"main.cpp": main,
"CMakeLists.txt": cmakelists
}, clean_first=True)

client.run("install .")
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -G 'Unix Makefiles'", assert_error=True)
assert "Please, set the CMAKE_BUILD_TYPE variable when calling to CMake" in client.out

client.save({
"conanfile.txt": conanfile,
"main.cpp": main,
"CMakeLists.txt": cmakelists
}, clean_first=True)

client.run("install .")

generator = {
"Windows": '-G "Visual Studio 15 2017"',
"Darwin": '-G "Xcode"',
"Linux": '-G "Ninja Multi-Config"'
}.get(platform.system())

client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake {}".format(generator))
client.run_command("cmake --build . --config Release")
run_app = r".\Release\app.exe" if platform.system() == "Windows" else "./Release/app"
client.run_command(run_app)
assert "Hello World Release!" in client.out
Expand Up @@ -241,8 +241,9 @@ class Conan(ConanFile):

client.run("create dep")
client.run("install .")
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=./conan_toolchain.cmake "
"-Werror=dev --warn-uninitialized")
build_type = "-DCMAKE_BUILD_TYPE=Release" if platform.system() != "Windows" else ""
client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=./conan_toolchain.cmake {}"
"-Werror=dev --warn-uninitialized".format(build_type))
assert "Using Conan toolchain" in client.out
# The real test is that there are no errors, it returns successfully

Expand Down
Expand Up @@ -29,7 +29,8 @@ def __init__(self, package=None, library=None, framework=None, include=None, pro


def _cmake_command_toolchain(find_root_path_modes):
cmake_command = "cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan_toolchain.cmake"
build_type = "-DCMAKE_BUILD_TYPE=Release" if platform.system() != "Windows" else ""
cmake_command = "cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan_toolchain.cmake {}".format(build_type)
if find_root_path_modes.package:
cmake_command += " -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE={}".format(find_root_path_modes.package)
if find_root_path_modes.library:
Expand Down

0 comments on commit db343fe

Please sign in to comment.