configure: Provide symlink for amdgcn-mesa3d instead of configure hack
[platform/upstream/llvm.git] / libclc / configure.py
1 #!/usr/bin/python
2 from __future__ import print_function
3
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")
7
8 version_major = 0;
9 version_minor = 2;
10 version_patch = 0;
11
12 from optparse import OptionParser
13 import os
14 import string
15 from subprocess import *
16 import sys
17
18 srcdir = os.path.dirname(sys.argv[0])
19
20 sys.path.insert(0, os.path.join(srcdir, 'build'))
21 import metabuild
22
23 p = OptionParser()
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()
41
42 llvm_config_exe = options.with_llvm_config or "llvm-config"
43
44 prefix = options.prefix
45 if not prefix:
46   prefix = '/usr/local'
47
48 libexecdir = options.libexecdir
49 if not libexecdir:
50   libexecdir = os.path.join(prefix, 'lib/clc')
51
52 includedir = options.includedir
53 if not includedir:
54   includedir = os.path.join(prefix, 'include')
55
56 pkgconfigdir = options.pkgconfigdir
57 if not pkgconfigdir:
58   pkgconfigdir = os.path.join(prefix, 'share/pkgconfig')
59
60 def llvm_config(args):
61   try:
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', ' ')
66   except OSError:
67     print("Error executing llvm-config.")
68     print("Please ensure that llvm-config is in your $PATH, or use --with-llvm-config.")
69     sys.exit(1)
70
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]
74
75 if llvm_int_version < 390:
76     print("libclc requires LLVM >= 3.9")
77     sys.exit(1)
78
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'])
87
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')
91
92 cxx_compiler = options.with_cxx_compiler
93 if not cxx_compiler:
94   cxx_compiler = os.path.join(llvm_bindir, 'clang++')
95
96 available_targets = {
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' : []} ]},
110 }
111
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']
116
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']
121
122
123 default_targets = ['nvptx--nvidiacl', 'nvptx64--nvidiacl', 'r600--', 'amdgcn--', 'amdgcn--amdhsa']
124
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')
129
130 targets = args
131 if not targets:
132   targets = default_targets
133
134 b = metabuild.from_name(options.g)
135
136 b.rule("LLVM_AS", "%s -o $out $in" % os.path.join(llvm_bindir, "llvm-as"),
137        'LLVM-AS $out')
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')
142
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')
145
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'))
151
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'])
156
157 manifest_deps = set([sys.argv[0], os.path.join(srcdir, 'build', 'metabuild.py'),
158                      os.path.join(srcdir, 'build', 'ninja_syntax.py')])
159
160 install_files_bc = []
161 install_deps = []
162
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)
169   b.default(obj)
170   install_files_bc.append((obj, obj))
171   install_deps.append(obj)
172
173 # Create libclc.pc
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})
177 clc.close()
178
179 for target in targets:
180   (t_arch, t_vendor, t_os) = target.split('-')
181   archs = [t_arch]
182   if t_arch == 'nvptx' or t_arch == 'nvptx64':
183     archs.append('ptx')
184   archs.append('generic')
185
186   subdirs = []
187   for arch in archs:
188     subdirs.append("%s-%s-%s" % (arch, t_vendor, t_os))
189     subdirs.append("%s-%s" % (arch, t_os))
190     subdirs.append(arch)
191     if arch == 'amdgcn' or arch == 'r600':
192         subdirs.append('amdgpu')
193
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])
199
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])
205
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 " \
209                      "-fno-builtin " \
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)
216
217     objects = []
218     sources_seen = set()
219     compats = []
220
221     if device['gpu'] == '':
222       full_target_name = target
223       obj_suffix = ''
224     else:
225       full_target_name = device['gpu'] + '-' + target
226       obj_suffix = '.' + device['gpu']
227
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)
237
238       # Build compat list
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)
244
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)
250
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)
256
257       files = open(subdir_list_file).readlines() if os.path.exists(subdir_list_file) else []
258       for src in files + compats:
259         src = src.rstrip()
260         if src not in sources_seen:
261           sources_seen.add(src)
262           obj = os.path.join(target, 'lib', src + obj_suffix + '.bc')
263           objects.append(obj)
264           src_path = libdir
265           src_file = os.path.join(src_path, src)
266           ext = os.path.splitext(src)[1]
267           if ext == '.ll':
268             b.build(obj, 'LLVM_AS', src_file)
269           else:
270             b.build(obj, clang_bc_rule, src_file)
271
272     obj = os.path.join('generic--', 'lib', 'subnormal_use_default.bc')
273     if  not options.enable_runtime_subnormal:
274       objects.append(obj)
275
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")
289
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)
295
296
297 install_cmd = ' && '.join(['mkdir -p ${DESTDIR}/%(dst)s && cp -r %(src)s ${DESTDIR}/%(dst)s' %
298                            {'src': file,
299                             'dst': libexecdir}
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' %
302                            {'old': install_cmd,
303                             'dst': includedir,
304                             'srcdir': srcdir}])
305 install_cmd = ' && '.join(['%(old)s && mkdir -p ${DESTDIR}/%(dst)s && cp -r libclc.pc ${DESTDIR}/%(dst)s' %
306                            {'old': install_cmd, 
307                             'dst': pkgconfigdir}])
308   
309 b.rule('install', command = install_cmd, description = 'INSTALL')
310 b.build('install', 'install', install_deps)
311
312 b.rule("configure", command = ' '.join(sys.argv), description = 'CONFIGURE',
313        generator = True)
314 b.build(b.output_filename(), 'configure', list(manifest_deps))
315
316 b.finish()