def test_binary_log_build(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 test_reuse_msbuild_object(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 test_user_properties_file(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 check_build_vs_project_with_test_requires(vs_version): client = TestClient() client.save(pkg_cmake("updep.pkg.team", "0.1")) client.run("create . -s compiler.version={vs_version}".format( vs_version=vs_version)) client.save(pkg_cmake("mydep.pkg.team", "0.1", requires=["updep.pkg.team/0.1"]), clean_first=True) client.run("create . -s compiler.version={vs_version}".format( vs_version=vs_version)) consumer = textwrap.dedent(""" from conans import ConanFile from conan.tools.microsoft import MSBuild class HelloConan(ConanFile): settings = "os", "build_type", "compiler", "arch" generators = "MSBuildDeps", "MSBuildToolchain" def build_requirements(self): self.build_requires("mydep.pkg.team/0.1", force_host_context=True) def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln") """) files = get_vs_project_files() main_cpp = gen_function_cpp(name="main", includes=["mydep_pkg_team"], calls=["mydep_pkg_team"]) files["MyProject/main.cpp"] = main_cpp files["conanfile.py"] = consumer props = os.path.join(client.current_folder, "conandeps.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 . -s compiler.version={vs_version}'.format( vs_version=vs_version)) client.run("build .") client.run_command(r"x64\Release\MyProject.exe") assert "mydep_pkg_team: Release!" in client.out assert "updep_pkg_team: Release!" in client.out
def test_build_vs_project(self, generator, props): client = TestClient() client.save({"conanfile.py": hello_conanfile_py, "hello.h": hello_h}) 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 = 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) 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() client.run_command(r"%s\MyProject.exe" % build_type) self.assertIn("Hello %s!!!" % build_type, client.out)
def check_build_vs_project_with_a(vs_version): client = TestClient() client.save({"conanfile.py": GenConanfile()}) client.run("create . updep.pkg.team/0.1@") conanfile = textwrap.dedent(""" from conans import ConanFile, CMake class HelloConan(ConanFile): settings = "os", "build_type", "compiler", "arch" exports = '*' requires = "updep.pkg.team/0.1@" 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 = gen_function_cpp(name="hello") hello_h = gen_function_h(name="hello") cmake = textwrap.dedent(""" set(CMAKE_CXX_COMPILER_WORKS 1) set(CMAKE_CXX_ABI_COMPILED 1) cmake_minimum_required(VERSION 3.15) 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.pkg.team/0.1@ -s compiler="Visual Studio"' ' -s compiler.version={vs_version}'.format(vs_version=vs_version)) consumer = textwrap.dedent(""" from conans import ConanFile from conan.tools.microsoft import MSBuild class HelloConan(ConanFile): settings = "os", "build_type", "compiler", "arch" requires = "mydep.pkg.team/0.1@" generators = "MSBuildDeps", "MSBuildToolchain" def build(self): msbuild = MSBuild(self) msbuild.build("MyProject.sln") """) files = get_vs_project_files() main_cpp = gen_function_cpp(name="main", includes=["hello"], calls=["hello"]) files["MyProject/main.cpp"] = main_cpp files["conanfile.py"] = consumer props = os.path.join(client.current_folder, "conandeps.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 . -s compiler="Visual Studio"' ' -s compiler.version={vs_version}'.format(vs_version=vs_version)) client.run("build .") client.run_command(r"x64\Release\MyProject.exe") assert "hello: Release!" in client.out
def test_build_vs_project(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) client.run('create . Hello/1.2.1@lasote/stable -s cppstd=11 -s ' 'compiler="Visual Studio" -s compiler.version=14', assert_error=True) 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.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.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.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.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.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")))
def test_build_vs_project_with_a(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)