CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions
condformats_serialization_generate.SerializationCodeGenerator Class Reference
Inheritance diagram for condformats_serialization_generate.SerializationCodeGenerator:

Public Member Functions

def __init__ (self, scramFlags=None)
 
def cleanFlags (self, flagsIn)
 
def generate (self, outFileName)
 

Public Attributes

 classes
 
 cmssw_base
 
 split_path
 

Private Member Functions

def _join_package_path (self, path)
 

Detailed Description

Definition at line 419 of file condformats_serialization_generate.py.

Constructor & Destructor Documentation

def condformats_serialization_generate.SerializationCodeGenerator.__init__ (   self,
  scramFlags = None 
)

Definition at line 421 of file condformats_serialization_generate.py.

421  def __init__(self, scramFlags=None):
422 
423  self.cmssw_base = os.getenv('CMSSW_BASE')
424  if self.cmssw_base is None:
425  raise Exception('CMSSW_BASE is not set.')
426  logging.debug('cmssw_base = %s', self.cmssw_base)
427 
428  cwd = os.getcwd()
429  logging.debug('cwd = %s', cwd)
430 
431  if not cwd.startswith(self.cmssw_base):
432  raise Exception('The filepath does not start with CMSSW_BASE.')
433 
434  relative_path = cwd[len(self.cmssw_base)+1:]
435  logging.debug('relative_path = %s', relative_path)
436 
437  self.split_path = split_path(relative_path)
438  logging.debug('splitpath = %s', self.split_path)
439 
440  if len(self.split_path) < 3:
441  raise Exception('This script requires to be run inside a CMSSW package (usually within CondFormats), e.g. CondFormats/Alignment. The current path is: %s' % self.split_path)
442 
443  if self.split_path[0] != 'src':
444  raise Exception('The first folder should be src.')
445 
446  if self.split_path[1] != 'CondFormats':
447  raise Exception('The second folder should be CondFormats.')
448 
449  product_name = '%s%s' % (self.split_path[1], self.split_path[2])
450  logging.debug('product_name = %s', product_name)
451 
452  if not scramFlags:
453  cpp_flags = get_flags(product_name, 'CPPFLAGS')
454  cxx_flags = get_flags(product_name, 'CXXFLAGS')
455  else:
456  cpp_flags = self.cleanFlags( scramFlags )
457  cxx_flags = []
458 
459  # We are using libClang, thus we have to follow Clang include paths
460  std_flags = get_default_gcc_search_paths(gcc='clang++')
461  log_flags('cpp_flags', cpp_flags)
462  log_flags('cxx_flags', cxx_flags)
463  log_flags('std_flags', std_flags)
464 
465  flags = ['-xc++'] + cpp_flags + cxx_flags + std_flags
466 
467  headers_h = self._join_package_path('src', 'headers.h')
468  logging.debug('headers_h = %s', headers_h)
469  if not os.path.exists(headers_h):
470  raise Exception('File %s does not exist. Impossible to serialize package.' % headers_h)
471 
472  logging.info('Searching serializable classes in %s/%s ...', self.split_path[1], self.split_path[2])
473 
474  logging.debug('Parsing C++ classes in file %s ...', headers_h)
475  # On macOS we need to costruct library search path
476  if "SCRAM_ARCH" in os.environ and re.match('osx10*',os.environ['SCRAM_ARCH']):
477  cindex=clang.cindex
478  libpath=os.path.dirname(os.path.realpath(clang.cindex.__file__))+"/../../lib"
479  cindex.Config.set_library_path(libpath)
480  index = cindex.Index.create()
481  else :
482  index = clang.cindex.Index.create()
483  translation_unit = index.parse(headers_h, flags)
484  if not translation_unit:
485  raise Exception('Unable to load input.')
486 
487  severity_names = ('Ignored', 'Note', 'Warning', 'Error', 'Fatal')
488  get_severity_name = lambda severity_num: severity_names[severity_num] if severity_num < len(severity_names) else 'Unknown'
489  max_severity_level = 0 # Ignored
490  diagnostics = get_diagnostics(translation_unit)
491  for diagnostic in diagnostics:
492  logf = logging.error
493 
494  # Ignore some known warnings
495  if diagnostic['spelling'].startswith('argument unused during compilation') \
496  or diagnostic['spelling'].startswith('unknown warning option'):
497  logf = logging.debug
498 
499  logf('Diagnostic: [%s] %s', get_severity_name(diagnostic['severity']), diagnostic['spelling'])
500  logf(' at line %s in %s', diagnostic['location'].line, diagnostic['location'].file)
501 
502  max_severity_level = max(max_severity_level, diagnostic['severity'])
503 
504  if max_severity_level >= 3: # Error
505  raise Exception('Please, resolve all errors before proceeding.')
506 
507  self.classes = get_serializable_classes_members(translation_unit.cursor, only_from_path=self._join_package_path())
508 
def get_default_gcc_search_paths(gcc='g++', language='c++')
def get_serializable_classes_members(node, all_template_types=None, namespace='', only_from_path=None)

