2 from __future__
import print_function
4 def c_compiler_rule(b
, name
, description
, compiler
, flags
):
5 command
= "%s -MMD -MF $out.d %s -c -o $out $in" % (compiler
, flags
)
6 b
.rule(name
, command
, description
+ " $out", depfile
="$out.d")
12 from optparse
import OptionParser
15 from subprocess
import *
18 srcdir
= os
.path
.dirname(sys
.argv
[0])
20 sys
.path
.insert(0, os
.path
.join(srcdir
, 'build'))
24 p
.add_option('--with-llvm-config', metavar
='PATH',
25 help='use given llvm-config script')
26 p
.add_option('--with-cxx-compiler', metavar
='PATH',
27 help='use given C++ compiler')
28 p
.add_option('--prefix', metavar
='PATH',
29 help='install to given prefix')
30 p
.add_option('--libexecdir', metavar
='PATH',
31 help='install *.bc to given dir')
32 p
.add_option('--includedir', metavar
='PATH',
33 help='install include files to given dir')
34 p
.add_option('--pkgconfigdir', metavar
='PATH',
35 help='install clc.pc to given dir')
36 p
.add_option('-g', metavar
='GENERATOR', default
='make',
37 help='use given generator (default: make)')
38 p
.add_option('--enable-runtime-subnormal', action
="store_true", default
=False,
39 help='Allow runtimes to choose subnormal support')
40 (options
, args
) = p
.parse_args()
42 llvm_config_exe
= options
.with_llvm_config
or "llvm-config"
44 prefix
= options
.prefix
48 libexecdir
= options
.libexecdir
50 libexecdir
= os
.path
.join(prefix
, 'lib/clc')
52 includedir
= options
.includedir
54 includedir
= os
.path
.join(prefix
, 'include')
56 pkgconfigdir
= options
.pkgconfigdir
58 pkgconfigdir
= os
.path
.join(prefix
, 'share/pkgconfig')
60 def llvm_config(args
):
62 # Universal newlines translate different newline formats to '\n'
63 # it also force the input to be string instead of bytes in python 3
64 proc
= Popen([llvm_config_exe
] + args
, stdout
=PIPE
, universal_newlines
=True)
65 return proc
.communicate()[0].rstrip().replace('\n', ' ')
67 print("Error executing llvm-config.")
68 print("Please ensure that llvm-config is in your $PATH, or use --with-llvm-config.")
71 llvm_version
= llvm_config(['--version']).replace('svn', '').split('.')
72 llvm_int_version
= int(llvm_version
[0]) * 100 + int(llvm_version
[1]) * 10
73 llvm_string_version
= llvm_version
[0] + '.' + llvm_version
[1]
75 if llvm_int_version
< 390:
76 print("libclc requires LLVM >= 3.9")
79 llvm_system_libs
= llvm_config(['--system-libs'])
80 llvm_bindir
= llvm_config(['--bindir'])
81 llvm_core_libs
= llvm_config(['--libs', 'core', 'bitreader', 'bitwriter']) + ' ' + \
82 llvm_system_libs
+ ' ' + \
83 llvm_config(['--ldflags'])
84 llvm_cxxflags
= llvm_config(['--cxxflags']) + ' -fno-exceptions -fno-rtti ' + \
85 '-DHAVE_LLVM=0x{:0=4}'.format(llvm_int_version
)
86 llvm_libdir
= llvm_config(['--libdir'])
88 llvm_clang
= os
.path
.join(llvm_bindir
, 'clang')
89 llvm_link
= os
.path
.join(llvm_bindir
, 'llvm-link')
90 llvm_opt
= os
.path
.join(llvm_bindir
, 'opt')
92 cxx_compiler
= options
.with_cxx_compiler
94 cxx_compiler
= os
.path
.join(llvm_bindir
, 'clang++')
97 'r600--' : { 'devices' :
98 [{'gpu' : 'cedar', 'aliases' : ['palm', 'sumo', 'sumo2', 'redwood', 'juniper']},
99 {'gpu' : 'cypress', 'aliases' : ['hemlock'] },
100 {'gpu' : 'barts', 'aliases' : ['turks', 'caicos'] },
101 {'gpu' : 'cayman', 'aliases' : ['aruba']} ]},
102 'amdgcn--': { 'devices' :
103 [{'gpu' : 'tahiti', 'aliases' : ['pitcairn', 'verde', 'oland', 'hainan', 'bonaire', 'kabini', 'kaveri', 'hawaii','mullins','tonga','carrizo','iceland','fiji','stoney','polaris10','polaris11']} ]},
104 'amdgcn--amdhsa': { 'devices' :
105 [{'gpu' : '', 'aliases' : ['bonaire', 'hawaii', 'kabini', 'kaveri', 'mullins', 'carrizo', 'stoney', 'fiji', 'iceland', 'tonga','polaris10','polaris11']} ]},
106 'nvptx--' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
107 'nvptx64--' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
108 'nvptx--nvidiacl' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
109 'nvptx64--nvidiacl' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
113 default_targets
= ['nvptx--nvidiacl', 'nvptx64--nvidiacl', 'r600--', 'amdgcn--', 'amdgcn--amdhsa']
115 #mesa is using amdgcn-mesa-mesa3d since llvm-4.0
116 if llvm_int_version
> 390:
117 available_targets
['amdgcn-mesa-mesa3d'] = available_targets
['amdgcn--']
118 default_targets
.append('amdgcn-mesa-mesa3d')
122 targets
= default_targets
124 b
= metabuild
.from_name(options
.g
)
126 b
.rule("LLVM_AS", "%s -o $out $in" % os
.path
.join(llvm_bindir
, "llvm-as"),
128 b
.rule("LLVM_LINK", command
= llvm_link
+ " -o $out $in",
129 description
= 'LLVM-LINK $out')
130 b
.rule("OPT", command
= llvm_opt
+ " -O3 -o $out $in",
131 description
= 'OPT $out')
133 c_compiler_rule(b
, "LLVM_TOOL_CXX", 'CXX', cxx_compiler
, llvm_cxxflags
)
134 b
.rule("LLVM_TOOL_LINK", cxx_compiler
+ " -o $out $in %s" % llvm_core_libs
+ " -Wl,-rpath %s" % llvm_libdir
, 'LINK $out')
136 prepare_builtins
= os
.path
.join('utils', 'prepare-builtins')
137 b
.build(os
.path
.join('utils', 'prepare-builtins.o'), "LLVM_TOOL_CXX",
138 os
.path
.join(srcdir
, 'utils', 'prepare-builtins.cpp'))
139 b
.build(prepare_builtins
, "LLVM_TOOL_LINK",
140 os
.path
.join('utils', 'prepare-builtins.o'))
142 b
.rule("PREPARE_BUILTINS", "%s -o $out $in" % prepare_builtins
,
143 'PREPARE-BUILTINS $out')
144 b
.rule("PYTHON_GEN", "python < $in > $out", "PYTHON_GEN $out")
145 b
.build('generic/lib/convert.cl', "PYTHON_GEN", ['generic/lib/gen_convert.py'])
147 manifest_deps
= set([sys
.argv
[0], os
.path
.join(srcdir
, 'build', 'metabuild.py'),
148 os
.path
.join(srcdir
, 'build', 'ninja_syntax.py')])
150 install_files_bc
= []
153 # Create rules for subnormal helper objects
154 for src
in ['subnormal_disable.ll', 'subnormal_use_default.ll']:
155 obj_name
= src
[:-2] + 'bc'
156 obj
= os
.path
.join('generic--', 'lib', obj_name
)
157 src_file
= os
.path
.join('generic', 'lib', src
)
158 b
.build(obj
, 'LLVM_AS', src_file
)
160 install_files_bc
.append((obj
, obj
))
161 install_deps
.append(obj
)
164 clc
= open('libclc.pc', 'w')
165 clc
.write('includedir=%(inc)s\nlibexecdir=%(lib)s\n\nName: libclc\nDescription: Library requirements of the OpenCL C programming language\nVersion: %(maj)s.%(min)s.%(pat)s\nCflags: -I${includedir}\nLibs: -L${libexecdir}' %
166 {'inc': includedir
, 'lib': libexecdir
, 'maj': version_major
, 'min': version_minor
, 'pat': version_patch
})
169 for target
in targets
:
170 (t_arch
, t_vendor
, t_os
) = target
.split('-')
172 if t_arch
== 'nvptx' or t_arch
== 'nvptx64':
174 archs
.append('generic')
178 subdirs
.append("%s-%s-%s" % (arch
, t_vendor
, t_os
))
179 subdirs
.append("%s-%s" % (arch
, t_os
))
181 subdirs
.append('amdgcn-amdhsa')
183 if arch
== 'amdgcn' or arch
== 'r600':
184 subdirs
.append('amdgpu')
186 incdirs
= filter(os
.path
.isdir
,
187 [os
.path
.join(srcdir
, subdir
, 'include') for subdir
in subdirs
])
188 libdirs
= filter(lambda d
: os
.path
.isfile(os
.path
.join(d
, 'SOURCES')) or
189 os
.path
.isfile(os
.path
.join(d
, 'SOURCES_' + llvm_string_version
)),
190 [os
.path
.join(srcdir
, subdir
, 'lib') for subdir
in subdirs
])
192 # The above are iterables in python3 but we might use them multiple times
193 # if more then one device is supported.
194 incdirs
= list(incdirs
)
195 libdirs
= list(libdirs
)
196 clang_cl_includes
= ' '.join(["-I%s" % incdir
for incdir
in incdirs
])
198 for device
in available_targets
[target
]['devices']:
199 # The rule for building a .bc file for the specified architecture using clang.
200 clang_bc_flags
= "-target %s -I`dirname $in` %s " \
202 "-D__CLC_INTERNAL " \
203 "-emit-llvm" % (target
, clang_cl_includes
)
204 if device
['gpu'] != '':
205 clang_bc_flags
+= ' -mcpu=' + device
['gpu']
206 clang_bc_rule
= "CLANG_CL_BC_" + target
+ "_" + device
['gpu']
207 c_compiler_rule(b
, clang_bc_rule
, "LLVM-CC", llvm_clang
, clang_bc_flags
)
213 if device
['gpu'] == '':
214 full_target_name
= target
217 full_target_name
= device
['gpu'] + '-' + target
218 obj_suffix
= '.' + device
['gpu']
220 for libdir
in libdirs
:
221 subdir_list_file
= os
.path
.join(libdir
, 'SOURCES')
222 if os
.path
.exists(subdir_list_file
):
223 manifest_deps
.add(subdir_list_file
)
224 override_list_file
= os
.path
.join(libdir
, 'OVERRIDES')
225 compat_list_file
= os
.path
.join(libdir
,
226 'SOURCES_' + llvm_string_version
)
227 compat_list_override
= os
.path
.join(libdir
,
228 'OVERRIDES_' + llvm_string_version
)
231 if os
.path
.exists(compat_list_file
):
232 manifest_deps
.add(compat_list_file
)
233 for compat
in open(compat_list_file
).readlines():
234 compat
= compat
.rstrip()
235 compats
.append(compat
)
237 # Add target compat overrides
238 if os
.path
.exists(compat_list_override
):
239 for override
in open(compat_list_override
).readlines():
240 override
= override
.rstrip()
241 sources_seen
.add(override
)
243 # Add target overrides
244 if os
.path
.exists(override_list_file
):
245 for override
in open(override_list_file
).readlines():
246 override
= override
.rstrip()
247 sources_seen
.add(override
)
249 files
= open(subdir_list_file
).readlines() if os
.path
.exists(subdir_list_file
) else []
250 for src
in files
+ compats
:
252 if src
not in sources_seen
:
253 sources_seen
.add(src
)
254 obj
= os
.path
.join(target
, 'lib', src
+ obj_suffix
+ '.bc')
257 src_file
= os
.path
.join(src_path
, src
)
258 ext
= os
.path
.splitext(src
)[1]
260 b
.build(obj
, 'LLVM_AS', src_file
)
262 b
.build(obj
, clang_bc_rule
, src_file
)
264 obj
= os
.path
.join('generic--', 'lib', 'subnormal_use_default.bc')
265 if not options
.enable_runtime_subnormal
:
268 builtins_link_bc
= os
.path
.join(target
, 'lib', 'builtins.link' + obj_suffix
+ '.bc')
269 builtins_opt_bc
= os
.path
.join(target
, 'lib', 'builtins.opt' + obj_suffix
+ '.bc')
270 builtins_bc
= os
.path
.join('built_libs', full_target_name
+ '.bc')
271 b
.build(builtins_link_bc
, "LLVM_LINK", objects
)
272 b
.build(builtins_opt_bc
, "OPT", builtins_link_bc
)
273 b
.build(builtins_bc
, "PREPARE_BUILTINS", builtins_opt_bc
, prepare_builtins
)
274 install_files_bc
.append((builtins_bc
, builtins_bc
))
275 install_deps
.append(builtins_bc
)
276 for alias
in device
['aliases']:
277 # Ninja cannot have multiple rules with same name so append suffix
278 ruleName
= "CREATE_ALIAS_{0}_for_{1}".format(alias
, device
['gpu'])
279 b
.rule(ruleName
, "ln -fs %s $out" % os
.path
.basename(builtins_bc
)
280 ,"CREATE-ALIAS $out")
282 alias_file
= os
.path
.join('built_libs', alias
+ '-' + target
+ '.bc')
283 b
.build(alias_file
, ruleName
, builtins_bc
)
284 install_files_bc
.append((alias_file
, alias_file
))
285 install_deps
.append(alias_file
)
286 b
.default(builtins_bc
)
289 install_cmd
= ' && '.join(['mkdir -p ${DESTDIR}/%(dst)s && cp -r %(src)s ${DESTDIR}/%(dst)s' %
292 for (file, dest
) in install_files_bc
])
293 install_cmd
= ' && '.join(['%(old)s && mkdir -p ${DESTDIR}/%(dst)s && cp -r %(srcdir)s/generic/include/clc ${DESTDIR}/%(dst)s' %
297 install_cmd
= ' && '.join(['%(old)s && mkdir -p ${DESTDIR}/%(dst)s && cp -r libclc.pc ${DESTDIR}/%(dst)s' %
299 'dst': pkgconfigdir
}])
301 b
.rule('install', command
= install_cmd
, description
= 'INSTALL')
302 b
.build('install', 'install', install_deps
)
304 b
.rule("configure", command
= ' '.join(sys
.argv
), description
= 'CONFIGURE',
306 b
.build(b
.output_filename(), 'configure', list(manifest_deps
))