def test_run_tests(self): settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.compiler.runtime = "MDd" settings.arch = "x86" settings.build_type = None conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cmake.test() self.assertIn( 'cmake --build ' '%s' % CMakeTest.scape('. --target RUN_TESTS -- /m:%i' % cpu_count()), conan_file.command) cmake.generator = "Ninja Makefiles" cmake.test() self.assertEqual( 'cmake --build ' '%s' % CMakeTest.scape('. --target test -- -j%i' % cpu_count()), conan_file.command) cmake.generator = "NMake Makefiles" cmake.test() self.assertEqual( 'cmake --build ' '%s' % CMakeTest.scape('. --target test -- -j%i' % cpu_count()), conan_file.command)
def test_shared(self): settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.arch = "x86" settings.os = "Windows" conan_file = ConanFileMock(shared=True) conan_file.settings = settings cmake = CMake(conan_file) self.assertEquals(cmake.definitions["BUILD_SHARED_LIBS"], "ON") conan_file = ConanFileMock(shared=False) conan_file.settings = settings cmake = CMake(conan_file) self.assertEquals(cmake.definitions["BUILD_SHARED_LIBS"], "OFF") conan_file = ConanFileMock(shared=None) conan_file.settings = settings cmake = CMake(conan_file) self.assertNotIn("BUILD_SHARED_LIBS", cmake.definitions)
def test_sysroot(self): settings = Settings.loads(default_settings_yml) conan_file = ConanFileMock() conan_file.settings = settings settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.arch = "x86" settings.os = "Windows" cmake = CMake(conan_file) if platform.system() == "Windows": self.assertNotIn("-DCMAKE_SYSROOT=", cmake.flags) # Now activate cross build and check sysroot and system processor with (tools.environment_append({ "CONAN_CMAKE_SYSTEM_NAME": "Android", "CONAN_CMAKE_SYSTEM_PROCESSOR": "somevalue" })): cmake = CMake(conan_file) self.assertEquals(cmake.definitions["CMAKE_SYSROOT"], "/path/to/sysroot") self.assertEquals(cmake.definitions["CMAKE_SYSTEM_PROCESSOR"], "somevalue")
def cmake_make_program_test(self): settings = Settings.loads(default_settings_yml) settings.os = "Linux" settings.compiler = "gcc" settings.compiler.version = "6.3" settings.arch = "x86" settings.build_type = "Release" conan_file = ConanFileMock() conan_file.settings = settings conan_file.source_folder = os.path.join(self.tempdir, "my_cache_source_folder") conan_file.build_folder = os.path.join(self.tempdir, "my_cache_build_folder") # Existing make make_path = os.path.join(self.tempdir, "make") save(make_path, "") st = os.stat(make_path) os.chmod(make_path, st.st_mode | stat.S_IEXEC) with tools.environment_append({"CONAN_MAKE_PROGRAM": make_path}): cmake = CMake(conan_file) self.assertEquals(cmake.definitions["CMAKE_MAKE_PROGRAM"], make_path) # Not existing make with tools.environment_append({"CONAN_MAKE_PROGRAM": "fake_path/make"}): cmake = CMake(conan_file) self.assertNotIn("CMAKE_MAKE_PROGRAM", cmake.definitions) self.assertIn( "The specified make program 'fake_path/make' cannot be found", conan_file.output)
def config_patch_test(self): conan_file = ConanFileMock() conan_file.name = "MyPkg" conan_file.settings = Settings() conan_file.source_folder = os.path.join(self.tempdir, "src") conan_file.build_folder = os.path.join(self.tempdir, "build") conan_file.package_folder = os.path.join(self.tempdir, "pkg") msg = "FOLDER: " + conan_file.package_folder for folder in (conan_file.build_folder, conan_file.package_folder): save(os.path.join(folder, "file1.cmake"), "Nothing") save(os.path.join(folder, "file2"), msg) save(os.path.join(folder, "file3.txt"), msg) save(os.path.join(folder, "file3.cmake"), msg) save(os.path.join(folder, "sub", "file3.cmake"), msg) cmake = CMake(conan_file, generator="Unix Makefiles") cmake.patch_config_paths() for folder in (conan_file.build_folder, conan_file.package_folder): self.assertEqual("Nothing", load(os.path.join(folder, "file1.cmake"))) self.assertEqual(msg, load(os.path.join(folder, "file2"))) self.assertEqual(msg, load(os.path.join(folder, "file3.txt"))) self.assertEqual("FOLDER: ${CONAN_MYPKG_ROOT}", load(os.path.join(folder, "file3.cmake"))) self.assertEqual("FOLDER: ${CONAN_MYPKG_ROOT}", load(os.path.join(folder, "sub", "file3.cmake")))
def set_toolset_test(self): settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "15" settings.arch = "x86" settings.compiler.toolset = "v140" # Will be overwritten by parameter conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file, toolset="v141") self.assertIn('-T "v141"', cmake.command_line) # DEPRECATED VARIABLE, NOT MODIFY ANYMORE THE TOOLSET with tools.environment_append({"CONAN_CMAKE_TOOLSET": "v141"}): cmake = CMake(conan_file) self.assertNotIn('-T "v141"', cmake.command_line) settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "15" settings.arch = "x86" settings.compiler.toolset = "v140" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) self.assertIn('-T "v140"', cmake.command_line)
def partial_build_test(self): conan_file = ConanFileMock() conan_file.settings = Settings() conan_file.should_configure = False conan_file.should_build = False conan_file.should_install = False cmake = CMake(conan_file, generator="Unix Makefiles") cmake.configure() self.assertIsNone(conan_file.command) cmake.build() self.assertIsNone(conan_file.command) cmake.install() self.assertIsNone(conan_file.command) conan_file.name = None cmake.patch_config_paths()
def deleted_os_test(self): partial_settings = """ os: [Linux] arch: [x86_64] compiler: gcc: version: ["4.9"] build_type: [ Release] """ settings = Settings.loads(partial_settings) settings.os = "Linux" settings.compiler = "gcc" settings.compiler.version = "4.9" settings.arch = "x86_64" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cross = "-DCMAKE_SYSTEM_NAME=\"Linux\" -DCMAKE_SYSROOT=\"/path/to/sysroot\" " if platform.system( ) != "Linux" else "" self.assertEqual( '-G "Unix Makefiles" %s-DCONAN_EXPORTED="1" -DCONAN_COMPILER="gcc" ' '-DCONAN_COMPILER_VERSION="4.9" -DCONAN_CXX_FLAGS="-m64" ' '-DCONAN_SHARED_LINKER_FLAGS="-m64" -DCONAN_C_FLAGS="-m64" -Wno-dev' % cross, cmake.command_line)
def cmake_generator_test(self): conan_file = ConanFileMock() conan_file.settings = Settings() with tools.environment_append( {"CONAN_CMAKE_GENERATOR": "My CMake Generator"}): cmake = CMake(conan_file) self.assertIn('-G "My CMake Generator"', cmake.command_line)
def check(text, build_config, generator=None): os = str(settings.os) os_ver = str(settings.os.version) if settings.get_safe( 'os.version') else None for cmake_system_name in (True, False): cross_ver = ("-DCMAKE_SYSTEM_VERSION=\"%s\" " % os_ver) if os_ver else "" cross = ( "-DCMAKE_SYSTEM_NAME=\"%s\" %s-DCMAKE_SYSROOT=\"/path/to/sysroot\" " % ({ "Macos": "Darwin" }.get(os, os), cross_ver) if (platform.system() != os and cmake_system_name) else "") cmake = CMake(conan_file, generator=generator, cmake_system_name=cmake_system_name) new_text = text.replace("-DCONAN_EXPORTED", "%s-DCONAN_EXPORTED" % cross) if "Visual Studio" in text: cores = ('-DCONAN_CXX_FLAGS="/MP{0}" ' '-DCONAN_C_FLAGS="/MP{0}" '.format( tools.cpu_count())) new_text = new_text.replace("-Wno-dev", "%s-Wno-dev" % cores) self.assertEqual(new_text, cmake.command_line) self.assertEqual(build_config, cmake.build_config)
def test_deprecated_behaviour(self): """"Remove when deprecate the old settings parameter to CMake and conanfile to configure/build/test""" settings = Settings.loads(default_settings_yml) settings.os = "Windows" conan_file = ConanFileMock() conan_file.settings = settings with self.assertRaises(ConanException): CMake(settings)
def test_cmake_system_version_android(self): with tools.environment_append({"CONAN_CMAKE_SYSTEM_NAME": "SomeSystem", "CONAN_CMAKE_GENERATOR": "SomeGenerator"}): settings = Settings.loads(default_settings_yml) settings.os = "WindowsStore" settings.os.version = "8.1" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) self.assertEquals(cmake.definitions["CMAKE_SYSTEM_VERSION"], "8.1") settings = Settings.loads(default_settings_yml) settings.os = "Android" settings.os.api_level = "32" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) self.assertEquals(cmake.definitions["CMAKE_SYSTEM_VERSION"], "32")
def assert_fpic(the_settings, input_shared, input_fpic, expected_option): options = [] values = {} if input_shared is not None: options.append('"shared": [True, False]') values["shared"] = input_shared if input_fpic is not None: options.append('"fPIC": [True, False]') values["fPIC"] = input_fpic conan_file = ConanFileMock(options='{%s}' % ", ".join(options), options_values=values) conan_file.settings = the_settings cmake = CMake(conan_file) cmake.configure() if expected_option is not None: self.assertEquals(cmake.definitions["CONAN_CMAKE_POSITION_INDEPENDENT_CODE"], expected_option) else: self.assertNotIn("CONAN_CMAKE_POSITION_INDEPENDENT_CODE", cmake.definitions)
def test_verbose(self): settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.arch = "x86" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) self.assertNotIn("CMAKE_VERBOSE_MAKEFILE", cmake.definitions) cmake.verbose = True self.assertEquals(cmake.definitions["CMAKE_VERBOSE_MAKEFILE"], "ON") cmake.verbose = False self.assertEquals(cmake.definitions["CMAKE_VERBOSE_MAKEFILE"], "OFF") cmake.definitions["CMAKE_VERBOSE_MAKEFILE"] = True self.assertTrue(cmake.verbose) cmake.definitions["CMAKE_VERBOSE_MAKEFILE"] = False self.assertFalse(cmake.verbose) del cmake.definitions["CMAKE_VERBOSE_MAKEFILE"] self.assertFalse(cmake.verbose)
def test_cmake_definitions_cmake_not_in_path(self): def raise_get_version(): raise ConanException('Error retrieving CMake version') with patch.object(conans.client.build.cmake.CMake, "get_version", side_effect=raise_get_version): settings_mock = _MockSettings(build_type="Release") conanfile = ConanFile(Mock(), None) install_folder = "/c/foo/testing" setattr(conanfile, "install_folder", install_folder) conanfile.initialize(settings_mock, EnvValues()) assert CMake(conanfile)
def test_build_folder_vars(self, conan_os, expected_os): settings = MockSettings({ "os": conan_os, "compiler": "apple-clang", "compiler.version": "11.0", "arch": "ios_fat" }) conanfile = ConanFileMock() conanfile.settings = settings cmake = CMake(conanfile) self.assertNotIn("CMAKE_OSX_ARCHITECTURES", cmake.definitions) self.assertIn(expected_os, cmake.definitions["CMAKE_OSX_SYSROOT"])
def config_patch_deps_test(self): conan_file = ConanFileMock() conan_file.name = "MyPkg" conan_file.settings = Settings() conan_file.source_folder = os.path.join(self.tempdir, "src") conan_file.build_folder = os.path.join(self.tempdir, "build") conan_file.package_folder = os.path.join(self.tempdir, "pkg") conan_file.deps_cpp_info = DepsCppInfo() ref = ConanFileReference.loads("MyPkg1/0.1@user/channel") cpp_info = CppInfo(self.tempdir2) conan_file.deps_cpp_info.update(cpp_info, ref.name) self.tempdir = temp_folder(path_with_spaces=False) self.assertEqual(list(conan_file.deps_cpp_info.deps), ['MyPkg1']) self.assertEqual(conan_file.deps_cpp_info['MyPkg1'].rootpath, self.tempdir2) msg = "FOLDER: " + self.tempdir2 for folder in (conan_file.build_folder, conan_file.package_folder): save(os.path.join(folder, "file1.cmake"), "Nothing") save(os.path.join(folder, "file2"), msg) save(os.path.join(folder, "file3.txt"), msg) save(os.path.join(folder, "file3.cmake"), msg) save(os.path.join(folder, "sub", "file3.cmake"), msg) cmake = CMake(conan_file, generator="Unix Makefiles") cmake.patch_config_paths() for folder in (conan_file.build_folder, conan_file.package_folder): self.assertEqual("Nothing", load(os.path.join(folder, "file1.cmake"))) self.assertEqual(msg, load(os.path.join(folder, "file2"))) self.assertEqual(msg, load(os.path.join(folder, "file3.txt"))) self.assertEqual("FOLDER: ${CONAN_MYPKG1_ROOT}", load(os.path.join(folder, "file3.cmake"))) self.assertEqual("FOLDER: ${CONAN_MYPKG1_ROOT}", load(os.path.join(folder, "sub", "file3.cmake")))
def test_cmake_definitions(self, conan_arch, conan_os, expected_arch, expected_os): settings = Settings.loads(get_default_settings_yml()) settings.os = conan_os settings.compiler = "apple-clang" settings.compiler.version = "11.0" settings.arch = conan_arch conanfile = ConanFileMock() conanfile.settings = settings cmake = CMake(conanfile) self.assertEqual(cmake.definitions["CMAKE_OSX_ARCHITECTURES"], expected_arch) self.assertIn(expected_os, cmake.definitions["CMAKE_OSX_SYSROOT"])
def test_cores_ancient_visual(self): settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "9" settings.compiler.runtime = "MDd" settings.arch = "x86" settings.build_type = None conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cmake.build() self.assertNotIn("/m", conan_file.command) settings.compiler.version = "10" cmake = CMake(conan_file) cmake.build() self.assertIn("/m", conan_file.command)
def conan_set_std_branch(): # Replicate logic from cmake_common definition of 'macro(conan_set_std)' cmake_version = CMake.get_version() return cmake_version < Version("3.12")
def convenient_functions_test(self): settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.compiler.runtime = "MDd" settings.arch = "x86" settings.build_type = None if sys.platform == 'win32': dot_dir = "." tempdir = self.tempdir else: dot_dir = "'.'" tempdir = "'" + self.tempdir + "'" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cross = "-DCMAKE_SYSTEM_NAME=\"Windows\" -DCMAKE_SYSROOT=\"/path/to/sysroot\" " if platform.system() != "Windows" else "" target_test = CMakeTest.scape('--target RUN_TESTS') cmake.configure() cores = '-DCONAN_CXX_FLAGS="/MP{0}" -DCONAN_C_FLAGS="/MP{0}" '.format(tools.cpu_count()) self.assertEqual('cd {0} && cmake -G "Visual Studio 12 2013" -DCONAN_LINK_RUNTIME="/MDd" {1}-DCONAN_EXPORTED="1"' ' -DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="12" {2}' '-Wno-dev {0}'.format(dot_dir, cross, cores), conan_file.command) cmake.build() self.assertEqual('cmake --build %s %s' % (dot_dir, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.test() self.assertEqual('cmake --build %s %s %s' % (dot_dir, target_test, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) settings.build_type = "Debug" cmake = CMake(conan_file) cmake.build() self.assertEqual('cmake --build %s --config Debug %s' % (dot_dir,(CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.test() self.assertEqual('cmake --build %s --config Debug %s %s' % (dot_dir, target_test, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.configure(source_dir="/source", build_dir=self.tempdir, args=['--foo "bar"'], defs={"SHARED": True}) if sys.platform == 'win32': escaped_args = r'"--foo \"bar\"" -DSHARED="True" /source' else: escaped_args = "'--foo \"bar\"' -DSHARED=\"True\" '/source'" self.assertEqual('cd %s && cmake -G "Visual Studio 12 2013" -DCONAN_LINK_RUNTIME="/MDd" %s-DCONAN_EXPORTED="1" ' '-DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="12" %s' '-Wno-dev %s' % (tempdir, cross, cores, escaped_args), conan_file.command) cmake.build(args=["--bar 'foo'"], target="install") if sys.platform == 'win32': escaped_args = '--target install "--bar \'foo\'"' else: escaped_args = r"'--target' 'install' '--bar '\''foo'\'''" self.assertEqual('cmake --build %s --config Debug %s %s' % (tempdir, escaped_args, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.test(args=["--bar 'foo'"]) if sys.platform == 'win32': escaped_args = '%s "--bar \'foo\'"' % target_test else: escaped_args = r"%s '--bar '\''foo'\'''" % target_test self.assertEqual('cmake --build %s --config Debug %s %s' % (tempdir, escaped_args, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) settings.build_type = "Release" cmake = CMake(conan_file) cmake.build() self.assertEqual('cmake --build %s --config Release %s' % (dot_dir, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.test() self.assertEqual('cmake --build %s --config Release %s %s' % (dot_dir, target_test, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.build(build_dir=self.tempdir) self.assertEqual('cmake --build %s --config Release %s' % (tempdir, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) cmake.test(build_dir=self.tempdir) self.assertEqual('cmake --build %s --config Release %s %s' % (tempdir, target_test, (CMakeTest.scape('-- /m:%i' % cpu_count()))), conan_file.command) settings.compiler = "gcc" settings.compiler.version = "5.4" cmake = CMake(conan_file) cmake.build() self.assertEqual('cmake --build %s' % (CMakeTest.scape('. -- -j%i' % cpu_count())), conan_file.command) cmake.test() self.assertEqual('cmake --build %s' % (CMakeTest.scape('. --target test -- -j%i' % cpu_count())), conan_file.command) cmake.build(args=['foo', '--', 'bar']) self.assertEqual('cmake --build %s' % (CMakeTest.scape('. foo -- bar -j%i' % cpu_count())), conan_file.command) cmake.test(args=['foo', '--', 'bar']) self.assertEqual('cmake --build %s' % (CMakeTest.scape('. --target test foo -- bar -j%i' % cpu_count())), conan_file.command) cmake = CMake(conan_file, parallel=False) cmake.build() self.assertEqual('cmake --build %s' % CMakeTest.scape('.'), conan_file.command) cmake.test() self.assertEqual('cmake --build %s' % CMakeTest.scape('. --target test'), conan_file.command)
def test_pkg_config_path(self): conanfile = ConanFileMock() conanfile.generators = ["pkg_config"] conanfile.install_folder = "/my_install/folder/" settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.arch = "x86" conanfile.settings = settings cmake = CMake(conanfile) cmake.configure() self.assertEquals(conanfile.captured_env["PKG_CONFIG_PATH"], "/my_install/folder/") conanfile.generators = [] cmake = CMake(conanfile) cmake.configure() self.assertNotIn("PKG_CONFIG_PATH", conanfile.captured_env) cmake = CMake(conanfile) cmake.configure(pkg_config_paths=["reldir1", "/abspath2/to/other"]) self.assertEquals( conanfile.captured_env["PKG_CONFIG_PATH"], os.path.pathsep.join( ["/my_install/folder/reldir1", "/abspath2/to/other"])) # If there is already a PKG_CONFIG_PATH do not set it conanfile.generators = ["pkg_config"] cmake = CMake(conanfile) with tools.environment_append( {"PKG_CONFIG_PATH": "do_not_mess_with_this"}): cmake.configure() self.assertEquals(conanfile.captured_env["PKG_CONFIG_PATH"], "do_not_mess_with_this")
def instance_with_os_build(os_build): settings = Settings.loads(default_settings_yml) settings.os_build = os_build conan_file = ConanFileMock() conan_file.settings = settings return CMake(conan_file)
def test_clean_sh_path(self): if platform.system() != "Windows": return os.environ["PATH"] = os.environ.get("PATH", "") + os.pathsep + self.tempdir save(os.path.join(self.tempdir, "sh.exe"), "Fake sh") conanfile = ConanFileMock() settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "12" settings.arch = "x86" conanfile.settings = settings cmake = CMake(conanfile) cmake.configure() self.assertIn(self.tempdir, conanfile.path) cmake.generator = "MinGW Makefiles" cmake.configure() self.assertNotIn(self.tempdir, conanfile.path) # Automatic gcc settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "gcc" settings.compiler.version = "5.4" settings.arch = "x86" conanfile.settings = settings cmake = CMake(conanfile) cmake.configure() self.assertNotIn(self.tempdir, conanfile.path)
def convenient_functions_test(self): settings = Settings.loads(default_settings_yml) settings.os = "Android" settings.os_build = "Windows" # Here we are declaring we are cross building settings.compiler = "gcc" settings.compiler.version = "5.4" settings.arch = "armv7" settings.build_type = None if platform.system() == 'Windows': dot_dir = "." tempdir = self.tempdir else: dot_dir = "'.'" tempdir = "'" + self.tempdir + "'" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cross = '-DCMAKE_SYSTEM_NAME="Android"' \ ' -DCMAKE_SYSROOT="/path/to/sysroot"' \ ' -DCMAKE_ANDROID_ARCH_ABI="armeabi-v7a"' target_test = CMakeTest.scape('--target test') cmake.configure() self.assertEqual( 'cd {0} && cmake -G "Unix Makefiles" ' '{1} -DCONAN_EXPORTED="1"' ' -DCONAN_COMPILER="gcc" -DCONAN_COMPILER_VERSION="5.4" ' '-Wno-dev {0}'.format(dot_dir, cross), conan_file.command) cmake.build() self.assertEqual( 'cmake --build %s %s' % (dot_dir, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.test() self.assertEqual( 'cmake --build %s %s %s' % (dot_dir, target_test, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) settings.build_type = "Debug" cmake = CMake(conan_file) cmake.build() self.assertEqual( 'cmake --build %s %s' % (dot_dir, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.test() self.assertEqual( 'cmake --build %s %s %s' % (dot_dir, target_test, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.configure(source_dir="/source", build_dir=self.tempdir, args=['--foo "bar"'], defs={"SHARED": True}) if sys.platform == 'win32': escaped_args = r'"--foo \"bar\"" -DSHARED="True" /source' else: escaped_args = "'--foo \"bar\"' -DSHARED=\"True\" '/source'" self.assertEqual( 'cd %s && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Debug" ' '%s -DCONAN_EXPORTED="1" ' '-DCONAN_COMPILER="gcc" -DCONAN_COMPILER_VERSION="5.4" ' '-Wno-dev %s' % (tempdir, cross, escaped_args), conan_file.command) cmake.build(args=["--bar 'foo'"], target="install") if platform.system() == 'Windows': escaped_args = '--target install "--bar \'foo\'"' else: escaped_args = r"'--target' 'install' '--bar '\''foo'\'''" self.assertEqual( 'cmake --build %s %s %s' % (tempdir, escaped_args, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.test(args=["--bar 'foo'"]) if sys.platform == 'win32': escaped_args = '%s "--bar \'foo\'"' % target_test else: escaped_args = r"%s '--bar '\''foo'\'''" % target_test self.assertEqual( 'cmake --build %s %s %s' % (tempdir, escaped_args, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) settings.build_type = "Release" cmake = CMake(conan_file) cmake.build() self.assertEqual( 'cmake --build %s %s' % (dot_dir, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.test() self.assertEqual( 'cmake --build %s %s %s' % (dot_dir, target_test, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.build(build_dir=self.tempdir) self.assertEqual( 'cmake --build %s %s' % (tempdir, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) cmake.test(build_dir=self.tempdir) self.assertEqual( 'cmake --build %s %s %s' % (tempdir, target_test, (CMakeTest.scape('-- -j%i' % cpu_count()))), conan_file.command) settings.compiler = "gcc" settings.compiler.version = "5.4" cmake = CMake(conan_file) cmake.build() self.assertEqual( 'cmake --build %s' % (CMakeTest.scape('. -- -j%i' % cpu_count())), conan_file.command) cmake.test() self.assertEqual( 'cmake --build ' '%s' % (CMakeTest.scape('. --target test -- -j%i' % cpu_count())), conan_file.command) cmake.build(args=['foo', '--', 'bar']) self.assertEqual( 'cmake --build %s' % (CMakeTest.scape('. foo -- bar -j%i' % cpu_count())), conan_file.command) cmake.test(args=['foo', '--', 'bar']) self.assertEqual( 'cmake --build ' '%s' % (CMakeTest.scape('. --target test foo -- bar -j%i' % cpu_count())), conan_file.command) cmake = CMake(conan_file, parallel=False) cmake.build() self.assertEqual('cmake --build %s' % CMakeTest.scape('.'), conan_file.command) cmake.test() self.assertEqual( 'cmake --build %s' % CMakeTest.scape('. --target test'), conan_file.command)
def should_flags_test(self): conanfile = ConanFileMock() conanfile.settings = Settings() conanfile.should_configure = False conanfile.should_build = True conanfile.should_install = False conanfile.should_test = True conanfile.package_folder = "pkg_folder" cmake = CMake(conanfile, generator="Unix Makefiles") cmake.configure() self.assertIsNone(conanfile.command) cmake.build() self.assertIn("cmake --build %s" % CMakeTest.scape(". -- -j%i" % cpu_count()), conanfile.command) cmake.install() self.assertNotIn("cmake --build %s" % CMakeTest.scape(". --target install -- -j%i" % cpu_count()), conanfile.command) cmake.test() self.assertIn("cmake --build %s" % CMakeTest.scape(". --target test -- -j%i" % cpu_count()), conanfile.command) conanfile.should_build = False cmake.configure() self.assertNotIn("cd . && cmake", conanfile.command) cmake.build() self.assertNotIn("cmake --build %s" % CMakeTest.scape(". -- -j%i" % cpu_count()), conanfile.command) cmake.install() self.assertNotIn("cmake --build %s" % CMakeTest.scape(". --target install -- -j%i" % cpu_count()), conanfile.command) cmake.test() self.assertIn("cmake --build %s" % CMakeTest.scape(". --target test -- -j%i" % cpu_count()), conanfile.command) conanfile.should_install = True conanfile.should_test = False cmake.configure() self.assertNotIn("cd . && cmake", conanfile.command) cmake.build() self.assertNotIn("cmake --build %s" % CMakeTest.scape(". -- -j%i" % cpu_count()), conanfile.command) cmake.install() self.assertIn("cmake --build %s" % CMakeTest.scape(". --target install -- -j%i" % cpu_count()), conanfile.command) cmake.test() self.assertNotIn("cmake --build %s" % CMakeTest.scape(". --target test -- -j%i" % cpu_count()), conanfile.command)
def build_type_ovewrite_test(self): settings = Settings.loads(default_settings_yml) settings.os = "Linux" settings.compiler = "gcc" settings.compiler.version = "6.3" settings.arch = "x86" settings.build_type = "Release" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cmake.build_type = "Debug" self.assertIn( 'WARN: Set CMake build type "Debug" is different than the ' 'settings build_type "Release"', conan_file.output) self.assertEquals(cmake.build_type, "Debug") self.assertIn('-DCMAKE_BUILD_TYPE="Debug"', cmake.command_line) conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) self.assertNotIn('WARN: Set CMake build type ', conan_file.output) self.assertEquals(cmake.build_type, "Release") # Now with visual, (multiconfig) settings = Settings.loads(default_settings_yml) settings.os = "Windows" settings.compiler = "Visual Studio" settings.compiler.version = "15" settings.arch = "x86" settings.build_type = "Release" conan_file = ConanFileMock() conan_file.settings = settings cmake = CMake(conan_file) cmake.build_type = "Debug" self.assertIn( 'WARN: Set CMake build type "Debug" is different than the ' 'settings build_type "Release"', conan_file.output) self.assertEquals(cmake.build_type, "Debug") self.assertNotIn('-DCMAKE_BUILD_TYPE="Debug"', cmake.command_line) self.assertIn("--config Debug", cmake.build_config) cmake = CMake(conan_file) cmake.build_type = "Release" self.assertIn("--config Release", cmake.build_config)
def folders_test(self): def quote_var(var): return "'%s'" % var if platform.system() != "Windows" else var settings = Settings.loads(default_settings_yml) settings.os = "Linux" settings.compiler = "gcc" settings.compiler.version = "6.3" settings.arch = "x86" settings.build_type = "Release" conan_file = ConanFileMock() conan_file.settings = settings conan_file.source_folder = os.path.join(self.tempdir, "my_cache_source_folder") conan_file.build_folder = os.path.join(self.tempdir, "my_cache_build_folder") cmake = CMake(conan_file) cmake.configure(source_dir="../subdir", build_dir="build") linux_stuff = '-DCMAKE_SYSTEM_NAME="Linux" ' \ '-DCMAKE_SYSROOT="/path/to/sysroot" ' if platform.system() != "Linux" else "" base_cmd = ' && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Release" %s' \ '-DCONAN_EXPORTED="1" -DCONAN_COMPILER="gcc" ' \ '-DCONAN_COMPILER_VERSION="6.3" ' \ '-DCONAN_CXX_FLAGS="-m32" -DCONAN_SHARED_LINKER_FLAGS="-m32" ' \ '-DCONAN_C_FLAGS="-m32" -Wno-dev ' % linux_stuff build_expected = quote_var("build") source_expected = quote_var("../subdir") self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) cmake.configure(build_dir="build") build_expected = quote_var("build") source_expected = quote_var( os.path.join(self.tempdir, "my_cache_source_folder")) self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) cmake.configure() build_expected = quote_var( os.path.join(self.tempdir, "my_cache_build_folder")) source_expected = quote_var( os.path.join(self.tempdir, "my_cache_source_folder")) self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) cmake.configure(source_folder="source", build_folder="build") build_expected = quote_var( os.path.join( os.path.join(self.tempdir, "my_cache_build_folder", "build"))) source_expected = quote_var( os.path.join( os.path.join(self.tempdir, "my_cache_source_folder", "source"))) self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) conan_file.in_local_cache = True cmake.configure(source_folder="source", build_folder="build", cache_build_folder="rel_only_cache") build_expected = quote_var( os.path.join(self.tempdir, "my_cache_build_folder", "rel_only_cache")) source_expected = quote_var( os.path.join(self.tempdir, "my_cache_source_folder", "source")) self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) conan_file.in_local_cache = False cmake.configure(source_folder="source", build_folder="build", cache_build_folder="rel_only_cache") build_expected = quote_var( os.path.join(self.tempdir, "my_cache_build_folder", "build")) source_expected = quote_var( os.path.join(self.tempdir, "my_cache_source_folder", "source")) self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) conan_file.in_local_cache = True cmake.configure(build_dir="build", cache_build_folder="rel_only_cache") build_expected = quote_var( os.path.join(self.tempdir, "my_cache_build_folder", "rel_only_cache")) source_expected = quote_var( os.path.join(self.tempdir, "my_cache_source_folder")) self.assertEquals( conan_file.command, 'cd %s' % build_expected + base_cmd + source_expected) # Raise mixing with self.assertRaisesRegexp(ConanException, "Use 'build_folder'/'source_folder'"): cmake.configure(source_folder="source", build_dir="build")