
    "Kb6j                         d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZd Z G d de      Zd Zd Zed	k(  r& ej*                   eej,                  d
d              yy)zUtility functions to perform Xcode-style build steps.

These functions are executed via gyp-mac-tool when using the Makefile generator.
    )print_functionNc                 j    t               }|j                  |       }|t        j                  |       y y N)MacToolDispatchsysexit)argsexecutor	exit_codes      ./usr/lib/python3/dist-packages/gyp/mac_tool.pymainr      s0    Y(%)HHY     c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)r   zThis class performs all the Mac tooling steps. The methods can either be
  executed directly, or dispatched from an argument list.c                     t        |      dk  rt        d      d| j                  |d         z  } t        | |      |dd  S )z(Dispatches a string command to a method.   zNot enough argumentszExec%sr   N)len	Exception_CommandifyNamegetattr)selfr
   methods      r   r   zMacTool.Dispatch&   sL    
4y1},--,,T!W55F 74 $qr(++r   c                 B    |j                         j                  dd      S )z<Transforms a tool name like copy-info-plist to CopyInfoPlist- )titlereplace)r   name_strings     r   r   zMacTool._CommandifyName.   s    &&sB//r   c                    |dk(  }t         j                  j                  |      d   j                         }t         j                  j	                  |      rKt         j                  j                  |      rt        j                  |       t        j                  ||       n|dk(  r| j                  ||      S |dk(  r| j                  ||      S |dk(  r|s| j                  ||       nJt         j                  j                  |      rt        j                  |       t        j                  ||       |r|dv r| j                  |       yyy)ztCopies a resource file to the bundle/Resources directory, performing any
    necessary compilation on each resource.Truer   z.xibz.storyboard.strings)z.plistr!   N)ospathsplitextlowerisdirexistsshutilrmtreecopytree_CopyXIBFile_CopyStringsFileunlinkcopy_ConvertToBinary)r   sourcedestconvert_to_binary	extensions        r   ExecCopyBundleResourcezMacTool.ExecCopyBundleResource2   s	    *V3  (+113I	ww}}V
 
	doofd#	f	vt,,	m	#vt,,	j	 ):
FD)		
		$kk&$Y*@@
D! Ar   c           	      x   t         j                  j                  t         j                  j                  t                    }t         j                  j                  |      r t         j                  j                  ||      }t         j                  j                  |      r t         j                  j                  ||      }g d}t         j                  d   dkD  rs|j                  dg       dt         j                  v r)|j                  ddddd	t         j                  d   g       n&|j                  dd
d	t         j                  d   g       |j                  ddd||g       t        j                  d      }t        j                  d      }	 t        j                  |      }d}	|j!                         D ]S  }
|
j#                  d      }|j%                  |      r|}	(|j%                  |      r:|	rt        |	       d}	t        |       U y# t        j                  $ r}t        |j                          d}~ww xY w)zBCompiles a XIB file with ibtool into a binary plist in the bundle.)xcrunibtool--errors
--warnings	--noticesXCODE_VERSION_ACTUAL0700z--auto-activate-custom-fontsIPHONEOS_DEPLOYMENT_TARGET--target-deviceiphoneipad--minimum-deployment-targetmacMACOSX_DEPLOYMENT_TARGET--output-formathuman-readable-text	--compilez/\*.*\*/z .*note:.*is clipping its contentNutf-8r   )r"   r#   dirnamerealpath__file__relpathjoinenvironextendrecompile
subprocesscheck_outputCalledProcessErrorprintoutput
splitlinesdecodematch)r   r0   r1   baser
   ibtool_section_re	ibtool_restdoutecurrent_section_headerlineline_decodeds               r   r+   zMacTool._CopyXIBFileM   s    77??277++H56D	wwvww||D&)f	wwtWW\\$%dED	zz()F2
kk123	%	3x):F)JJ34
 	 	u)JJ12
 	 	KK"$9;  

;/

