def ReadProject(self): """Reads a project. Returns: VSSolutionProject: project if successful or None otherwise. """ # 8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942 is a Visual C++ related GUID. line = self._ReadLine(look_ahead=True) if not line or not line.startswith( 'Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = '): return None # For more than 1 match findall will return a list with a tuple. values = re.findall(( 'Project\\("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"\\) = "([^"]*)", ' '"([^"]*)\\.vcproj", ' '"{([0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*)}"'), line) if len(values) != 1: return None values = values[0] if len(values) != 3: return None solution_project = resources.VSSolutionProject(values[0], values[1], values[2]) found_dependencies = False line = self._ReadLine() while line and line != 'EndProject': line = self._ReadLine() if found_dependencies: if line == 'EndProjectSection': found_dependencies = False else: # The dependencies are defined as: {%GUID%} = {%GUID%} # For more than 1 match findall will return a list with a tuple. guids = re.findall(( '{([0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*)} = ' '{([0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*-[0-9A-F]*)}' ), line) if len(guids) == 1: guids = guids[0] if len(guids) == 2 and guids[0] == guids[1]: solution_project.AddDependency(guids[0]) elif line == 'ProjectSection(ProjectDependencies) = postProject': found_dependencies = True return solution_project
def testWriteProjects(self): """Tests the WriteProjects function.""" solution_project = resources.VSSolutionProject('name', 'file', 'guid') file_writer = writers.VSSolutionFileWriter() file_writer._file = io.BytesIO() file_writer.WriteProjects([solution_project]) file_writer._file.seek(0, os.SEEK_SET) output_data = file_writer._file.read() self.assertEqual(output_data, b'')
def testWriteProject(self): """Tests the WriteProject function.""" solution_project = resources.VSSolutionProject('name', 'filename', 'guid') file_writer = writers.VS2012SolutionFileWriter() file_writer._file = io.BytesIO() file_writer.WriteProject(solution_project) file_writer._file.seek(0, os.SEEK_SET) output_data = file_writer._file.read() expected_output_data = ( b'Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "name", ' b'"filename.vcxproj", "{GUID}"\r\nEndProject\r\n') self.assertEqual(output_data, expected_output_data)
def testWriteConfigurations(self): """Tests the WriteConfigurations function.""" solution_configurations = resources.VSConfigurations() solution_project = resources.VSSolutionProject('name', 'filename', 'guid') file_writer = writers.VS2010SolutionFileWriter() file_writer._file = io.BytesIO() file_writer.WriteConfigurations(solution_configurations, [solution_project]) file_writer._file.seek(0, os.SEEK_SET) output_data = file_writer._file.read() expected_output_data = ( b'Global\r\n' b'\tGlobalSection(SolutionProperties) = preSolution\r\n' b'\t\tHideSolutionNode = FALSE\r\n' b'\tEndGlobalSection\r\n' b'EndGlobal\r\n') self.assertEqual(output_data, expected_output_data)
def testAddDependency(self): """Tests the AddDependency function.""" solution_project = resources.VSSolutionProject('name', 'file', 'guid') solution_project.AddDependency('dependency_guid')
def testInitialize(self): """Tests the __init__ function.""" solution_project = resources.VSSolutionProject('name', 'file', 'guid') self.assertIsNotNone(solution_project)
def Convert(self, input_directory, output_version): """Converts a Visual Studio solution. Args: input_directory (str): path of the input directory. output_version (str): output Visual Studio version. Returns: bool: True if the conversion successful or False if not. """ configure_ac_path = os.path.join(input_directory, 'configure.ac') if not os.path.exists(configure_ac_path): logging.warning('No such file: {0:s}.'.format(configure_ac_path)) return False solution_name = None # pylint: disable=consider-using-with file_object = io.open(configure_ac_path, 'r', encoding='utf8') in_ac_init_section = False for line in file_object.readlines(): line = line.strip() if in_ac_init_section: if line.startswith('[') and line.endswith('],'): solution_name = line[1:-2] break if line.startswith('AC_INIT('): in_ac_init_section = True file_object.close() if not solution_name: logging.warning('Unable to determine solution name.') return False # Use the existing msvscpp solution file to determine the project # GUID so that they can be reused. project_guids_by_name = {} input_sln_path = os.path.join( input_directory, 'msvscpp', '{0:s}.sln'.format(solution_name)) if os.path.exists(input_sln_path): solution_reader = readers.VS2008SolutionFileReader() solution_reader.Open(input_sln_path) if not solution_reader.ReadHeader(): logging.warning('Unable to read solution file: {0:s} header.'.format( input_sln_path)) return False solution_projects = solution_reader.ReadProjects() solution_reader.Close() for solution_project in solution_projects: project_guids_by_name[solution_project.name] = solution_project.guid solution_projects = [] projects_by_guid = {} for directory_entry in os.listdir(input_directory): if not os.path.isdir(directory_entry): continue if (not directory_entry.startswith('lib') and not directory_entry.startswith('py') and directory_entry not in ('src', 'tests') and not directory_entry.endswith('.net') and not directory_entry.endswith('tools')): continue # Ignore the Python version specific build directories. if (directory_entry.startswith('py') and ( directory_entry.endswith('2') or directory_entry.endswith('3') or not self._generate_python_dll)): continue makefile_am_path = os.path.join( input_directory, directory_entry, 'Makefile.am') if not os.path.exists(makefile_am_path): logging.warning('No such file: {0:s}.'.format(makefile_am_path)) continue if (directory_entry in ('src', 'tests') or directory_entry.endswith('tools')): project_names = self._ReadMakefilePrograms(makefile_am_path) else: project_names = [directory_entry] for project_name in project_names: project_filename = '{0:s}\\{0:s}'.format(project_name) project_guid = project_guids_by_name.get(project_name, '') if not project_guid: project_guid = project_guids_by_name.get( '{0:s}.dll'.format(project_name), '') if not project_guid: project_guid = str(uuid.uuid4()) solution_project = resources.VSSolutionProject( project_name, project_filename, project_guid) solution_projects.append(solution_project) project_information = resources.VSProjectInformation() project_information.name = project_name project_information.guid = project_guid project_information.root_name_space = project_name if project_name == solution_name: release_project_configuration = ReleaseDllVSProjectConfiguration() debug_project_configuration = VSDebugDllVSProjectConfiguration() elif project_name.endswith('.net'): release_project_configuration = ( ReleaseDotNetDllVSProjectConfiguration()) debug_project_configuration = VSDebugDotNetDllVSProjectConfiguration() elif project_name.startswith('py'): release_project_configuration = ( ReleasePythonDllVSProjectConfiguration( python_path=self._python_path)) debug_project_configuration = VSDebugPythonDllVSProjectConfiguration( python_path=self._python_path) elif project_name.startswith('lib'): release_project_configuration = ReleaseLibraryVSProjectConfiguration() debug_project_configuration = VSDebugLibraryVSProjectConfiguration() else: project_information.keyword = 'Win32Proj' release_project_configuration = ReleaseExeVSProjectConfiguration() debug_project_configuration = VSDebugExeVSProjectConfiguration() # TODO: determine autogenerated source. self._ReadMakefile( makefile_am_path, solution_name, project_information, release_project_configuration, debug_project_configuration) # TODO: add additional Python 3 project. project_information.configurations.Append(release_project_configuration) if debug_project_configuration: project_information.configurations.Append(debug_project_configuration) projects_by_guid[project_guid] = project_information self._CreateThirdPartyDependencies( solution_projects, projects_by_guid, project_guids_by_name) # Set-up the solution configurations. solution_configurations = resources.VSConfigurations() solution_configuration = resources.VSSolutionConfiguration( name='Release', platform='Win32') solution_configurations.Append(solution_configuration) solution_configuration = resources.VSSolutionConfiguration( name='VSDebug', platform='Win32') solution_configurations.Append(solution_configuration) if output_version not in ['2008']: # Add x64 as a platform. solution_configurations.ExtendWithX64(output_version) # Create some look-up dictionaries. solution_project_guids_by_name = {} solution_projects_by_guid = {} for solution_project in solution_projects: solution_project_guids_by_name[solution_project.name] = ( solution_project.guid) solution_projects_by_guid[solution_project.guid] = solution_project # Set-up the solution dependencies. for guid, project_information in projects_by_guid.items(): solution_project = solution_projects_by_guid[guid] for dependency in project_information.dependencies: if dependency in ['pthread']: continue dependency_guid = solution_project_guids_by_name.get(dependency, '') if not dependency_guid: logging.info('Missing GUID for dependency: {0:s}'.format(dependency)) solution_project.AddDependency(dependency_guid) solution_filename = '{0:s}.sln'.format(solution_name) self._WriteSolution( solution_filename, output_version, solution_projects, solution_configurations) for solution_project in solution_projects: project_information = projects_by_guid[solution_project.guid] self._WriteProject( output_version, solution_project, project_information, solution_projects_by_guid) # Create the corresponding Makefile.am solution_project_filenames = [] for solution_project in solution_projects: if output_version in ['2008']: solution_project_extension = 'vcproj' else: solution_project_extension = 'vcxproj' path_segments = solution_project.filename.split('\\') solution_project_filenames.append('\t{0:s}.{1:s} \\'.format( os.path.join(*path_segments), solution_project_extension)) makefile_am_lines = ['MSVSCPP_FILES = \\'] for solution_project_filename in sorted(solution_project_filenames): makefile_am_lines.append(solution_project_filename) makefile_am_lines.append('\t{0:s}'.format(solution_filename)) makefile_am_lines.extend([ '', 'EXTRA_DIST = \\', '\t$(MSVSCPP_FILES)', '', 'MAINTAINERCLEANFILES = \\', '\tMakefile.in', '', 'distclean: clean', '\t/bin/rm -f Makefile', '', '']) filename = os.path.join('vs{0:s}'.format(output_version), 'Makefile.am') logging.info('Writing: {0:s}'.format(filename)) with io.open(filename, 'w', encoding='utf8') as makefile_am: lines = '\n'.join(makefile_am_lines) makefile_am.write(lines) return True
def _CreateThirdPartyDependencies( self, solution_projects, projects_by_guid, project_guids_by_name): """Creates the project files for third party dependencies. Args: solution_projects (list[VSSolutionProject]): projects. projects_by_guid (dict[str, VSProjectInformation]): projects per lower case GUID. project_guids_by_name (dict[str, VSProjectInformation]): lower case project GUID per name. This dictionary is use as a lookup table to preserve the existing GUIDs. """ third_party_dependencies = [] for project_information in projects_by_guid.values(): for dependency in project_information.third_party_dependencies: if dependency not in third_party_dependencies: third_party_dependencies.append(dependency) for project_name in third_party_dependencies: if project_name not in self._SUPPORTED_THIRD_PARTY_DEPENDENCIES: logging.info('Unsupported third party dependency: {0:s}'.format( project_name)) continue project_filename = '{0:s}\\{0:s}'.format(project_name) project_guid = project_guids_by_name.get(project_name, '') if not project_guid: project_guid = project_guids_by_name.get( '{0:s}.dll'.format(project_name), '') if not project_guid: project_guid = str(uuid.uuid4()) solution_project = resources.VSSolutionProject( project_name, project_filename, project_guid) solution_projects.append(solution_project) if project_name == 'bzip2': project_information = Bzip2VSProjectInformation() elif project_name == 'zlib': project_information = ZlibVSProjectInformation() else: project_information = resources.VSProjectInformation() project_information.name = project_name project_information.guid = project_guid project_information.root_name_space = project_name if project_name in ('bzip2', 'zlib'): release_project_configuration = ReleaseDllVSProjectConfiguration() debug_project_configuration = VSDebugDllVSProjectConfiguration() else: release_project_configuration = ReleaseVSProjectConfiguration() debug_project_configuration = VSDebugVSProjectConfiguration() if project_name == 'bzip2': self._ConfigureAsBzip2Dll( project_information, release_project_configuration, debug_project_configuration) elif project_name == 'zlib': self._ConfigureAsZlibDll( project_information, release_project_configuration, debug_project_configuration) project_information.configurations.Append(release_project_configuration) project_information.configurations.Append(debug_project_configuration) projects_by_guid[project_guid] = project_information