B
    0ªa¥`  ã               @   s¼  d dl mZ d dl mZ d dl m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mZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z%m&Z& d dl'm(Z( ddl)m*Z* dd„ Z+dd„ Z,dd„ Z-dd„ Z.dd„ Z/dd„ Z0d d!„ Z1d"d#„ Z2d$d%„ Z3d&d'„ Z4d(d)„ Z5d*d+„ Z6d,d-„ Z7d.d/„ Z8d0d1„ Z9d2d3„ Z:d4d5„ Z;d6d7„ Z<d8d9„ Z=dS ):é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Úmessage)ÚGtkDocCommentBlockParser)ÚIncludeÚ	Namespace)Úcompile_introspection_binary)ÚGDumpParserÚIntrospectionBinary)ÚIntrospectablePass)Ú	GIRParser)Ú	GIRWriter)ÚMainTransformer)Úresolve_shlibs)ÚSourceScannerÚALL_EXTS)ÚTransformeré   )Úutilsc             C   s|   t |j| jƒ}xht|jƒdkrv|jd dkrv|j d¡}|dkrd|jrd|jd dkrd||j d¡7 }| t |¡¡ qW d S )Nr   z--cflags-endz-I)	ÚgetattrÚvaluesÚdestÚlenZrargsÚpopÚappendr   Zcflag_real_include_path)ÚoptionÚoptÚvalueÚparserÚcflagsÚarg© r#   ú7/usr/lib/gobject-introspection/giscanner/scannermain.pyÚprocess_cflags_begin5   s    r%   c             C   s   d S )Nr#   )r   r   r   r    r#   r#   r$   Úprocess_cflags_end?   s    r&   c             C   s$   t |j| jƒ}| tj |¡¡ d S )N)r   r   r   r   ÚosÚpathÚrealpath)r   r   r   r    Úcpp_includesr#   r#   r$   Úprocess_cpp_includesC   s    r+   c          	   C   sŒ   t  | d¡}|jddddg dtd |jddd	dtd
 |jdddg ddtd |jddddg d |jddddg d |jdddd |S )NzPreprocessor optionsÚ z--cflags-beginz!Start preprocessor/compiler flagsr!   Úcallback)Úhelpr   ÚdefaultÚactionr-   z--cflags-endzEnd preprocessor/compiler flags)r.   r0   r-   z-IzPre-processor include filer*   Ústring)r.   r   r/   Útyper0   r-   z-DzPre-processor definer   Úcpp_defines)r.   r0   r   r/   z-UzPre-processor undefineÚcpp_undefinesz-pZIgnored)r   r.   )ÚoptparseÚOptionGroupÚ
add_optionr%   r&   r+   )r    Úgroupr#   r#   r$   Úget_preprocessor_option_groupH   s&    


r9   c             C   s$   t  | d¡}|jddddg d |S )NzMachine Dependent Optionsz-mzsome machine dependent optionr   Zm_option)r.   r0   r   r/   )r5   r6   r7   )r    r8   r#   r#   r$   Úget_windows_option_group^   s
    r:   c              C   s¼  t  d¡} | jddddddd | jdd	d
dddd | jddddg dd | jddddg dd | jddddg dd | jddd
dd dd | jddddg dd | jddd
d d d!d | jdd"dd#dd$d | jdd%dd&dd'd | jd(d)dd*g d+d | jdd,dd-g d.d | jd/d0dd1g d2d | jdd3dd4g d5d | jd6d7d
d8d9d: | jdd;d
d<d=d: | jdd>d
d?d@d: | jddAddBg dCd | jddDd
dEddFd | jddGddHg dId | jddJd
dKddLd | jddMddNddOd | jddPddQg dRd | jdSdTd
dUdVdWd | jddXddYg dZd | jdd[dd\g d]d | jdd^dd_dd`d | jddaddbdcd: | jdddeddfdgd: | jddhddig djd | jddkd
dlg dmd t| ƒ}|  |¡ tj dn¡}|rì| do¡rìt	| ƒ}|  |¡ | jddpd
dqd t j
d | jddrd
dsd t j
d | jddtddudt j
d | jddvddwt j
d: | jddxddyg dzd | jdd{dd|g d}d | jdd~ddg d€d | jdddd‚g dƒd | jdd„dd…g d†d | S )‡Nz%prog [options] sourcesr,   z--quietÚ
store_trueÚquietFz3If passed, do not print details of normal operation)r0   r   r/   r.   z--formatZstoreÚformatÚgirzformat to use, one of gidl, girz-iz	--includer   Úincludesz$Add specified gir file as dependencyz--include-uninstalledÚincludes_uninstalledz¢A file path to a dependency; only use this "
                            "when building multiple .gir files inside a "
                            "single module.z--add-include-pathÚinclude_pathsz!include paths for other GIR filesz	--programÚprogramzprogram to executez--program-argÚprogram_argszextra arguments to programz	--libtoolZlibtool_pathzfull path to libtoolz--no-libtoolZ	nolibtoolzdo not use libtoolz--external-libraryZexternal_libraryzJIf true, the library is located on the system,not in the current directoryz-lz	--libraryÚ	librarieszlibraries of this unitz--extra-libraryZextra_librariesz*Extra libraries to link the binary againstz-Lz--library-pathZlibrary_pathsz#directories to search for librariesz--header-onlyÚheader_onlyz<If specified, just generate a GIR for the given header filesz-nz--namespaceÚnamespace_namez]name of namespace for this unit, also used to compute --identifier-prefix and --symbol-prefix)r0   r   r.   z--nsversionÚnamespace_versionz"version of namespace for this unitz--strip-prefixÚstrip_prefixzXOption --strip-prefix is deprecated, please see --identifier-prefix
