<commit_before># Copyright (C) 2009-2014, Quentin "mefyl" Hocquet
#
# This software is provided "as is" without warranty of any kind,
# either expressed or implied, including but not limited to the
# implied warranties of fitness for a particular purpose.
#
# See the LICENSE file for more information.

import itertools
import drake
import platform
import re
import os

from itertools import chain

from .. import Builder, Exception, Node, Path, node, debug
from .  import Config, StaticLib, Header, Object, Source
from .qt_headers import headers as per_version_headers
import drake.cxx

deps_handler_name = 'drake.cxx.qt.moc'

def moc_file(linker, header):
  for i in linker.config.system_include_path:
    if header.name_relative.dirname() == i:
      return None
  path = Path(header.name()).with_extension('moc.cc')
  src = node(path)
  if src.builder is None:
    Moc(linker.toolkit.qt, header, src)
  return Object(src, linker.toolkit, linker.config)

def find(prefix = None,
         cxx_toolkit = None,
         version = drake.Version(),
         prefer_shared = True):
  if isinstance(prefix, Qt):
    if prefix.version not in version:
      raise Exception('given Qt %s does not fit '
                      'the requested version %s' %
                      (prefix.version, version))
    return prefix
  else:
    return Qt(prefix = prefix,
              cxx_toolkit = cxx_toolkit,
              version = version,
              prefer_shared = prefer_shared)

class Qt(drake.Configuration):
  __libraries = {
    'core': 'QtCore',
    'support_3': 'Qt3Support',
    'declarative': 'QtDeclarative',
    'gui': 'QtGui',
    'multimedia': 'QtMultimedia',
    'network': 'QtNetwork',
    'webkit': 'QtWebKit',
    'xml': 'QtXml',
    'xml_pattars': 'QtXmlPatterns',
#    'phonon': 'phonon',
    }

  imageformats = {
    "gif",
    "ico",
    "jpeg",
    "svg",
  }

  def __init__(self,
               cxx_toolkit = None,
               prefix = None,
               version = drake.Version(),
               version_effective = None,
               prefer_shared = True):
    """Find and create a configuration for Qt.

    prefix -- Where to find Qt, should contain
              include/Qt/qglobal.h among others. /usr and
              /usr/local are searched by default. If relative, it
              is rooted in the source tree.
    version -- Requested version.
    prefer_shared -- Check dynamic libraries first.
    """
    self.__moc_cache = {}
    self.__dependencies = {}
    cxx_toolkit = cxx_toolkit or drake.cxx.Toolkit()
    self.__cxx_toolkit = cxx_toolkit
    self.__prefer_shared = prefer_shared
    include_subdirs = set(drake.Path(p)
                          for p in ['include', 'include/qt4'])
    if prefix is None:
      test = [path.dirname() for path in cxx_toolkit.include_path
              if path.basename() in include_subdirs]
    else:
      test = [Path(prefix)]
    for i in range(len(test)):
      if not test[i].absolute():
        test[i] = drake.path_source() / test[i]
    token = drake.Path('Qt/qglobal.h')
    include_subdirs = {drake.Path('include')}
    for prefix in test:
      for subdir in include_subdirs:
        if not (prefix / subdir).exists():
          continue
        include_subdirs = include_subdirs.union(
          (subdir / p for p in (prefix / subdir).list()
           if p.startswith('Qt')))
    tokens = map(lambda p: p / token, include_subdirs)
    prefixes = self._search_many_all(list(tokens), test)
    miss = []
    if version_effective is not None:
      assert prefix is not None
    # Try every search path
    for path, include_subdir in prefixes:
      include_subdir = include_subdir.without_suffix(token)

      # Create basic configuration for version checking.
      cfg = Config()
      cfg.add_system_include_path(path.without_prefix(
        drake.path_build()) / include_subdir)
      self.__lib_path = path / 'lib'
      cfg.lib_path(path.without_prefix(drake.path_build()) / 'lib')
      # XXX: Check the version.
      if version_effective not in version:
        miss.append(version_effective)
        continue
      # Fill configuration
      self.__prefix = path
      self.__cfg = cfg
      for prop in self.__libraries:
        setattr(self, '_Qt__config_%s_dynamic' % prop, None)
        setattr(self, '_Qt__config_%s_static' % prop, None)
        setattr(self, '_Qt__config_%s_dynamic_header' % prop, None)
        setattr(self, '_Qt__config_%s_static_header' % prop, None)
        setattr(self, '_Qt__%s_dynamic' % prop, None)
        setattr(self, '_Qt__%s_static' % prop, None)
      self.__version = version_effective
      self.plug(cxx_toolkit)
      Qt.__include_dir = path.without_prefix(drake.path_build()) / include_subdir
      return

    raise Exception('no matching Qt for the requested version '
                    '(%s) in %s. Found versions: %s.' % \
                    (version, self._format_search([path for path, include_subdir in prefixes]),
                     ', '.join(map(str, miss))))

  def __find_lib(self, lib, lib_path, cxx_toolkit, static):
    suffixes = ['', '-mt']
    # Suffixes
    if static:
      suffixes.append('-mt-s')
      suffixes.append('-s')
    if cxx_toolkit.os == drake.os.windows:
      suffixes = list(map(lambda x: x + str(self.version.major), suffixes))
    if isinstance(cxx_toolkit, drake.cxx.VisualToolkit):
      suffix = '-vc%s0-mt-%s_%s' % (cxx_toolkit.version,
                                    self.version.major,
                                    self.version.minor)
      suffixes = [suffix] + suffixes
      mgw = '-mgw-mt-%s_%s' % (self.version.major, self.version.minor)
      suffixes += [mgw]

    # Variants
    variants = ['']
    if isinstance(lib, str):
      lib = (lib,)
    for lib, suffix, variant in itertools.product(lib, suffixes, variants):
      libname = '%s%s%s' % (lib, variant, suffix)
      tests = []
      if static:
        filename = cxx_toolkit.libname_static(self.__cfg, libname)
        tests.append(lib_path / filename)
      else:
        prefix = '' if cxx_toolkit.os == drake.os.windows else 'lib'
        filename = cxx_toolkit.libname_dyn(libname, self.__cfg, prefix)
        tests.append(lib_path / ('%s.%s' % (filename, self.__version)))
        tests.append(lib_path / filename)<commit_msg>drake.cxx.qt: Only check for major version in find_lib.<commit_after># Copyright (C) 2009-2014, Quentin "mefyl" Hocquet
