parameters: (target_name, rules, extra_sources, extra_deps,
                 path_to_gyp, output)
docstring: Write CMake for the 'rules' in the target.
  
    Args:
      target_name: the name of the CMake target being generated.
      actions: the Gyp 'actions' dict for this target.
      extra_sources: [(<cmake_src>, <src>)] to append with generated source files.
      extra_deps: [<cmake_taget>] to append with generated targets.
      path_to_gyp: relative path from CMakeLists.txt being generated to
          the Gyp file in which the target being generated is defined.
docstring_summary: Write CMake for the 'rules' in the target.
docstring_tokens: Write CMake for the rules in the target .
function: def WriteRules(target_name, rules, extra_sources, extra_deps,
                 path_to_gyp, output):
    """Write CMake for the 'rules' in the target.
  
    Args:
      target_name: the name of the CMake target being generated.
      actions: the Gyp 'actions' dict for this target.
      extra_sources: [(<cmake_src>, <src>)] to append with generated source files.
      extra_deps: [<cmake_taget>] to append with generated targets.
      path_to_gyp: relative path from CMakeLists.txt being generated to
          the Gyp file in which the target being generated is defined.
    """
    for rule in rules:
      rule_name = StringToCMakeTargetName(target_name + '__' + rule['rule_name'])
  
      inputs = rule.get('inputs', [])
      inputs_name = rule_name + '__input'
      SetVariableList(output, inputs_name,
          [NormjoinPathForceCMakeSource(path_to_gyp, dep) for dep in inputs])
      outputs = rule['outputs']
      var_outputs = []
  
      for count, rule_source in enumerate(rule.get('rule_sources', [])):
        action_name = rule_name + '_' + str(count)
  
        rule_source_dirname, rule_source_basename = os.path.split(rule_source)
        rule_source_root, rule_source_ext = os.path.splitext(rule_source_basename)
  
        SetVariable(output, 'RULE_INPUT_PATH', rule_source)
        SetVariable(output, 'RULE_INPUT_DIRNAME', rule_source_dirname)
        SetVariable(output, 'RULE_INPUT_NAME', rule_source_basename)
        SetVariable(output, 'RULE_INPUT_ROOT', rule_source_root)
        SetVariable(output, 'RULE_INPUT_EXT', rule_source_ext)
  
        # Build up a list of outputs.
        # Collect the output dirs we'll need.
        dirs = set(dir for dir in (os.path.dirname(o) for o in outputs) if dir)
  
        # Create variables for the output, as 'local' variable will be unset.
        these_outputs = []
        for output_index, out in enumerate(outputs):
          output_name = action_name + '_' + str(output_index)
          SetVariable(output, output_name,
                       NormjoinRulePathForceCMakeSource(path_to_gyp, out,
                                                        rule_source))
          if int(rule.get('process_outputs_as_sources', False)):
            extra_sources.append(('${' + output_name + '}', out))
          these_outputs.append('${' + output_name + '}')
          var_outputs.append('${' + output_name + '}')
  
        # add_custom_command
        output.write('add_custom_command(OUTPUT\n')
        for out in these_outputs:
          output.write('  ')
          output.write(out)
          output.write('\n')
  
        for directory in dirs:
          output.write('  COMMAND ${CMAKE_COMMAND} -E make_directory ')
          output.write(directory)
          output.write('\n')
  
        output.write('  COMMAND ')
        output.write(gyp.common.EncodePOSIXShellList(rule['action']))
        output.write('\n')
  
        output.write('  DEPENDS ')
        WriteVariable(output, inputs_name)
        output.write(' ')
        output.write(NormjoinPath(path_to_gyp, rule_source))
        output.write('\n')
  
        # CMAKE_CURRENT_LIST_DIR is where the CMakeLists.txt lives.
        # The cwd is the current build directory.
        output.write('  WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/')
        output.write(path_to_gyp)
        output.write('\n')
  
        output.write('  COMMENT ')
        if 'message' in rule:
          output.write(rule['message'])
        else:
          output.write(action_name)
        output.write('\n')
  
        output.write('  VERBATIM\n')
        output.write(')\n')
  
        UnsetVariable(output, 'RULE_INPUT_PATH')
        UnsetVariable(output, 'RULE_INPUT_DIRNAME')
        UnsetVariable(output, 'RULE_INPUT_NAME')
        UnsetVariable(output, 'RULE_INPUT_ROOT')
        UnsetVariable(output, 'RULE_INPUT_EXT')
  
      # add_custom_target
      output.write('add_custom_target(')
      output.write(rule_name)
      output.write(' DEPENDS\n')
      for out in var_outputs:
        output.write('  ')
        output.write(out)
        output.write('\n')
      output.write('SOURCES ')
      WriteVariable(output, inputs_name)
      output.write('\n')
      for rule_source in rule.get('rule_sources', []):
        output.write('  ')
        output.write(NormjoinPath(path_to_gyp, rule_source))
        output.write('\n')
      output.write(')\n')
  
      extra_deps.append(rule_name)
