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splitextZgenerator_outputjoinmakedirsOSErrorerrnoZEEXISTbasenamexmlZsaxZsaxutilsZ	quoteattropenreadIOErrorwrite)main_gypsources_gypparamsbuild_file_rootbuild_file_extZworkspace_pathr   eZoutput_stringgyp_namenameZworkspace_fileZ
input_fileZinput_stringZ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_flagsZxcode_ninja_jobstarget_nameproduct_nameproduct_extensiontoolsetdefault_configurationconfigurationsz-%s/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)ZCONFIGURATION_BUILD_DIRxcode_settingsZIPHONEOS_DEPLOYMENT_TARGETZNOZCODE_SIGNING_REQUIRED)ZBUNDLE_LOADERZ	TEST_HOST
mac_bundlemac_xctest_bundleZios_app_extensionZios_watchkit_extensionZios_watchkit_apptypezCompile and copy %s via ninjaenvzPATH=%sPATHninjaz-C)action_nameinputsZoutputsactionmessageactionsr-   z-j)r   r   r   Ztoplevel_dirgyp	generatorr*   ZComputeOutputDirgetkeysenvironextend)Zold_specr   Zninja_toplevelZjobsr   r   r   r    Zninja_targetZnew_xcode_settingsZconfigZold_xcode_settingskeyr   r   r   _TargetFromSpec8   s~   



r7   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   NTr&   r   r'    
executabler    ZbundleF)r2   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.
  Zbuild_filesr   Z_DEPTHr   Zxcode_ninja_main_gypNz.ninjaZincluded_filestargetsr$   Z%xcode_ninja_executable_target_patternZxcode_ninja_target_patternr   z%s:%s#target:r!   Zsources_for_indexingtargetZDefault0r9   )r   r!   r"   r%   r'   Zinclude_dirsr#   Zxcode_ninja_list_excluded_filesTsourcesZmac_bundle_resourcesZsources_excludedZmac_bundle_resources_excludedr/   r,   Z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   rH   baseZrelative_pathr   r   rK     s    z.gyp)itemsr2   r   r   r   r@   appendr7   splitdirnamer5   sortedsetr   r   )!Ztarget_listZtarget_dictsdatar   Zorig_gypr   Zgyp_dictdepthr   r   r   r   Znew_target_listZnew_target_dictsZnew_datar>   r=   Zold_qualified_targetr?   r   Znew_target_nameZ
old_targetZnew_data_targetZsources_target_nameZsources_targetZskip_excluded_filesrE   rC   Ztarget_dictfilesr-   r   Zfully_qualified_target_namer   rM   r   CreateWrapper   s   



	







rX   )__doc__r   Zgyp.generator.ninjar0   r   r:   Zxml.sax.saxutilsr   r   r7   r@   rX   r   r   r   r   <module>   s   
"K