o
    uJAfA"                     @  s<  d 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mZ ddl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 ddlmZ ejrkddlZddlmZmZ G dd deZ h dZ!h de!B Z"h dZ#dZ$dZ%G dd deZ&dZ'd(ddZ(d)d*dd Z)d+d#d$Z*d,d&d'Z+dS )-z*Code that creates simple startup projects.    )annotations)Path)EnumN)glob)buildmesonlibmlog)FORBIDDEN_TARGET_NAMES)detect_ninja)create_meson_build)sample_generator)ProtocolLiteralc                   @  sf   e Zd ZU ded< ded< ded< ded< ded< ded	< d
ed< ded< d
ed< ded< ded< dS )	ArgumentszT.List[Path]srcfilesstrwdname
executabledepsz[Literal['c', 'cpp', 'cs', 'cuda', 'd', 'fortran', 'java', 'rust', 'objc', 'objcpp', 'vala']languageboolr   builddirforcez Literal['executable', 'library']typeversionN)__name__
__module____qualname____annotations__ r    r    5/usr/lib/python3.10/site-packages/mesonbuild/minit.pyr   (   s   
 r   >   z.F90z.Fz.fz.forz.f90>   .cs.m.cu.c.vala.cpp.mm.rs.java.cc.d>   cudacppcsobjcobjcpprustvalajavafortrandcr   z0.1c                   @  s   e Zd ZdZdZdS )DEFAULT_TYPESr   libraryN)r   r   r   EXELIBr    r    r    r!   r8   =   s    r8   zpSample project created. To build it run the
following commands:

meson setup builddir
meson compile -C builddir
optionsreturnNonec                 C  sP   t | }| jtd jkr|  n| jtd jkr|  ntdtt dS )z
    Based on what arguments are passed we check for a match in language
    then check for project type and create new Meson samples project.
    r:   r;   zUnreachable codeN)	r   r   r8   valueZcreate_executableZcreate_libraryRuntimeErrorprintINFO_MESSAGE)r<   Z
sample_genr    r    r!   create_sampleI   s   

rC   Fsampler   c                 C  s
  | j s%t  j| _ td| j s|rtd| j  dtd| j  d | js5| j | _td| j d | jt	v rDt
d| jd|rHd	S | js{g }d
d t  D D ]}|jtv rc|| qW|sjtd|| _tdddd |D   | js| jD ]p}|jdkrd| _ ne|jdv rd| _ n[|jdkrd| _ nQ|jdkrd| _ nG|jdkrd| _ n=|jtv rd| _ n3|jdkrd| _ n)|jdkrd| _ n|jdkrd | _ n|jd!krd"| _ n|jd#krd$| _ nq| jstd%td&| j  d	S d	S )'z`
    Here we autodetect options for args not passed in so don't have to
    think about it.
    z[a-zA-Z_][a-zA-Z0-9]*zName of current directory "zM" is not usable as a sample project name.
Specify a project name with --name.zUsing "z." (name of current directory) as project name.z0" (project name) as name of executable to build.zExecutable name zI is reserved for Meson internal use. Refusing to init an invalid project.Nc                 s  s    | ]	}|  r|V  qd S N)is_file).0fr    r    r!   	<genexpr>m   s    z%autodetect_options.<locals>.<genexpr>zdNo recognizable source files found.
Run meson init in an empty directory to create a sample project.zDetected source files:  c                 s  s    | ]}t |V  qd S rE   )r   )rG   sr    r    r!   rI   t   s    r%   r7   >   r'   r+   r.   r"   r/   r$   r-   r,   r6   r5   r)   r2   r#   r0   r(   r1   r*   r4   r&   r3   z5Can't autodetect language, please specify it with -l.zDetected language: )r   r   resolvestemrematch
SystemExitrA   r   r	   r   MesonExceptionr   iterdirsuffixLANG_SUFFIXESappendjoinr   FORTRAN_SUFFIXES)r<   rD   r   rH   r    r    r!   autodetect_optionsW   s|   














rX   parser'argparse.ArgumentParser'c                 C  s   | j dddtdd | j ddtjdd	 | j d
ddd | j dddd | j dddd | j ddttdd | j ddddd | j dddd  | j d!d"dd#d | j d$td%d&t d'd( | j d)td*t d  d+S ),z\
    Here we add args for that the user can passed when making a new
    Meson project.
    r   Z
sourcefile*z@source files. default: all recognized files in current directory)metavarnargsr   help-Cr   z#directory to cd into before running)destactionr^   z-nz--namez0project name. default: name of current directory)r^   z-ez--executablez&executable name. default: project namez-dz--depszdependencies, comma-separatedz-lz
--languagez=project language. default: autodetected based on source files)choicesr^   z-bz--build
store_truezbuild after generation)ra   r^   z
--builddirr   zdirectory for build)defaultr^   z-fz--forcez2force overwrite of existing files and directories.z--type)r   r9   zproject type. default: z based project)rd   rb   r^   z	--versionzproject version. default: N)add_argumentr   r   RealPathActionsortedLANG_SUPPORTEDDEFAULT_PROJECTDEFAULT_VERSION)rY   r    r    r!   add_arguments   s   rk   intc                 C  sL  t | j std t| j tds+t| dd | j	s&t
d d| _	t|  nt|  t d r<| js<tdt|  | jrt | j rW| jrWt
d	 t| j t
d
 t d| jg }t|}|jrntt| j}td|jjt d}t!|}|rt"#t"$dd t% d| jg }t|}|jrtdS )z8
    Here we generate the new Meson sample project.
    zSProject source root directory not found. Run this command in source directory root.r[   T)rD   z.Defaulting to generating a C language project.r7   zmeson.buildz5meson.build already exists. Use --force to overwrite.z,Build directory already exists, deleting it.zBuilding...setupr   vsenvzINFO:z1automatically activated MSVC compiler environmentr_   r   )&r   r   existssysexitoschdirr   rX   r   rA   rC   rF   r   rP   r   r   r   is_dirshutilrmtreer   get_meson_command
subprocessrun
returncodeloadTcastenvironmentcoredata
get_option	OptionKeysetup_vsenvr   loggreenr
   )r<   cmdretb
need_vsenvZvsenv_activer    r    r!   ry      s@   




ry   )r<   r   r=   r>   )F)r<   r   rD   r   r=   r>   )rY   rZ   r=   r>   )r<   r   r=   rl   ),__doc__
__future__r   pathlibr   enumr   rx   ru   rp   rr   rN   r   typingr|   
mesonbuildr   r   r   Zmesonbuild.coredatar	   Zmesonbuild.environmentr
   Z#mesonbuild.templates.mesontemplatesr   Z"mesonbuild.templates.samplefactoryr   TYPE_CHECKINGargparseZtyping_extensionsr   r   r   rW   rT   rh   ri   rj   r8   rB   rC   rX   rk   ry   r    r    r    r!   <module>   s>   

G