(self, flags: T.List[str], phase: _Phase = _Phase.COMPILER)
return self._merge_flags(xflags)
Translate generic "GCC-speak" plus particular "NVCC-speak" flags to NVCC flags.

        NVCC's "short" flags have broad similarities to the GCC standard, but have
        gratuitous, irritating differences.
Translate generic "GCC-speak" plus particular "NVCC-speak" flags to NVCC flags.
Translate generic GCC - speak plus particular NVCC - speak flags to NVCC flags .
def _to_host_flags(self, flags: T.List[str], phase: _Phase = _Phase.COMPILER) -> T.List[str]:
        """
        Translate generic "GCC-speak" plus particular "NVCC-speak" flags to NVCC flags.

        NVCC's "short" flags have broad similarities to the GCC standard, but have
        gratuitous, irritating differences.
        """

        xflags = []
        flagit = iter(flags)

        for flag in flagit:
            # The CUDA Toolkit Documentation, in 4.1. Command Option Types and Notation,
            # specifies that NVCC does not parse the standard flags as GCC does. It has
            # its own strategy, to wit:
            #
            #     nvcc recognizes three types of command options: boolean options, single
            #     value options, and list options.
            #
            #     Boolean options do not have an argument; they are either specified on a
            #     command line or not. Single value options must be specified at most once,
            #     and list options may be repeated. Examples of each of these option types
            #     are, respectively: --verbose (switch to verbose mode), --output-file
            #     (specify output file), and --include-path (specify include path).
            #
            #     Single value options and list options must have arguments, which must
            #     follow the name of the option itself by either one of more spaces or an
            #     equals character. When a one-character short name such as -I, -l, and -L
            #     is used, the value of the option may also immediately follow the option
            #     itself without being separated by spaces or an equal character. The
            #     individual values of list options may be separated by commas in a single
            #     instance of the option, or the option may be repeated, or any
            #     combination of these two cases.
            #
            # One strange consequence of this choice is that directory and filenames that
            # contain commas (',') cannot be passed to NVCC (at least, not as easily as
            # in GCC). Another strange consequence is that it is legal to supply flags
            # such as
            #
            #     -lpthread,rt,dl,util
            #     -l pthread,rt,dl,util
            #     -l=pthread,rt,dl,util
            #
            # and each of the above alternatives is equivalent to GCC-speak
            #
            #     -lpthread -lrt -ldl -lutil
            #     -l pthread -l rt -l dl -l util
            #     -l=pthread -l=rt -l=dl -l=util
            #
            # *With the exception of commas in the name*, GCC-speak for these list flags
            # is a strict subset of NVCC-speak, so we passthrough those flags.
            #
            # The -D macro-define flag is documented as somehow shielding commas from
            # splitting a definition. Balanced parentheses, braces and single-quotes
            # around the comma are not sufficient, but balanced double-quotes are. The
            # shielding appears to work with -l, -I, -L flags as well, for instance.
            #
            # Since our goal is to replicate GCC-speak as much as possible, we check for
            # commas in all list-arguments and shield them with double-quotes. We make
            # an exception for -D (where this would be value-changing) and -U (because
            # it isn't possible to define a macro with a comma in the name).

            if flag in self._FLAG_PASSTHRU_NOARGS:
                xflags.append(flag)
                continue

            # Handle breakup of flag-values into a flag-part and value-part.
            if   flag[:1] not in '-/':
                # This is not a flag. It's probably a file input. Pass it through.
                xflags.append(flag)
                continue
            elif flag[:1] == '/':
                # This is ambiguously either an MVSC-style /switch or an absolute path
                # to a file. For some magical reason the following works acceptably in
                # both cases.
                wrap = '"' if ',' in flag else ''
                xflags.append(f'-X{phase.value}={wrap}{flag}{wrap}')
                continue
            elif len(flag) >= 2 and flag[0] == '-' and flag[1] in 'IDULlmOxmte':
                # This is a single-letter short option. These options (with the
                # exception of -o) are allowed to receive their argument with neither
                # space nor = sign before them. Detect and separate them in that event.
                if   flag[2:3] == '':            # -I something
                    try:
                        val = next(flagit)
                    except StopIteration:
                        pass
                elif flag[2:3] == '=':           # -I=something
                    val = flag[3:]
                else:                            # -Isomething
                    val = flag[2:]
                flag = flag[:2]                  # -I
            elif flag in self._FLAG_LONG2SHORT_WITHARGS or \
                 flag in self._FLAG_SHORT2LONG_WITHARGS:
                # This is either -o or a multi-letter flag, and it is receiving its
                # value isolated.
                try:
                    val = next(flagit)           # -o something
                except StopIteration:
                    pass
            elif flag.split('=', 1)[0] in self._FLAG_LONG2SHORT_WITHARGS or \
                 flag.split('=', 1)[0] in self._FLAG_SHORT2LONG_WITHARGS:
                # This is either -o or a multi-letter flag, and it is receiving its
                # value after an = sign.
                flag, val = flag.split('=', 1)    # -o=something
            # Some dependencies (e.g., BoostDependency) add unspaced "-isystem/usr/include" arguments
            elif flag.startswith('-isystem'):
                val = flag[8:].strip()
                flag = flag[:8]
            else:
                # This is a flag, and it's foreign to NVCC.
                #
                # We do not know whether this GCC-speak flag takes an isolated
                # argument. Assuming it does not (the vast majority indeed don't),
                # wrap this argument in an -Xcompiler flag and send it down to NVCC.
                if   flag == '-ffast-math':
                    xflags.append('-use_fast_math')
                    xflags.append('-Xcompiler='+flag)
                elif flag == '-fno-fast-math':
                    xflags.append('-ftz=false')
                    xflags.append('-prec-div=true')
                    xflags.append('-prec-sqrt=true')
                    xflags.append('-Xcompiler='+flag)
                elif flag == '-freciprocal-math':
                    xflags.append('-prec-div=false')
                    xflags.append('-Xcompiler='+flag)
                elif flag == '-fno-reciprocal-math':
                    xflags.append('-prec-div=true')
                    xflags.append('-Xcompiler='+flag)
                else:
                    xflags.append('-Xcompiler='+self._shield_nvcc_list_arg(flag))
                    # The above should securely handle GCC's -Wl, -Wa, -Wp, arguments.
                continue

            assert val is not None  # Should only trip if there is a missing argument.

            # Take care of the various NVCC-supported flags that need special handling.
            flag = self._FLAG_LONG2SHORT_WITHARGS.get(flag, flag)

            if   flag in {'-include', '-isystem', '-I', '-L', '-l'}:
                # These flags are known to GCC, but list-valued in NVCC. They potentially
                # require double-quoting to prevent NVCC interpreting the flags as lists
                # when GCC would not have done so.
                #
                # We avoid doing this quoting for -D to avoid redefining macros and for
                # -U because it isn't possible to define a macro with a comma in the name.
                # -U with comma arguments is impossible in GCC-speak (and thus unambiguous
                #in NVCC-speak, albeit unportable).
                if len(flag) == 2:
                    xflags.append(flag+self._shield_nvcc_list_arg(val))
                elif flag == '-isystem' and val in self.host_compiler.get_default_include_dirs():
                    # like GnuLikeCompiler, we have to filter out include directories specified
                    # with -isystem that overlap with the host compiler's search path
                    pass
                else:
                    xflags.append(flag)
                    xflags.append(self._shield_nvcc_list_arg(val))
            elif flag == '-O':
                # Handle optimization levels GCC knows about that NVCC does not.
                if   val == 'fast':
                    xflags.append('-O3')
                    xflags.append('-use_fast_math')
                    xflags.append('-Xcompiler')
                    xflags.append(flag+val)
                elif val in {'s', 'g', 'z'}:
                    xflags.append('-Xcompiler')
                    xflags.append(flag+val)
                else:
                    xflags.append(flag+val)
            elif flag in {'-D', '-U', '-m', '-t'}:
                xflags.append(flag+val)       # For style, keep glued.
            elif flag in {'-std'}:
                xflags.append(flag+'='+val)   # For style, keep glued.
            else:
                xflags.append(flag)
                xflags.append(val)

        return self._merge_flags(xflags)
