aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler_wrapper/README.md12
-rwxr-xr-xcompiler_wrapper/build36
-rwxr-xr-xcompiler_wrapper/build.py50
-rw-r--r--compiler_wrapper/bundle.README13
-rwxr-xr-xcompiler_wrapper/bundle.py59
5 files changed, 133 insertions, 37 deletions
diff --git a/compiler_wrapper/README.md b/compiler_wrapper/README.md
index fd38d894..42198432 100644
--- a/compiler_wrapper/README.md
+++ b/compiler_wrapper/README.md
@@ -1,3 +1,13 @@
# Compiler wrapper
-See the comments on the top of main.go
+See the comments on the top of main.go.
+Build is split into 2 steps via separate commands:
+- bundle: copies the sources and the `build.py` file into
+ a folder.
+- build: builds the actual go binary, assuming it is executed
+ from the folder created by `bundle.py`.
+
+This allows to copy the sources to a ChromeOS / Android
+package, including the build script, and then
+build from there without a dependency on toolchain-utils
+itself.
diff --git a/compiler_wrapper/build b/compiler_wrapper/build
deleted file mode 100755
index 2b051967..00000000
--- a/compiler_wrapper/build
+++ /dev/null
@@ -1,36 +0,0 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8 -*-
-# Copyright 2019 The Chromium OS Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-"""Build script that wraps go build and sets the correct linker variables."""
-
-from __future__ import print_function
-
-import argparse
-import os
-import os.path
-import subprocess
-import sys
-
-def parse_args():
- parser = argparse.ArgumentParser()
- parser.add_argument("--config", required=True,
- choices=['cros.hardened', 'cros.nonhardened',
- 'cros.host'], type=str)
- parser.add_argument("--use_ccache", required=True,
- choices=['true', 'false'], type=str)
- return parser.parse_known_args()
-
-def calc_go_args(parsed_args, rest_args):
- src_dir = os.path.dirname(sys.argv[0])
- ldFlags = ['-X', 'main.ConfigName=' + parsed_args.config,
- '-X', 'main.UseCCache='+parsed_args.use_ccache]
- return (['go', 'build', '-ldflags', ' '.join(ldFlags)]
- + rest_args + [src_dir])
-
-def main():
- sys.exit(subprocess.call(calc_go_args(*parse_args())))
-
-if __name__ == '__main__':
- main()
diff --git a/compiler_wrapper/build.py b/compiler_wrapper/build.py
new file mode 100755
index 00000000..dae18314
--- /dev/null
+++ b/compiler_wrapper/build.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Build script that builds a binary from a bundle."""
+
+from __future__ import print_function
+
+import argparse
+import os.path
+import subprocess
+import sys
+
+
+def parse_args():
+ parser = argparse.ArgumentParser()
+ parser.add_argument(
+ '--config',
+ required=True,
+ choices=['cros.hardened', 'cros.nonhardened', 'cros.host'])
+ parser.add_argument('--use_ccache', required=True, choices=['true', 'false'])
+ parser.add_argument('--output_file', required=True, type=str)
+ return parser.parse_args()
+
+
+def calc_go_args(args):
+ # See https://github.com/golang/go/issues/26492 for how to
+ # build a fully static binary in go.
+ ldFlags = [
+ '-X', 'main.ConfigName=' + args.config, '-X',
+ 'main.UseCCache=' + args.use_ccache, '-extldflags \'-fno-PIC -static\''
+ ]
+ return [
+ 'go', 'build', '-o',
+ os.path.abspath(args.output_file), '-ldflags', ' '.join(ldFlags),
+ '-buildmode', 'pie', '-tags', 'osusergo netgo static_build'
+ ]
+
+
+def main():
+ args = parse_args()
+ # Note: Go does not support using absolute package names.
+ # So we run go inside the directory of the the build file.
+ sys.exit(subprocess.call(calc_go_args(args), cwd=os.path.dirname(__file__)))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/compiler_wrapper/bundle.README b/compiler_wrapper/bundle.README
new file mode 100644
index 00000000..cf796d44
--- /dev/null
+++ b/compiler_wrapper/bundle.README
@@ -0,0 +1,13 @@
+Toolchain utils compiler wrapper sources.
+
+Build the wrapper:
+./build --config=<config name> --use_ccache=<bool> --output_file=<file>
+
+ATTENTION:
+The files in this folder are generated. Do not modify manually!
+
+To update:
+- modify third_party/toolchain_utils/compiler_wrapper
+- run third_party/toolchain_utils/compiler_wrapper/bundle.py --output_dir=...
+
+Source: https://chromium-review.googlesource.com/q/{change_id}
diff --git a/compiler_wrapper/bundle.py b/compiler_wrapper/bundle.py
new file mode 100755
index 00000000..44799b6f
--- /dev/null
+++ b/compiler_wrapper/bundle.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Build script that copies the go sources to a build destination."""
+
+from __future__ import print_function
+
+import argparse
+import os.path
+import re
+import shutil
+import subprocess
+import sys
+
+
+def parse_args():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('output_dir')
+ return parser.parse_args()
+
+
+def copy_files(input_dir, output_dir):
+ for filename in os.listdir(input_dir):
+ if ((filename.endswith('.go') and not filename.endswith('_test.go')) or
+ filename == 'build'):
+ shutil.copy(
+ os.path.join(input_dir, filename), os.path.join(output_dir, filename))
+
+
+def read_change_id():
+ last_commit_msg = subprocess.check_output(['git', 'log', '-1', '--pretty=%B'])
+ match = re.search('Change-Id: (\\w+)', last_commit_msg)
+ if not match:
+ sys.exit('Couldn\'t find Change-Id in last commit message.')
+ return match.group(1)
+
+
+def write_readme(input_dir, output_dir, change_id):
+ with open(os.path.join(input_dir, 'bundle.README'), 'r') as r, \
+ open(os.path.join(output_dir, 'README'), 'w') as w:
+ content = r.read()
+ w.write(content.format(change_id=change_id))
+
+
+def main():
+ args = parse_args()
+ change_id = read_change_id()
+ shutil.rmtree(args.output_dir, ignore_errors=True)
+ os.makedirs(args.output_dir)
+ input_dir = os.path.dirname(__file__)
+ copy_files(input_dir, args.output_dir)
+ write_readme(input_dir, args.output_dir, change_id)
+
+
+if __name__ == '__main__':
+ main()