bin/gbuild-to-ide | 240 +++++++++++++++--------------------------------------- 1 file changed, 68 insertions(+), 172 deletions(-)
New commits: commit 5d1eafab5233e6eaaf99071554b5b41addf7fa56 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Thu Jan 25 11:36:55 2024 +0600 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Thu Jan 25 07:44:48 2024 +0100 Deduplicate, refactor and simplify GbuildLinkTarget initialization Change-Id: I0c2d9823454ff84af2967c20a383b334e4933967 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162550 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/bin/gbuild-to-ide b/bin/gbuild-to-ide index 730191a9f127..5869870de1a9 100755 --- a/bin/gbuild-to-ide +++ b/bin/gbuild-to-ide @@ -26,101 +26,14 @@ import collections import urllib.parse class GbuildLinkTarget: - def __init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target): - (self.name, self.location, self.include, self.include_sys, self.defs, self.cxxobjects, self.cxxflags, self.cobjects, self.objcxxobjects, self.cflags, self.linked_libs, self.linked_static_libs, self.link_target) = ( - name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target) - - def short_name(self): - return self.name - - def is_empty(self): - return not self.include and not self.defs and not self.cxxobjects and not self.cobjects and not self.linked_libs and not self.linked_static_libs - - def __str__(self): - return '%s at %s with include path: %s, isystem includes: %s, defines: %s, objects: %s, cxxflags: %s, cobjects: %s, cflags: %s, linked libs: %s and linked static libs: %s' % ( - self.short_name(), self.location, self.include, self.include_sys, self.defs, self.cxxobjects, - self.cxxflags, self.cobjects, self.cflags, self.linked_libs, self.linked_static_libs) - - -class GbuildLib(GbuildLinkTarget): - def __init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target): - GbuildLinkTarget.__init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target) - - def short_name(self): - """Return the short name of target based on the Library_* makefile name""" - return 'Library %s' % self.name - - def target_name(self): - return 'Library_%s' % self.name - - def library_name(self): - return self.name - -class GbuildStaticLib(GbuildLinkTarget): - def __init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target): - GbuildLinkTarget.__init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target) - - def short_name(self): - """Return the short name of target based on the StaticLibrary_* makefile name""" - return 'StaticLibrary %s' % self.name - - def target_name(self): - return 'StaticLibrary_%s' % self.name - - def library_name(self): - return self.name - -class GbuildTest(GbuildLinkTarget): - def __init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target): - GbuildLinkTarget.__init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target) - - def short_name(self): - """Return the short name of target based n the CppunitTest_* makefile names""" - return 'CppunitTest %s' % self.name - - def target_name(self): - return 'CppunitTest_%s' % self.name - -class GbuildExe(GbuildLinkTarget): - def __init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target): - GbuildLinkTarget.__init__(self, name, location, include, include_sys, defs, cxxobjects, cxxflags, cobjects, objcxxobjects, cflags, linked_libs, linked_static_libs, link_target) - - def short_name(self): - """Return the short name of target based on the Executable_* makefile name""" - return 'Executable %s' % self.name - - def target_name(self): - return 'Executable_%s' % self.name - - -class GbuildParser: - """Main data model object. - - Attributes: - target_by_path : dict[path:string, set(target)] - where target is one of the GbuildLinkTarget subclasses - target_by_location : dict[path:string, set(target)] - where target is one of the GbuildLinkTarget subclasses - """ - def __init__(self, makecmd): - self.makecmd = makecmd - self.binpath = os.path.dirname(os.environ['GPERF']) # woha, this is quite a hack - (self.srcdir, self.builddir, self.instdir, self.workdir) = (os.environ['SRCDIR'], os.environ['BUILDDIR'], os.environ['INSTDIR'], os.environ['WORKDIR']) - (self.libs, self.static_libs, self.exes, self.tests, self.modulenamelist) = ([], [], [], [], []) - (self.target_by_path, self.target_by_location) = ({}, {}) - includepattern = re.compile(r'-I(\S+)') isystempattern = re.compile(r'-isystem\s*(\S+)') warningpattern = re.compile(r'-W\S+') - libpattern = re.compile(r'Library_(.*)\.mk') - staticlibpattern = re.compile(r'StaticLibrary_(.*)\.mk') - exepattern = re.compile(r'Executable_(.*)\.mk') - testpattern = re.compile(r'CppunitTest_(.*)\.mk') @staticmethod def __split_includes(includes): - foundisystem = GbuildParser.isystempattern.findall(includes) - foundincludes = [includeswitch.strip() for includeswitch in GbuildParser.includepattern.findall(includes) if + foundisystem = GbuildLinkTarget.isystempattern.findall(includes) + foundincludes = [includeswitch.strip() for includeswitch in GbuildLinkTarget.includepattern.findall(includes) if len(includeswitch) > 2] return (foundincludes, foundisystem) @@ -147,106 +60,89 @@ class GbuildParser: @staticmethod def __split_flags(flagsline, flagslineappend): - return [cxxflag.strip() for cxxflag in GbuildParser.warningpattern.sub('', '%s %s' % (flagsline, flagslineappend)).split(' ') if len(cxxflag) > 1] + return [cxxflag.strip() for cxxflag in GbuildLinkTarget.warningpattern.sub('', '%s %s' % (flagsline, flagslineappend)).split(' ') if len(cxxflag) > 1] - @staticmethod - def __lib_from_json(json): - (foundincludes, foundisystem) = GbuildParser.__split_includes(json['INCLUDE']) - return GbuildLib( - GbuildParser.libpattern.match(os.path.basename(json['MAKEFILE'])).group(1), + def __init__(self, json): + (foundincludes, foundisystem) = GbuildLinkTarget.__split_includes(json['INCLUDE']) + (self.name, self.location, self.include, self.include_sys, self.defs, self.cxxobjects, self.cxxflags, self.cobjects, self.objcxxobjects, self.cflags, self.linked_libs, self.linked_static_libs, self.link_target) = ( + type(self).targetpattern.match(os.path.basename(json['MAKEFILE'])).group(1), os.path.dirname(json['MAKEFILE']), foundincludes, foundisystem, - GbuildParser.__split_defs(json['DEFS']), - GbuildParser.__split_objs(json['CXXOBJECTS']), - GbuildParser.__split_flags(json['CXXFLAGS'], json['CXXFLAGSAPPEND']), - GbuildParser.__split_objs(json['COBJECTS']), - GbuildParser.__split_objs(json['OBJCXXOBJECTS']), - GbuildParser.__split_flags(json['CFLAGS'], json['CFLAGSAPPEND']), + GbuildLinkTarget.__split_defs(json['DEFS']), + GbuildLinkTarget.__split_objs(json['CXXOBJECTS']), + GbuildLinkTarget.__split_flags(json['CXXFLAGS'], json['CXXFLAGSAPPEND']), + GbuildLinkTarget.__split_objs(json['COBJECTS']), + GbuildLinkTarget.__split_objs(json['OBJCXXOBJECTS']), + GbuildLinkTarget.__split_flags(json['CFLAGS'], json['CFLAGSAPPEND']), json['LINKED_LIBS'].strip().split(' '), json['LINKED_STATIC_LIBS'].strip().split(' '), json['LINKTARGET'].strip()) - @staticmethod - def __static_lib_from_json(json): - (foundincludes, foundisystem) = GbuildParser.__split_includes(json['INCLUDE']) - return GbuildStaticLib( - GbuildParser.staticlibpattern.match(os.path.basename(json['MAKEFILE'])).group(1), - os.path.dirname(json['MAKEFILE']), - foundincludes, - foundisystem, - GbuildParser.__split_defs(json['DEFS']), - GbuildParser.__split_objs(json['CXXOBJECTS']), - GbuildParser.__split_flags(json['CXXFLAGS'], json['CXXFLAGSAPPEND']), - GbuildParser.__split_objs(json['COBJECTS']), - GbuildParser.__split_objs(json['OBJCXXOBJECTS']), - GbuildParser.__split_flags(json['CFLAGS'], json['CFLAGSAPPEND']), - json['LINKED_LIBS'].strip().split(' '), - json['LINKED_STATIC_LIBS'].strip().split(' '), - json['LINKTARGET'].strip()) + def short_name(self): + """Return the short name of target based on the Foo_* makefile name""" + return '%s %s' % (type(self).targetprefix, self.name) - @staticmethod - def __test_from_json(json): - (foundincludes, foundisystem) = GbuildParser.__split_includes(json['INCLUDE']) - testname_match = GbuildParser.testpattern.match(os.path.basename(json['MAKEFILE'])) + def target_name(self): + return '%s_%s' % (type(self).targetprefix, self.name) - # Workaround strange writer test makefile setup - if testname_match is None: - testname = "StrangeWriterMakefiles" - else: - testname = testname_match.group(1) + def __str__(self): + return '%s at %s with include path: %s, isystem includes: %s, defines: %s, objects: %s, cxxflags: %s, cobjects: %s, cflags: %s, linked libs: %s and linked static libs: %s' % ( + self.short_name(), self.location, self.include, self.include_sys, self.defs, self.cxxobjects, + self.cxxflags, self.cobjects, self.cflags, self.linked_libs, self.linked_static_libs) - return GbuildTest( - testname, - os.path.dirname(json['MAKEFILE']), - foundincludes, - foundisystem, - GbuildParser.__split_defs(json['DEFS']), - GbuildParser.__split_objs(json['CXXOBJECTS']), - GbuildParser.__split_flags(json['CXXFLAGS'], json['CXXFLAGSAPPEND']), - GbuildParser.__split_objs(json['COBJECTS']), - GbuildParser.__split_objs(json['OBJCXXOBJECTS']), - GbuildParser.__split_flags(json['CFLAGS'], json['CFLAGSAPPEND']), - json['LINKED_LIBS'].strip().split(' '), - json['LINKED_STATIC_LIBS'].strip().split(' '), - json['LINKTARGET'].strip()) - @staticmethod - def __exe_from_json(json): - (foundincludes, foundisystem) = GbuildParser.__split_includes(json['INCLUDE']) - return GbuildExe( - GbuildParser.exepattern.match(os.path.basename(json['MAKEFILE'])).group(1), - os.path.dirname(json['MAKEFILE']), - foundincludes, - foundisystem, - GbuildParser.__split_defs(json['DEFS']), - GbuildParser.__split_objs(json['CXXOBJECTS']), - GbuildParser.__split_flags(json['CXXFLAGS'], json['CXXFLAGSAPPEND']), - GbuildParser.__split_objs(json['COBJECTS']), - GbuildParser.__split_objs(json['OBJCXXOBJECTS']), - GbuildParser.__split_flags(json['CFLAGS'], json['CFLAGSAPPEND']), - json['LINKED_LIBS'].strip().split(' '), - json['LINKED_STATIC_LIBS'].strip().split(' '), - json['LINKTARGET'].strip()) +class GbuildLib(GbuildLinkTarget): + targetpattern = re.compile(r'Library_(.*)\.mk') + targetprefix = "Library" + +class GbuildStaticLib(GbuildLinkTarget): + targetpattern = re.compile(r'StaticLibrary_(.*)\.mk') + targetprefix = "StaticLibrary" + +class GbuildTest(GbuildLinkTarget): + targetpattern = re.compile(r'CppunitTest_(.*)\.mk') + targetprefix = "CppunitTest" + +class GbuildExe(GbuildLinkTarget): + targetpattern = re.compile(r'Executable_(.*)\.mk') + targetprefix = "Executable" + + +class GbuildParser: + """Main data model object. + + Attributes: + target_by_path : dict[path:string, set(target)] + where target is one of the GbuildLinkTarget subclasses + target_by_location : dict[path:string, set(target)] + where target is one of the GbuildLinkTarget subclasses + """ + def __init__(self, makecmd): + self.makecmd = makecmd + self.binpath = os.path.dirname(os.environ['GPERF']) # woha, this is quite a hack + (self.srcdir, self.builddir, self.instdir, self.workdir) = (os.environ['SRCDIR'], os.environ['BUILDDIR'], os.environ['INSTDIR'], os.environ['WORKDIR']) + (self.libs, self.static_libs, self.exes, self.tests, self.modulenamelist) = (set(), set(), set(), set(), []) + (self.target_by_path, self.target_by_location) = ({}, {}) def parse(self): for jsonfilename in os.listdir(os.path.join(self.workdir, 'GbuildToJson', 'Library')): with open(os.path.join(self.workdir, 'GbuildToJson', 'Library', jsonfilename), 'r') as f: - lib = self.__lib_from_json(json.load(f)) - self.libs.append(lib) + lib = GbuildLib(json.load(f)) + self.libs.add(lib) for jsonfilename in os.listdir(os.path.join(self.workdir, 'GbuildToJson', 'StaticLibrary')): with open(os.path.join(self.workdir, 'GbuildToJson', 'StaticLibrary', jsonfilename), 'r') as f: - static_lib = self.__static_lib_from_json(json.load(f)) - self.static_libs.append(static_lib) + static_lib = GbuildStaticLib(json.load(f)) + self.static_libs.add(static_lib) for jsonfilename in os.listdir(os.path.join(self.workdir, 'GbuildToJson', 'Executable')): with open(os.path.join(self.workdir, 'GbuildToJson', 'Executable', jsonfilename), 'r') as f: - exe = self.__exe_from_json(json.load(f)) - self.exes.append(exe) + exe = GbuildExe(json.load(f)) + self.exes.add(exe) for jsonfilename in os.listdir(os.path.join(self.workdir, 'GbuildToJson', 'CppunitTest')): with open(os.path.join(self.workdir, 'GbuildToJson', 'CppunitTest', jsonfilename), 'r') as f: - test = self.__test_from_json(json.load(f)) - self.tests.append(test) - for target in set(self.libs) | set(self.static_libs) | set(self.exes) | set(self.tests): + test = GbuildTest(json.load(f)) + self.tests.add(test) + for target in self.libs | self.static_libs | self.exes | self.tests: if target.location not in self.target_by_location: self.target_by_location[target.location] = set() self.target_by_location[target.location] |= set([target]) @@ -433,7 +329,7 @@ class CodeliteIntegrationGenerator(IdeIntegrationGenerator): path = '/'.join(file.split('/')[1:-1]) virtual_dirs[path].add(relative_file + '.cxx') # add HXX files - all_libs = set(self.gbuildparser.libs) | set(self.gbuildparser.exes) + all_libs = self.gbuildparser.libs | self.gbuildparser.exes for lib in all_libs: if lib.name == module_name: for hdir in lib.include: @@ -588,7 +484,7 @@ class VimIntegrationGenerator(IdeIntegrationGenerator): def emit(self): global_list = [] - for lib in set(self.gbuildparser.libs) | set(self.gbuildparser.tests) | set(self.gbuildparser.exes): + for lib in self.gbuildparser.libs | self.gbuildparser.tests | self.gbuildparser.exes: entries = [] for file in lib.cxxobjects: filePath = os.path.join(self.gbuildparser.srcdir, file) + ".cxx" @@ -1023,7 +919,7 @@ class VisualStudioIntegrationGenerator(IdeIntegrationGenerator): dependency_libs = {} for linked_lib in linked_libs: for library_project in library_projects: - if library_project.target.library_name() == linked_lib: + if library_project.target.name == linked_lib: dependency_libs[library_project.guid] = library_project return dependency_libs @@ -1310,7 +1206,7 @@ class QtCreatorIntegrationGenerator(IdeIntegrationGenerator): def __init__(self, gbuildparser, ide): IdeIntegrationGenerator.__init__(self, gbuildparser, ide) self.target_by_location = {} - for target in set(self.gbuildparser.libs) | set(self.gbuildparser.exes) | set(self.gbuildparser.tests): + for target in self.gbuildparser.libs | self.gbuildparser.exes | self.gbuildparser.tests: if target.location not in self.target_by_location: self.target_by_location[target.location] = set() self.target_by_location[target.location] |= set([target]) @@ -1746,7 +1642,7 @@ class QtCreatorIntegrationGenerator(IdeIntegrationGenerator): self.data_libs = {} - all_libs = set(self.gbuildparser.libs) | set(self.gbuildparser.exes) | set(self.gbuildparser.tests) + all_libs = self.gbuildparser.libs | self.gbuildparser.exes | self.gbuildparser.tests for lib in all_libs: self._log(" library : %s, loc=%s" % (lib.short_name(), lib.location)) lib_name = os.path.basename(lib.location)