def reuse_msbuild_object_test(self): # https://github.com/conan-io/conan/issues/2865 conan_build_vs = """ from conans import ConanFile, MSBuild class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler", "cppstd" def configure(self): del self.settings.compiler.runtime del self.settings.build_type def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln", build_type="Release") msbuild.build("MyProject.sln", build_type="Debug") self.output.info("build() completed") """ client = TestClient() files = get_vs_project_files() files[CONANFILE] = conan_build_vs client.save(files) client.run("create . danimtb/testing") self.assertIn("build() completed", client.out)
def binary_log_build_test(self, value): conan_build_vs = """ from conans import ConanFile, MSBuild class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln", output_binary_log=%s) """ client = TestClient() files = get_vs_project_files() files[CONANFILE] = conan_build_vs % value client.save(files) client.run( "install . -s compiler=\"Visual Studio\" -s compiler.version=15") client.run("build .") if value == "'my_log.binlog'": log_name = value[1:1] flag = "/bl:%s" % log_name else: log_name = "msbuild.binlog" flag = "/bl" self.assertIn(flag, client.out) log_path = os.path.join(client.current_folder, log_name) self.assertTrue(os.path.exists(log_path))
def build_vs_project_test(self): if platform.system() != "Windows": return conan_build_vs = """ from conans import ConanFile, tools, ConfigureEnvironment import platform class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler" def build(self): build_command = tools.build_sln_command(self.settings, "MyProject.sln") env = ConfigureEnvironment(self) command = "%s && %s" % (env.command_line_env, build_command) self.output.warn(command) self.run(command) def package(self): self.copy(pattern="*.exe") """ client = TestClient() files = get_vs_project_files() files[CONANFILE] = conan_build_vs # Try with x86_64 client.save(files) client.run("export lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86_64") self.assertTrue("Release|x64", client.user_io.out) self.assertTrue("Copied 1 '.exe' files: MyProject.exe", client.user_io.out) # Try with x86 client.save(files, clean_first=True) client.run("export lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86") self.assertTrue("Release|x86", client.user_io.out) self.assertTrue("Copied 1 '.exe' files: MyProject.exe", client.user_io.out) # Try with x86 debug client.save(files, clean_first=True) client.run("export lasote/stable") client.run( "install Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug" ) self.assertTrue("Debug|x86", client.user_io.out) self.assertTrue("Copied 1 '.exe' files: MyProject.exe", client.user_io.out)
def user_properties_file_test(self): conan_build_vs = textwrap.dedent(""" from conans import ConanFile, MSBuild class HelloConan(ConanFile): exports = "*" settings = "os", "build_type", "arch", "compiler" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln", verbosity="normal", definitions={"MyCustomDef": "MyCustomValue"}, user_property_file_name="myuser.props") def package(self): self.copy(pattern="*.exe") """) client = TestClient() files = get_vs_project_files() files[CONANFILE] = conan_build_vs props = textwrap.dedent("""<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup Label="UserMacros" /> <ItemDefinitionGroup> <ClCompile> <RuntimeLibrary>MultiThreaded</RuntimeLibrary> </ClCompile> </ItemDefinitionGroup> <ItemGroup /> </Project> """) files["myuser.props"] = props client.save(files) client.run('create . Hello/1.2.1@lasote/stable') self.assertNotIn("/EHsc /MD", client.out) self.assertIn("/EHsc /MT", client.out) self.assertIn("/D MyCustomDef=MyCustomValue", client.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) full_ref = "Hello/1.2.1@lasote/stable:6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7" pref = PackageReference.loads(full_ref) build_folder = client.cache.package_layout(pref.ref).build(pref) self.assertTrue( os.path.exists(os.path.join(build_folder, "myuser.props"))) conan_props = os.path.join(build_folder, "conan_build.props") content = load(conan_props) self.assertIn("<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>", content)
def build_vs_project_test(self, generator, props): client = TestClient() files = {} files["conanfile.py"] = hello_conanfile_py files["hello.h"] = hello_h client.save(files) client.run("create . lasote/testing") files = get_vs_project_files() files["MyProject/main.cpp"] = main_cpp files["conanfile.txt"] = conanfile_txt.format(generator=generator) props = os.path.join(client.current_folder, props) old = '<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />' new = old + '<Import Project="{props}" />'.format(props=props) files["MyProject/MyProject.vcxproj"] = files[ "MyProject/MyProject.vcxproj"].replace(old, new) client.save(files, clean_first=True) for build_type in ["Debug", "Release"]: arch = "x86" runner = ConanRunner(print_commands_to_output=True, generate_run_log_file=False, log_run_to_output=True, output=TestBufferConanOutput()) settings = MockSettings({ "os": "Windows", "build_type": build_type, "arch": arch, "compiler": "Visual Studio", "compiler.version": "15", "compiler.toolset": "v141" }) conanfile = MockConanfile(settings, runner=runner) settings = " -s os=Windows " \ " -s build_type={build_type} " \ " -s arch={arch}" \ " -s compiler=\"Visual Studio\"" \ " -s compiler.toolset=v141" \ " -s compiler.version=15".format(build_type=build_type, arch=arch) client.run("install . %s" % settings) with tools.chdir(client.current_folder): msbuild = MSBuild(conanfile) msbuild.build(project_file="MyProject.sln", build_type=build_type, arch=arch) output = TestBufferConanOutput() runner("%s\MyProject.exe" % build_type, output) self.assertIn("Hello %s!!!" % build_type, output)
def build_vs_project_test(self): if platform.system() != "Windows": return conan_build_vs = """ from conans import ConanFile, tools, ConfigureEnvironment import platform class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler" def build(self): build_command = tools.build_sln_command(self.settings, "MyProject.sln") env = ConfigureEnvironment(self) command = "%s && %s" % (env.command_line_env, build_command) self.output.warn(command) self.run(command) def package(self): self.copy(pattern="*.exe") """ client = TestClient() files = get_vs_project_files() files[CONANFILE] = conan_build_vs # Try with x86_64 client.save(files) client.run("export lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86_64") self.assertTrue("Release|x64", client.user_io.out) self.assertTrue("Copied 1 '.exe' files: MyProject.exe", client.user_io.out) # Try with x86 client.save(files, clean_first=True) client.run("export lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86") self.assertTrue("Release|x86", client.user_io.out) self.assertTrue("Copied 1 '.exe' files: MyProject.exe", client.user_io.out) # Try with x86 debug client.save(files, clean_first=True) client.run("export lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug") self.assertTrue("Debug|x86", client.user_io.out) self.assertTrue("Copied 1 '.exe' files: MyProject.exe", client.user_io.out)
def build_vs_project_test(self): if platform.system() != "Windows": return conan_build_vs = """ from conans import ConanFile, tools, MSBuild import platform class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln") def package(self): self.copy(pattern="*.exe") """ client = TestClient() files = get_vs_project_files() files[CONANFILE] = conan_build_vs # Try with x86_64 client.save(files) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86_64") self.assertIn("Release|x64", client.user_io.out) self.assertIn("Copied 1 '.exe' files: MyProject.exe", client.user_io.out) # Try with x86 client.save(files, clean_first=True) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86") self.assertIn("Release|x86", client.user_io.out) self.assertIn("Copied 1 '.exe' files: MyProject.exe", client.user_io.out) # Try with x86 debug client.save(files, clean_first=True) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug") self.assertIn("Debug|x86", client.user_io.out) self.assertIn("Copied 1 '.exe' files: MyProject.exe", client.user_io.out)
def build_test(self): conan_build_vs = """ from conans import ConanFile, MSBuild, tools class HelloConan(ConanFile): name = "Hello" version = "1.2.1" settings = "os", "build_type", "arch", "compiler" export_source = "*" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln", upgrade_project=False) """ client = TestClient() files = get_vs_project_files() files["conanfile.py"] = conan_build_vs client.save(files) with (tools.environment_append({"CONAN_PRINT_RUN_COMMANDS": "1"})): with (tools.environment_append( {"CONAN_VS_INSTALLATION_PREFERENCE": "BuildTools"})): client.run("install .") client.run("build .") self.assertIn("BuildTools", client.out) conan_build_vs = conan_build_vs.replace("upgrade_project=False", "upgrade_project=True") files["conanfile.py"] = conan_build_vs client.save(files) with (tools.environment_append({ "CONAN_VS_INSTALLATION_PREFERENCE": "BuildTools", "CONAN_SKIP_VS_PROJECTS_UPGRADE": "True" })): client.run("install .") client.run("build .") self.assertIn("BuildTools", client.out)
def build_vs_project_test(self): conan_build_vs = """ from conans import ConanFile, MSBuild class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler", "cppstd" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln") def package(self): self.copy(pattern="*.exe") """ client = TestClient() # Test cpp standard stuff files = get_vs_project_files(std="cpp17_2015") files[CONANFILE] = conan_build_vs client.save(files) error = client.run( 'create . Hello/1.2.1@lasote/stable -s cppstd=11 -s ' 'compiler="Visual Studio" -s compiler.version=14', ignore_error=True) self.assertTrue(error) client.run('create . Hello/1.2.1@lasote/stable -s cppstd=17 ' '-s compiler="Visual Studio" -s compiler.version=14') self.assertIn("Copied 1 '.exe' file: MyProject.exe", client.user_io.out) files = get_vs_project_files() files[CONANFILE] = conan_build_vs # Try to not update the project client.client_cache._conan_config = None # Invalidate cached config tools.replace_in_file(client.client_cache.conan_conf_path, "[general]", "[general]\nskip_vs_projects_upgrade = True") client.save(files, clean_first=True) client.run("create . Hello/1.2.1@lasote/stable --build") self.assertNotIn("devenv", client.user_io.out) self.assertIn("Skipped sln project upgrade", client.user_io.out) # Try with x86_64 client.save(files) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86_64") self.assertIn("Release|x64", client.user_io.out) self.assertIn("Copied 1 '.exe' file: MyProject.exe", client.user_io.out) # Try with x86 client.save(files, clean_first=True) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86") self.assertIn("Release|x86", client.user_io.out) self.assertIn("Copied 1 '.exe' file: MyProject.exe", client.user_io.out) # Try with x86 debug client.save(files, clean_first=True) client.run("export . lasote/stable") client.run( "install Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug" ) self.assertIn("Debug|x86", client.user_io.out) self.assertIn("Copied 1 '.exe' file: MyProject.exe", client.user_io.out)
def build_vs_project_with_a_test(self): client = TestClient() conanfile = textwrap.dedent(""" from conans import ConanFile, CMake class HelloConan(ConanFile): settings = "os", "build_type", "compiler", "arch" exports = '*' def build(self): cmake = CMake(self) cmake.configure() cmake.build() def package(self): self.copy("*.h", dst="include") self.copy("*.a", dst="lib", keep_path=False) def package_info(self): self.cpp_info.libs = ["hello.a"] """) hello_cpp = textwrap.dedent(""" #include <iostream> #include "hello.h" void hello(){ std::cout << "Hello world!!!" << std::endl; }""") hello_h = "void hello();" cmake = textwrap.dedent(""" cmake_minimum_required(VERSION 2.8.12) project(MyLib CXX) set(CMAKE_STATIC_LIBRARY_SUFFIX ".a") add_library(hello hello.cpp) """) client.save({"conanfile.py": conanfile, "CMakeLists.txt": cmake, "hello.cpp": hello_cpp, "hello.h": hello_h}) client.run("create . mydep/0.1@lasote/testing") consumer = textwrap.dedent(""" from conans import ConanFile, MSBuild import os class HelloConan(ConanFile): settings = "os", "build_type", "compiler", "arch" requires = "mydep/0.1@lasote/testing" generators = "visual_studio" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln") """) files = get_vs_project_files() files["MyProject/main.cpp"] = main_cpp files["conanfile.py"] = consumer props = os.path.join(client.current_folder, "conanbuildinfo.props") old = r'<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />' new = old + '<Import Project="{props}" />'.format(props=props) files["MyProject/MyProject.vcxproj"] = files["MyProject/MyProject.vcxproj"].replace(old, new) client.save(files, clean_first=True) client.run("install .") client.run("build .") client.run_command(r"x64\Release\MyProject.exe") self.assertIn("Hello world!!!", client.out)
def build_vs_project_test(self): conan_build_vs = """ from conans import ConanFile, MSBuild class HelloConan(ConanFile): name = "Hello" version = "1.2.1" exports = "*" settings = "os", "build_type", "arch", "compiler", "cppstd" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln", verbosity="normal") def package(self): self.copy(pattern="*.exe") """ client = TestClient() # Test cpp standard stuff files = get_vs_project_files(std="cpp17_2015") files[CONANFILE] = conan_build_vs client.save(files) with catch_deprecation_warning(self): client.run( 'create . Hello/1.2.1@lasote/stable -s cppstd=11 -s ' 'compiler="Visual Studio" -s compiler.version=14', assert_error=True) with catch_deprecation_warning(self): client.run('create . Hello/1.2.1@lasote/stable -s cppstd=17 ' '-s compiler="Visual Studio" -s compiler.version=14') self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) files = get_vs_project_files() files[CONANFILE] = conan_build_vs # Try to not update the project client.cache._config = None # Invalidate cached config replace_in_file(client.cache.conan_conf_path, "[general]", "[general]\nskip_vs_projects_upgrade = True", output=client.out) client.save(files, clean_first=True) client.run("create . Hello/1.2.1@lasote/stable --build") self.assertNotIn("devenv", client.user_io.out) self.assertIn("Skipped sln project upgrade", client.out) # Try with x86_64 client.save(files) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86_64") self.assertIn("Release|x64", client.user_io.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) # Try with x86 client.save(files, clean_first=True) client.run("export . lasote/stable") client.run("install Hello/1.2.1@lasote/stable --build -s arch=x86") self.assertIn("Release|x86", client.user_io.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) # Try with x86 debug client.save(files, clean_first=True) client.run("export . lasote/stable") client.run( "install Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug" ) self.assertIn("Debug|x86", client.user_io.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) # Try with a custom property file name files[CONANFILE] = conan_build_vs.replace( 'msbuild.build("MyProject.sln", verbosity="normal")', 'msbuild.build("MyProject.sln", verbosity="normal", property_file_name="mp.props")' ) client.save(files, clean_first=True) client.run( "create . Hello/1.2.1@lasote/stable --build -s arch=x86 -s build_type=Debug" ) self.assertIn("Debug|x86", client.user_io.out) self.assertIn("Packaged 1 '.exe' file: MyProject.exe", client.out) full_ref = "Hello/1.2.1@lasote/stable:b786e9ece960c3a76378ca4d5b0d0e922f4cedc1" pref = PackageReference.loads(full_ref) build_folder = client.cache.package_layout(pref.ref).build(pref) self.assertTrue(os.path.exists(os.path.join(build_folder, "mp.props")))