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', 'iceland', 'carrizo', 'fiji', 'stoney', 'polaris10', 'polaris11']} ]},
104 'amdgcn--amdhsa': { 'devices' :
105 [{'gpu' : '', 'aliases' : ['bonaire', 'kabini', 'kaveri', 'hawaii', 'mullins', 'tonga', 'iceland', 'carrizo', 'fiji', 'stoney', 'polaris10', 'polaris11']} ]},
106 'nvptx--' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
107 'nvptx64--' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
108 'nvptx--nvidiacl' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
109 'nvptx64--nvidiacl' : { 'devices' : [{'gpu' : '', 'aliases' : []} ]},
112 # Support for gfx9 was added in LLVM 5 (r295554)
113 if llvm_int_version >= 500:
114 available_targets['amdgcn--']['devices'][0]['aliases'] += ['gfx900', 'gfx902']
115 available_targets['amdgcn--amdhsa']['devices'][0]['aliases'] += ['gfx900', 'gfx902']
117 # Support for Vega12 and Vega20 was added in LLVM 7 (r331215)
118 if llvm_int_version >= 700:
119 available_targets['amdgcn--']['devices'][0]['aliases'] += ['gfx904', 'gfx906']
120 available_targets['amdgcn--amdhsa']['devices'][0]['aliases'] += ['gfx904', 'gfx906']
123 default_targets = ['nvptx--nvidiacl', 'nvptx64--nvidiacl', 'r600--', 'amdgcn--', 'amdgcn--amdhsa']
125 #mesa is using amdgcn-mesa-mesa3d since llvm-4.0
126 if llvm_int_version > 390:
127 available_targets['amdgcn-mesa-mesa3d'] = available_targets['amdgcn--']
128 default_targets.append('amdgcn-mesa-mesa3d')
132 targets = default_targets
134 b = metabuild.from_name(options.g)
136 b.rule("LLVM_AS", "%s -o $out $in" % os.path.join(llvm_bindir, "llvm-as"),
138 b.rule("LLVM_LINK", command = llvm_link + " -o $out $in",
139 description = 'LLVM-LINK $out')
140 b.rule("OPT", command = llvm_opt + " -O3 -o $out $in",
141 description = 'OPT $out')
143 c_compiler_rule(b, "LLVM_TOOL_CXX", 'CXX', cxx_compiler, llvm_cxxflags)
144 b.rule("LLVM_TOOL_LINK", cxx_compiler + " -o $out $in %s" % llvm_core_libs + " -Wl,-rpath %s" % llvm_libdir, 'LINK $out')
146 prepare_builtins = os.path.join('utils', 'prepare-builtins')
147 b.build(os.path.join('utils', 'prepare-builtins.o'), "LLVM_TOOL_CXX",
148 os.path.join(srcdir, 'utils', 'prepare-builtins.cpp'))
149 b.build(prepare_builtins, "LLVM_TOOL_LINK",
150 os.path.join('utils', 'prepare-builtins.o'))
152 b.rule("PREPARE_BUILTINS", "%s -o $out $in" % prepare_builtins,
153 'PREPARE-BUILTINS $out')
154 b.rule("PYTHON_GEN", "python < $in > $out", "PYTHON_GEN $out")
155 b.build('generic/lib/convert.cl', "PYTHON_GEN", ['generic/lib/gen_convert.py'])
157 manifest_deps = set([sys.argv[0], os.path.join(srcdir, 'build', 'metabuild.py'),
158 os.path.join(srcdir, 'build', 'ninja_syntax.py')])
160 install_files_bc = []
163 # Create rules for subnormal helper objects
164 for src in ['subnormal_disable.ll', 'subnormal_use_default.ll']:
165 obj_name = src[:-2] + 'bc'
166 obj = os.path.join('generic--', 'lib', obj_name)
167 src_file = os.path.join('generic', 'lib', src)
168 b.build(obj, 'LLVM_AS', src_file)
170 install_files_bc.append((obj, obj))
171 install_deps.append(obj)
174 clc = open('libclc.pc', 'w')
175 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}' %
176 {'inc': includedir, 'lib': libexecdir, 'maj': version_major, 'min': version_minor, 'pat': version_patch})
179 for target in targets:
180 (t_arch, t_vendor, t_os) = target.split('-')
182 if t_arch == 'nvptx' or t_arch == 'nvptx64':
184 archs.append('generic')
188 subdirs.append("%s-%s-%s" % (arch, t_vendor, t_os))
189 subdirs.append("%s-%s" % (arch, t_os))
191 if arch == 'amdgcn' or arch == 'r600':
192 subdirs.append('amdgpu')
194 incdirs = filter(os.path.isdir,
195 [os.path.join(srcdir, subdir, 'include') for subdir in subdirs])
196 libdirs = filter(lambda d: os.path.isfile(os.path.join(d, 'SOURCES')) or
197 os.path.isfile(os.path.join(d, 'SOURCES_' + llvm_string_version)),
198 [os.path.join(srcdir, subdir, 'lib') for subdir in subdirs])
200 # The above are iterables in python3 but we might use them multiple times
201 # if more then one device is supported.
202 incdirs = list(incdirs)
203 libdirs = list(libdirs)
204 clang_cl_includes = ' '.join(["-I%s" % incdir for incdir in incdirs])
206 for device in available_targets[target]['devices']:
207 # The rule for building a .bc file for the specified architecture using clang.
208 clang_bc_flags = "-target %s -I`dirname $in` %s " \
210 "-D__CLC_INTERNAL " \
211 "-emit-llvm" % (target, clang_cl_includes)
212 if device['gpu'] != '':
213 clang_bc_flags += ' -mcpu=' + device['gpu']
214 clang_bc_rule = "CLANG_CL_BC_" + target + "_" + device['gpu']
215 c_compiler_rule(b, clang_bc_rule, "LLVM-CC", llvm_clang, clang_bc_flags)
221 if device['gpu'] == '':
222 full_target_name = target
225 full_target_name = device['gpu'] + '-' + target
226 obj_suffix = '.' + device['gpu']
228 for libdir in libdirs:
229 subdir_list_file = os.path.join(libdir, 'SOURCES')
230 if os.path.exists(subdir_list_file):
231 manifest_deps.add(subdir_list_file)
232 override_list_file = os.path.join(libdir, 'OVERRIDES')
233 compat_list_file = os.path.join(libdir,
234 'SOURCES_' + llvm_string_version)
235 compat_list_override = os.path.join(libdir,
236 'OVERRIDES_' + llvm_string_version)
239 if os.path.exists(compat_list_file):
240 manifest_deps.add(compat_list_file)
241 for compat in open(compat_list_file).readlines():
242 compat = compat.rstrip()
243 compats.append(compat)
245 # Add target compat overrides
246 if os.path.exists(compat_list_override):
247 for override in open(compat_list_override).readlines():
248 override = override.rstrip()
249 sources_seen.add(override)
251 # Add target overrides
252 if os.path.exists(override_list_file):
253 for override in open(override_list_file).readlines():
254 override = override.rstrip()
255 sources_seen.add(override)
257 files = open(subdir_list_file).readlines() if os.path.exists(subdir_list_file) else []
258 for src in files + compats:
260 if src not in sources_seen:
261 sources_seen.add(src)
262 obj = os.path.join(target, 'lib', src + obj_suffix + '.bc')
265 src_file = os.path.join(src_path, src)
266 ext = os.path.splitext(src)[1]
268 b.build(obj, 'LLVM_AS', src_file)
270 b.build(obj, clang_bc_rule, src_file)
272 obj = os.path.join('generic--', 'lib', 'subnormal_use_default.bc')
273 if not options.enable_runtime_subnormal:
276 builtins_link_bc = os.path.join(target, 'lib', 'builtins.link' + obj_suffix + '.bc')
277 builtins_opt_bc = os.path.join(target, 'lib', 'builtins.opt' + obj_suffix + '.bc')
278 builtins_bc = os.path.join('built_libs', full_target_name + '.bc')
279 b.build(builtins_link_bc, "LLVM_LINK", objects)
280 b.build(builtins_opt_bc, "OPT", builtins_link_bc)
281 b.build(builtins_bc, "PREPARE_BUILTINS", builtins_opt_bc, prepare_builtins)
282 install_files_bc.append((builtins_bc, builtins_bc))
283 install_deps.append(builtins_bc)
284 for alias in device['aliases']:
285 # Ninja cannot have multiple rules with same name so append suffix
286 ruleName = "CREATE_ALIAS_{0}_for_{1}".format(alias, device['gpu'])
287 b.rule(ruleName, "ln -fs %s $out" % os.path.basename(builtins_bc)
288 ,"CREATE-ALIAS $out")
290 alias_file = os.path.join('built_libs', alias + '-' + target + '.bc')
291 b.build(alias_file, ruleName, builtins_bc)
292 install_files_bc.append((alias_file, alias_file))
293 install_deps.append(alias_file)
294 b.default(builtins_bc)
297 install_cmd = ' && '.join(['mkdir -p ${DESTDIR}/%(dst)s && cp -r %(src)s ${DESTDIR}/%(dst)s' %
300 for (file, dest) in install_files_bc])
301 install_cmd = ' && '.join(['%(old)s && mkdir -p ${DESTDIR}/%(dst)s && cp -r %(srcdir)s/generic/include/clc ${DESTDIR}/%(dst)s' %
305 install_cmd = ' && '.join(['%(old)s && mkdir -p ${DESTDIR}/%(dst)s && cp -r libclc.pc ${DESTDIR}/%(dst)s' %
307 'dst': pkgconfigdir}])
309 b.rule('install', command = install_cmd, description = 'INSTALL')
310 b.build('install', 'install', install_deps)
312 b.rule("configure", command = ' '.join(sys.argv), description = 'CONFIGURE',
314 b.build(b.output_filename(), 'configure', list(manifest_deps))