diff --git a/README.md b/README.md index 6f7e54ba..8d840076 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,9 @@ conan_cmake_configure(REQUIRES fmt/6.1.2 This function will return the auto-detected settings (things like *build_type*, *compiler* or *system name*) so you can pass that information to `conan_cmake_install`. This step is optional as you may -want to rely on profiles, lockfiles or any other way of passing that information. +want to rely on profiles, lockfiles or any other way of passing that information. This function will +also accept as arguments `BUILD_TYPE` and `ARCH`. Setting those arguments will force that settings +to the value provided (this can be useful for the multi-configuration generator scenario below). ```cmake conan_cmake_autodetect(settings) @@ -109,6 +111,36 @@ conan_cmake_run(REQUIRES fmt/1.9.4 SETTINGS build_type=Debug) ``` +## Using conan_cmake_autodetect() and conan_cmake_install() with Multi Configuration generators + +The recommended approach when using Multi Configuration generators like Visual Studio or Xcode is +looping through the `CMAKE_CONFIGURATION_TYPES` in your _CMakeLists.txt_ and calling +`conan_cmake_autodetect` with the `BUILD_TYPE` argument and `conan_cmake_install` for each one using +a Conan multiconfig generator like `cmake_find_package_multi`. Please check the example: + +```cmake +cmake_minimum_required(VERSION 3.5) +project(FormatOutput CXX) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) +list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}) +add_definitions("-std=c++11") +include(conan.cmake) + +conan_cmake_configure(REQUIRES fmt/6.1.2 GENERATORS cmake_find_package_multi) + +foreach(TYPE ${CMAKE_CONFIGURATION_TYPES}) + conan_cmake_autodetect(settings BUILD_TYPE ${TYPE}) + conan_cmake_install(PATH_OR_REFERENCE . + BUILD missing + REMOTE conan-center + SETTINGS ${settings}) +endforeach() + +find_package(fmt CONFIG) +add_executable(main main.cpp) +target_link_libraries(main fmt::fmt) +``` + ## conan_cmake_run() high level wrapper This function is not the recommended way of using cmake-conan any more and will be deprecated in the diff --git a/conan.cmake b/conan.cmake index f9879bf7..cabb352f 100644 --- a/conan.cmake +++ b/conan.cmake @@ -426,11 +426,11 @@ function(_collect_settings result) set(${result} ${detected_setings} PARENT_SCOPE) endfunction() -function(conan_cmake_autodetect detected_settings) - _conan_detect_build_type() +function(conan_cmake_autodetect detected_settings ${ARGV}) + _conan_detect_build_type(${ARGV}) _conan_check_system_name() _conan_check_language() - _conan_detect_compiler() + _conan_detect_compiler(${ARGV}) _collect_settings(collected_settings) set(${detected_settings} ${collected_settings} PARENT_SCOPE) endfunction() diff --git a/tests.py b/tests.py index 4dbac54b..3e6b704d 100644 --- a/tests.py +++ b/tests.py @@ -938,6 +938,30 @@ def test_vs_toolset(self): cmd = os.sep.join([".", "bin", "main"]) run(cmd) + @unittest.skipIf(platform.system() != "Windows", "Multi-config only in Windows") + def test_multi_new_flow(self): + content = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.5) + project(HelloProject CXX) + list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) + list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}) + add_definitions("-std=c++11") + include(conan.cmake) + conan_cmake_configure(REQUIRES hello/1.0 GENERATORS cmake_find_package_multi) + foreach(TYPE ${CMAKE_CONFIGURATION_TYPES}) + conan_cmake_autodetect(settings BUILD_TYPE ${TYPE}) + conan_cmake_install(PATH_OR_REFERENCE . + BUILD missing + REMOTE conan-center + SETTINGS ${settings}) + endforeach() + find_package(hello CONFIG) + add_executable(main main.cpp) + target_link_libraries(main hello::hello) + """) + save("CMakeLists.txt", content) + self._build_multi(["Release", "Debug", "MinSizeRel", "RelWithDebInfo"]) + @unittest.skipIf(platform.system() != "Windows", "Multi-config only in Windows") def test_multi(self): content = textwrap.dedent("""