65fa4c
#!/usr/bin/python3
65fa4c
# -*- coding:utf-8 -*-
65fa4c
#
65fa4c
# Copyright (C) 2015 Daniel Vrátil <dvratil@redhat.com>
65fa4c
# Copyright (C) 2017 Daniel Vrátil <dvratil@fedoraproject.org>
65fa4c
#
65fa4c
#
65fa4c
# This program is free software; you can redistribute it and/or modify
65fa4c
# it under the terms of the GNU Library General Public License as
65fa4c
# published by the Free Software Foundation; either version 2 of the
65fa4c
# License, or (at your option) any later version.
65fa4c
#
65fa4c
# This program is distributed in the hope that it will be useful,
65fa4c
# but WITHOUT ANY WARRANTY; without even the implied warranty of
65fa4c
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
65fa4c
# GNU General Public License for more details.
65fa4c
#
65fa4c
# You should have received a copy of the GNU Library General Public
65fa4c
# License along with this program; if not, write to the
65fa4c
# Free Software Foundation, Inc.,
65fa4c
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
65fa4c
#
65fa4c
65fa4c
import sys
65fa4c
import re
65fa4c
import glob
65fa4c
65fa4c
class CMakeParser:
65fa4c
    def __init__(self, filelist = None):
65fa4c
        if filelist == None:
65fa4c
            filelist = sys.stdin
65fa4c
65fa4c
        paths = map(lambda x: x.rstrip(), filelist.readlines())
65fa4c
        for path in paths:
65fa4c
            modulePath, cmakeModule, lowercase = self.parseCmakeModuleConfig(path)
65fa4c
            if modulePath and cmakeModule:
65fa4c
                version = self.resolveCMakeModuleVersion(modulePath, cmakeModule, lowercase)
65fa4c
65fa4c
                if version:
65fa4c
                    string = "cmake(" + cmakeModule + ") = " + version
65fa4c
                else:
65fa4c
                    string = "cmake(" + cmakeModule + ")"
65fa4c
                if string == string.lower():
65fa4c
                    print(string)
65fa4c
                else:
65fa4c
                    # Temporarily print both variants to satisfy requires
65fa4c
                    # by the old version of this generator which made mistakes
65fa4c
                    print(string)
65fa4c
                    print(string.lower())
65fa4c
65fa4c
65fa4c
    def parseCmakeModuleConfig(self, configFile):
65fa4c
        paths = configFile.rsplit("/", 3)
65fa4c
65fa4c
        modulePath = "%s/cmake/%s" % (paths[0], paths[2])
65fa4c
        cfgFile = paths[3]
65fa4c
        if cfgFile.endswith("Config.cmake"):
65fa4c
           return (modulePath, cfgFile[0:-len("Config.cmake")], False)
65fa4c
        elif cfgFile.endswith("-config.cmake"):
65fa4c
           return (modulePath, cfgFile[0:-len("-config.cmake")], True)
65fa4c
        else:
65fa4c
            return (None, None, False)
65fa4c
65fa4c
    def resolveCMakeModuleVersion(self, modulePath, cmakeModule, lowercase):
65fa4c
        versionFile = ("%s/%s-config-version.cmake" if lowercase else "%s/%sConfigVersion.cmake") % (modulePath, cmakeModule)
65fa4c
        try:
65fa4c
            f = open(versionFile, 'r')
65fa4c
        except:
65fa4c
            return None
65fa4c
65fa4c
        for line in f:
65fa4c
            line = line.strip()
65fa4c
65fa4c
            # set(PACKAGE_VERSION <version>)
65fa4c
            version = re.match(r"^set[\ ]*\([\ ]*PACKAGE_VERSION[\ ]+[\"]*([0-9\.]+)[\"]*[\ ]*[.]*\)", line)
65fa4c
            if version:
65fa4c
                return version.groups(1)[0]
65fa4c
65fa4c
        return None
65fa4c
65fa4c
if __name__ == "__main__":
65fa4c
    parser = CMakeParser()