#
# This software is provided "as is" without warranty of any kind,
# either expressed or implied, including but not limited to the
# implied warranties of fitness for a particular purpose.
#
# See the LICENSE file for more information.

import itertools
import drake
import platform
import re
import os

from itertools import chain

from .. import Builder, Exception, Node, Path, node, debug
from .  import Config, StaticLib, Header, Object, Source
from .qt_headers import headers as per_version_headers
import drake.cxx

deps_handler_name = 'drake.cxx.qt.moc'

def moc_file(linker, header):
  for i in linker.config.system_include_path:
    if header.name_relative.dirname() == i:
      return None
  path = Path(header.name()).with_extension('moc.cc')
  src = node(path)
  if src.builder is None:
    Moc(linker.toolkit.qt, header, src)
  return Object(src, linker.toolkit, linker.config)

def find(prefix = None,
         cxx_toolkit = None,
         version = drake.Version(),
         prefer_shared = True):
  if isinstance(prefix, Qt):
    if prefix.version not in version:
      raise Exception('given Qt %s does not fit '
                      'the requested version %s' %
                      (prefix.version, version))
    return prefix
  else:
    return Qt(prefix = prefix,
              cxx_toolkit = cxx_toolkit,
              version = version,
              prefer_shared = prefer_shared)

class Qt(drake.Configuration):
  __libraries = {
    'core': 'QtCore',
    'support_3': 'Qt3Support',
    'declarative': 'QtDeclarative',
    'gui': 'QtGui',
    'multimedia': 'QtMultimedia',
    'network': 'QtNetwork',
    'webkit': 'QtWebKit',
    'xml': 'QtXml',
    'xml_pattars': 'QtXmlPatterns',
#    'phonon': 'phonon',
    }

  imageformats = {
    "gif",
    "ico",
    "jpeg",
    "svg",
  }

  def __init__(self,
               cxx_toolkit = None,
               prefix = None,
               version = drake.Version(),
               version_effective = None,
               prefer_shared = True):
    """Find and create a configuration for Qt.

    prefix -- Where to find Qt, should contain
              include/Qt/qglobal.h among others. /usr and
              /usr/local are searched by default. If relative, it
              is rooted in the source tree.
    version -- Requested version.
    prefer_shared -- Check dynamic libraries first.
    """
    self.__moc_cache = {}
    self.__dependencies = {}
    cxx_toolkit = cxx_toolkit or drake.cxx.Toolkit()
    self.__cxx_toolkit = cxx_toolkit
    self.__prefer_shared = prefer_shared
    include_subdirs = set(drake.Path(p)
                          for p in ['include', 'include/qt4'])
    if prefix is None:
      test = [path.dirname() for path in cxx_toolkit.include_path
              if path.basename() in include_subdirs]
    else:
      test = [Path(prefix)]
    for i in range(len(test)):
      if not test[i].absolute():
        test[i] = drake.path_source() / test[i]
    token = drake.Path('Qt/qglobal.h')
    include_subdirs = {drake.Path('include')}
    for prefix in test:
      for subdir in include_subdirs:
        if not (prefix / subdir).exists():
          continue
        include_subdirs = include_subdirs.union(
          (subdir / p for p in (prefix / subdir).list()
           if p.startswith('Qt')))
    tokens = map(lambda p: p / token, include_subdirs)
    prefixes = self._search_many_all(list(tokens), test)
    miss = []
    if version_effective is not None:
      assert prefix is not None
    # Try every search path
    for path, include_subdir in prefixes:
      include_subdir = include_subdir.without_suffix(token)

      # Create basic configuration for version checking.
      cfg = Config()
      cfg.add_system_include_path(path.without_prefix(
        drake.path_build()) / include_subdir)
      self.__lib_path = path / 'lib'
      cfg.lib_path(path.without_prefix(drake.path_build()) / 'lib')
      # XXX: Check the version.
      if version_effective not in version:
        miss.append(version_effective)
        continue
      # Fill configuration
      self.__prefix = path
      self.__cfg = cfg
      for prop in self.__libraries:
        setattr(self, '_Qt__config_%s_dynamic' % prop, None)
        setattr(self, '_Qt__config_%s_static' % prop, None)
        setattr(self, '_Qt__config_%s_dynamic_header' % prop, None)
        setattr(self, '_Qt__config_%s_static_header' % prop, None)
        setattr(self, '_Qt__%s_dynamic' % prop, None)
        setattr(self, '_Qt__%s_static' % prop, None)
      self.__version = version_effective
      self.plug(cxx_toolkit)
      Qt.__include_dir = path.without_prefix(drake.path_build()) / include_subdir
      return

    raise Exception('no matching Qt for the requested version '
                    '(%s) in %s. Found versions: %s.' % \
                    (version, self._format_search([path for path, include_subdir in prefixes]),
                     ', '.join(map(str, miss))))

  def __find_lib(self, lib, lib_path, cxx_toolkit, static):
    suffixes = ['', '-mt']
    # Suffixes
    if static:
      suffixes.append('-mt-s')
      suffixes.append('-s')
    if cxx_toolkit.os == drake.os.windows:
      suffixes = list(map(lambda x: x + str(self.version.major), suffixes))
    if isinstance(cxx_toolkit, drake.cxx.VisualToolkit):
      suffix = '-vc%s0-mt-%s_%s' % (cxx_toolkit.version,
                                    self.version.major,
                                    self.version.minor)
      suffixes = [suffix] + suffixes
      mgw = '-mgw-mt-%s_%s' % (self.version.major, self.version.minor)
      suffixes += [mgw]

    # Variants
    variants = ['']
    if isinstance(lib, str):
      lib = (lib,)
    for lib, suffix, variant in itertools.product(lib, suffixes, variants):
      libname = '%s%s%s' % (lib, variant, suffix)
      tests = []
      if static:
        filename = cxx_toolkit.libname_static(self.__cfg, libname)
        tests.append(lib_path / filename)
      else:
        prefix = '' if cxx_toolkit.os == drake.os.windows else 'lib'
        filename = cxx_toolkit.libname_dyn(libname, self.__cfg, prefix)
        tests.append(lib_path / ('%s.%s' % (filename, self.__version.major)))
        tests.append(lib_path / filename)
{'fraction_non_alphanumeric': 0.11303317535545024, 'fraction_numerical': 0.0018957345971563982, 'mean_word_length': 2.4898015435501653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5731011', 'n_tokens_mistral': 3995, 'n_tokens_neox': 3931, 'n_words': 1026}