Member Function Documentation

def condformats_serialization_generate.SerializationCodeGenerator._join_package_path (   self,
  path 
)
private
def condformats_serialization_generate.SerializationCodeGenerator.cleanFlags (   self,
  flagsIn 
)

Definition at line 512 of file condformats_serialization_generate.py.

512  def cleanFlags(self, flagsIn):
513  flags = [ flag for flag in flagsIn if not flag.startswith(('-march', '-mtune', '-fdebug-prefix-map', '-ax', '-wd')) ]
514  blackList = ['--', '-fipa-pta', '-xSSE3', '-fno-crossjumping', '-fno-aggressive-loop-optimizations']
515  return [x for x in flags if x not in blackList]
516 
def condformats_serialization_generate.SerializationCodeGenerator.generate (   self,
  outFileName 
)

Definition at line 517 of file condformats_serialization_generate.py.

References condformats_serialization_generate.SerializationCodeGenerator._join_package_path(), condformats_serialization_generate.SerializationCodeGenerator.classes, join(), condformats_serialization_generate.sanitise(), and condformats_serialization_generate.SerializationCodeGenerator.split_path.

517  def generate(self, outFileName):
518 
519  filename = outFileName
520  if not filename: # in case we're not using scram, this may not be set, use the default then, assuming we're in the package dir ...
521  filename = self._join_package_path('src', 'Serialization.cc')
522 
523  n_serializable_classes = 0
524 
525  source = headers_template.format(headers=os.path.join(self.split_path[1], self.split_path[2], 'src', 'headers.h'))
526 
527  for klass in sorted(self.classes):
528  (node, serializable, all_template_types, base_objects, members, transients) = self.classes[klass]
529 
530  if not serializable:
531  continue
532 
533  n_serializable_classes += 1
534 
535  skip_instantiation = False
536  for template_types in all_template_types:
537  if template_types:
538  skip_instantiation = True
539  source += ('template <%s>' % ', '.join(['%s %s' % template_type for template_type in template_types])) + '\n'
540 
541  source += serialize_method_begin_template.format(klass=klass) + '\n'
542 
543  for base_object_name in base_objects:
544  base_object_name_sanitised = sanitise(base_object_name)
545  source += serialize_method_base_object_template.format(base_object_name=base_object_name, base_object_name_sanitised=base_object_name_sanitised) + '\n'
546 
547  for member_name in members:
548  member_name_sanitised = sanitise(member_name)
549  source += serialize_method_member_template.format(member_name=member_name, member_name_sanitised=member_name_sanitised) + '\n'
550 
551  source += serialize_method_end
552 
553  if skip_instantiation:
554  source += '\n'
555  else:
556  source += instantiation_template.format(klass=klass) + '\n'
557 
558  if n_serializable_classes == 0:
559  raise Exception('No serializable classes found, while this package has a headers.h file.')
560 
561  # check if we have a file for template instantiations and other "special" code:
562  if os.path.exists( './src/SerializationManual.h' ) :
563  source += '#include "%s/%s/src/SerializationManual.h"\n' % (self.split_path[1], self.split_path[2])
564 
565  logging.info('Writing serialization code for %s classes in %s ...', n_serializable_classes, filename)
566  with open(filename, 'wb') as fd:
567  fd.write(source)
568 
569 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18

Member Data Documentation

condformats_serialization_generate.SerializationCodeGenerator.classes
condformats_serialization_generate.SerializationCodeGenerator.cmssw_base
condformats_serialization_generate.SerializationCodeGenerator.split_path