|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# go.py - Waf tool for the Go programming language |
| 4 | +# By: Tom Wambold <tom5760@gmail.com> |
| 5 | + |
| 6 | +import platform |
| 7 | + |
| 8 | +import Task |
| 9 | +import Utils |
| 10 | +from TaskGen import feature, extension, after |
| 11 | + |
| 12 | +Task.simple_task_type('gocompile', '${GOC} ${GOCFLAGS} -o ${TGT} ${SRC}', shell=False) |
| 13 | +Task.simple_task_type('gopack', '${GOP} grc ${TGT} ${SRC}', shell=False) |
| 14 | +Task.simple_task_type('golink', '${GOL} ${GOLFLAGS} -o ${TGT} ${SRC}', shell=False) |
| 15 | + |
| 16 | +def detect(conf): |
| 17 | + |
| 18 | + def set_def(var, val): |
| 19 | + if not conf.env[var]: |
| 20 | + conf.env[var] = val |
| 21 | + |
| 22 | + set_def('GO_PLATFORM', platform.machine()) |
| 23 | + |
| 24 | + if conf.env.GO_PLATFORM == 'x86_64': |
| 25 | + set_def('GO_COMPILER', '6g') |
| 26 | + set_def('GO_LINKER', '6l') |
| 27 | + set_def('GO_EXTENSION', '.6') |
| 28 | + elif conf.env.GO_PLATFORM == 'i386': |
| 29 | + set_def('GO_COMPILER', '8g') |
| 30 | + set_def('GO_LINKER', '8l') |
| 31 | + set_def('GO_EXTENSION', '.8') |
| 32 | + |
| 33 | + if not (conf.env.GO_COMPILER or conf.env.GO_LINKER or conf.env.GO_EXTENSION): |
| 34 | + raise conf.fatal('Unsupported platform ' + platform.machine()) |
| 35 | + |
| 36 | + set_def('GO_PACK', 'gopack') |
| 37 | + set_def('GO_PACK_EXTENSION', '.a') |
| 38 | + |
| 39 | + conf.find_program(conf.env.GO_COMPILER, var='GOC', mandatory=True) |
| 40 | + conf.find_program(conf.env.GO_LINKER, var='GOL', mandatory=True) |
| 41 | + conf.find_program(conf.env.GO_PACK, var='GOP', mandatory=True) |
| 42 | + |
| 43 | +@extension('.go') |
| 44 | +def compile_go(self, node): |
| 45 | + try: |
| 46 | + self.go_nodes.append(node) |
| 47 | + except AttributeError: |
| 48 | + self.go_nodes = [node] |
| 49 | + |
| 50 | +@feature('go') |
| 51 | +@after('apply_core') |
| 52 | +def apply_compile_go(self): |
| 53 | + try: |
| 54 | + nodes = self.go_nodes |
| 55 | + except AttributeError: |
| 56 | + self.go_compile_task = None |
| 57 | + else: |
| 58 | + self.go_compile_task = self.create_task('gocompile', |
| 59 | + nodes, |
| 60 | + [self.path.find_or_declare(self.target + self.env.GO_EXTENSION)]) |
| 61 | + |
| 62 | +@feature('gopackage', 'goprogram') |
| 63 | +@after('apply_compile_go') |
| 64 | +def apply_goinc(self): |
| 65 | + if not getattr(self, 'go_compile_task', None): |
| 66 | + return |
| 67 | + |
| 68 | + names = self.to_list(getattr(self, 'uselib_local', [])) |
| 69 | + for name in names: |
| 70 | + obj = self.name_to_obj(name) |
| 71 | + if not obj: |
| 72 | + raise Utils.WafError('object %r was not found in uselib_local ' |
| 73 | + '(required by %r)' % (lib_name, self.name)) |
| 74 | + obj.post() |
| 75 | + self.go_compile_task.set_run_after(obj.go_package_task) |
| 76 | + self.go_compile_task.deps_nodes.extend(obj.go_package_task.outputs) |
| 77 | + self.env.append_unique('GOCFLAGS', '-I' + obj.path.abspath(obj.env)) |
| 78 | + self.env.append_unique('GOLFLAGS', '-L' + obj.path.abspath(obj.env)) |
| 79 | + |
| 80 | +@feature('gopackage') |
| 81 | +@after('apply_goinc') |
| 82 | +def apply_gopackage(self): |
| 83 | + self.go_package_task = self.create_task('gopack', |
| 84 | + self.go_compile_task.outputs[0], |
| 85 | + self.path.find_or_declare(self.target + self.env.GO_PACK_EXTENSION)) |
| 86 | + self.go_package_task.set_run_after(self.go_compile_task) |
| 87 | + self.go_package_task.deps_nodes.extend(self.go_compile_task.outputs) |
| 88 | + |
| 89 | +@feature('goprogram') |
| 90 | +@after('apply_goinc') |
| 91 | +def apply_golink(self): |
| 92 | + self.go_link_task = self.create_task('golink', |
| 93 | + self.go_compile_task.outputs[0], |
| 94 | + self.path.find_or_declare(self.target)) |
| 95 | + self.go_link_task.set_run_after(self.go_compile_task) |
| 96 | + self.go_link_task.deps_nodes.extend(self.go_compile_task.outputs) |
| 97 | + |
0 commit comments