/[escript]/trunk/cusplibrary/build/nvcc.py
ViewVC logotype

Contents of /trunk/cusplibrary/build/nvcc.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5706 - (show annotations)
Mon Jun 29 03:41:36 2015 UTC (4 years, 2 months ago) by sshaw
File MIME type: text/x-python
File size: 4635 byte(s)
all python files now force use of python3 prints and division syntax to stop sneaky errors appearing in py3 environs
1 """SCons.Tool.nvcc
2
3 Tool-specific initialization for NVIDIA CUDA Compiler.
4
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
7 selection method.
8
9 """
10 from __future__ import print_function, division
11
12
13 import SCons.Tool
14 import SCons.Scanner.C
15 import SCons.Defaults
16 import os
17 import platform
18
19
20 def get_cuda_paths():
21 """Determines CUDA {bin,lib,include} paths
22
23 returns (bin_path,lib_path,inc_path)
24 """
25
26 # determine defaults
27 if os.name == 'nt':
28 bin_path = 'C:/CUDA/bin'
29 lib_path = 'C:/CUDA/lib'
30 inc_path = 'C:/CUDA/include'
31 elif os.name == 'posix':
32 bin_path = '/usr/local/cuda/bin'
33 lib_path = '/usr/local/cuda/lib'
34 inc_path = '/usr/local/cuda/include'
35 else:
36 raise ValueError, 'Error: unknown OS. Where is nvcc installed?'
37
38 if platform.machine()[-2:] == '64':
39 lib_path += '64'
40
41 # override with environement variables
42 if 'CUDA_BIN_PATH' in os.environ:
43 bin_path = os.path.abspath(os.environ['CUDA_BIN_PATH'])
44 if 'CUDA_LIB_PATH' in os.environ:
45 lib_path = os.path.abspath(os.environ['CUDA_LIB_PATH'])
46 if 'CUDA_INC_PATH' in os.environ:
47 inc_path = os.path.abspath(os.environ['CUDA_INC_PATH'])
48
49 return (bin_path,lib_path,inc_path)
50
51
52
53 CUDASuffixes = ['.cu']
54
55 # make a CUDAScanner for finding #includes
56 # cuda uses the c preprocessor, so we can use the CScanner
57 CUDAScanner = SCons.Scanner.C.CScanner()
58
59 def add_common_nvcc_variables(env):
60 """
61 Add underlying common "NVIDIA CUDA compiler" variables that
62 are used by multiple builders.
63 """
64
65 # "NVCC common command line"
66 if not env.has_key('_NVCCCOMCOM'):
67 # nvcc needs '-I' prepended before each include path, regardless of platform
68 env['_NVCCWRAPCPPPATH'] = '${_concat("-I ", CPPPATH, "", __env__)}'
69 # prepend -Xcompiler before each flag
70 env['_NVCCWRAPCFLAGS'] = '${_concat("-Xcompiler ", CFLAGS, "", __env__)}'
71 env['_NVCCWRAPSHCFLAGS'] = '${_concat("-Xcompiler ", SHCFLAGS, "", __env__)}'
72 env['_NVCCWRAPCCFLAGS'] = '${_concat("-Xcompiler ", CCFLAGS, "", __env__)}'
73 env['_NVCCWRAPSHCCFLAGS'] = '${_concat("-Xcompiler ", SHCCFLAGS, "", __env__)}'
74 # assemble the common command line
75 env['_NVCCCOMCOM'] = '${_concat("-Xcompiler ", CPPFLAGS, "", __env__)} $_CPPDEFFLAGS $_NVCCWRAPCPPPATH'
76
77 def generate(env):
78 """
79 Add Builders and construction variables for CUDA compilers to an Environment.
80 """
81
82 # create a builder that makes PTX files from .cu files
83 ptx_builder = SCons.Builder.Builder(action = '$NVCC -ptx $NVCCFLAGS $_NVCCWRAPCFLAGS $NVCCWRAPCCFLAGS $_NVCCCOMCOM $SOURCES -o $TARGET',
84 emitter = {},
85 suffix = '.ptx',
86 src_suffix = CUDASuffixes)
87 env['BUILDERS']['PTXFile'] = ptx_builder
88
89 # create builders that make static & shared objects from .cu files
90 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
91
92 for suffix in CUDASuffixes:
93 # Add this suffix to the list of things buildable by Object
94 static_obj.add_action('$CUDAFILESUFFIX', '$NVCCCOM')
95 shared_obj.add_action('$CUDAFILESUFFIX', '$SHNVCCCOM')
96 static_obj.add_emitter(suffix, SCons.Defaults.StaticObjectEmitter)
97 shared_obj.add_emitter(suffix, SCons.Defaults.SharedObjectEmitter)
98
99 # Add this suffix to the list of things scannable
100 SCons.Tool.SourceFileScanner.add_scanner(suffix, CUDAScanner)
101
102 add_common_nvcc_variables(env)
103
104 # set the "CUDA Compiler Command" environment variable
105 # windows is picky about getting the full filename of the executable
106 if os.name == 'nt':
107 env['NVCC'] = 'nvcc.exe'
108 env['SHNVCC'] = 'nvcc.exe'
109 else:
110 env['NVCC'] = 'nvcc'
111 env['SHNVCC'] = 'nvcc'
112
113 # set the include path, and pass both c compiler flags and c++ compiler flags
114 env['NVCCFLAGS'] = SCons.Util.CLVar('')
115 env['SHNVCCFLAGS'] = SCons.Util.CLVar('') + ' -shared'
116
117 # 'NVCC Command'
118 env['NVCCCOM'] = '$NVCC -ccbin=g++-4.8 -o $TARGET -c $NVCCFLAGS $_NVCCWRAPCFLAGS $NVCCWRAPCCFLAGS $_NVCCCOMCOM $SOURCES'
119 env['SHNVCCCOM'] = '$SHNVCC -ccbin=g++-4.8 -o $TARGET -c $SHNVCCFLAGS $_NVCCWRAPSHCFLAGS $_NVCCWRAPSHCCFLAGS $_NVCCCOMCOM $SOURCES'
120
121 # the suffix of CUDA source files is '.cu'
122 env['CUDAFILESUFFIX'] = '.cu'
123
124 # XXX add code to generate builders for other miscellaneous
125 # CUDA files here, such as .gpu, etc.
126
127 # XXX intelligently detect location of nvcc and cuda libraries here
128 (bin_path,lib_path,inc_path) = get_cuda_paths()
129
130 env.PrependENVPath('PATH', bin_path)
131
132 def exists(env):
133 return env.Detect('nvcc')
134

  ViewVC Help
Powered by ViewVC 1.1.26