and --symbol-prefix.z--identifier-prefixÚidentifier_prefixesz¸Remove this prefix from C identifiers (structure typedefs, etc.).
May be specified multiple times.  This is also used as the default for --symbol-prefix if
the latter is not specified.z--identifier-filter-cmdÚidentifier_filter_cmdzÁFilter identifiers (struct and union typedefs) through the given shell command which will receive the identifier name as input to stdin and is expected to output the filtered results to stdout.z--symbol-prefixÚsymbol_prefixesz2Remove this prefix from C symbols (function names)z--symbol-filter-cmdÚsymbol_filter_cmdz®Filter symbols (function names) through the given shell command which will receive the symbol name as input to stdin and is expected to output the filtered results to stdout.z--accept-unprefixedÚaccept_unprefixedzTIf specified, accept symbols and identifiers that do not
match the namespace prefix.z--add-init-sectionZinit_sectionsz:add extra initialization code in the introspection programz-oz--outputÚoutputú-z3output filename to write to, defaults to - (stdout)z--pkgÚpackagesz&pkg-config packages to get cflags fromz--pkg-exportÚpackages_exportz/Associated pkg-config packages for this libraryz
--warn-allÚwarn_allz.If true, enable all warnings for introspectionz--warn-errorÚ
warn_fatalzTurn warnings into fatal errorsz-vz	--verboseÚverbosez
be verbosez--c-includeÚ
c_includesz.headers which should be included in C programsz
--filelistÚfilelistz1file containing headers and sources to be scannedZMSYSTEMZMINGWz--generate-typelib-testsÚtest_codegenz--passthrough-girÚpassthrough_girz--reparse-validateÚreparse_validate_girz--typelib-xmlZtypelib_xmlz--function-decorationÚfunction_decorationz-Macro to decorate functions in generated codez--include-first-in-headerÚinclude_first_headerz+Header to include first in generated headerz--include-last-in-headerÚinclude_last_headerz=Header to include after the other headers in generated headerz--include-first-in-srcÚinclude_first_srcz,Header to include first in generated sourcesz--include-last-in-srcÚinclude_last_srcz>Header to include after the other headers in generated sources)r5   ZOptionParserr7   r9   Zadd_option_groupr'   ÚenvironÚgetÚ
startswithr:   ZSUPPRESS_HELP)r    r8   Z
msystemenvr#   r#   r$   Ú_get_option_parserg   s   






rb   c             C   s   t d| f ƒ‚d S )Nz	ERROR: %s)Ú
SystemExit)Úmsgr#   r#   r$   Ú_errorý   s    re   c             C   s.   t ƒ }| | ¡ t| ¡ ƒ}| | ¡ ¡ d S )N)r   Úparser   Zget_namespaceÚwriteÚget_encoded_xml)r(   Úfr    Úwriterr#   r#   r$   rX     s    
rX   c             C   sT   |   d¡\}}}|dkrBddlm}	 |	|||||||ƒ}
|
 ¡  ntd|f ƒ dS )Nú,Z
Everythingr   )ÚEverythingCodeGeneratorzInvaild namespace '%s'r   )ÚsplitZtestcodegenrl   rg   re   )Z	optstringrZ   r[   r\   r]   r^   Ú	namespaceZout_h_filenameZout_c_filenamerl   Úgenr#   r#   r$   rW   	  s    
rW   c             c   s8   x2|   ¡ D ]&}x |D ]}| |¡s$q|V  P qW q
W d S )N)rm   ra   )rN   Zallowed_flagsr   Úflagr#   r#   r$   Úprocess_options  s    