def _to_host_flags ( self , flags : T . List [ str ] , phase : _Phase = _Phase . COMPILER ) -> T . List [ str ] : xflags = [ ] flagit = iter ( flags ) for flag in flagit : # The CUDA Toolkit Documentation, in 4.1. Command Option Types and Notation, # specifies that NVCC does not parse the standard flags as GCC does. It has # its own strategy, to wit: # #     nvcc recognizes three types of command options: boolean options, single #     value options, and list options. # #     Boolean options do not have an argument; they are either specified on a #     command line or not. Single value options must be specified at most once, #     and list options may be repeated. Examples of each of these option types #     are, respectively: --verbose (switch to verbose mode), --output-file #     (specify output file), and --include-path (specify include path). # #     Single value options and list options must have arguments, which must #     follow the name of the option itself by either one of more spaces or an #     equals character. When a one-character short name such as -I, -l, and -L #     is used, the value of the option may also immediately follow the option #     itself without being separated by spaces or an equal character. The #     individual values of list options may be separated by commas in a single #     instance of the option, or the option may be repeated, or any #     combination of these two cases. # # One strange consequence of this choice is that directory and filenames that # contain commas (',') cannot be passed to NVCC (at least, not as easily as # in GCC). Another strange consequence is that it is legal to supply flags # such as # #     -lpthread,rt,dl,util #     -l pthread,rt,dl,util #     -l=pthread,rt,dl,util # # and each of the above alternatives is equivalent to GCC-speak # #     -lpthread -lrt -ldl -lutil #     -l pthread -l rt -l dl -l util #     -l=pthread -l=rt -l=dl -l=util # # *With the exception of commas in the name*, GCC-speak for these list flags # is a strict subset of NVCC-speak, so we passthrough those flags. # # The -D macro-define flag is documented as somehow shielding commas from # splitting a definition. Balanced parentheses, braces and single-quotes # around the comma are not sufficient, but balanced double-quotes are. The # shielding appears to work with -l, -I, -L flags as well, for instance. # # Since our goal is to replicate GCC-speak as much as possible, we check for # commas in all list-arguments and shield them with double-quotes. We make # an exception for -D (where this would be value-changing) and -U (because # it isn't possible to define a macro with a comma in the name). if flag in self . _FLAG_PASSTHRU_NOARGS : xflags . append ( flag ) continue # Handle breakup of flag-values into a flag-part and value-part. if flag [ : 1 ] not in '-/' : # This is not a flag. It's probably a file input. Pass it through. xflags . append ( flag ) continue elif flag [ : 1 ] == '/' : # This is ambiguously either an MVSC-style /switch or an absolute path # to a file. For some magical reason the following works acceptably in # both cases. wrap = '"' if ',' in flag else '' xflags . append ( f'-X{phase.value}={wrap}{flag}{wrap}' ) continue elif len ( flag ) >= 2 and flag [ 0 ] == '-' and flag [ 1 ] in 'IDULlmOxmte' : # This is a single-letter short option. These options (with the # exception of -o) are allowed to receive their argument with neither # space nor = sign before them. Detect and separate them in that event. if flag [ 2 : 3 ] == '' : # -I something try : val = next ( flagit ) except StopIteration : pass elif flag [ 2 : 3 ] == '=' : # -I=something val = flag [ 3 : ] else : # -Isomething val = flag [ 2 : ] flag = flag [ : 2 ] # -I elif flag in self . _FLAG_LONG2SHORT_WITHARGS or flag in self . _FLAG_SHORT2LONG_WITHARGS : # This is either -o or a multi-letter flag, and it is receiving its # value isolated. try : val = next ( flagit ) # -o something except StopIteration : pass elif flag . split ( '=' , 1 ) [ 0 ] in self . _FLAG_LONG2SHORT_WITHARGS or flag . split ( '=' , 1 ) [ 0 ] in self . _FLAG_SHORT2LONG_WITHARGS : # This is either -o or a multi-letter flag, and it is receiving its # value after an = sign. flag , val = flag . split ( '=' , 1 ) # -o=something # Some dependencies (e.g., BoostDependency) add unspaced "-isystem/usr/include" arguments elif flag . startswith ( '-isystem' ) : val = flag [ 8 : ] . strip ( ) flag = flag [ : 8 ] else : # This is a flag, and it's foreign to NVCC. # # We do not know whether this GCC-speak flag takes an isolated # argument. Assuming it does not (the vast majority indeed don't), # wrap this argument in an -Xcompiler flag and send it down to NVCC. if flag == '-ffast-math' : xflags . append ( '-use_fast_math' ) xflags . append ( '-Xcompiler=' + flag ) elif flag == '-fno-fast-math' : xflags . append ( '-ftz=false' ) xflags . append ( '-prec-div=true' ) xflags . append ( '-prec-sqrt=true' ) xflags . append ( '-Xcompiler=' + flag ) elif flag == '-freciprocal-math' : xflags . append ( '-prec-div=false' ) xflags . append ( '-Xcompiler=' + flag ) elif flag == '-fno-reciprocal-math' : xflags . append ( '-prec-div=true' ) xflags . append ( '-Xcompiler=' + flag ) else : xflags . append ( '-Xcompiler=' + self . _shield_nvcc_list_arg ( flag ) ) # The above should securely handle GCC's -Wl, -Wa, -Wp, arguments. continue assert val is not None # Should only trip if there is a missing argument. # Take care of the various NVCC-supported flags that need special handling. flag = self . _FLAG_LONG2SHORT_WITHARGS . get ( flag , flag ) if flag in { '-include' , '-isystem' , '-I' , '-L' , '-l' } : # These flags are known to GCC, but list-valued in NVCC. They potentially # require double-quoting to prevent NVCC interpreting the flags as lists # when GCC would not have done so. # # We avoid doing this quoting for -D to avoid redefining macros and for # -U because it isn't possible to define a macro with a comma in the name. # -U with comma arguments is impossible in GCC-speak (and thus unambiguous #in NVCC-speak, albeit unportable). if len ( flag ) == 2 : xflags . append ( flag + self . _shield_nvcc_list_arg ( val ) ) elif flag == '-isystem' and val in self . host_compiler . get_default_include_dirs ( ) : # like GnuLikeCompiler, we have to filter out include directories specified # with -isystem that overlap with the host compiler's search path pass else : xflags . append ( flag ) xflags . append ( self . _shield_nvcc_list_arg ( val ) ) elif flag == '-O' : # Handle optimization levels GCC knows about that NVCC does not. if val == 'fast' : xflags . append ( '-O3' ) xflags . append ( '-use_fast_math' ) xflags . append ( '-Xcompiler' ) xflags . append ( flag + val ) elif val in { 's' , 'g' , 'z' } : xflags . append ( '-Xcompiler' ) xflags . append ( flag + val ) else : xflags . append ( flag + val ) elif flag in { '-D' , '-U' , '-m' , '-t' } : xflags . append ( flag + val ) # For style, keep glued. elif flag in { '-std' } : xflags . append ( flag + '=' + val ) # For style, keep glued. else : xflags . append ( flag ) xflags . append ( val ) return self . _merge_flags ( xflags )