Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | def c_compiler_rule(b, name, description, compiler, flags): |
| 4 | command = "%s -MMD -MF $out.d %s -c -o $out $in" % (compiler, flags) |
| 5 | b.rule(name, command, description + " $out", depfile="$out.d") |
| 6 | |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 7 | version_major = 0; |
| 8 | version_minor = 0; |
| 9 | version_patch = 1; |
| 10 | |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 11 | from optparse import OptionParser |
| 12 | import os |
| 13 | from subprocess import * |
| 14 | import sys |
| 15 | |
| 16 | srcdir = os.path.dirname(sys.argv[0]) |
| 17 | |
| 18 | sys.path.insert(0, os.path.join(srcdir, 'build')) |
| 19 | import metabuild |
| 20 | |
| 21 | p = OptionParser() |
| 22 | p.add_option('--with-llvm-config', metavar='PATH', |
| 23 | help='use given llvm-config script') |
Tom Stellard | 9f48bb3 | 2013-10-23 02:49:27 | [diff] [blame] | 24 | p.add_option('--with-cxx-compiler', metavar='PATH', |
| 25 | help='use given C++ compiler') |
Peter Collingbourne | bae6833 | 2012-06-01 17:29:59 | [diff] [blame] | 26 | p.add_option('--prefix', metavar='PATH', |
| 27 | help='install to given prefix') |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 28 | p.add_option('--libexecdir', metavar='PATH', |
| 29 | help='install *.bc to given dir') |
| 30 | p.add_option('--includedir', metavar='PATH', |
| 31 | help='install include files to given dir') |
| 32 | p.add_option('--pkgconfigdir', metavar='PATH', |
| 33 | help='install clc.pc to given dir') |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 34 | p.add_option('-g', metavar='GENERATOR', default='make', |
| 35 | help='use given generator (default: make)') |
| 36 | (options, args) = p.parse_args() |
| 37 | |
| 38 | llvm_config_exe = options.with_llvm_config or "llvm-config" |
| 39 | |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 40 | prefix = options.prefix |
| 41 | if not prefix: |
| 42 | prefix = '/usr/local' |
| 43 | |
| 44 | libexecdir = options.libexecdir |
| 45 | if not libexecdir: |
| 46 | libexecdir = os.path.join(prefix, 'lib/clc') |
| 47 | |
| 48 | includedir = options.includedir |
| 49 | if not includedir: |
| 50 | includedir = os.path.join(prefix, 'include') |
| 51 | |
| 52 | pkgconfigdir = options.pkgconfigdir |
| 53 | if not pkgconfigdir: |
Tom Stellard | 93d674f | 2013-09-05 23:27:58 | [diff] [blame] | 54 | pkgconfigdir = os.path.join(prefix, 'share/pkgconfig') |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 55 | |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 56 | def llvm_config(args): |
| 57 | try: |
| 58 | proc = Popen([llvm_config_exe] + args, stdout=PIPE) |
| 59 | return proc.communicate()[0].rstrip().replace('\n', ' ') |
| 60 | except OSError: |
| 61 | print "Error executing llvm-config." |
| 62 | print "Please ensure that llvm-config is in your $PATH, or use --with-llvm-config." |
| 63 | sys.exit(1) |
| 64 | |
| 65 | llvm_bindir = llvm_config(['--bindir']) |
Peter Collingbourne | db47eac | 2012-12-05 07:39:02 | [diff] [blame] | 66 | llvm_core_libs = llvm_config(['--libs', 'core', 'bitreader', 'bitwriter']) + ' ' + \ |
| 67 | llvm_config(['--ldflags']) |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 68 | llvm_cxxflags = llvm_config(['--cxxflags']) + ' -fno-exceptions -fno-rtti' |
| 69 | |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 70 | llvm_clang = os.path.join(llvm_bindir, 'clang') |
| 71 | llvm_link = os.path.join(llvm_bindir, 'llvm-link') |
| 72 | llvm_opt = os.path.join(llvm_bindir, 'opt') |
| 73 | |
Tom Stellard | 9f48bb3 | 2013-10-23 02:49:27 | [diff] [blame] | 74 | cxx_compiler = options.with_cxx_compiler |
| 75 | if not cxx_compiler: |
| 76 | cxx_compiler = os.path.join(llvm_bindir, 'clang++') |
| 77 | |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 78 | available_targets = { |
| 79 | 'r600--' : { 'devices' : |
| 80 | [{'gpu' : 'cedar', 'aliases' : ['palm', 'sumo', 'sumo2', 'redwood', 'juniper']}, |
| 81 | {'gpu' : 'cypress', 'aliases' : ['hemlock']}, |
| 82 | {'gpu' : 'barts', 'aliases' : ['turks', 'caicos']}, |
| 83 | {'gpu' : 'cayman', 'aliases' : ['aruba']}, |
| 84 | {'gpu' : 'tahiti', 'aliases' : ['pitcairn', 'verde', 'oland']}]}, |
| 85 | 'nvptx--nvidiacl' : { 'devices' : [{'gpu' : '', 'aliases' : []}] }, |
| 86 | 'nvptx64--nvidiacl' : { 'devices' : [{'gpu' : '', 'aliases' : []}] } |
| 87 | } |
| 88 | |
Tom Stellard | 879327f | 2013-06-26 18:18:59 | [diff] [blame] | 89 | default_targets = ['nvptx--nvidiacl', 'nvptx64--nvidiacl', 'r600--'] |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 90 | |
| 91 | targets = args |
| 92 | if not targets: |
| 93 | targets = default_targets |
| 94 | |
| 95 | b = metabuild.from_name(options.g) |
| 96 | |
| 97 | b.rule("LLVM_AS", "%s -o $out $in" % os.path.join(llvm_bindir, "llvm-as"), |
| 98 | 'LLVM-AS $out') |
| 99 | b.rule("LLVM_LINK", command = llvm_link + " -o $out $in", |
| 100 | description = 'LLVM-LINK $out') |
| 101 | b.rule("OPT", command = llvm_opt + " -O3 -o $out $in", |
| 102 | description = 'OPT $out') |
| 103 | |
Tom Stellard | 9f48bb3 | 2013-10-23 02:49:27 | [diff] [blame] | 104 | c_compiler_rule(b, "LLVM_TOOL_CXX", 'CXX', cxx_compiler, llvm_cxxflags) |
| 105 | b.rule("LLVM_TOOL_LINK", cxx_compiler + " -o $out $in %s" % llvm_core_libs, 'LINK $out') |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 106 | |
| 107 | prepare_builtins = os.path.join('utils', 'prepare-builtins') |
| 108 | b.build(os.path.join('utils', 'prepare-builtins.o'), "LLVM_TOOL_CXX", |
| 109 | os.path.join(srcdir, 'utils', 'prepare-builtins.cpp')) |
| 110 | b.build(prepare_builtins, "LLVM_TOOL_LINK", |
| 111 | os.path.join('utils', 'prepare-builtins.o')) |
| 112 | |
| 113 | b.rule("PREPARE_BUILTINS", "%s -o $out $in" % prepare_builtins, |
| 114 | 'PREPARE-BUILTINS $out') |
Tom Stellard | f21e3ea | 2013-10-10 19:09:01 | [diff] [blame] | 115 | b.rule("PYTHON_GEN", "python < $in > $out", "PYTHON_GEN $out") |
| 116 | b.build('generic/lib/convert.cl', "PYTHON_GEN", ['generic/lib/gen_convert.py']) |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 117 | |
| 118 | manifest_deps = set([sys.argv[0], os.path.join(srcdir, 'build', 'metabuild.py'), |
| 119 | os.path.join(srcdir, 'build', 'ninja_syntax.py')]) |
| 120 | |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 121 | install_files_bc = [] |
Peter Collingbourne | bae6833 | 2012-06-01 17:29:59 | [diff] [blame] | 122 | install_deps = [] |
| 123 | |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 124 | # Create libclc.pc |
| 125 | clc = open('libclc.pc', 'w') |
| 126 | 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}' % |
| 127 | {'inc': includedir, 'lib': libexecdir, 'maj': version_major, 'min': version_minor, 'pat': version_patch}) |
| 128 | clc.close() |
| 129 | |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 130 | for target in targets: |
| 131 | (t_arch, t_vendor, t_os) = target.split('-') |
| 132 | archs = [t_arch] |
Peter Collingbourne | a3fc645 | 2012-05-28 20:42:15 | [diff] [blame] | 133 | if t_arch == 'nvptx' or t_arch == 'nvptx64': |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 134 | archs.append('ptx') |
| 135 | archs.append('generic') |
| 136 | |
| 137 | subdirs = [] |
| 138 | for arch in archs: |
| 139 | subdirs.append("%s-%s-%s" % (arch, t_vendor, t_os)) |
| 140 | subdirs.append("%s-%s" % (arch, t_os)) |
| 141 | subdirs.append(arch) |
| 142 | |
Peter Collingbourne | bae6833 | 2012-06-01 17:29:59 | [diff] [blame] | 143 | incdirs = filter(os.path.isdir, |
| 144 | [os.path.join(srcdir, subdir, 'include') for subdir in subdirs]) |
| 145 | libdirs = filter(lambda d: os.path.isfile(os.path.join(d, 'SOURCES')), |
| 146 | [os.path.join(srcdir, subdir, 'lib') for subdir in subdirs]) |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 147 | |
Peter Collingbourne | bae6833 | 2012-06-01 17:29:59 | [diff] [blame] | 148 | clang_cl_includes = ' '.join(["-I%s" % incdir for incdir in incdirs]) |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 149 | |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 150 | for device in available_targets[target]['devices']: |
| 151 | # The rule for building a .bc file for the specified architecture using clang. |
| 152 | clang_bc_flags = "-target %s -I`dirname $in` %s " \ |
| 153 | "-Dcl_clang_storage_class_specifiers " \ |
| 154 | "-Dcl_khr_fp64 " \ |
Tom Stellard | f21e3ea | 2013-10-10 19:09:01 | [diff] [blame] | 155 | "-Dcles_khr_int64 " \ |
Tom Stellard | 6c7b86c | 2013-10-10 19:08:51 | [diff] [blame] | 156 | "-D__CLC_INTERNAL " \ |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 157 | "-emit-llvm" % (target, clang_cl_includes) |
| 158 | if device['gpu'] != '': |
| 159 | clang_bc_flags += ' -mcpu=' + device['gpu'] |
| 160 | clang_bc_rule = "CLANG_CL_BC_" + target |
| 161 | c_compiler_rule(b, clang_bc_rule, "LLVM-CC", llvm_clang, clang_bc_flags) |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 162 | |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 163 | objects = [] |
| 164 | sources_seen = set() |
Tom Stellard | d54f6ba | 2013-06-26 18:20:05 | [diff] [blame] | 165 | |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 166 | if device['gpu'] == '': |
| 167 | full_target_name = target |
| 168 | obj_suffix = '' |
| 169 | else: |
| 170 | full_target_name = device['gpu'] + '-' + target |
| 171 | obj_suffix = '.' + device['gpu'] |
Tom Stellard | d54f6ba | 2013-06-26 18:20:05 | [diff] [blame] | 172 | |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 173 | for libdir in libdirs: |
| 174 | subdir_list_file = os.path.join(libdir, 'SOURCES') |
| 175 | manifest_deps.add(subdir_list_file) |
| 176 | override_list_file = os.path.join(libdir, 'OVERRIDES') |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 177 | |
Tom Stellard | 30f554b | 2013-06-26 18:20:38 | [diff] [blame] | 178 | # Add target overrides |
| 179 | if os.path.exists(override_list_file): |
| 180 | for override in open(override_list_file).readlines(): |
| 181 | override = override.rstrip() |
| 182 | sources_seen.add(override) |
| 183 | |
| 184 | for src in open(subdir_list_file).readlines(): |
| 185 | src = src.rstrip() |
| 186 | if src not in sources_seen: |
| 187 | sources_seen.add(src) |
| 188 | obj = os.path.join(target, 'lib', src + obj_suffix + '.bc') |
| 189 | objects.append(obj) |
| 190 | src_file = os.path.join(libdir, src) |
| 191 | ext = os.path.splitext(src)[1] |
| 192 | if ext == '.ll': |
| 193 | b.build(obj, 'LLVM_AS', src_file) |
| 194 | else: |
| 195 | b.build(obj, clang_bc_rule, src_file) |
| 196 | |
| 197 | builtins_link_bc = os.path.join(target, 'lib', 'builtins.link' + obj_suffix + '.bc') |
| 198 | builtins_opt_bc = os.path.join(target, 'lib', 'builtins.opt' + obj_suffix + '.bc') |
| 199 | builtins_bc = os.path.join('built_libs', full_target_name + '.bc') |
| 200 | b.build(builtins_link_bc, "LLVM_LINK", objects) |
| 201 | b.build(builtins_opt_bc, "OPT", builtins_link_bc) |
| 202 | b.build(builtins_bc, "PREPARE_BUILTINS", builtins_opt_bc, prepare_builtins) |
| 203 | install_files_bc.append((builtins_bc, builtins_bc)) |
| 204 | install_deps.append(builtins_bc) |
| 205 | for alias in device['aliases']: |
| 206 | b.rule("CREATE_ALIAS", "ln -fs %s $out" % os.path.basename(builtins_bc) |
| 207 | ,"CREATE-ALIAS $out") |
| 208 | |
| 209 | alias_file = os.path.join('built_libs', alias + '-' + target + '.bc') |
| 210 | b.build(alias_file, "CREATE_ALIAS", builtins_bc) |
| 211 | install_files_bc.append((alias_file, alias_file)) |
| 212 | install_deps.append(alias_file) |
| 213 | b.default(builtins_bc) |
Peter Collingbourne | bae6833 | 2012-06-01 17:29:59 | [diff] [blame] | 214 | |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 215 | |
| 216 | install_cmd = ' && '.join(['mkdir -p $(DESTDIR)/%(dst)s && cp -r %(src)s $(DESTDIR)/%(dst)s' % |
| 217 | {'src': file, |
| 218 | 'dst': libexecdir} |
| 219 | for (file, dest) in install_files_bc]) |
Tom Stellard | 5668ea2 | 2013-06-26 18:20:35 | [diff] [blame] | 220 | install_cmd = ' && '.join(['%(old)s && mkdir -p $(DESTDIR)/%(dst)s && cp -r %(srcdir)s/generic/include/clc $(DESTDIR)/%(dst)s' % |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 221 | {'old': install_cmd, |
Tom Stellard | 5668ea2 | 2013-06-26 18:20:35 | [diff] [blame] | 222 | 'dst': includedir, |
| 223 | 'srcdir': srcdir}]) |
Tom Stellard | 1de7761 | 2013-06-26 18:20:03 | [diff] [blame] | 224 | install_cmd = ' && '.join(['%(old)s && mkdir -p $(DESTDIR)/%(dst)s && cp -r libclc.pc $(DESTDIR)/%(dst)s' % |
| 225 | {'old': install_cmd, |
| 226 | 'dst': pkgconfigdir}]) |
| 227 | |
| 228 | b.rule('install', command = install_cmd, description = 'INSTALL') |
| 229 | b.build('install', 'install', install_deps) |
Peter Collingbourne | d5395fb | 2012-01-08 22:09:58 | [diff] [blame] | 230 | |
| 231 | b.rule("configure", command = ' '.join(sys.argv), description = 'CONFIGURE', |
| 232 | generator = True) |
| 233 | b.build(b.output_filename(), 'configure', list(manifest_deps)) |
| 234 | |
| 235 | b.finish() |