a :jg, @sdZddlZddlZddlZddlZddlZddlZddlZGdddZGdddZ iZ GdddZ Gd d d Z Gd d d Z Gd ddZGdddee e e e eZddZdS)aProvides the `CCompilerOpt` class, used for handling the CPU/hardware optimization, starting from parsing the command arguments, to managing the relation between the CPU baseline and dispatch-able features, also generating the required C headers and ending with compiling the sources with proper compiler's flags. `CCompilerOpt` doesn't provide runtime detection for the CPU features, instead only focuses on the compiler side, but it creates abstract C headers that can be used later for the final runtime dispatching process.Nc(@seZdZdZdZdZdZdZej ej ej e dZiZdZdZeeddd d eddd d ed dd d ed ddd edddd edddd dZeddddddddZeeddddeddd d!ed"dd#d!ed$d%d&d!ed'd(d)d!ed*d+d,d!ed-d.d/ed0d1d2dd3ed4d5d6d!ed7d5d6d!ed8d5d/ed9d:d/ed;d:d/edd?ed@dAd/edBdCdDdEddFedGdEdHdIddFedJdCdKdLddMdNedOdLdPdQdRedSdLdTdUddFedVdWdXdYddFedZdYd[d\ddFedd]d^d_edd`ddaed"dbddcd?ed$dddded?eddfdgeddhddaed"diddaeddjdgeddkd/ed"dld/ed$dmddaed'dnd/ed*dnd/ed-dod/dp$ZdqdrZdsdtZdS)u_ConfigaAn abstract class holds all configurable attributes of `CCompilerOpt`, these class attributes can be used to change the default behavior of `CCompilerOpt` in order to fit other requirements. Attributes ---------- conf_nocache : bool Set True to disable memory and file cache. Default is False. conf_noopt : bool Set True to forces the optimization to be disabled, in this case `CCompilerOpt` tends to generate all expected headers in order to 'not' break the build. Default is False. conf_cache_factors : list Add extra factors to the primary caching factors. The caching factors are utilized to determine if there are changes had happened that requires to discard the cache and re-updating it. The primary factors are the arguments of `CCompilerOpt` and `CCompiler`'s properties(type, flags, etc). Default is list of two items, containing the time of last modification of `ccompiler_opt` and value of attribute "conf_noopt" conf_tmp_path : str, The path of temporary directory. Default is auto-created temporary directory via ``tempfile.mkdtemp()``. conf_check_path : str The path of testing files. Each added CPU feature must have a **C** source file contains at least one intrinsic or instruction that related to this feature, so it can be tested against the compiler. Default is ``./distutils/checks``. conf_target_groups : dict Extra tokens that can be reached from dispatch-able sources through the special mark ``@targets``. Default is an empty dictionary. **Notes**: - case-insensitive for tokens and group names - sign '#' must stick in the begin of group name and only within ``@targets`` **Example**: .. code-block:: console $ "@targets #avx_group other_tokens" > group_inside.c >>> CCompilerOpt.conf_target_groups["avx_group"] = \ "$werror $maxopt avx2 avx512f avx512_skx" >>> cco = CCompilerOpt(cc_instance) >>> cco.try_dispatch(["group_inside.c"]) conf_c_prefix : str The prefix of public C definitions. Default is ``"NPY_"``. conf_c_prefix_ : str The prefix of internal C definitions. Default is ``"NPY__"``. conf_cc_flags : dict Nested dictionaries defining several compiler flags that linked to some major functions, the main key represent the compiler name and sub-keys represent flags names. Default is already covers all supported **C** compilers. Sub-keys explained as follows: "native": str or None used by argument option `native`, to detect the current machine support via the compiler. "werror": str or None utilized to treat warning as errors during testing CPU features against the compiler and also for target's policy `$werror` via dispatch-able sources. "maxopt": str or None utilized for target's policy '$maxopt' and the value should contains the maximum acceptable optimization by the compiler. e.g. in gcc ``'-O3'`` **Notes**: * case-sensitive for compiler names and flags * use space to separate multiple flags * any flag will tested against the compiler and it will skipped if it's not applicable. conf_min_features : dict A dictionary defines the used CPU features for argument option ``'min'``, the key represent the CPU architecture name e.g. ``'x86'``. Default values provide the best effort on wide range of users platforms. **Note**: case-sensitive for architecture names. conf_features : dict Nested dictionaries used for identifying the CPU features. the primary key is represented as a feature name or group name that gathers several features. Default values covers all supported features but without the major options like "flags", these undefined options handle it by method `conf_features_partial()`. Default value is covers almost all CPU features for *X86*, *IBM/Power64* and *ARM 7/8*. Sub-keys explained as follows: "implies" : str or list, optional, List of CPU feature names to be implied by it, the feature name must be defined within `conf_features`. Default is None. "flags": str or list, optional List of compiler flags. Default is None. "detect": str or list, optional List of CPU feature names that required to be detected in runtime. By default, its the feature name or features in "group" if its specified. "implies_detect": bool, optional If True, all "detect" of implied features will be combined. Default is True. see `feature_detect()`. "group": str or list, optional Same as "implies" but doesn't require the feature name to be defined within `conf_features`. "interest": int, required a key for sorting CPU features "headers": str or list, optional intrinsics C header file "disable": str, optional force disable feature, the string value should contains the reason of disabling. "autovec": bool or None, optional True or False to declare that CPU feature can be auto-vectorized by the compiler. By default(None), treated as True if the feature contains at least one applicable flag. see `feature_can_autovec()` "extra_checks": str or list, optional Extra test case names for the CPU feature that need to be tested against the compiler. Each test case must have a C file named ``extra_xxxx.c``, where ``xxxx`` is the case name in lower case, under 'conf_check_path'. It should contain at least one intrinsic or function related to the test case. If the compiler able to successfully compile the C file then `CCompilerOpt` will add a C ``#define`` for it into the main dispatch header, e.g. ``#define {conf_c_prefix}_XXXX`` where ``XXXX`` is the case name in upper case. **NOTES**: * space can be used as separator with options that supports "str or list" * case-sensitive for all values and feature name must be in upper-case. * if flags aren't applicable, its will skipped rather than disable the CPU feature * the CPU feature will disabled if the compiler fail to compile the test file FNZchecksZNPY_ZNPY__z -march=nativez-O3z-Werror)nativeoptwerrorz-Werror=switch -Werrorz-xHostz/QxHostz/O3z/Werrorz/O2z/WXz -mcpu=a64fx)gccclangicciccwmsvcfcczSSE SSE2z SSE SSE2 SSE3zVSX VSX2zNEON NEON_FP16 NEON_VFPV4 ASIMD)x86x64ppc64ppc64les390xarmhfaarch64z xmmintrin.hSSE2)interestheadersimpliesSSEz emmintrin.h)rrrz pmmintrin.hSSE3z tmmintrin.hSSSE3z smmintrin.hSSE41zpopcntintrin.hPOPCNT)rrSSE42z immintrin.h)rrrimplies_detect AVXz x86intrin.h F16C z FMA3 AVX2ZAVX512F_REDUCE)rrr& extra_checksAVX512F(AVX512CDzAVX512ER AVX512PF AVX512_KNL)rrgroupdetectr&)z)AVX5124FMAPS AVX5124VNNIW AVX512VPOPCNTDQ AVX512_KNM*zAVX512VL AVX512BW AVX512DQ AVX512_SKXzAVX512BW_MASK AVX512DQ_MASK)rrr5r6r&r/+Z AVX512VNNI AVX512_CLX)rrr5r6,zAVX512IFMA AVX512VBMI AVX512_CNL-zAVX512_CLX AVX512_CNLz(AVX512VBMI2 AVX512BITALG AVX512VPOPCNTDQ AVX512_ICL.Z AVX512FP16 AVX512_SPRz altivec.hZVSX_ASM)rrr/VSX)rrr&VSX2ZVSX3_HALF_DOUBLEVSX3ZVSX4_MMAz vecintrin.h)rrVXVXEz arm_neon.hNEON NEON_FP16zNEON_FP16 NEON_VFPV4ASIMDASIMDHP)$rrrrr!r#r%r(XOPFMA4r,FMA3AVX2r1r3r4r8r:r<r>r@rBrCrDrEVSX4rFrGVXE2rHrI NEON_VFPV4rJrKASIMDDPASIMDFHMcCs|jr iS|jp|j}|jp&|jp&|j}|r|rttddtddtddtddtddtddtddtd dtd dtd dtd dtd dtddtddtddtddtddtddtddtddtddtdddS|r|jrttddtddtddtddtdditddtd ditddtddtdddtdddtdd dtd!d dtd"dtd#dtd$dtd%dtd&dtd'dtd(ddS|rl|jrlttd)dtd*dtd+dtd,dtd-ditd.dtd/ditddtddtdd0dtdd0dtdd1dtd!d1dtd2dtd3dtd4dtd5dtd6dtd7dtd(ddS|r"|j r"t|jrtd)dni|jrtd*dniiiitd8d9itd/ditd:d9td:d9tdd;dtddtd?d>dtd@dtd@dtd>diiitd@ddS|j p.|j }|rtt|j rFdAndBdCdtdDdEdFtdGdEdFtdHdEdFdI}|jrdJ|dKdL<dD|dAdL<dM|dNdL<dO|dPdL<|S|j }|rttdQdtdRdEdFtdSdEdFdT}|S|j r8|r8ttdUdVdWtdXdVdWtdYdVdWtdZdVdWtd[dtd\dtd]dd^S|jr|rttd_dtd`dtdadtdbdtd[dtd\dtd]dd^SiS)ca7Return a dictionary of supported CPU features by the platform, and accumulate the rest of undefined options in `conf_features`, the returned dict has same rules and notes in class attribute `conf_features`, also its override any options that been set in 'conf_features'. z-msse)flagsz-msse2z-msse3z-mssse3z-msse4.1z-mpopcntz-msse4.2z-mavxz-mf16cz-mxopz-mfma4z-mfmaz-mavx2z-mavx512f -mno-mmxz -mavx512cdz-mavx512er -mavx512pfz/-mavx5124fmaps -mavx5124vnniw -mavx512vpopcntdqz -mavx512vl -mavx512bw -mavx512dqz -mavx512vnniz-mavx512ifma -mavx512vbmiz.-mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdqz -mavx512fp16)rrrrr!r#r%r(r,rLrMrNrOr1r3r4r8r:r<r>r@rBz!Intel Compiler doesn't support it)disablez F16C AVX2z-march=core-avx2)rrUrNz AVX2 AVX512CDz-march=common-avx512z AVX2 AVX512Fz-xKNLz-xKNMz-xSKYLAKE-AVX512z -xCASCADELAKEz -xCANNONLAKEz-xICELAKE-CLIENTzNot supported yetz /arch:SSEz /arch:SSE2z /arch:SSE3z /arch:SSSE3z /arch:SSE4.1z /arch:SSE4.2z /arch:AVXz/arch:CORE-AVX2z/Qx:COMMON-AVX512z/Qx:KNLz/Qx:KNMz/Qx:SKYLAKE-AVX512z/Qx:CASCADELAKEz/Qx:CANNONLAKEz/Qx:ICELAKE-CLIENTz nmmintrin.h)rz ammintrin.hz /arch:AVX2z F16C FMA3zAVX2 AVX512CD AVX512_SKXz /arch:AVX512zAVX512F AVX512_SKXz MSVC compiler doesn't support itrDr z-mvsxz -mcpu=power8F)rUr&z-mcpu=power9 -mtune=power9z-mcpu=power10 -mtune=power10)rCrDrErPz-maltivec -mvsxrCrUz -mcpu=power9rEz -mcpu=power10rPz-march=arch11 -mzvectorz -march=arch12z -march=arch13)rFrGrQzNEON_FP16 NEON_VFPV4 ASIMDT)rautoveczNEON NEON_VFPV4 ASIMDzNEON NEON_FP16 ASIMDzNEON NEON_FP16 NEON_VFPV4z-march=armv8.2-a+fp16z-march=armv8.2-a+dotprodz-march=armv8.2-a+fp16fml)rHrIrRrJrKrSrTz -mfpu=neonz"-mfpu=neon-fp16 -mfp16-format=ieeez-mfpu=neon-vfpv4z'-mfpu=neon-fp-armv8 -march=armv8-a+simd)cc_noopt cc_on_x86 cc_on_x64 cc_is_gcc cc_is_clang cc_is_fccdict cc_is_icc cc_is_iccw cc_is_msvc cc_on_ppc64le cc_on_ppc64 cc_on_s390x cc_on_aarch64 cc_on_armhf)selfZon_x86Zis_unixZon_powerpartialZon_zarchriG/usr/local/lib/python3.9/site-packages/numpy/distutils/ccompiler_opt.pyconf_features_partialJs    "*     z_Config.conf_features_partialcsb|jdur@ddlddl}|fdd}t||_|jdur^tj t |j g|_dS)Nrcs&zWnty Yn0dSN)rmtreeOSErrorrishutiltmprirjrm_temp@s z!_Config.__init__..rm_temp) conf_tmp_pathrptempfilemkdtempatexitregisterconf_cache_factorsospathgetmtime__file__ conf_nocache)rgrtrrrirorj__init__;s    z_Config.__init__)__name__ __module__ __qualname____doc__r} conf_nooptrxrsryrzjoindirnamerealpathr|conf_check_pathconf_target_groups conf_c_prefixconf_c_prefix_r^ conf_cc_flagsconf_min_features conf_featuresrkr~riririrjrs" '                         [rrc@seZdZdZddZdddZgfddZd d Zed d Z ed dZ eddddZ eddZ eddZ dddZedZedddZdS) _DistutilsaA helper class that provides a collection of fundamental methods implemented in a top of Python and NumPy Distutils. The idea behind this class is to gather all methods that it may need to override in case of reuse 'CCompilerOpt' in environment different than of what NumPy has. Parameters ---------- ccompiler : `CCompiler` The generate instance that returned from `distutils.ccompiler.new_compiler()`. cCs ||_dSrl) _ccompiler)rg ccompilerririrjr~[sz_Distutils.__init__NcKsLt|tsJt|tsJ|dg|}|s6|j}|j|fd|i|S)zWrap CCompiler.compile()Zextra_postargs) isinstancelistpoprcompile)rgsourcesrUrkwargsririrj dist_compile^s z_Distutils.dist_compilec Cst|tsJddlm}|j}t|dd}|rdt|jdd}|dvrVt|d|jnt|d|jd}z|j |g|||j d d }Wn6|y} z|j t| d d WYd} ~ n d} ~ 00|rt|d||S) zgReturn True if 'CCompiler.compile()' able to compile a source file with certain flags. r CompileErrorspawnN compiler_typer )r F)macros output_dirTstderr) rstrdistutils.errorsrrgetattrsetattr_dist_test_spawn_paths_dist_test_spawnrrsdist_log) rgsourcerUrrccZbk_spawncc_typetesteririrj dist_tesths(   ( z_Distutils.dist_testcCst|dr|jSt|jdd}|dvr,d}n |dvr:d}ndd lm}|}t|jd t|jd d}|rp|d krt|d r|d}qt|}n|}t|d rt|dkrd|dd}n t j dd}|t j dd7}|||f|_|jS)a! Return a tuple containing info about (platform, compiler, extra_args), required by the abstract class '_CCompiler' for discovering the platform environment. This is also used as a cache factor in order to detect any changes happening from outside. _dist_inforr )ZintelemZintelemwx86_64)intelZintelwZinteler r) get_platformcompiler compiler_sounix__iter__r NCFLAGSCPPFLAGS) hasattrrrrdistutils.utilrrlenrryenvironget)rgrplatformrZcc_infor extra_argsririrj dist_infos*       z_Distutils.dist_infocGsddlm}|tj|dS)zRaise a compiler errorrrN)rrr _dist_str)argsrririrj dist_errors z_Distutils.dist_errorcGsddlm}|tj|dS)zRaise a distutils errorr)DistutilsErrorN)rrrr)rrririrj dist_fatals z_Distutils.dist_fatalFrcGs4ddlm}tj|}|r&||n ||dS)zPrint a console messager)logN)Znumpy.distutilsrrrwarninfo)rrroutririrjrs    z_Distutils.dist_logc CsPddlm}z |||WStyJ}ztj|ddWYd}~n d}~00dS)zALoad a module from file, required by the abstract class '_Cache'.r)exec_mod_from_locationTrN)Z misc_utilr Exceptionrr)namerzrrririrjdist_load_modules   $z_Distutils.dist_load_modulecsNfddtd}d|j|jf}dfddg|RD}||S)z+Return a string to print by log and errors.csJt|tsBt|drBg}|D]}||qdd|dSt|S)Nr(r))rrrappendr)argretato_strrirjrs z$_Distutils._dist_str..to_strrzCCompilerOpt.%s[%d] : rcsg|] }|qSriri).0rrrirj sz(_Distutils._dist_str..)inspectstackfunctionlinenor)rrstartrrirrjrs  z_Distutils._dist_strc Cs\t|jds||dStd}z&|jjtjd<||W|tjd<n |tjd<0dS)z Fix msvc SDK ENV path same as distutils do without it we get c1: fatal error C1356: unable to find mspdbcore.dll _pathsNrz)rrrrygetenvrr)rgcmddisplayold_pathririrjrs    z!_Distutils._dist_test_spawn_pathsz/.*(warning D9002|invalid argument for option).*c Csz:tj|tjdd}|r8ttj|r8td|d|Wn\tjyl}z|j }|j }WYd}~n8d}~0t y}z|}d}WYd}~nd}~00dStd|d||fdS)NT)rtextzFlags in commandz/aren't supported by the compiler, output -> %sCommandz(failed with exit status %d output -> %s) subprocess check_outputSTDOUTrematchr_dist_warn_regexrCalledProcessErroroutput returncodern)rroexcsrririrjrs, z_Distutils._dist_test_spawn)N)N)N)rrrrr~rrr staticmethodrrrrrrrrrrriririrjrNs*  %      rc@sHeZdZdZedZdddZddZdd Z d d Z e d d Z dS)_Cachea'An abstract class handles caching functionality, provides two levels of caching, in-memory by share instances attributes among each other and by store attributes into files. **Note**: any attributes that start with ``_`` or ``conf_`` will be ignored. Parameters ---------- cache_path : str or None The path of cache file, if None then cache in file will disabled. *factors : The caching factors that need to utilize next to `conf_cache_factors`. Attributes ---------- cache_private : set Hold the attributes that need be skipped from "in-memory cache". cache_infile : bool Utilized during initializing this class, to determine if the cache was able to loaded from the specified cache path in 'cache_path'. z ^(_|conf_)NcGsti|_t|_d|_d|_|jr.|ddS|jg||jR|_ ||_|rt j |r|d|| d|}|s|jdddnlt|drt|d s|jd ddnH|j |jkr|d |jD]\}}t|||qd|_n |d |jsZt|j }|rZ|d |jD]6\}}||jvs"t|j|rJq"t|||q"|t|j <t|jdS)NFzcache is disabled by `Config`zload cache from file ->cachez)unable to load the cache file as a moduleTrhashdatazinvalid cache filezhit the file cachezmiss the file cachezhit the memory cache)cache_meset cache_private cache_infile _cache_pathr}r cache_hashrx _cache_hashryrzexistsrrrritemsr _share_cacher__dict__rr _cache_ignorervrw cache_flush)rg cache_pathfactorsZ cache_modattrvalZ other_cacheririrjr~&sP             z_Cache.__init__cCs,tD]\}}||krt|q(qdSrl)rrr)rghrririrj__del__Ss z_Cache.__del__cCs|js dS|d|j|j}|jD]}t|j|r,||q,t j |j}t j |snt |tj|dd}t|jd2}|td|j||Wdn1s0YdS)z) Force update the cache. Nzwrite cache to path ->T)compactwz # AUTOGENERATED DON'T EDIT # Please make changes to the code generator (distutils/ccompiler_opt.py) hash = {} data = \ )rrrcopykeysrrrrryrzrrmakedirspprintpformatopenwritetextwrapdedentformatr)rgZcdictrd repr_dictfririrjrYs     z_Cache.cache_flushcGsDd}|D]6}t|D](}t||d>|d>|}|dM}qq|S)Nrr l)rord)rgrZchashrcharririrjrus   z_Cache.cache_hashcsfdd}|S)zr A static method that can be treated as a decorator to dynamically cache certain methods. csZtjg|||R}||jvr6|j|S|g|Ri|}||j|<|Srl)rrr valuesr)rgrr cache_keyZccbcbrirj cache_wrap_mes   z _Cache.me..cache_wrap_meri)rrrirrjmes z _Cache.me)N) rrrrrrrr~rrrrrriririrjr s  - rc@seZdZdZddZejddZejgfddZdd Z e d Z e d Z e d Ze d ZddZe dZe dZddZdS) _CCompilera2A helper class for `CCompilerOpt` containing all utilities that related to the fundamental compiler's functions. Attributes ---------- cc_on_x86 : bool True when the target architecture is 32-bit x86 cc_on_x64 : bool True when the target architecture is 64-bit x86 cc_on_ppc64 : bool True when the target architecture is 64-bit big-endian powerpc cc_on_ppc64le : bool True when the target architecture is 64-bit litle-endian powerpc cc_on_s390x : bool True when the target architecture is IBM/ZARCH on linux cc_on_armhf : bool True when the target architecture is 32-bit ARMv7+ cc_on_aarch64 : bool True when the target architecture is 64-bit Armv8-a+ cc_on_noarch : bool True when the target architecture is unknown or not supported cc_is_gcc : bool True if the compiler is GNU or if the compiler is unknown cc_is_clang : bool True if the compiler is Clang cc_is_icc : bool True if the compiler is Intel compiler (unix like) cc_is_iccw : bool True if the compiler is Intel compiler (msvc like) cc_is_nocc : bool True if the compiler isn't supported directly, Note: that cause a fail-back to gcc cc_has_debug : bool True if the compiler has debug flags cc_has_native : bool True if the compiler has native flags cc_noopt : bool True if the compiler has definition 'DISABLE_OPT*', or 'cc_on_noarch' is True cc_march : str The target architecture name, or "unknown" if the architecture isn't supported cc_name : str The compiler name, or "unknown" if the compiler isn't supported cc_flags : dict Dictionary containing the initialized flags of `_Config.conf_cc_flags` cCsVt|drdSd}d}d}|}|\}}}|||fD] }|D]\} } } t|| dq>q6||f||ffD]N\} } | D]@\} } } | rt| | tjsqt| r|| sqtt|| dqhqtqh|D]<\} } } | rt| |tjsq| r|| sqt|| dq|jr|jd|ddd d|_ |j r:|jd dd d|_ |j r^|jd |ddd d|_ d |_ d D]"}t|d|rh||_ qqhd |_dD]"}t|d|r||_qqi|_|j|j}|dur|d|j|D]Z\}}g|j|<}|rt|tsJ|}|D]}||gr(||q(qd|_dS)N cc_is_cached))rZz.*(x|x86_|amd)64.*r )rYz.*(win32|x86|i386|i686).*r )rbz&.*(powerpc|ppc)64(el|le).*|.*powerpc.*z4defined(__powerpc64__) && defined(__LITTLE_ENDIAN__))rcz.*(powerpc|ppc).*|.*powerpc.*z1defined(__powerpc64__) && defined(__BIG_ENDIAN__))rez.*(aarch64|arm64).*r )rfz.*arm.*z3defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))rdz .*s390x.*r ) cc_on_noarchr r ))r[z.*(gcc|gnu\-g).*r )r\z .*clang.*r )r`z.*(intelw|intelemw|iccw).*r )r_z.*(intel|icc).*r )raz.*msvc.*r )r]z.*fcc.*r ) cc_is_noccr r )) cc_has_debugz$.*(O0|Od|ggdb|coverage|debug:full).*r ) cc_has_nativez..*(-march=native|-xHost|/QxHost|-mcpu=a64fx).*r )rXz.*DISABLE_OPT.*r FTz]unable to detect CPU architecture which lead to disable the optimization. check dist_info:<< z >>rz&Optimization is disabled by the Configzunable to detect compiler type which leads to treating it as GCC. this is a normal behavior if you're using gcc-like compiler such as MinGW or IBM/XLC.check dist_info:<< unknown)r rrrrrrZcc_on_)rrr rr r Zcc_is_z=undefined flag for compiler '%s', leave an empty dict instead)rrrrr IGNORECASE cc_test_cexprr"rrXrr#r[cc_marchrcc_namecc_flagsrrrrrrsplit cc_test_flagsrr!)rgZ detect_archZdetect_compilerZ detect_argsrrZ compiler_inforsectionrZrgexcexprr6ZsearchinarchrZcompiler_flagsrUZnflagsrririrjr~s       z_CCompiler.__init__cCsLt|tsJ|d|tj|jd}|||}|sH|jddd|S)z@ Returns True if the compiler supports 'flags'. z testing flagsz test_flags.ctesting failedTr)rrrryrzrrr)rgrU test_pathrririrjr-8s  z_CCompiler.cc_test_flagscCs|d|tj|jd}t|d(}|td|dWdn1sT0Y| ||}|s||jddd |S) zJ Same as the above but supports compile-time expressions. ztesting compiler expressionznpy_dist_test_cexpr.cr z #if !(zq) #error "unsupported expression" #endif int dummy; Nr1Tr) rryrzrrsrrrrr)rgr/rUr2fdrririrjr(Es   * z_CCompiler.cc_test_cexprcCsDt|tsJ|js |js |jr*||S|js6|jr@||S|S)a Remove the conflicts that caused due gathering implied features flags. Parameters ---------- 'flags' list, compiler flags flags should be sorted from the lowest to the highest interest. Returns ------- list, filtered from any conflicts. Examples -------- >>> self.cc_normalize_flags(['-march=armv8.2-a+fp16', '-march=armv8.2-a+dotprod']) ['armv8.2-a+fp16+dotprod'] >>> self.cc_normalize_flags( ['-msse', '-msse2', '-msse3', '-mssse3', '-msse4.1', '-msse4.2', '-mavx', '-march=core-avx2'] ) ['-march=core-avx2'] ) rrr[r\r__cc_normalize_unixrar`_cc_normalize_win)rgrUririrjcc_normalize_flagsXs    z_CCompiler.cc_normalize_flagsz^(-mcpu=|-march=|-x[A-Z0-9\-])zB^(?!(-mcpu=|-march=|-x[A-Z0-9\-]|-m[a-z0-9\-\.]*.$))|(?:-mzvector)z^(-mfpu|-mtune)z[0-9.]csPfdd}t|dkr|Stt|D]\}}tj|s@q(|d|d }|| d}ttjj |}||\}} } |dkrt| dkr|D]"} || \} } }|| kr|| } q| dd | }||g}|dkr||7}qq(g}t }t|D]J}tj |}|sn"|d|vr.qn| |d|d|q|S)Nc s@|d}tddtj|d}||d|ddfS)N+0r rr)r,floatrrfindall_cc_normalize_arch_ver)rtokensverrgrirj ver_flagss  z0_CCompiler._cc_normalize_unix..ver_flagsrrr7)r enumeratereversedrr_cc_normalize_unix_mrgxrfilter_cc_normalize_unix_frgxsearchrr_cc_normalize_unix_krgxaddinsert)rgrUr?iZcur_flagZ lower_flagsZ upper_flagsfilteredr=r0ZsubflagsZxflagZxver_Z xsubflagsZ final_flagsZmatchedrrrir>rjr4sB    z_CCompiler._cc_normalize_unixz^(?!(/arch\:|/Qx\:))z ^(/arch|/Qx:)cCs^tt|D]L\}}t|j|s$q |d7}tt|jj|d| || dS|S)Nr) r@rArr_cc_normalize_win_mrgxrrC_cc_normalize_win_frgxrE)rgrUrIrririrjr5s z_CCompiler._cc_normalize_winN)rrrrr~rrr-r(r6rrrBrDrFr;r4rMrLr5riririrjr s60v 1r c@seZdZdZddZddgfddZddZd%d d Zd&d d ZddZ ddZ ddZ ddZ ddZ ejddZejdgfddZejdgfddZejddZejd d!Zd'd#d$ZdS)(_FeatureaA helper class for `CCompilerOpt` that managing CPU features. Attributes ---------- feature_supported : dict Dictionary containing all CPU features that supported by the platform, according to the specified values in attribute `_Config.conf_features` and `_Config.conf_features_partial()` feature_min : set The minimum support of CPU features, according to the specified values in attribute `_Config.conf_min_features`. c st|drdS||_}t|D]}|||j|}fdd|Dd}|dur| ||j d||ddq(dD]$}|}t |t r| |<qq(t|_|j|jd }| D]}||jvr|j|qd|_dS) Nfeature_is_cachedcsi|]\}}|vr||qSriri)rkvfeaturerirj sz%_Feature.__init__..rVzfeature '%s' is disabled,Tr)rr5r6rrUr/r )rrkfeature_supportedrr rupdaterrrrrrr,r feature_minrr)upperrGrO) rgZ pfeatures feature_nameZcfeaturedisabledoptionZovalZmin_fFrirRrjr~s6       z_Feature.__init__NcCsv|dus t|tst|ds J|dus6t|ts6J|durH|j}t}|D]}|j|||drR||qR|S)a Returns a set of CPU feature names that supported by platform and the **C** compiler. Parameters ---------- names : sequence or None, optional Specify certain CPU features to test it against the **C** compiler. if None(default), it will test all current supported features. **Note**: feature names must be in upper-case. force_flags : list or None, optional If None(default), default compiler flags for every CPU feature will be used during the test. macros : list of tuples, optional A list of C macro definitions. Nr force_flagsr) rrrrrUr rfeature_is_supportedrG)rgnamesr^rZsupported_namesrririrj feature_namess   z_Feature.feature_namescCs|s J||jvS)z Returns True if a certain feature is exist and covered within ``_Config.conf_features``. Parameters ---------- 'name': str feature name in uppercase. )isupperr)rgrririrjfeature_is_exists z_Feature.feature_is_existFcsfdd}t|||dS)az Sort a list of CPU features ordered by the lowest interest. Parameters ---------- 'names': sequence sequence of supported feature names in uppercase. 'reverse': bool, optional If true, the sorted features is reversed. (highest interest) Returns ------- list, sorted CPU features csBt|trj|dStfdd|D}|t|d7}|S)Nrcsg|]}j|dqS)r)rUrrr>rirjr=z<_Feature.feature_sorted..sort_cb..r)rrrUmaxr)rPZrankr>rirjsort_cb9s  z(_Feature.feature_sorted..sort_cb)reversekey)sorted)rgr`rhrgrir>rjfeature_sorted*s z_Feature.feature_sortedcsltffdd t|tr.|}|g}n,t|ds>> self.feature_implies("SSE3") {'SSE', 'SSE2'} >>> self.feature_implies("SSE2") {'SSE'} >>> self.feature_implies("SSE2", keep_origins=True) # 'SSE2' found here since 'SSE' and 'SSE2' imply each other {'SSE', 'SSE2'} csTt}j|}|dgD]2}||||vr4q|||||}q|S)Nr)rrUrrGunion)r_callerrrrI get_impliesrgrirjro\s   z-_Feature.feature_implies..get_impliesr)rrrrrldifference_update)rgr` keep_originsrnrirnrjfeature_impliesDs  z_Feature.feature_impliescCs.t|trt|f}nt|}|||S)z/same as feature_implies() but combining 'names')rrrrlrs)rgr`ririrjfeature_implies_cus  z_Feature.feature_implies_ccs^t|tst|dsJ|j|ddfdd|D}t|dkrZ|j|dddd }|S) a Return list of features in 'names' after remove any implied features and keep the origins. Parameters ---------- 'names': sequence sequence of CPU feature names in uppercase. Returns ------- list of CPU features sorted as-is 'names' Examples -------- >>> self.feature_ahead(["SSE2", "SSE3", "SSE41"]) ["SSE41"] # assume AVX2 and FMA3 implies each other and AVX2 # is the highest interest >>> self.feature_ahead(["SSE2", "SSE3", "SSE41", "AVX2", "FMA3"]) ["AVX2"] # assume AVX2 and FMA3 don't implies each other >>> self.feature_ahead(["SSE2", "SSE3", "SSE41", "AVX2", "FMA3"]) ["AVX2", "FMA3"] rT)rqcsg|]}|vr|qSriri)rrrrrirjrrez*_Feature.feature_ahead..rrhNr)rrrrsrrk)rgr`Zaheadrirurj feature_ahead}s z_Feature.feature_aheadcst|tst|dsJg}|D]jfdd|D}|r|g}|ddvrjq ||ddd|q |S)am same as 'feature_ahead()' but if both features implied each other and keep the highest interest. Parameters ---------- 'names': sequence sequence of CPU feature names in uppercase. Returns ------- list of CPU features sorted as-is 'names' Examples -------- >>> self.feature_untied(["SSE2", "SSE3", "SSE41"]) ["SSE2", "SSE3", "SSE41"] # assume AVX2 and FMA3 implies each other >>> self.feature_untied(["SSE2", "SSE3", "SSE41", "FMA3", "AVX2"]) ["SSE2", "SSE3", "SSE41", "AVX2"] rcs&g|]}|vr|vr|qSri)rs)rnnrrrrgrirjrsz+_Feature.feature_untied..rNr)rrrrsrkremover)rgr`finalZtiedriryrjfeature_untieds"  z_Feature.feature_untiedcs^fddt|ts$t|dkr8|}||S|}fdd|D}|S)z same as `feature_implies_c()` but stop collecting implied features when feature's option that provided through parameter 'keyisfalse' is False, also sorting the returned features. csT|}j|dd}t|D].\}}j|ds |d|d}qPq |S)NTrvr)rtrkr@rUr)ZtnamesrIrr) keyisfalsergrirjtils z%_Feature.feature_get_til..tilrcsh|]}|D]}|qqSriri)rrrt)r~rirj rez+_Feature.feature_get_til..)rrrrhrwrk)rgr`r}ri)r}rgr~rjfeature_get_tils  z_Feature.feature_get_tilc CsB||d}g}|D](}|j|}||d|d|g7}q|S)z Return a list of CPU features that required to be detected sorted from the lowest to highest interest. r&r6r5)rrUr)rgr`r6rrrririrjfeature_detects   z_Feature.feature_detectcCsV|||}g}|D]2}|j|}|dg}|r||sBq||7}q||S)zi Return a list of CPU features flags sorted from the lowest to highest interest. rU)rkrtrUrr-r6)rgr`rUrrrrririrj feature_flagss   z_Feature.feature_flagscCs|dur||}|d|d|ftj|jd|}tj|sZ|d||j |||j d|d}|s|jdd d |S) a Test a certain CPU feature against the compiler through its own check file. Parameters ---------- name : str Supported CPU feature name. force_flags : list or None, optional If None(default), the returned flags from `feature_flags()` will be used. macros : list of tuples, optional A list of C macro definitions. Nz$testing feature '%s' with flags (%s)rzcpu_%s.czfeature test file is not existrrr1Tr) rrrryrzrlowerrrrr+)rgrr^rr2rririrj feature_tests$    z_Feature.feature_testcCsn|s J|dus"t|ts"J||jv}|rj||D]}|j|||ds:dSq:|j|||dsjdS|S)a Check if a certain CPU feature is supported by the platform and compiler. Parameters ---------- name : str CPU feature name in uppercase. force_flags : list or None, optional If None(default), default compiler flags for every CPU feature will be used during test. macros : list of tuples, optional A list of C macro definitions. NrF)rbrrrUrsr)rgrr^r supportedimplririrjr_+s  z_Feature.feature_is_supportedcsVt|tsJj|}|dd}|durRfdd|dgD}|oPt|}|S)zM check if the feature can be auto-vectorized by the compiler rWNcsg|]}|gqSri)r-rdr>rirjrQsz0_Feature.feature_can_autovec..rU)rrrUrany)rgrrcanZ valid_flagsrir>rjfeature_can_autovecHs     z_Feature.feature_can_autovecc Cst|tsJ|j|}|dg}|s,gS|d||||}g}g}|D]d}tj|j d| }tj |s| d|| |||jd} | r||qR||qR|r|jd|dd|S) z Return a list of supported extra checks after testing them against the compiler. Parameters ---------- names : str CPU feature name in uppercase. r/z%Testing extra checks for feature '%s'z extra_%s.czextra check file does not existrztesting failed for checksTr)rrrUrrrryrzrrrrrrr+r) rgrrr/rU availableZ not_availableZchkr2 is_supportedririrjfeature_extra_checksWs,        z_Feature.feature_extra_checksrcs|s J|j|}|dus$Jd|d|j|fg}|dd|dgD7}|dg}|||7}|D]&}|d|j|fd |j|fd g7}qrd krfd d|D}d |S)a" Generate C preprocessor definitions and include headers of a CPU feature. Parameters ---------- 'feature_name': str CPU feature name in uppercase. 'tabs': int if > 0, align the generated strings to the right depend on number of tabs. Returns ------- str, generated C preprocessor Examples -------- >>> self.feature_c_preprocessor("SSE3") /** SSE3 **/ #define NPY_HAVE_SSE3 1 #include Nz /** %s **/z#define %sHAVE_%s 1cSsg|] }d|qS)z #include <%s>ri)rrririrjrsz3_Feature.feature_c_preprocessor..rr5z#ifndef %sHAVE_%sz #define %sHAVE_%s 1z#endifrcsg|]}d|qS) ri)rltabsrirjrre )rbrUrrrr)rgrYrrSZpreprZ extra_defsZedefrirrjfeature_c_preprocessor~s(        z_Feature.feature_c_preprocessor)F)F)r)rrrrr~rarcrkrsrtrwr|rrrrrrr_rrrriririrjrNs, "#  1&)  '  &rNc@seZdZdZddZddZedZddZ ed Z d d Z d d Z ddZ ddZddZddZddZddZddZddZdS)_ParseaA helper class that parsing main arguments of `CCompilerOpt`, also parsing configuration statements in dispatch-able sources. Parameters ---------- cpu_baseline : str or None minimal set of required CPU features or special options. cpu_dispatch : str or None dispatched set of additional CPU features or special options. Special options can be: - **MIN**: Enables the minimum CPU features that utilized via `_Config.conf_min_features` - **MAX**: Enables all supported CPU features by the Compiler and platform. - **NATIVE**: Enables all CPU features that supported by the current machine. - **NONE**: Enables nothing - **Operand +/-**: remove or add features, useful with options **MAX**, **MIN** and **NATIVE**. NOTE: operand + is only added for nominal reason. NOTES: - Case-insensitive among all CPU features and special options. - Comma or space can be used as a separator. - If the CPU feature is not supported by the user platform or compiler, it will be skipped rather than raising a fatal error. - Any specified CPU features to 'cpu_dispatch' will be skipped if its part of CPU baseline features - 'cpu_baseline' force enables implied features. Attributes ---------- parse_baseline_names : list Final CPU baseline's feature names(sorted from low to high) parse_baseline_flags : list Compiler flags of baseline features parse_dispatch_names : list Final CPU dispatch-able feature names(sorted from low to high) parse_target_groups : dict Dictionary containing initialized target groups that configured through class attribute `conf_target_groups`. The key is represent the group name and value is a tuple contains three items : - bool, True if group has the 'baseline' option. - list, list of CPU features. - list, list of extra compiler flags. c stdjgfjjgfjdgfjdgfjddgfd_tdrNdSg_ g_ g_ i_ j rtd}}d|durd|}|}|_ |_ d|dur$d|}fdd |D}||}|_ t|d kr$d |d d jD]b\}}d||}|rd|sxdggfj |<q8|\} } } | | | fj |<q8d_dS)NMAXOPT)Z KEEP_BASELINEZ KEEP_SORTrZWERRORZAUTOVECparse_is_cachedzcheck requested baseline cpu_baselinez&check requested dispatch-able features cpu_dispatchcsh|]}|jvr|qSriparse_baseline_namesrdr>rirjrs z"_Parse.__init__..rz skip featureszsince its part of baselinezinitialize targets groupszparse target groupFT)r^_parse_policy_not_keepbase_parse_policy_keepsort_parse_policy_not_keepsort_parse_policy_maxopt_parse_policy_werror_parse_policy_autovec_parse_policiesrrparse_baseline_flagsparse_dispatch_namesparse_target_groupsrXr_parse_arg_featuresrarrkrt differencerrrrXstrip_parse_target_tokensr) rgrrbaseline_namesZ cpu_dispatch_Zconflict_baseline group_namer<Z GROUP_NAME has_baselinefeatures extra_flagsrir>rjr~sz             z_Parse.__init__c Cs|d|t|}d}d}d}d}d}d}t|D]x\} } | |krT|dq|dkr|| |}|dkrpq6|t|7}|| 7}| |}|dkr6|t|t| 7}qq6Wdn1s0Y|dkr|d ||dkr|d ||||}||S) a Fetch and parse configuration statements that required for defining the targeted CPU features, statements should be declared in the top of source in between **C** comment and start with a special mark **@targets**. Configuration statements are sort of keywords representing CPU features names, group of statements and policies, combined together to determine the required optimization. Parameters ---------- source : str the path of **C** source file. Returns ------- - bool, True if group has the 'baseline' option - list, list of CPU features - list, list of extra compiler flags z!looking for '@targets' inside -> r iz@targetsz*/zreached the max of linesNz(expected to find '%s' within a C commentzexpected to end with '%s')rrr@rfindrr) rgrr3r<Z max_to_reachZ start_with start_posZend_withend_pos current_linelineririrj parse_targets0s8      $ z_Parse.parse_targetsz \s|,|([+-])c Cs>t|ts|d|t}ttdt|j|}d}|D]}|ddvrZ||d|dkrhd}q>|dkrvd}q>| }t}|d krn|d kr|j d } | s||d |j | d gd}nV|dkr|j }nB|dkr|j}n2||j vr||n||s||d||r*||}n ||}d}q>|S)Nzexpected a string in '%s'Tr)#$zYtarget groups and policies aren't allowed from arguments, only from dispatch-able sourcesr7-FNONEZNATIVErz-native option isn't supported by the compiler)ZDETECT_FEATURESrr]MAXZMINz&, '%s' isn't a known feature or option)rrrrrrCrr,_parse_regex_argrXr+rarUr rWrGrcrlr) rgZarg_nameZ req_featuresZfinal_featuresr<rtokTOKZ features_torririrjrgsX         z_Parse._parse_arg_featuresz\s|[*,/]|([()])cCst|tsJg}g}d}t}t}d}ttdt|j|}|sP|d|D]}| } |d} | dvr~|dqT| dkr|dur|d| | | qT| dkr|dur|d | | |||\}}}qT| d kr|dur|d t}qT| d krx|dur|d | |} | durB| t|n0t| dkrX| d} | rr| |vrr|| d}qT| dkr|dur|dd}qT|dur| | qT|| s|d| | |jvp| |jv} | r| |vrT|| qT| | qT|dur|d |r0|d|d||}t|D]L} |j| \}}}|D]0}||vrlqZ|d| |f| |qZqB|jD]R\} \}}}d}| |vr|}|d| n|}|sؐq||||\}}}q|||fS)NFzexpected one token at leastr)r7rze+/- are 'not' allowed from target's groups or @targets, only from cpu_baseline and cpu_dispatch parmsrzCpolicies aren't allowed inside multi-target '()', only CPU featuresrzHtarget groups aren't allowed inside multi-target '()', only CPU featuresrz"unclosed multi-target, missing ')'rz$multi-target opener '(' wasn't foundrZBASELINEz/baseline isn't allowed inside multi-target '()'Tzinvalid target name '%s'z skip targetsz.not part of baseline or dispatch-able featureszpolicy '%s' force enables '%s'zpolicy '%s' is ON)rrrrrCrr,_parse_regex_targetrrXrG_parse_token_policy_parse_token_group_parse_multi_targettuplerrrcrrrr|rr)rgr< final_targetsrrskippedZpoliciesZ multi_targetrrchtargets is_enabledprKdepsrZhaveZnhavefuncririrjrs                          z_Parse._parse_target_tokenscCsZt|dks |dd|dkr*|d|dd}||jvrV|d||j|S)zvalidate policy tokenrrNrz*'$' must stuck in the begin of policy namez6'%s' is an invalid policy name, available policies are)rrrr )rgtokenririrjrs    z_Parse._parse_token_policycst|dks |dd|dkr*|d|dd}|j|ddgf\}}}|durr|d|d|j|rzd }fd d |D7fd d |D7|fS) zvalidate group tokenrrNrz)'#' must stuck in the begin of group nameFz&'%s' is an invalid target group name, zavailable target groups areTcsg|]}|vr|qSrirird)rrirjr2rez-_Parse._parse_token_group..csg|]}|vr|qSrirird)rrirjr3re)rrrrr )rgrrrrZ ghas_baselineZgtargetsZ gextra_flagsri)rrrjr s$    z_Parse._parse_token_groupcsr|sdtfdd|Ds0d|tfdd|DsJdS|}|s\dS|}t|}|S)z9validate multi targets that defined between parentheses()zempty multi-target '()'csg|]}|qSri)rcrtarr>rirjr;sz._Parse._parse_multi_target..z#invalid target name in multi-targetcs g|]}|jvp|jvqSri)rrrr>rirjr?s N)rallrwrkr)rgrrir>rjr6s       z_Parse._parse_multi_targetcsxg}|ddD]L}d}t|tr.|jv}ntfdd|D}|r||||q|rnd||||fS)zskip all baseline featuresNFcsg|]}|jvqSrirrdr>rirjrXsz5_Parse._parse_policy_not_keepbase..zskip baseline features)rrrrrrzr)rgrrrrrZis_baserir>rjrOs      z!_Parse._parse_policy_not_keepbasecCs|d|d|||fS)z$leave a notice that $keep_sort is onz/policy 'keep_sort' is on, dispatch-able targetszo are 'not' sorted depend on the highest interest butas specified in the dispatch-able source or the extra group)rrgrrrririrjresz_Parse._parse_policy_keepsortcCs|j|dd}|||fS)z%sorted depend on the highest interestTrv)rkrririrjrnsz!_Parse._parse_policy_not_keepsortcCsT|jr|dn8|jr$|dn&|jd}|sB|jdddn||7}|||fS)z&append the compiler optimization flagsz3debug mode is detected, policy 'maxopt' is skipped.z5optimization is disabled, policy 'maxopt' is skipped.rzOcurrent compiler doesn't support optimization flags, policy 'maxopt' is skippedTr)r$rrXr+rgrrrrUririrjrss   z_Parse._parse_policy_maxoptcCs:|jd}|s|jdddn|d||7}|||fS)z#force warnings to treated as errorsrzTcurrent compiler doesn't support werror flags, warnings will 'not' treated as errorsTrz'compiler warnings are treated as errors)r+rrririrjrs  z_Parse._parse_policy_werrorcstg}|ddD]H}t|tr*|}ntfdd|D}|s||||q|rjd||||fS)z=skip features that has no auto-vectorized support by compilerNcsg|]}|qSri)rrrr>rirjrsz0_Parse._parse_policy_autovec..z!skip non auto-vectorized features)rrrrrzrr)rgrrrrrrrir>rjrs      z_Parse._parse_policy_autovecN)rrrrr~rrrrrrrrrrrrrrrrriririrjrs .R6 8 t   rc@sjeZdZdZdddZddZd d Zd d Zd dZdddZ ddZ dddZ dddZ dddZ dS) CCompilerOptz A helper class for `CCompiler` aims to provide extra build options to effectively control of compiler optimizations that are directly related to CPU features. minrfNcCst|t||t|||||t|t||jsZ|jrZ| dd}t |||||_ ||_ t |di|_|jdt|d|_dS)NzSnative flag is specified through environment variables. force cpu-baseline='native'rsources_status hit_cache)rr~rrrr rNrXr%rr_requested_baseline_requested_dispatchrrrrGrr)rgrrrrririrjr~s       zCCompilerOpt.__init__cCs |jo |jS)zF Returns True if the class loaded from the cache file )rrr>ririrj is_cachedszCCompilerOpt.is_cachedcCs|jS)zE Returns a list of final CPU baseline compiler flags )rr>ririrjcpu_baseline_flagsszCCompilerOpt.cpu_baseline_flagscCs|jS)zC return a list of final CPU baseline feature names rr>ririrjcpu_baseline_namesszCCompilerOpt.cpu_baseline_namescCs|jS)zC return a list of final CPU dispatch feature names )rr>ririrjcpu_dispatch_namesszCCompilerOpt.cpu_dispatch_namescKs&i}|}|dg}|D]}tj|} |rZ| |sHtj|| } | |vrZ|| ||\} } } | | || | } | D]:}|j | ||| d}t | | |}||g|q~| rt | |}||g|| | f|j |<qg}|D](\}}||j|t|fd|i|7}q|S)a Compile one or more dispatch-able sources and generates object files, also generates abstract C config headers and macros that used later for the final runtime dispatching process. The mechanism behind it is to takes each source file that specified in 'sources' and branching it into several files depend on special configuration statements that must be declared in the top of each source which contains targeted CPU features, then it compiles every branched source with the proper compiler flags. Parameters ---------- sources : list Must be a list of dispatch-able sources file paths, and configuration statements must be declared inside each file. src_dir : str Path of parent directory for the generated headers and wrapped sources. If None(default) the files will generated in-place. ccompiler : CCompiler Distutils `CCompiler` instance to be used for compilation. If None (default), the provided instance during the initialization will be used instead. **kwargs : any Arguments to pass on to the `CCompiler.compile()` Returns ------- list : generated object files Raises ------ CompileError Raises by `CCompiler.compile()` on compiling failure. DistutilsError Some errors during checking the sanity of configuration statements. See Also -------- parse_targets : Parsing the configuration statements of dispatch-able sources. include_dirs)nochanger)r setdefaultryrzr startswithrrr_generate_config _wrap_targetrrrrrr)rgrsrc_dirrrZ to_compilebaseline_flagsrsrcrrrrrrZtar_srcrUobjectsZsrcsririrj try_dispatchs</      zCCompilerOpt.try_dispatchc spd|}}t|}t|}tj|}tj|sfjd|dddt|t |d}d fdd |D}d fd d |D} | t d jjd |d ||||| d d} |D]} | j| ddd7} qd} |D],} | t djj| j| ddd7} q| t djj| | dWdn1sb0YdS)ax Generate the dispatch header which contains the #definitions and headers for platform-specific instruction-sets for the enabled CPU baseline and dispatch-able features. Its highly recommended to take a look at the generated header also the generated source files via `try_dispatch()` in order to get the full picture. z"generate CPU dispatch header: (%s)zdispatch header dir z does not exist, creating itTrr  \ csg|]}dj|fqSz3 %sWITH_CPU_EXPAND_(MACRO_TO_CALL(%s, __VA_ARGS__))rrdr>rirjrN sz9CCompilerOpt.generate_dispatch_header..csg|]}dj|fqSrrrdr>rirjrT sa /* * AUTOGENERATED DON'T EDIT * Please make changes to the code generator (distutils/ccompiler_opt.py) */ #define {pfx}WITH_CPU_BASELINE "{baseline_str}" #define {pfx}WITH_CPU_DISPATCH "{dispatch_str}" #define {pfx}WITH_CPU_BASELINE_N {baseline_len} #define {pfx}WITH_CPU_DISPATCH_N {dispatch_len} #define {pfx}WITH_CPU_EXPAND_(X) X #define {pfx}WITH_CPU_BASELINE_CALL(MACRO_TO_CALL, ...) \ {baseline_calls} #define {pfx}WITH_CPU_DISPATCH_CALL(MACRO_TO_CALL, ...) \ {dispatch_calls} r)pfxZ baseline_strZ dispatch_str baseline_len dispatch_lenbaseline_callsdispatch_callsr rrrz #ifdef {pfx}CPU_TARGET_{name} {pre} #endif /*{pfx}CPU_TARGET_{name}*/ )rrprez /******* baseline features *******/ {baseline_pre} /******* dispatch features *******/ {dispatch_pre} )r baseline_pre dispatch_preN)rrrrryrzrrr rrrrrrrrr) rgZ header_pathrdispatch_namesrrZ header_dirrrrrrrrir>rjgenerate_dispatch_header4 sN           z%CCompilerOpt.generate_dispatch_headerFc Csg}g}g}g}|d|f|d|d|f|d|d|f|d|jr^dn|jf|d|jrxdn|jf|jr|d n|d t|jf|}|d |rd |nd f| }|d|rd |nd fg}|D]} || | 7}q|d|r"d |nd f|jr>|d n|d t|j f| } |d | rpd | nd fi} |jD],\} \} }|D]}| |g| qq|r| s4d}|| D]@}| |}t|tr|n dd |} || dt|7}q|d|r(|ddnd fn|d|| D]}| |}t|trf|n dd |}d ||}d |||}d ||}g}t|tr|fn|D]} || | 7}q|rd |nd }|d|||f|d|f|d|f|d|f|D]}|d|fq4qHg}dd|D}dd|D}d}tt|t|}|D]~\}}|s|dq|d |t|7}|||d|D]4\}}|d |t|7}|||d|qΐqd |S)NPlatform)r r z CPU baselinez CPU dispatchZ Architecture unsupportedZCompilerz unix-like) Requestedzoptimization disabledrZEnabledrnoneFlagsz Extra checksr z(%s)z[%d] Generatedr)rr ZDetectcSsg|]\}}t|qSrir)rZsecsrKririrjr rez'CCompilerOpt.report..cSs&g|]\}}|D]\}}t|qqSrir)rrKrowscolririrjr rez z: r)rr"r)r#r*rXreprrrrrrrrrrrrkrrrrrsrrf) rgfullreportZ platform_rowsZ baseline_rowsZ dispatch_rowsrrr/rrZtarget_sourcesrrKrr generatedr pretty_namerUrr6rrZsecs_lenZcols_lentabpadsecrrrririrjr s      $     zCCompilerOpt.reportc st|ttfsJt|tr&|}}nd|}d|}tj|tj|}djgtj|| R}|rtj |r|S| d|| | |}d|jfdd|D} d| } t|d 8} | td j|j|tj|| d Wdn1s0Y|S) N.__z {0}.{2}{1}zwrap dispatch-able target -> z#define %sCPU_TARGET_csg|] }|qSririrdZ target_joinrirjr rez-CCompilerOpt._wrap_target..rr aR /** * AUTOGENERATED DON'T EDIT * Please make changes to the code generator (distutils/ccompiler_opt.py) */ #define {pfx}CPU_TARGET_MODE #define {pfx}CPU_TARGET_CURRENT {target_name} {target_defs} #include "{path}" )r target_namerz target_defs)rrrrryrzbasenamersplitextrrrrkrtrrrrrabspath) rgr dispatch_srctargetrZext_namerZ wrap_pathrr r3rirrjr s(           (zCCompilerOpt._wrap_targetc Cstj|}tj|dd}tj||}|||}zht|L}|d}t |dkrt |d|krWdWdSWdn1s0YWnt yYn0tj tj |dd|d|g} |D]\} t| tr| } nd d d | D} || } d d d | D} | d|j| | fqd| } |rZd|j} nd} t|d0}|tdj|j| | |dWdn1s0YdS)Nrz.hz cache_hash:rrT)exist_okzgenerate dispatched config -> rcSsg|]}|qSririrririrjr0 rez1CCompilerOpt._generate_config..z&&cSsg|] }d|qS)zCHK(%s)rirdririrjr2 sz2 %sCPU_DISPATCH_EXPAND_(CB((%s), %s, __VA_ARGS__))rz( %sCPU_DISPATCH_EXPAND_(CB(__VA_ARGS__))r r aZ // cache_hash:{cache_hash} /** * AUTOGENERATED DON'T EDIT * Please make changes to the code generator (distutils/ccompiler_opt.py) */ #ifndef {pfx}CPU_DISPATCH_EXPAND_ #define {pfx}CPU_DISPATCH_EXPAND_(X) X #endif #undef {pfx}CPU_DISPATCH_BASELINE_CALL #undef {pfx}CPU_DISPATCH_CALL #define {pfx}CPU_DISPATCH_BASELINE_CALL(CB, ...) \ {baseline_calls} #define {pfx}CPU_DISPATCH_CALL(CHK, CB, ...) \ {dispatch_calls} )rrrrF)ryrzr r rrrreadliner,rintrnr rrrrrrrrrrr)rgrr rrZ config_pathrrZ last_hashrrrZ req_detectrr3ririrjr sN   4       (zCCompilerOpt._generate_config)rrfN)NN)F)F)F)rrrrr~rrrrrrrrrriririrjrs  WS m &rcKs2t|fi|}tj|r$|s.|||S)a Create a new instance of 'CCompilerOpt' and generate the dispatch header which contains the #definitions and headers of platform-specific instruction-sets for the enabled CPU baseline and dispatch-able features. Parameters ---------- compiler : CCompiler instance dispatch_hpath : str path of the dispatch header **kwargs: passed as-is to `CCompilerOpt(...)` Returns ------- new instance of CCompilerOpt )rryrzrrr)rZdispatch_hpathrrririrjnew_ccompiler_optX s r)rrvrryr rrrrrrrr rNrrrriririrjsB ?; ;hy6