function_tokens: def WriteRules ( target_name , rules , extra_sources , extra_deps , path_to_gyp , output ) : for rule in rules : rule_name = StringToCMakeTargetName ( target_name + '__' + rule [ 'rule_name' ] ) inputs = rule . get ( 'inputs' , [ ] ) inputs_name = rule_name + '__input' SetVariableList ( output , inputs_name , [ NormjoinPathForceCMakeSource ( path_to_gyp , dep ) for dep in inputs ] ) outputs = rule [ 'outputs' ] var_outputs = [ ] for count , rule_source in enumerate ( rule . get ( 'rule_sources' , [ ] ) ) : action_name = rule_name + '_' + str ( count ) rule_source_dirname , rule_source_basename = os . path . split ( rule_source ) rule_source_root , rule_source_ext = os . path . splitext ( rule_source_basename ) SetVariable ( output , 'RULE_INPUT_PATH' , rule_source ) SetVariable ( output , 'RULE_INPUT_DIRNAME' , rule_source_dirname ) SetVariable ( output , 'RULE_INPUT_NAME' , rule_source_basename ) SetVariable ( output , 'RULE_INPUT_ROOT' , rule_source_root ) SetVariable ( output , 'RULE_INPUT_EXT' , rule_source_ext ) # Build up a list of outputs. # Collect the output dirs we'll need. dirs = set ( dir for dir in ( os . path . dirname ( o ) for o in outputs ) if dir ) # Create variables for the output, as 'local' variable will be unset. these_outputs = [ ] for output_index , out in enumerate ( outputs ) : output_name = action_name + '_' + str ( output_index ) SetVariable ( output , output_name , NormjoinRulePathForceCMakeSource ( path_to_gyp , out , rule_source ) ) if int ( rule . get ( 'process_outputs_as_sources' , False ) ) : extra_sources . append ( ( '${' + output_name + '}' , out ) ) these_outputs . append ( '${' + output_name + '}' ) var_outputs . append ( '${' + output_name + '}' ) # add_custom_command output . write ( 'add_custom_command(OUTPUT\n' ) for out in these_outputs : output . write ( '  ' ) output . write ( out ) output . write ( '\n' ) for directory in dirs : output . write ( '  COMMAND ${CMAKE_COMMAND} -E make_directory ' ) output . write ( directory ) output . write ( '\n' ) output . write ( '  COMMAND ' ) output . write ( gyp . common . EncodePOSIXShellList ( rule [ 'action' ] ) ) output . write ( '\n' ) output . write ( '  DEPENDS ' ) WriteVariable ( output , inputs_name ) output . write ( ' ' ) output . write ( NormjoinPath ( path_to_gyp , rule_source ) ) output . write ( '\n' ) # CMAKE_CURRENT_LIST_DIR is where the CMakeLists.txt lives. # The cwd is the current build directory. output . write ( '  WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/' ) output . write ( path_to_gyp ) output . write ( '\n' ) output . write ( '  COMMENT ' ) if 'message' in rule : output . write ( rule [ 'message' ] ) else : output . write ( action_name ) output . write ( '\n' ) output . write ( '  VERBATIM\n' ) output . write ( ')\n' ) UnsetVariable ( output , 'RULE_INPUT_PATH' ) UnsetVariable ( output , 'RULE_INPUT_DIRNAME' ) UnsetVariable ( output , 'RULE_INPUT_NAME' ) UnsetVariable ( output , 'RULE_INPUT_ROOT' ) UnsetVariable ( output , 'RULE_INPUT_EXT' ) # add_custom_target output . write ( 'add_custom_target(' ) output . write ( rule_name ) output . write ( ' DEPENDS\n' ) for out in var_outputs : output . write ( '  ' ) output . write ( out ) output . write ( '\n' ) output . write ( 'SOURCES ' ) WriteVariable ( output , inputs_name ) output . write ( '\n' ) for rule_source in rule . get ( 'rule_sources' , [ ] ) : output . write ( '  ' ) output . write ( NormjoinPath ( path_to_gyp , rule_source ) ) output . write ( '\n' ) output . write ( ')\n' ) extra_deps . append ( rule_name )