Skip to content

Commit

Permalink
Fix conan_cmake_detect_vs_runtime when setting BUILD_TYPE without set…
Browse files Browse the repository at this point in the history
…ting CMAKE_BUILD_TYPE (conan-io#209)

* add test to reproduce

* check arguments to set build_type

* add test to check BUILD_TYPE over CMAKE_BUILD_TYPE

* minor changes

* Update conan.cmake

Co-authored-by: Ethan Slattery <[email protected]>

* simplify

* fix tests

* fix test

Co-authored-by: Ethan Slattery <[email protected]>
  • Loading branch information
czoido and CrustyAuklet authored Feb 23, 2021
1 parent 3290f8e commit 88fe226
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
16 changes: 14 additions & 2 deletions conan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function(conan_cmake_settings result)
endif()
endif()

conan_cmake_detect_vs_runtime(_vs_runtime)
conan_cmake_detect_vs_runtime(_vs_runtime ${ARGV})
message(STATUS "Conan: Detected VS runtime: ${_vs_runtime}")
set(_CONAN_SETTING_COMPILER_RUNTIME ${_vs_runtime})

Expand Down Expand Up @@ -357,7 +357,19 @@ function(conan_cmake_detect_unix_libcxx result)
endfunction()

function(conan_cmake_detect_vs_runtime result)
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)

conan_parse_arguments(${ARGV})
if(ARGUMENTS_BUILD_TYPE)
set(build_type "${ARGUMENTS_BUILD_TYPE}")
elseif(CMAKE_BUILD_TYPE)
set(build_type "${CMAKE_BUILD_TYPE}")
else()
message(FATAL_ERROR "Please specify in command line CMAKE_BUILD_TYPE (-DCMAKE_BUILD_TYPE=Release)")
endif()

if(build_type)
string(TOUPPER "${build_type}" build_type)
endif()
set(variables CMAKE_CXX_FLAGS_${build_type} CMAKE_C_FLAGS_${build_type} CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
foreach(variable ${variables})
if(NOT "${${variable}}" STREQUAL "")
Expand Down
40 changes: 30 additions & 10 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import json
import textwrap
from contextlib import contextmanager

from nose.plugins.attrib import attr

Expand All @@ -29,6 +30,16 @@ def run(cmd, ignore_errors=False):
generator = '-G "Unix Makefiles"'
# TODO: Test Xcode

@contextmanager
def ch_build_dir():
os.makedirs("build")
os.chdir("build")
try:
yield
finally:
os.chdir("../")
shutil.rmtree("build")

class CMakeConanTest(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -318,22 +329,31 @@ def test_build_type(self):
set(CMAKE_CXX_ABI_COMPILED 1)
message(STATUS "COMPILING-------")
cmake_minimum_required(VERSION 2.8)
project(FormatOutput CXX)
message(STATUS "CMAKE VERSION: ${CMAKE_VERSION}")
project(conan_wrapper CXX)
message(STATUS "CMAKE VERSION: ${{CMAKE_VERSION}}")
include(conan.cmake)
conan_cmake_run(BASIC_SETUP
BUILD_TYPE None)
BUILD_TYPE {0})
if(NOT ${CONAN_SETTINGS_BUILD_TYPE} STREQUAL "None")
message(FATAL_ERROR "CMAKE BUILD TYPE is not None!")
if(NOT ${{CONAN_SETTINGS_BUILD_TYPE}} STREQUAL "{0}")
message(FATAL_ERROR "CMAKE BUILD TYPE is not {0}!")
endif()
""")
save("CMakeLists.txt", content)

os.makedirs("build")
os.chdir("build")
run("cmake .. %s -DCMAKE_BUILD_TYPE=Release" % (generator))
save("CMakeLists.txt", content.format("Release"))
with ch_build_dir():
run("cmake .. %s -DCMAKE_BUILD_TYPE=Debug > output.txt" % (generator))
with open('output.txt', 'r') as file:
data = file.read()
assert "build_type=Release" in data

# https://github.com/conan-io/cmake-conan/issues/89
save("CMakeLists.txt", content.format("Debug"))
with ch_build_dir():
run("cmake .. %s > output.txt" % (generator))
with open('output.txt', 'r') as file:
data = file.read()
assert "build_type=Debug" in data

def test_settings(self):
content = textwrap.dedent("""
Expand Down

0 comments on commit 88fe226

Please sign in to comment.