rq   c       	      C   s²   t j dd¡dg}| |¡ tj|tjd ¡ d }|d kr@dS | d¡}dd	d
ddg}t	t
||ƒƒ}tƒ }| |¡\}}| j dd„ |jD ƒ¡ | j |j¡ | j |j¡ d S )NZ
PKG_CONFIGz
pkg-configz--cflags)Ústdoutr   r   Úasciiz-Iz-Dz-Uz-lz-Lc             S   s   g | ]}t j |¡‘qS r#   )r'   r(   r)   )Ú.0ri   r#   r#   r$   ú
<listcomp>8  s    z$process_packages.<locals>.<listcomp>)r'   r_   r`   ÚextendÚ
subprocessÚPopenÚPIPEZcommunicateÚdecodeÚlistrq   rb   Ú
parse_argsr*   r3   r4   )	ÚoptionsrP   ÚargsrN   Zoptions_whitelistZfiltered_outputr    Zpkg_optionsZunusedr#   r#   r$   Úprocess_packages(  s    

r   c             C   sN   g }xD| D ]<}t j |¡d tkr
t j |¡s<td|f ƒ | |¡ q
W |S )Nr   z%s: no such a file or directory)r'   r(   Úsplitextr   Úexistsre   r   )r~   Ú	filenamesr"   r#   r#   r$   Úextract_filenames=  s    
rƒ   c          	   C   sÆ   g }t j | j¡s"td| jf ƒ t| jdƒ}| ¡ }W d Q R X x~|D ]v}| ¡ }| d¡sš| d¡sš| d¡sš| d¡sš| d¡sš| d¡sš| d	¡rHt j |¡s´td
|f ƒ | 	|¡ qHW |S )Nz%s: no such filelist fileÚrz.cz.cppz.ccz.cxxz.hz.hppz.hxxz4%s: Invalid filelist entry-no such file or directory)
r'   r(   r   rV   re   ÚopenÚ	readlinesÚstripÚendswithr   )r}   r‚   Zfilelist_fileÚlinesÚlineÚfilenamer#   r#   r$   Úextract_filelistK  s    