>?I&&t,f "!!# [[)l		 	 	.!-|,!
&
'#'
 l  (( AHHos   H H9H44H9c           	      <    t        j                  ddddd||g       y )Nr6   plutil-convertbinary1-o)rQ   
check_call)r   r1   s     r   r/   zMacTool._ConvertToBinary~   s&    :y$dD Er   c                 l   | j                  |      xs d}ddl}t        |d      j                         }|j	                  d|t        |            }|j                  d|dd      \  }}|ryt        |d      }	|	j                  |j                  |      j                  d             |	j                          y)zFCopies a .strings file using iconv to reconvert the input into UTF-16.UTF-8r   NrbwbUTF-16)_DetectInputEncodingCoreFoundationopenreadCFDataCreater   CFPropertyListCreateFromXMLDatawriterW   encodeclose)
r   r0   r1   
input_coderm   sd_errorfps
             r   r,   zMacTool._CopyStringsFile   s    **62=gJ VT!A##D!SV4A==dAq$OHAu	dD	BHHQXXj!((23HHJr   c                     t        |d      }	 |j                  d      }|j                          |j                  d      ry|j                  d      ry|j                  d      ryy#  |j                          Y yxY w)	zReads the first few bytes from file_name and tries to guess the text
    encoding. Returns None as a guess if it can't detect it.ri      Ns   rk   s   s   ﻿rh   )rn   ro   rt   
startswith)r   	file_namerz   headers       r   rl   zMacTool._DetectInputEncoding   sv     
i	Bwwqzf HHJ%			;	'			?	+hhjs   A& &A:c                 L   t        |d      }|j                         }|j                          t        j                  |      }|r'|j                  t        j                  |d                t        j                  |      }t        j                  d      }t        j                  D ]  }	|	j                  d      rd|	z  }
t        j                  |	   }|j                  |
|      }d|	z  }
|j                  dt        j                  |	         }|j                  |
|      }d|	z  }
|j                  dt        j                  |	         }|j                  |
|      } |j!                  d	      }t#        t%        |            D ]2  }||   j'                         j                  d
      s&d||<   d||dz
  <   4 d	j)                  t+        d |            }t        |d      }|j-                  |       |j                          | j/                  |       |dk(  r| j1                  |       yy)zCCopies the |source| Info.plist to the destination directory |dest|.rr   z[_/\s]rx   z${%s}z${%s:identifier}z${%s:rfc1034identifier}r   
z
<string>${Nr   c                 
    | d uS r    xs    r   <lambda>z+MacTool.ExecCopyInfoPlist.<locals>.<lambda>   s
    q} r   wr    )rn   ro   rt   plistlibreadPlistFromStringupdatejsonloadswritePlistToStringrO   rP   r"   rM   r}   r   subsplitranger   striprL   filterrr   _WritePkgInfor/   )r   r0   r1   r2   keysfdlinesplistIDENT_REkeyevarevalueis                r   ExecCopyInfoPlistzMacTool.ExecCopyInfoPlist   s    
