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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5148 - (show annotations)
Mon Sep 15 01:25:23 2014 UTC (6 years, 1 month ago) by caltinay
File MIME type: text/x-python
File size: 4586 byte(s)
Merging ripley diagonal storage + CUDA support into trunk.
Options file version has been incremented due to new options
'cuda' and 'nvccflags'.

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

  ViewVC Help
Powered by ViewVC 1.1.26