rŒ   c             C   sx   | j rtdƒ | j | j ¡ | jr*| j}nd }| jr`x"| jD ]}| ¡ |kr<tdƒ q<W | j}nd }t| j| j	||dS )Nzng-ir-scanner: warning: Option --strip-prefix has been deprecated;
see --identifier-prefix and --symbol-prefix.z5Values for --symbol-prefix must be entirely lowercase)rI   rK   )
rH   ÚprintrI   r   rK   Úlowerre   r	   rF   rG   )r}   rI   ÚprefixrK   r#   r#   r$   Úcreate_namespacea  s"    r   c             C   s¸   t | |j|j|jd}| |j¡ |js.|jr>| ¡  | 	¡  xZ|j
D ]P}tj|krbtd|f ƒ yt |¡}W n   td|f ƒ Y nX | |¡ qFW x|jD ]}| |¡ q¢W |S )N)rM   rJ   rL   zInvalid include path '%s'zMalformed include '%s'
)r   rM   rJ   rL   Zset_include_pathsrA   rX   rY   Zdisable_cacheZset_passthrough_moder?   r'   Úsepre   r   Zfrom_stringZregister_includer@   Zregister_include_uninstalled)rn   r}   ÚtransformerZincludeZinclude_objZinclude_pathr#   r#   r$   Úcreate_transformer~  s&    

r“   c             C   sl   t | ƒ}| ¡  |jr4|jg}| |j¡ t|ƒ}nt|| ¡ | ¡ ƒ}t	|||j
ƒ}| |¡ | ¡  |S )N)r   Z
init_parserB   rv   rC   r   r
   Zget_get_type_functionsZget_error_quark_functionsr   rD   Zset_introspection_binaryrf   )r’   r}   r~   Zgdump_parserZbinaryÚshlibsr#   r#   r$   Úcreate_binary–  s    


r•   c             C   sr   t | dƒr| jrt| ƒ}nt|ƒ}t ¡ dkr:| j d¡ tƒ }|j	| j
| j| j| jd | |¡ | |¡ |S )NrV   ZDarwinZ
__BLOCKS__)r!   )ÚhasattrrV   rŒ   rƒ   ÚplatformÚsystemr4   r   r   Zset_cpp_optionsr*   r3   r!   Zparse_filesZparse_macros)r}   r~   r‚   Ússr#   r#   r$   Úcreate_source_scanner®  s    



rš   c          
   C   sÈ  |j dkrtj}nl|jr:tjdd\}}tj |j ¡rJt	 
|j |¡ n t |tjtjB tjB tjB ¡ t |d¡}| | ¡ W dQ R X tjdd\}}t |d¡}t||ƒ W dQ R X t ||¡sÚtd||f ƒ t |¡ yt	 ||j ¡ W n> tk
r4 } z|jtjkr"t |¡ ‚ W dd}~X Y nX dS yt|j dƒ}W n4 tk
r~ } ztd|jf ƒ W dd}~X Y nX y| | ¡ W n4 tk
rÂ } ztd	|jf ƒ W dd}~X Y nX dS )
z@Write encoded XML 'data' to the filename specified in 'options'.rO   z.gir)ÚsuffixÚwbNz:Failed to re-parse gir file; scanned='%s' passthrough='%s'r   zopening output for writing: %szwhile writing output: %s)rN   Úsysrr   rY   ÚtempfileZmkstempr'   r(   ÚisfileÚshutilZcopystatÚchmodÚstatÚS_IWUSRÚS_IRUSRÚS_IRGRPÚS_IROTHÚfdopenrg   rX   r   Zfiles_are_identicalre   ÚunlinkZmoveÚOSErrorÚerrnoZEPERMr…   ÚIOErrorÚstrerror)Údatar}   rN   Zmain_fZmain_f_nameZtemp_fZtemp_f_nameÚer#   r#   r$   Úwrite_outputÃ  s@    



"r¯   c             C   sD  t ƒ }| | ¡\}} |jr(t|jtjƒ |jrLt|j|j|j|j|j	|j
ƒS t|dƒrp|jspt| ƒdkrptdƒ |js~tdƒ |jdkr–ddlm} ntd|jf ƒ |jsÀ|jsÀ|jsÀtd	ƒ t|ƒ}tjj|d
}|jrò| tjtjtjf¡ t||ƒ}t |j!ƒ}| "| #¡ ¡ |r.t$||ƒ}|r.|S t%|| ƒ}	t&ƒ }
|
 '|	 (¡ ¡}| )|	 *¡ ¡ |jspt+||| ƒ}ng }||j,_-t.||ƒ}| /¡  t0 1d¡ t2||ƒ}| 3¡  | 4¡ }|j5rÒ|dkrÒt 6d¡ dS |dkrü|jdkrüt7d|j,j8|f ƒ |j9r|j9}n|j!}|j:|j,_:||j,_;||j,ƒ}| <¡ }t=||ƒ dS )NrV   r   zNeed at least one filenamezNamespace name missingr>   r   )r   zUnknown format: %sz#Must specify --program or --library)rn   Ztreezwarnings configured as fatalFzNg-ir-scanner: %s: warning: %d warnings suppressed (use --warn-all to see them))>rb   r|   rX   r   rr   rW   rZ   r[   r\   r]   r^   r–   rV   r   re   rF   r=   Úgiscanner.girwriterr   rD   rB   rE   r   r   ZMessageLoggerr`   rR   Zenable_warningsZWARNINGZERRORZFATALr“   ÚsetrP   ÚupdateZget_pkgconfig_packagesr   rš   r   Zparse_comment_blocksZget_commentsrf   Zget_symbolsr•   rn   Zshared_librariesr   Z	transformr   Zbreak_on_debug_flagr   ZvalidateZget_warning_countrS   Zfatalr   ÚnamerQ   rU   Úexported_packagesrh   r¯   )r~   r    r}   ÚWriterrn   Zloggerr’   rP   Z	exit_coder™   ZcbpZblocksr”   ÚmainÚfinalZwarning_countr´   rj   r­   r#   r#   r$   Úscanner_mainí  s~    











r¸   )>Z
__future__r   r   r   r   rª   r5   r'   r    r¢   rw   r   rž   r—   Z	giscannerr   Zgiscanner.annotationparserr   Zgiscanner.astr   r	   Zgiscanner.dumperr
   Zgiscanner.gdumpparserr   r   Zgiscanner.introspectablepassr   Zgiscanner.girparserr   r°   r   Zgiscanner.maintransformerr   Zgiscanner.shlibsr   Zgiscanner.sourcescannerr   r   Zgiscanner.transformerr   r,   r   r%   r&   r+   r9   r:   rb   re   rX   rW   rq   r   rƒ   rŒ   r   r“   r•   rš   r¯   r¸   r#   r#   r#   r$   Ú<module>   sZ   
	 	*