github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/gpus/crosstool/windows/msvc_wrapper_for_nvcc.py.tpl (about) 1 #!/usr/bin/env python 2 # Copyright 2015 The TensorFlow Authors. All Rights Reserved. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # ============================================================================== 16 17 """Crosstool wrapper for compiling CUDA programs with nvcc on Windows. 18 19 DESCRIPTION: 20 This script is the Windows version of //third_party/gpus/crosstool/crosstool_wrapper_is_not_gcc 21 """ 22 23 from __future__ import print_function 24 25 from argparse import ArgumentParser 26 import os 27 import subprocess 28 import re 29 import sys 30 import pipes 31 import tempfile 32 33 # Template values set by cuda_autoconf. 34 CPU_COMPILER = ('%{cpu_compiler}') 35 GCC_HOST_COMPILER_PATH = ('%{gcc_host_compiler_path}') 36 37 NVCC_PATH = '%{nvcc_path}' 38 NVCC_VERSION = '%{cuda_version}' 39 NVCC_TEMP_DIR = "%{nvcc_tmp_dir}" 40 supported_cuda_compute_capabilities = [ %{cuda_compute_capabilities} ] 41 42 def Log(s): 43 print('gpus/crosstool: {0}'.format(s)) 44 45 46 def GetOptionValue(argv, option): 47 """Extract the list of values for option from options. 48 49 Args: 50 option: The option whose value to extract, without the leading '/'. 51 52 Returns: 53 1. A list of values, either directly following the option, 54 (eg., /opt val1 val2) or values collected from multiple occurrences of 55 the option (eg., /opt val1 /opt val2). 56 2. The leftover options. 57 """ 58 59 parser = ArgumentParser(prefix_chars='/') 60 parser.add_argument('/' + option, nargs='*', action='append') 61 args, leftover = parser.parse_known_args(argv) 62 if args and vars(args)[option]: 63 return (sum(vars(args)[option], []), leftover) 64 return ([], leftover) 65 66 def _update_options(nvcc_options): 67 if NVCC_VERSION in ("7.0",): 68 return nvcc_options 69 70 update_options = { "relaxed-constexpr" : "expt-relaxed-constexpr" } 71 return [ update_options[opt] if opt in update_options else opt 72 for opt in nvcc_options ] 73 74 def GetNvccOptions(argv): 75 """Collect the -nvcc_options values from argv. 76 77 Args: 78 argv: A list of strings, possibly the argv passed to main(). 79 80 Returns: 81 1. The string that can be passed directly to nvcc. 82 2. The leftover options. 83 """ 84 85 parser = ArgumentParser() 86 parser.add_argument('-nvcc_options', nargs='*', action='append') 87 88 args, leftover = parser.parse_known_args(argv) 89 90 if args.nvcc_options: 91 options = _update_options(sum(args.nvcc_options, [])) 92 return (['--' + a for a in options], leftover) 93 return ([], leftover) 94 95 96 def InvokeNvcc(argv, log=False): 97 """Call nvcc with arguments assembled from argv. 98 99 Args: 100 argv: A list of strings, possibly the argv passed to main(). 101 log: True if logging is requested. 102 103 Returns: 104 The return value of calling os.system('nvcc ' + args) 105 """ 106 107 src_files = [f for f in argv if 108 re.search('\.cpp$|\.cc$|\.c$|\.cxx$|\.C$', f)] 109 if len(src_files) == 0: 110 raise Error('No source files found for cuda compilation.') 111 112 out_file = [ f for f in argv if f.startswith('/Fo') ] 113 if len(out_file) != 1: 114 raise Error('Please sepecify exactly one output file for cuda compilation.') 115 out = ['-o', out_file[0][len('/Fo'):]] 116 117 nvcc_compiler_options, argv = GetNvccOptions(argv) 118 119 opt_option, argv = GetOptionValue(argv, 'O') 120 opt = ['-g', '-G'] 121 if (len(opt_option) > 0 and opt_option[0] != 'd'): 122 opt = ['-O2'] 123 124 include_options, argv = GetOptionValue(argv, 'I') 125 includes = ["-I " + include for include in include_options] 126 127 defines, argv = GetOptionValue(argv, 'D') 128 defines = ['-D' + define for define in defines] 129 130 undefines, argv = GetOptionValue(argv, 'U') 131 undefines = ['-U' + define for define in undefines] 132 133 # The rest of the unrecongized options should be passed to host compiler 134 host_compiler_options = [option for option in argv if option not in (src_files + out_file)] 135 136 m_options = ["-m64"] 137 138 nvccopts = ['-D_FORCE_INLINES'] 139 for capability in supported_cuda_compute_capabilities: 140 capability = capability.replace('.', '') 141 nvccopts += [r'-gencode=arch=compute_%s,"code=sm_%s,compute_%s"' % ( 142 capability, capability, capability)] 143 nvccopts += nvcc_compiler_options 144 nvccopts += undefines 145 nvccopts += defines 146 nvccopts += m_options 147 nvccopts += ['--compiler-options="' + " ".join(host_compiler_options) + '"'] 148 nvccopts += ['-x', 'cu'] + opt + includes + out + ['-c'] + src_files 149 # Specify an unique temp directory for nvcc to generate intermediate files, 150 # then Bazel can ignore files under NVCC_TEMP_DIR during dependency check 151 # http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#options-for-guiding-compiler-driver 152 # Different actions are sharing NVCC_TEMP_DIR, so we cannot remove it if the directory already exists. 153 if os.path.isfile(NVCC_TEMP_DIR): 154 os.remove(NVCC_TEMP_DIR) 155 if not os.path.exists(NVCC_TEMP_DIR): 156 os.makedirs(NVCC_TEMP_DIR) 157 # Provide an unique dir for each compiling action to avoid conflicts. 158 tempdir = tempfile.mkdtemp(dir = NVCC_TEMP_DIR) 159 nvccopts += ['--keep', '--keep-dir', tempdir] 160 cmd = [NVCC_PATH] + nvccopts 161 if log: 162 Log(cmd) 163 proc = subprocess.Popen(cmd, 164 stdout=sys.stdout, 165 stderr=sys.stderr, 166 env=os.environ.copy(), 167 shell=True) 168 proc.wait() 169 return proc.returncode 170 171 def main(): 172 parser = ArgumentParser() 173 parser.add_argument('-x', nargs=1) 174 parser.add_argument('--cuda_log', action='store_true') 175 args, leftover = parser.parse_known_args(sys.argv[1:]) 176 177 if args.x and args.x[0] == 'cuda': 178 if args.cuda_log: Log('-x cuda') 179 leftover = [pipes.quote(s) for s in leftover] 180 if args.cuda_log: Log('using nvcc') 181 return InvokeNvcc(leftover, log=args.cuda_log) 182 183 # Strip our flags before passing through to the CPU compiler for files which 184 # are not -x cuda. We can't just pass 'leftover' because it also strips -x. 185 # We not only want to pass -x to the CPU compiler, but also keep it in its 186 # relative location in the argv list (the compiler is actually sensitive to 187 # this). 188 cpu_compiler_flags = [flag for flag in sys.argv[1:] 189 if not flag.startswith(('--cuda_log')) 190 and not flag.startswith(('-nvcc_options'))] 191 192 return subprocess.call([CPU_COMPILER] + cpu_compiler_flags) 193 194 if __name__ == '__main__': 195 sys.exit(main())