
    7j                        d 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mZ ddlmZ ddlmZ ddlmZ  ed	
      dedee	   fd       Z ed	
      dededee   fd       Z ej.                  d      Z ej.                  d      Zdee   deeeef      fdZddedz  ddfdedede	dedee   defdZy)z=Creates a compilation database for the given keyboard build.
    N)	lru_cache)Path)DictIteratorList)cli)	find_make)QMK_FIRMWARE
   )maxsizebinaryreturnc                 *   t         j                  j                  d|        | j                  d      s| j                  d      rt        j                  | dddgdddd	
      }g }|j
                  j                         D ]J  }|j                  d      s|j                  t        |j                               j                                L |S | rEt        t        |       j                         j                  j                  j                  d            S g S )zFFind the system include directory that the given build tool uses.
    z5searching for system library directory for binary: %sgccg++-Ez-Wp,-v-TN
capture_outputcheckstdininput z	*/include)r   logdebugendswithrunstderr
splitlines
startswithappendr   stripresolvelistparentglob)r   resultpathslines       Q/home/hanthebot/chat/keyboard/qmk_firmware/lib/python/qmk/compilation_database.pysystem_libsr,      s     GGMMI6R u!7&$#6tSW_ckopMM,,. 	;Ds#T$**,/779:	; KQ4V$$&--4499+FGYWYY    compiler_argsc           
      t   t         j                  j                  d| |       | j                  d      s| j                  d      rr| ddg}| j                  d      r|j	                  g d       n$| j                  d      r|j	                  g d       |j	                  t        j                  |             |j                  d       t        j                  |d	d	d d
      }g }|j                  j                         D ]w  }|j                  dd      }t        |      dk(  r&|d   dk(  r|j                  d|d    d|d           It        |      dk(  sX|d   dk(  sa|j                  d|d           y t        j                  d      t        t        t!        t#        fd|                        S g S )Nz,gathering definitions for compilation: %s %sr   r   z-dMr   )-xcz
-std=gnu11)r0   zc++z-std=gnu++14r   Tr   r   r         r   z#definez-D   =a  ^-D__(SIZE|INT|UINT|WINT|WCHAR|BYTE|SHRT|SIG|FLOAT|LONG|CHAR|SCHAR|DBL|FLT|LDBL|PTRDIFF|QQ|DQ|DA|HA|HQ|SA|SQ|TA|TQ|UDA|UDQ|UHA|UHQ|USQ|USA|UTQ|UTA|UQQ|UQA|ACCUM|FRACT|UACCUM|UFRACT|LACCUM|LFRACT|ULACCUM|ULFRACT|LLACCUM|LLFRACT|ULLACCUM|ULLFRACT|SACCUM|SFRACT|USACCUM|USFRACT)c                 (    j                  |        S N)match)xtype_filters    r+   <lambda>zcpu_defines.<locals>.<lambda>=   s    K4E4Ea4H0H r-   )r   r   r   r   extendshlexsplitr"   r   stdoutr    lenrecompiler%   sortedsetfilter)r   r.   
invocationr(   define_argsr*   	line_argsr:   s          @r+   cpu_definesrI   &   s   GGMM@&-Xu!7eT*
??5!78__U#;<%++m45#DDX\]MM,,. 	8D

3*I9~"y|y'@""R	!~Qy|n#EFY1$1)B""R	!~#67	8 jj c
 F3v&H+VWXYYIr-   zprintf "Compiling: ([^"]+)zLOG=\$\((.+?)&&fc           
      r   d}d}g }| D ]*  }|dk(  r*t         j                  |      }|r|j                  d      }d}|dk(  s9|sJ t        j                  |      }|sU|j                  d      }t	        j
                  |      }t        j                  |d         }t        t        d |            }	t        |      D ]  }
|dd|
z  gz  } |j                  t        |d	j                  d
 |	D                           ||d<   |j                  |t        t!        j"                               |d       d}- |S )zparse the output of `make -n <target>`

    This function makes many assumptions about the format of your build log.
    This happens to work right now for qmk.
    startNr4   cmdr   c                 J    | j                  d      xs | j                  d      S )Nz-mz-f)r!   )r9   s    r+   r;   zparse_make_n.<locals>.<lambda>^   s    Q\\$5G5]1<<X\K] r-   z-isystemz%sr   c              3   F   K   | ]  }t        j                  |        y wr7   )r=   quote).0ss     r+   	<genexpr>zparse_make_n.<locals>.<genexpr>a   s     8_AQ8_s   !)	arguments	directoryfile)file_researchgroupcmd_rer=   r>   shutilwhichrD   rE   r,   r<   rI   joinr"   strr
   r$   )rJ   state	this_filerecordsr*   mthis_cmdargsr   r.   rR   s              r+   parse_make_nre   E   s.    EIG  Gt$AGGAJ	E>9d#A771:{{8,d1g. #F+]_c$d e$V, 3AZ22D3K8_Q^8_0_`a QTLDXDXDZ@[enop+ . Nr-   zcompile_commands.jsonFkeyboardkeymapoutput_path
skip_cleancommandc                    |s#ddl m}  || |      } |j                  dddi|}|s5t        j                  j                  d       t        j                  d       yt        j                  j                         }|j                  dd        |sTt               d	g}	t        j                  j                  d
dj                  |	             t        j                  |	dd|       t        j                  j                  ddj                  |             t        j                  |dd|      }
t        |
j                   j#                               }|s+t        j                  j                  d|
j                          yt        j                  j                  dt%        |             t        j                  j                  d|        |j'                  t)        j*                  |d             y)Nr   )KeyboardKeymapBuildTargetdry_runTz`You must supply both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.zDusage: qmk generate-compilation-database [-kb KEYBOARD] [-km KEYMAP]F	MAKEFLAGScleanzMaking clean with {fg_cyan}%sr   )r   r   envz-Gathering build instructions from {fg_cyan}%sz+Failed to parse output from make output:
%szFound %s compile commandszWriting build database to    )indent )qmk.build_targetsrl   compile_commandr   r   errorechoosenvironcopypopr	   infor]   r   re   r?   r    r@   
write_textjsondumps)rf   rg   rh   ri   rj   env_varsrl   targetrp   clean_commandr(   dbs               r+   write_compilation_databaser   i   s`   ?*8V<(&((BBBxyWX **//
CGGK "g.4chh}6MNe4SIGGLL@#((7BSTWWWT3GF	fmm..0	1BDfmmTGGLL,c"g6GGLL-k];<4::b34r-   )__doc__r~   rx   rA   r=   r[   	functoolsr   pathlibr   typingr   r   r   milcr   qmk.commandsr	   qmk.constantsr
   r^   r,   rI   rB   rW   rZ   re   boolr   rs   r-   r+   <module>r      sM    	 	     ' '  " & 2Z ZT
 Z Z$ 2 C DI  4 "**2
3	&	'!HSM !d4S>&: !H 044]i  mD  ^D  Y^  uy # #S #VZ #  RV #  im  nq  ir #  JN #r-   