Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(709)

Side by Side Diff: mojo/go/go.py

Issue 1384243002: Adds the ability to run pure Go unit tests in the Mojo test suite. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Adds dummy go tests to run. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """ 6 """
7 This script invokes the go build tool. 7 This script invokes the go build tool.
8 Must be called as follows: 8 Must be called as follows:
9 python go.py [--android] <go-tool> <build directory> <output file> 9 python go.py [--android] <go-tool> <build directory> <output file>
10 <src directory> <CGO_CFLAGS> <CGO_LDFLAGS> <go-binary options> 10 <src directory> <CGO_CFLAGS> <CGO_LDFLAGS> <go-binary options>
(...skipping 19 matching lines...) Expand all
30 parser.add_argument('output_file') 30 parser.add_argument('output_file')
31 parser.add_argument('src_root') 31 parser.add_argument('src_root')
32 parser.add_argument('out_root') 32 parser.add_argument('out_root')
33 parser.add_argument('cgo_cflags') 33 parser.add_argument('cgo_cflags')
34 parser.add_argument('cgo_ldflags') 34 parser.add_argument('cgo_ldflags')
35 parser.add_argument('go_option', nargs='*') 35 parser.add_argument('go_option', nargs='*')
36 args = parser.parse_args() 36 args = parser.parse_args()
37 go_tool = os.path.abspath(args.go_tool) 37 go_tool = os.path.abspath(args.go_tool)
38 build_dir = args.build_directory 38 build_dir = args.build_directory
39 out_file = os.path.abspath(args.output_file) 39 out_file = os.path.abspath(args.output_file)
40 # The src directory specified is relative. We need this as an absolute path.
41 src_root = os.path.abspath(args.src_root)
42 # GOPATH must be absolute, and point to one directory up from |src_Root|
43 go_path = os.path.abspath(os.path.join(src_root, '..'))
44 # GOPATH also includes any third_party/go libraries that have been imported
45 go_path += ':' + os.path.join(src_root, 'third_party', 'go')
46 go_path += ':' + os.path.abspath(os.path.join(args.out_root, 'gen', 'go'))
47 if 'MOJO_GOPATH' in os.environ:
48 go_path += ':' + os.environ['MOJO_GOPATH']
49 go_options = args.go_option 40 go_options = args.go_option
50 try: 41 try:
51 shutil.rmtree(build_dir, True) 42 shutil.rmtree(build_dir, True)
52 os.mkdir(build_dir) 43 os.mkdir(build_dir)
53 except Exception: 44 except Exception:
54 pass 45 pass
55 old_directory = os.getcwd() 46
56 os.chdir(build_dir) 47 call_result = InvokeGo(go_tool, go_options,
48 work_dir=build_dir,
49 src_root=args.src_root,
50 out_root=args.out_root,
51 cgo_cflags=args.cgo_cflags,
52 cgo_ldflags=args.cgo_ldflags,
53 target_android=args.android)
54 if call_result != 0:
55 return call_result
56 out_files = sorted([ f for f in os.listdir(build_dir) if os.path.isfile(f)])
57 if (len(out_files) > 0):
58 shutil.move(out_files[0], out_file)
59 try:
60 shutil.rmtree(build_dir, True)
61 except Exception:
62 pass
63
64 def InvokeGo(go_tool, go_options, work_dir=None, src_root=None,
viettrungluu 2015/10/09 21:39:49 Probably this should be put under mojo/tools/mopy:
rudominer 2015/10/09 23:42:06 Done.
65 out_root=None, cgo_cflags=None, cgo_ldflags=None,
66 target_android=False):
67 """ Invokes the go tool after setting up the environment.
68 go_tool The absolute path to the 'go' binary
69
70 go_options A list of string arguments that will be passed to 'go'
71
72 work_dir Optional specification of a directory to temporarily switch into
73 before invoking the go tool. If set it must be a path relative to
74 the current working directory or an absolute path.
75
76 src_root Optional specification of the Mojo src root. If set it must be
77 a path relative to the current working directory or an absolute
78 path. This will be used to add additional elements to
79 the GOPATH environment variable.
80
81 out_root Optional specification of the Mojo out dir. If set it must be
82 a path relative to the current working directory or an absolute
83 path. This will be used to add additional elements to
84 the GOPATH environment variable.
85
86 cgo_cflags Optional value to set for the CGO_CFLAGS environment variable.
87
88 cgo_ldflags Optional value to set for the CGO_LDFLAGS environment variable.
89
90 target_android
91 Set to true to target Android.
92 """
93 assert os.path.isabs(go_tool)
57 env = os.environ.copy() 94 env = os.environ.copy()
58 env['GOPATH'] = go_path
59 env['GOROOT'] = os.path.dirname(os.path.dirname(go_tool)) 95 env['GOROOT'] = os.path.dirname(os.path.dirname(go_tool))
60 env['CGO_CFLAGS'] = args.cgo_cflags 96
61 env['CGO_LDFLAGS'] = args.cgo_ldflags 97 go_path = ''
62 if args.android: 98 if src_root is not None:
99 # The src directory specified is relative. We need this as an absolute path.
100 src_root = os.path.abspath(src_root)
101 # GOPATH must be absolute, and point to one directory up from |src_Root|
102 go_path = os.path.abspath(os.path.join(src_root, '..'))
103 # GOPATH also includes any third_party/go libraries that have been imported
104 go_path += ':' + os.path.join(src_root, 'third_party', 'go')
105
106 if out_root is not None:
107 go_path += ':' + os.path.abspath(os.path.join(out_root, 'gen', 'go'))
108
109 if 'MOJO_GOPATH' in os.environ:
110 go_path += ':' + os.environ['MOJO_GOPATH']
111
112 if len(go_path) > 0 :
113 env['GOPATH'] = go_path
114
115 if cgo_cflags is not None:
116 env['CGO_CFLAGS'] = cgo_cflags
117
118 if cgo_ldflags is not None:
119 env['CGO_LDFLAGS'] = cgo_ldflags
120
121 if target_android:
63 env['CGO_ENABLED'] = '1' 122 env['CGO_ENABLED'] = '1'
64 env['GOOS'] = 'android' 123 env['GOOS'] = 'android'
65 env['GOARCH'] = 'arm' 124 env['GOARCH'] = 'arm'
66 env['GOARM'] = '7' 125 env['GOARM'] = '7'
67 # The Android go tool prebuilt binary has a default path to the compiler, 126 # The Android go tool prebuilt binary has a default path to the compiler,
68 # which with high probability points to an invalid path, so we override the 127 # which with high probability points to an invalid path, so we override the
69 # CC env var that will be used by the go tool. 128 # CC env var that will be used by the go tool.
70 if 'CC' not in env: 129 if 'CC' not in env:
130 if src_root is None:
131 raise Exception('src_root must be set if is_android is true and '
132 '"CC" is not in env.')
71 ndk_path = os.path.join(src_root, 'third_party', 'android_tools', 'ndk') 133 ndk_path = os.path.join(src_root, 'third_party', 'android_tools', 'ndk')
72 if sys.platform.startswith('linux'): 134 if sys.platform.startswith('linux'):
73 arch = 'linux-x86_64' 135 arch = 'linux-x86_64'
74 elif sys.platform == 'darwin': 136 elif sys.platform == 'darwin':
75 arch = 'darwin-x86_64' 137 arch = 'darwin-x86_64'
76 else: 138 else:
77 raise Exception('unsupported platform: ' + sys.platform) 139 raise Exception('unsupported platform: ' + sys.platform)
78 ndk_cc = os.path.join(ndk_path, 'toolchains', NDK_TOOLCHAIN, 140 ndk_cc = os.path.join(ndk_path, 'toolchains', NDK_TOOLCHAIN,
79 'prebuilt', arch, 'bin', 'arm-linux-androideabi-gcc') 141 'prebuilt', arch, 'bin', 'arm-linux-androideabi-gcc')
80 sysroot = os.path.join(ndk_path, 'platforms', NDK_PLATFORM, 'arch-arm') 142 sysroot = os.path.join(ndk_path, 'platforms', NDK_PLATFORM, 'arch-arm')
81 env['CGO_CFLAGS'] += ' --sysroot %s' % sysroot 143 env['CGO_CFLAGS'] += ' --sysroot %s' % sysroot
82 env['CGO_LDFLAGS'] += ' --sysroot %s' % sysroot 144 env['CGO_LDFLAGS'] += ' --sysroot %s' % sysroot
83 env['CC'] = '%s --sysroot %s' % (ndk_cc, sysroot) 145 env['CC'] = '%s --sysroot %s' % (ndk_cc, sysroot)
84 146
85 call_result = subprocess.call([go_tool] + go_options, env=env) 147 save_cwd = os.getcwd()
86 if call_result != 0: 148 if work_dir is not None:
87 return call_result 149 os.chdir(work_dir)
88 out_files = sorted([ f for f in os.listdir('.') if os.path.isfile(f)]) 150 result = subprocess.call([go_tool] + go_options, env=env)
89 if (len(out_files) > 0): 151 if work_dir is not None:
90 shutil.move(out_files[0], out_file) 152 os.chdir(save_cwd)
91 os.chdir(old_directory) 153 return result
92 try:
93 shutil.rmtree(build_dir, True)
94 except Exception:
95 pass
96 154
97 if __name__ == '__main__': 155 if __name__ == '__main__':
98 sys.exit(main()) 156 sys.exit(main())
OLDNEW
« no previous file with comments | « mojo/go/__init__.py ('k') | mojo/tools/data/gotests » ('j') | mojo/tools/data/gotests » ('J')

Powered by Google App Engine
This is Rietveld 408576698