o
    p.a(,                     @   sP   d Z ddlZddlZddlZddlZddlZdd Zdd Z	dd Z
d	d
 ZdS )a  Xcode-ninja wrapper project file generator.

This updates the data structures passed to the Xcode gyp generator to build
with ninja instead. The Xcode project itself is transformed into a list of
executable targets, each with a build step to build with ninja, and a target
with every source and resource file.  This appears to sidestep some of the
major performance headaches experienced using complex projects and large number
of targets within Xcode.
    Nc              
   C   sz  t j| \}}|d }|d }|jrt j|j|}zt | W n ty; } z|jtjkr1 W Y d}~nd}~ww d}| |fD ]}	t jt j	|	d d }
t
jjd|
 }
|d|
 7 }qB|d	7 }t j|d
}z)t|d}| }||kr	 W d   W dS W d   n1 sw   Y  W n	 ty   Y nw t|d}|| W d   dS 1 sw   Y  dS )z8 Create a workspace to wrap main and sources gyp paths. z.xcworkspaceoptionsNzC<?xml version="1.0" encoding="UTF-8"?>
<Workspace version = "1.0">
r   z
.xcodeprojzgroup:z$  <FileRef location = %s></FileRef>
z</Workspace>
zcontents.xcworkspacedatarw)ospathsplitextgenerator_outputjoinmakedirsOSErrorerrnoEEXISTbasenamexmlsaxsaxutils	quoteattropenreadIOErrorwrite)main_gypsources_gypparamsbuild_file_rootbuild_file_extworkspace_pathr   eoutput_stringgyp_namenameworkspace_file
input_fileinput_stringoutput_file r%   1/usr/lib/python3/dist-packages/gyp/xcode_ninja.py_WriteWorkspace   sF   "r'   c              	   C   s  d}d}|r!|d }t j|jtjj|}|di dd}| d}| d|}| d}i }||d< ||d< |rA||d< | d	|d	< | d
|d
< i |d< i }	|r]d| |	d< d| v r| d 	 D ]7}
| d |
 di }d|v rd|	d< |d |	d< dD ]}||v r|| |	|< qi |d |
< |	|d |
 d< qg| dd|d< | dd|d< | dd|d< | dd|d< | dd|d< | d |d< |rd| g g ddt j
d  dd|	d |gd| dg|d < |dkr|d  d d! d"|f |S )#z- Create fake target for xcode-ninja wrapper. Nr   r   generator_flagsxcode_ninja_jobstarget_nameproduct_nameproduct_extensiontoolsetdefault_configurationconfigurationsz-%s/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)CONFIGURATION_BUILD_DIRxcode_settingsIPHONEOS_DEPLOYMENT_TARGETNOCODE_SIGNING_REQUIRED)BUNDLE_LOADER	TEST_HOST
mac_bundlemac_xctest_bundleios_app_extensionios_watchkit_extensionios_watchkit_apptypezCompile and copy %s via ninjaenvzPATH=%sPATHninjaz-C)action_nameinputsoutputsactionmessageactionsrC   z-j)r   r   r	   toplevel_dirgyp	generatorr?   ComputeOutputDirgetkeysenvironextend)old_specr   ninja_topleveljobsr   r*   r+   r,   ninja_targetnew_xcode_settingsconfigold_xcode_settingskeyr%   r%   r&   _TargetFromSpec8   s~   



rV   c                 C   sv   | d}| durt| |rdS t| dddks+| dddkr9| d	dd
kr9|dur7t||s7dS dS dS )ap  Limit targets for Xcode wrapper.

  Xcode sometimes performs poorly with too many targets, so only include
  proper executable targets, with filters to customize.
  Arguments:
    target_extras: Regular expression to always add, matching any target.
    executable_target_pattern: Regular expression limiting executable targets.
    spec: Specifications for target.
  r*   NTr8   r   r<    
executabler,   bundleF)rJ   researchint)target_extrasexecutable_target_patternspecr*   r%   r%   r&   IsValidTargetForWrapper   s   

r`   c           !         s6  |d d }|  D ]\}}||kr|d }q
|di }|dd}	|	du r5tj|\}
}|
d | }	g }i }i }i ||	< g ||	 d< g ||	 d	< || d
i ||	 d
< |dd}|dd}| D ]N}|| }t|||r|d}d|	|f }|| t||||< ||dd  d	 D ]}|d |kri }|d |d< |d |d< ||	 d	 | qqed}t|dddddd}dd|gii|d< |dd }g }|  D ]_\}}tj	| |dg |dg  }|s|
|dg |dg   |dg D ]}|
|d g  |s|
|d!g  qd"d# |D }tj	|	| fd$d#|D 7 }qtt||d< tjtj	|	|d% }d||f } ||  ||| < i }|d |d< ||d< d|d< i ||< g || d	< g || d< || d
i || d
< || d	 | t|	|| |||fS )&a  Initialize targets for the ninja wrapper.

  This sets up the necessary variables in the targets to generate Xcode projects
  that use ninja as an external builder.
  Arguments:
    target_list: List of target pairs: 'base/base.gyp:base'.
    target_dicts: Dict of target properties keyed on target pair.
    data: Dict of flattened build files keyed on gyp path.
    params: Dict of global options for gyp.
  build_filesr   _DEPTHr(   xcode_ninja_main_gypNz.ninjaincluded_filestargetsr1   %xcode_ninja_executable_target_patternxcode_ninja_target_patternr*   z%s:%s#target:r-   sources_for_indexingtargetDefault0rX   )r*   r-   r.   r7   r<   include_dirsr/   xcode_ninja_list_excluded_filesTsourcesmac_bundle_resourcessources_excludedmac_bundle_resources_excludedrE   rA   inputs_excludedc                 S   s   g | ]	}| d s|qS )$)
startswith.0filer%   r%   r&   
<listcomp>  s    z!CreateWrapper.<locals>.<listcomp>c                    s$   g | ]}t jt j |qS r%   )r   r   relpathr	   rv   baserelative_pathr%   r&   ry     s    z.gyp)itemsrJ   r   r   r   r`   appendrV   splitdirnamerM   sortedsetr	   r'   )!target_listtarget_dictsdatar   orig_gypr   gyp_dictdepthr(   r   r   r   new_target_listnew_target_dictsnew_datar^   r]   old_qualified_targetr_   r*   new_target_name
old_targetnew_data_targetsources_target_namesources_targetskip_excluded_filesro   rj   target_dictfilesrC   r   fully_qualified_target_namer%   r{   r&   CreateWrapper   s   



	







r   )__doc__r   gyp.generator.ninjarG   r   rZ   xml.sax.saxutilsr   r'   rV   r`   r   r%   r%   r%   r&   <module>   s   
"K