fc	BGGIEHHJ ((/Ell4::d1g&'''.E zz)$Hzz *		s]dzz#fmmD&)e  #%d||CC1fmmD&)e&,d||CC1fmmD&)e'*, KKE3u: 	q		$	$\	2aa!e IIf4e<=E 
dCBHHUOHHJ 	tF"
D! #r   c                 j   t        j                  |      }|sy|d   }|dk7  ry|j                  dd      }t        |      dk7  rd}t        j
                  j                  t        j
                  j                  |      d      }t        |d      }|j                  ||       |j                          y)	z@This writes the PkgInfo file from the data stored in Info.plist.NCFBundlePackageTypeAPPLCFBundleSignaturez????   PkgInfor   )r   	readPlistgetr   r"   r#   rL   rH   rn   rr   rt   )r   
info_plistr   package_typesignature_coder1   rz   s          r   r   zMacTool._WritePkgInfo   s    z*E ./Lv
 YY2F;N
>an77<<
3Y?D	dCBHH|^45HHJr   c                    t        j                  |t         j                  t         j                  z  t         j                  z  d      }t        j                  |t
        j                         t        j                  |      S )z5Emulates the most basic behavior of Linux's flock(1).i  )
r"   rn   O_RDONLYO_NOCTTYO_CREATfcntlflockLOCK_EXrQ   call)r   lockfilecmd_listr   s       r   	ExecFlockzMacTool.ExecFlock   sM     
2;;r{{22::=u	EB	KKEMM"??8$$r   c                    t        j                  d      }t        j                  d      }t        j                  j	                         }d|d<   t        j                  |t
        j                  |      }|j                         \  }}|j                         D ]R  }|j                  d      }	|j                  |	      r&|j                  |	      r8t        |	t        j                         T |j                  set!        t#        |      dz
        D ]K  }
||
   d	k(  s||
dz      j%                  d
      s$t        j&                  ||
dz      d        |j                  S  |j                  S )zQCalls libtool and filters out '/path/to/libtool: file: foo.o has no
    symbols'.z?^.*libtool: (?:for architecture: \S* )?file: .* has no symbols$z^.*libtool: warning for library: .* the table of contents is empty \(no object file members in the library define global symbols\)$1ZERO_AR_DATE)stderrenvrG   filer   re   z.aN)rO   rP   r"   rM   r.   rQ   PopenPIPEcommunicaterV   rW   rX   rT   r   r   
returncoder   r   endswithutime)r   r   
libtool_relibtool_re5r   
libtooloutrx   errr_   r`   r   s              r   ExecFilterLibtoolzMacTool.ExecFilterLibtool   s=     8 9J**	LMK **//
C
 C!!(:??LJ##%FAs  -[[)ll+K4E4El4Sl,-   S]Q&' !A;$8AaC=#9#9$#?
((8AaC=$
'
   	    r   c                    t         j                  j                  |      j                  d      d   }t         j                  j	                  |d      }t         j                  j                  |      st        j                  |       d|d|d}t        t         j                  j	                  |d      d      }|j                  |       |j                          y )	N.r   Moduleszframework module z {
  umbrella header "z*.h"

  export *
  module * { export * }
}
zmodule.modulemapr   )
r"   r#   basenamer   rL   r'   mkdirrn   rr   rt   )r   	frameworkbinarymodule_pathmodule_templatemodule_files         r   ExecPackageIosFrameworkzMacTool.ExecPackageIosFramework  s    WWi(..s3A6F'',,y)4K77>>+&hh{  &v/O rww||K1CDcJKo&r   c                    t         j                  j                  |      j                  d      d   }d}d}d}t         j                  j	                  t         j                  j                  ||||            syt        j                         }t        j                  |       | j                  |t         j                  j                  ||             | j                  t         j                  j                  |||      |       | j                  t         j                  j                  |||      |       t        j                  |       y)zeTakes a path to Something.framework and the Current version of that and
    sets up all the symlinks.r   r   Current	ResourcesVersionsN)	r"   r#   r   r   r'   rL   getcwdchdir_Relink)r   r   versionr   CURRENT	RESOURCESVERSIONSpwds           r   ExecPackageFrameworkzMacTool.ExecPackageFramework/  s     WWi(..s3A6FGIH77>>"'',,y(GVLM  ))+CHHY 	LL"'',,x9: 	LLh8&ALLh;YG HHSMr   c                     t         j                  j                  |      rt        j                  |       t        j                  ||       y)zZCreates a symlink to |dest| named |link|. If |link| already exists,
    it is overwritten.N)r"   r#   lexistsremovesymlink)r   r1   links      r   r   zMacTool._RelinkL  s-     
wwtiioJJtTr   c                 f   t         j                  j                  |      j                  d      d   }t	        t         j                  j
                  |      }i }|D ]I  }t         j                  j                  |      }|||<   ||t         j                  j                  ||      <   K t        ||       y )Nr   r   )r"   r#   r   r   mapabspathrL   	WriteHmap)r   outr   all_headersframework_namefilelistr   filenames           r    ExecCompileIosFrameworkHeaderMapz(MacTool.ExecCompileIosFrameworkHeaderMapS  s    WW%%i066s;A>Nbggoo{3KH @!!&)h!hx9?hrww||NH56@ c8r   c           
      \   t         j                  j                  |d      }t         j                  j                  |      st        j                  |       |D ]S  }t        j                  |t         j                  j                  |t         j                  j                  |                   U y )NHeaders)r"   r#   rL   r'   makedirsr(   r.   r   )r   r   copy_headersheader_pathr   s        r   ExecCopyIosFrameworkHeadersz#MacTool.ExecCopyIosFrameworkHeaders]  so    '',,y)4K77>>+&kk+ Okk&"'',,{BGG4D4DV4LMNOr   c                    g d}dt         j                  v }|rt         j                  d   j                  d      d   }|dvrd}|j                  d|d	d
d	ddt         j                  d   dt         j                  j                  t         j                  d         g
       nX|j                  ddd	ddt         j                  d   dt         j                  j                  t         j                  d         g       |rt        j                  |      }|j                         D ]  \  }}d|z   }t        |t              r|s|j                  |       0t        |t              r3|D ]-  }	|j                  |       |j                  t        |	             / s|j                  |       |j                  t        |              |j                  t        t         j                  j
                  |             t        j                   |       y)a  Compiles multiple .xcassets files into a single .car file.

    This invokes 'actool' to compile all the inputs .xcassets files. The
    |keys| arguments is a json-encoded dictionary of extra arguments to
    pass to 'actool' when the asset catalogs contains an application icon
    or a launch image.

    Note that 'actool' does not create the Assets.car file if the asset
    catalogs does not contains imageset.
    )r6   actoolrD   rE   z--compress-pngsr:   r9   r8   r=   CONFIGURATIONr   )iphoneosiphonesimulatorr   z
--platformr>   r?   r@   rA   rF   CONTENTS_FOLDER_PATHmacosxrB   rC   !UNLOCALIZED_RESOURCES_FOLDER_PATHz--N)r"   rM   r   rN   r#   r   r   r   items
isinstanceboolappendliststrr   rQ   rf   )
r   r   inputscommand_lineis_iphone_targetplatformr   valuearg_namevs
             r   ExecCompileXcassetszMacTool.ExecCompileXcassetsd  s   L 4rzzAO,2237;h	8	8$
"3X
V%B
**1
2K
''//"**%;<
=	 	 
"3U
'4N)O

''//"**%HI
J	 	 ZZd

 **#u#:eT")t$ (a)A'( 

h
'


c%j
)* BGGOOV45,'r   c                     i }|D ]%  }| j                  |      }| j                  ||       ' t        j                  ||       y)z6Merge multiple .plist files into a single .plist file.N)_LoadPlistMaybeBinary_MergePlistr   
writePlist)r   rU   r  merged_plistr#   r   s         r   ExecMergeInfoPlistzMacTool.ExecMergeInfoPlist  sH    L ,((.e
|U+, f-r   c                 $   | j                  || j                               \  }}| j                  |||      }ddd|g}	|dk(  r|	j                  ddg       n|	j                  d|g       |	j                  d|g       t	        j
                  |	       y	)
am  Code sign a bundle.

    This function tries to code sign an iOS bundle, following the same
    algorithm as Xcode:
      1. pick the provisioning profile that best match the bundle identifier,
         and copy it into the bundle as embedded.mobileprovision,
      2. copy Entitlements.plist from user or SDK next to the bundle,
      3. code sign the bundle.
    codesignz--forcez--signr    z--deepz+--preserve-metadata=identifier,entitlementsz--entitlementsz--timestamp=noneN)_InstallProvisioningProfile_GetCFBundleIdentifier_InstallEntitlementsrN   rQ   rf   )
r   r   entitlementsprovisioningr#   preservesubstitutions	overridesentitlements_pathr
   s
             r   ExecCodeSignBundlezMacTool.ExecCodeSignBundle  s      $??d113 5M911mY0 	8S1D6
kk8JKL
kk#%678KK#T*+$r   c                    | j                  ||      \  }}}t        j                  j                  t        j                  d   t        j                  d   d      }t        j                  ||       | j                  ||dz         }||d   fS )a  Installs embedded.mobileprovision into the bundle.

    Args:
      profile: string, optional, short name of the .mobileprovision file
        to use, if empty or the file is missing, the best file installed
        will be used
      bundle_identifier: string, value of CFBundleIdentifier from Info.plist

    Returns:
      A tuple containing two dictionary: variables substitutions and values
      to overrides when generating the entitlements file.
    BUILT_PRODUCTS_DIRr   zembedded.mobileprovisionr   Entitlements)_FindProvisioningProfiler"   r#   rL   rM   r(   copy2_GetSubstitutions)r   profilebundle_identifiersource_pathprovisioning_datateam_idtarget_pathr  s           r   r  z#MacTool._InstallProvisioningProfile  s     /3.K.K"/$+K"G'',,


'(


)*"$K LLk***+<gmLM+N;;;r   c                    t         j                  j                  t         j                  d   ddd      }t         j                  j	                  |      s3t        d|z  t        j                         t        j                  d       d}|rEt         j                  j                  ||d	z         }t         j                  j                  |      r|g}|s3t        j                  t         j                  j                  |d
            }i }|D ]r  }| j                  |      }|j                  di       j                  dd      }|j                  dg       D ](  }	|	d|}
t        j                  |
|      s!|||	f||<   * t |s3t        d|z  t        j                         t        j                  d       t        |d       }||   S )a/  Finds the .mobileprovision file to use for signing the bundle.

    Checks all the installed provisioning profiles (or if the user specified
    the PROVISIONING_PROFILE variable, only consult it) and select the most
    specific that correspond to the bundle identifier.

    Args:
      profile: string, optional, short name of the .mobileprovision file
        to use, if empty or the file is missing, the best file installed
        will be used
      bundle_identifier: string, value of CFBundleIdentifier from Info.plist

    Returns:
      A tuple of the path to the selected provisioning profile, the data of
      the embedded plist in the provisioning profile and the team identifier
      to use for code signing.

    Raises:
      SystemExit: if no .mobileprovision can be used to sign the bundle.
    HOMELibraryMobileDevicezProvisioning Profilesz&cannot find mobile provisioning for %sr   r   Nz.mobileprovisionz*.mobileprovisionr  zapplication-identifierr   TeamIdentifierr   c                     t        |       S r   r   )r	  s    r   r   z2MacTool._FindProvisioningProfile.<locals>.<lambda>  s
    #a& r   r   )r"   r#   rL   rM   r&   rT   r   r   r	   r'   glob_LoadProvisioningProfiler   fnmatchmax)r   r#  r$  profiles_dirprovisioning_profilesprofile_pathvalid_provisioning_profilesprofile_dataapp_id_patternteam_identifierapp_idselected_keys               r   r   z MacTool._FindProvisioningProfile  s   * 77<<


6I~7NPL77==&
25F
Fzz 
hhqk WW\\,:L0LMl		%!- "ii
'',,|%8
9;"$- ;22<@l#''
"!c":B? )--.>C ;/+->???6>2L/9;
%n
5;	; '
25F
Fzz 
hhqk 28HIL&|44r   c           
          t        j                         5 }t        j                  dddd|d|j                  g       | j                  |j                        cddd       S # 1 sw Y   yxY w)zExtracts the plist embedded in a provisioning profile.

    Args:
      profile_path: string, path to the .mobileprovision file

    Returns:
      Content of the plist embedded in the provisioning profile as a dictionary.
    securitycmsz-Dz-ire   N)tempfileNamedTemporaryFilerQ   rf   namer  )r   r7  temps      r   r2  z MacTool._LoadProvisioningProfile  sa     
	$	$	& 3$
eT4tTYYH I''		23 3 3s   AA  A)c                     |j                         D ]Z  \  }}t        |t              r@|j                  |i       }t        |t              r| j	                  ||       |||<   P|||<   V|||<   \ y)z"Merge |plist| into |merged_plist|.N)r   r   dictr   r  )r   r  r   r   r  merged_values         r   r  zMacTool._MergePlist  so    kkm 	"
U	E4	 #''R0lD)


<
/*,s
#,s
!S	"r   c                 T   	 t        j                  |      S #  Y nxY wt        j                         5 }t	        j
                  ||j                         t        j                  ddd|j                  g       t        j                  |j                        cddd       S # 1 sw Y   yxY w)a}  Loads into a memory a plist possibly encoded in binary format.

    This is a wrapper around plistlib.readPlist that tries to convert the
    plist to the XML format if it can't be parsed (assuming that it is in
    the binary format).

    Args:
      plist_path: string, path to a plist file, in XML or binary format

    Returns:
      Content of the plist as a dictionary.
    rb   rc   xml1N)	r   r   rA  rB  r(   r!  rC  rQ   rf   )r   
plist_pathrD  s      r   r  zMacTool._LoadPlistMaybeBinary"  s     
++
		$	$	& +$ll:tyy)Xz6499EF		*+ + +s    A"BB'c                     ||dS )aF  Constructs a dictionary of variable substitutions for Entitlements.plist.

    Args:
      bundle_identifier: string, value of CFBundleIdentifier from Info.plist
      app_identifier_prefix: string, value for AppIdentifierPrefix

    Returns:
      Dictionary of substitutions to apply when generating Entitlements.plist.
    )CFBundleIdentifierAppIdentifierPrefixr   )r   r$  app_identifier_prefixs      r   r"  zMacTool._GetSubstitutions;  s     .2 r   c                     t         j                  j                  t         j                  d   t         j                  d         }| j	                  |      }|d   S )zExtracts CFBundleIdentifier value from Info.plist in the bundle.

    Returns:
      Value of CFBundleIdentifier in the Info.plist located in the bundle.
    TARGET_BUILD_DIRINFOPLIST_PATHrL  )r"   r#   rL   rM   r  )r   info_plist_pathinfo_plist_datas      r   r  zMacTool._GetCFBundleIdentifierJ  sN     ggll


%&


#$&O 00AO/00r   c                    |}t         j                  j                  t         j                  d   t         j                  d   dz         }|s1t         j                  j                  t         j                  d   d      }t	        j
                  ||       | j                  |      }| j                  ||      }|r|D ]  }||vs||   ||<    t        j                  ||       |S )a[  Generates and install the ${BundleName}.xcent entitlements file.

    Expands variables "$(variable)" pattern in the source entitlements file,
    add extra entitlements defined in the .mobileprovision file and the copy
    the generated plist to "${BundlePath}.xcent".

    Args:
      entitlements: string, optional, path to the Entitlements.plist template
        to use, defaults to "${SDKROOT}/Entitlements.plist"
      substitutions: dictionary, variable substitutions
      overrides: dictionary, values to add to the entitlements

    Returns:
      Path to the generated entitlements file.
    r  PRODUCT_NAMEz.xcentSDKROOTzEntitlements.plist)
r"   r#   rL   rM   r(   r!  r  _ExpandVariablesr   r  )r   r  r  r  r%  r(  datar   s           r   r  zMacTool._InstallEntitlementsV  s      K'',,


'(


>"X-/K GGLL
**Y

 k LLk*%%k2D  }5D %#d?n$s)% k*r   c                 `   t        |t              r/|j                         D ]  \  }}|j                  d|z  |      } |S t        |t              r |D cg c]  }| j                  ||       c}S t        |t              r$|D ci c]  }|| j                  ||   |       c}S |S c c}w c c}w )az  Expands variables "$(variable)" in data.

    Args:
      data: object, can be either string, list or dictionary
      substitutions: dictionary, variable substitutions to perform

    Returns:
      Copy of data where each references to "$(variable)" has been replaced
      by the corresponding value found in substitutions, or left intact if
      the key was not found.
    z$(%s))r   r  r   r   r  rW  rF  )r   rX  r  r   r  r	  ks          r   rW  zMacTool._ExpandVariablesx  s     $%++- 2*#u||GcM512k$?CD!d##A}5DD$HLM1a&&tAw>>MMK EMs   B&B+N) __name__
__module____qualname____doc__r   r   r4   r+   r/   r,   rl   r   r   r   r   r   r   r   r   r   r
  r  r  r  r   r2  r  r  r"  r  r  rW  r   r   r   r   r   "   s    =,0"6/bE(&7"r.%!<":O2(h. .<.75r3"+2
1 Dr   r   c                 (    d| j                         z  S )N   )
bit_lengthr   s    r   NextGreaterPowerOf2rb    s    	
QNN	r   c                    d}d}d}t        |      }t        |      }dd|z  z   }t        t        |j                         d       d         }t	        | d      }	|	j                  t        j                  d	|||||||             d
g|z  }
|j                         D ]V  \  }}d}|D ]!  }|t        |j                               dz  z  }# |
||dz
  z     |dz   }|
||dz
  z     ||f|
||dz
  z  <   X d}|
D ]  }|(|	j                  t        j                  dddd             -|\  }}|}|t        |      z   dz   }|t        t        j                  j                  |      t        j                  z         z   dz   }|t        t        j                  j                  |            z   dz   }|	j                  t        j                  d|||              |	j                  t        j                  d             |
D ]\  }||\  }}|	j                  t        j                  dt        |      z  |             |	j                  t        j                  dd             t        j                  j                  |      t        j                  z   }|	j                  t        j                  dt        |      z  |             |	j                  t        j                  dd             t        j                  j                  |      }|	j                  t        j                  dt        |      z  |             |	j                  t        j                  dd             _ y
)a  Generates a header map based on |filelist|.

  Per Mark Mentovai:
    A header map is structured essentially as a hash table, keyed by names used
    in #includes, and providing pathnames to the actual files.

  The implementation below and the comment above comes from inspecting:
    http://www.opensource.apple.com/source/distcc/distcc-2503/distcc_dist/include_server/headermap.py?txt
  while also looking at the implementation in clang in:
    https://llvm.org/svn/llvm-project/cfe/trunk/lib/Lex/HeaderMap.cpp
  ipamhr   r         c                     t        | d         S )Nr   r/  )ts    r   r   zWriteHmap.<locals>.<lambda>  s    S1Y r   r0  rj   z<LHHLLLLN   z<LLLz<xz<%dsz<s )r   rb  r4  r   rn   rr   structpackordr%   r"   r#   rH   sepr   )output_namer   magicr   	_reservedcountcapacitystrings_offsetmax_value_lengthr   bucketsr   r#   r   cnext_offsetbucket
key_offsetprefix_offsetsuffix_offsetrY   s                        r   r   r     s    %')
h-% '(h'.X^^-3FGJK[$#))FKK
E7I~x)9; < FX'NN$ /jdD
C !	S^b  c! #1$
%
1!Gc #1$
%
1#',GC(Q,/ + 	Of~	iiFAq!,-ltTj 3t9,q0m#c"''//$*?"&&*H&IIAMm!C(8(8(>$??!Ck	iiFJ}MN	O ))FKK 
)fltT	iiFSY.56	iiD$'(WW__T"RVV+d	iiFSY.56	iiD$'(WWd#d	iiFSY.56	iiD$'(
)r   __main__r   )r^  
__future__r   r   r3  r1  r   r"   r   rO   r(   rj  rQ   r   rA  r   objectr   rb  r   r[  r	   argvr   r   r   <module>r     s~   
 &     	  	    
 j	f j	X=)~ z
#((4 r   