<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> from pants . base . workunit import WorkUnitLabel <EOL> from pants . contrib . cpp . tasks . cpp_task import CppTask <EOL> class CppBinaryCreate ( CppTask ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( CppBinaryCreate , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> @ classmethod <EOL> def product_types ( cls ) : <EOL> return [ '<STR_LIT>' ] <EOL> @ classmethod <EOL> def prepare ( cls , options , round_manager ) : <EOL> super ( CppBinaryCreate , cls ) . prepare ( options , round_manager ) <EOL> round_manager . require ( '<STR_LIT>' ) <EOL> round_manager . require ( '<STR_LIT>' ) <EOL> @ property <EOL> def cache_target_dirs ( self ) : <EOL> return True <EOL> def execute ( self ) : <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , labels = [ WorkUnitLabel . TASK ] ) : <EOL> targets = self . context . targets ( self . is_binary ) <EOL> with self . invalidated ( targets , invalidate_dependents = True ) as invalidation_check : <EOL> binary_mapping = self . context . products . get ( '<STR_LIT>' ) <EOL> for vt in invalidation_check . all_vts : <EOL> binary_path = os . path . join ( vt . results_dir , vt . target . name ) <EOL> if not vt . valid : <EOL> self . _create_binary ( vt . target , binary_path ) <EOL> binary_mapping . add ( vt . target , vt . results_dir ) . append ( binary_path ) <EOL> def _create_binary ( self , target , binary_path ) : <EOL> objects = [ ] <EOL> for basedir , objs in self . context . products . get ( '<STR_LIT>' ) . get ( target ) . items ( ) : <EOL> objects . extend ( [ os . path . join ( basedir , obj ) for obj in objs ] ) <EOL> self . _link_binary ( target , binary_path , objects ) <EOL> self . context . log . info ( '<STR_LIT>' . format ( binary_path ) ) <EOL> def _libname ( self , libpath ) : <EOL> """<STR_LIT>""" <EOL> return os . path . basename ( libpath ) [ <NUM_LIT:3> : - <NUM_LIT:2> ] <EOL> def _link_binary ( self , target , binary_path , objects ) : <EOL> cmd = [ self . cpp_toolchain . compiler ] <EOL> library_dirs = [ ] <EOL> libraries = [ ] <EOL> def add_library ( target ) : <EOL> product_map = self . context . products . get ( '<STR_LIT>' ) . get ( target ) <EOL> if product_map : <EOL> for dir , libs in product_map . items ( ) : <EOL> library_dirs . append ( dir ) <EOL> libraries . extend ( ( self . _libname ( l ) for l in libs ) ) <EOL> target . walk ( add_library ) <EOL> if target . libraries : <EOL> libraries . extend ( target . libraries ) <EOL> cmd . extend ( objects ) <EOL> cmd . extend ( ( '<STR_LIT>' . format ( L ) for L in library_dirs ) ) <EOL> cmd . extend ( ( '<STR_LIT>' . format ( l ) for l in libraries ) ) <EOL> cmd . extend ( [ '<STR_LIT>' + binary_path ] ) <EOL> if self . get_options ( ) . ld_options != None : <EOL> cmd . extend ( ( '<STR_LIT>' . format ( o ) for o in self . get_options ( ) . ld_options . split ( '<STR_LIT:U+0020>' ) ) ) <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , labels = [ WorkUnitLabel . COMPILER ] ) as workunit : <EOL> self . run_command ( cmd , workunit ) </s>
