
    %in#                     2    d Z ddlZddlZddlZd Zd Zd Zy)a  
Centralized compiler flag selection for NVX (Native Vector Extensions) builds.

This module determines appropriate architecture-specific optimization flags
for building native C extensions using CFFI. It balances portability and
performance based on the build context.

Strategy
--------

For **WHEEL BUILDS** (distribution via PyPI):
    Use safe, portable baseline architectures to ensure wheels work on a wide
    range of CPUs without causing SIGILL (Illegal Instruction) crashes.

For **SOURCE BUILDS** (local installation):
    Use -march=native to generate optimal code for the specific CPU where
    the build is happening, maximizing performance.

Architecture Baselines
----------------------

x86-64 (portable baseline):
    -march=x86-64-v2 (microarchitecture level 2, 2009+)
    Includes: SSE4.2, POPCNT, SSSE3, SSE4.1
    Compatible with: Intel Nehalem+, AMD Bulldozer+ (2009+)
    Coverage: ~99% of x86-64 CPUs from 2010 onwards

ARM64 (portable baseline):
    -march=armv8-a (baseline ARMv8-A architecture)
    Compatible with: Raspberry Pi 4/5, AWS Graviton, Apple M1/M2, etc.
    Coverage: All 64-bit ARM CPUs

Environment Variables
---------------------

AUTOBAHN_ARCH_TARGET : str, optional
    User/distro override for architecture target:
    - "native" : Force -march=native (maximum performance, may break portability)
    - "safe"   : Force portable baseline (ensures compatibility)
    - Not set  : Auto-detect based on build context (recommended)

AUTOBAHN_WHEEL_BUILD : str, optional
    Explicit marker for wheel builds ("true" or "1")
    When set, forces portable baseline regardless of auto-detection

CI : str, optional
    Standard CI environment variable. When "true" or "1", assumes wheel build.

CIBUILDWHEEL : str, optional
    Set by cibuildwheel. Indicates wheel build for distribution.

AUDITWHEEL_PLAT : str, optional
    Set by auditwheel/manylinux builds. Indicates wheel build.

Examples
--------

**GitHub Actions building wheels:**
    >>> # Automatically detects CI=true, uses -march=x86-64-v2
    >>> get_compile_args()
    ['-std=c99', '-Wall', '-Wno-strict-prototypes', '-O3', '-march=x86-64-v2']

**User installing from source:**
    >>> # Detects local build, uses -march=native
    >>> get_compile_args()
    ['-std=c99', '-Wall', '-Wno-strict-prototypes', '-O3', '-march=native']

**Gentoo building package:**
    >>> # Gentoo wants -march=native for optimized binaries
    >>> import os
    >>> os.environ['AUTOBAHN_ARCH_TARGET'] = 'native'
    >>> get_compile_args()
    ['-std=c99', '-Wall', '-Wno-strict-prototypes', '-O3', '-march=native']

**Debian building package:**
    >>> # Debian wants portable binaries for all users
    >>> import os
    >>> os.environ['AUTOBAHN_ARCH_TARGET'] = 'safe'
    >>> get_compile_args()
    ['-std=c99', '-Wall', '-Wno-strict-prototypes', '-O3', '-march=x86-64-v2']

Notes
-----

This module is used by all NVX components:
- autobahn.nvx._xormasker (WebSocket frame XOR masking)
- autobahn.nvx._utf8validator (WebSocket UTF-8 validation)

Related Issues
--------------
- #1717: SIGILL crashes from -march=native in distributed wheels

See Also
--------
- https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels
- https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html
    Nc                     t         j                  j                  d      dv ryt         j                  j                  d      ryt         j                  j                  d      ryt         j                  j                  d      dv ryy)a  
    Detect if we're building a wheel for distribution vs. a local source install.

    Returns
    -------
    bool
        True if building a wheel (need portability), False if building locally.

    Notes
    -----
    Detection heuristics:
    1. CI environment variables (CI, CIBUILDWHEEL)
    2. manylinux/auditwheel markers (AUDITWHEEL_PLAT)
    3. Explicit marker (AUTOBAHN_WHEEL_BUILD)
    4. Default: False (assume local source build)
    CI)true1TCIBUILDWHEELAUDITWHEEL_PLATAUTOBAHN_WHEEL_BUILDF)osenvironget     t/var/www/html/navyabakers_fullstack/navyabakers_prod/venv/lib/python3.12/site-packages/autobahn/nvx/_compile_args.pyis_building_wheelr      sg    $ 
zz~~d}, 
zz~~n% 
zz~~'( 
zz~~,-> r   c                  l   t         j                  dk(  rddgS t        j                         j                         } g d}t        j
                  j                  dd      j                         }|dk(  r|dgz   S |d	k(  rt        |       }|r||gz   S |S t               rt        |       }|r||gz   S |S |dgz   S )
a  
    Returns appropriate compiler arguments for building NVX native extensions.

    This function determines the optimal compiler flags based on:
    - Target platform (Windows, Linux, macOS, etc.)
    - Target architecture (x86-64, ARM64, etc.)
    - Build context (wheel distribution vs. local source install)
    - User/distro overrides via environment variables

    Returns
    -------
    list of str
        Compiler arguments suitable for current build context.
        For MSVC: ['/O2', '/W3']
        For GCC/Clang: ['-std=c99', '-Wall', '-O3', '-march=...']

    Examples
    --------
    >>> args = get_compile_args()
    >>> # Use with CFFI:
    >>> ffi.set_source("_nvx_module", c_source, extra_compile_args=args)
    win32z/O2z/W3)z-std=c99z-Wallz-Wno-strict-prototypesz-O3AUTOBAHN_ARCH_TARGET nativez-march=nativesafe)	sysplatformmachinelowerr
   r   r   _get_safe_march_flagr   )r   	base_argsarch_override	arch_flags       r   get_compile_argsr      s    . ||w u~  &&(GI JJNN#92>DDFM  O,,,	&	  )1		{** 		 )1		{** 
 O,,,r   c                     | dv ry| dv ryy)aD  
    Returns safe -march flag for portable binaries on the given machine architecture.

    Parameters
    ----------
    machine : str
        Machine architecture from platform.machine().lower()

    Returns
    -------
    str or None
        Safe -march flag for the architecture, or None for unknown architectures.
    )x86_64amd64x64z-march=x86-64-v2)aarch64arm64z-march=armv8-aNr   )r   s    r   r   r      s%     ,, "	(	(  
 r   )__doc__r
   r   r   r   r   r   r   r   r   <module>r'      s)   6`D 
 
 "JG-Tr   