Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fuchsia] [packaging] Layout debug symbols for Fuchsia #13338

Merged
merged 7 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tools/fuchsia/copy_debug_symbols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python
#
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
""" Gather the build_id, prefix_dir, and exec_name given the path to executable
also copies to the specified destination.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a line about how the build ID directory is structure. That section from the presentation will work great :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"""

import argparse
import json
import os
import re
import shutil
import subprocess
import sys


def Touch(fname):
with open(fname, 'a'):
os.utime(fname, None)


def GetBuildIdParts(exec_path):
file_out = subprocess.check_output(['file', exec_path])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang, is there no other way to get the build ID for an ELF binary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work on cirrus for some reason. I'm wondering if file is sandboxed away from us. I will look for alternatives.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Fuchsia toolchain includes a version of llvm-readelf.

Try extracting the build ID with:
llvm-readelf --hex-dump=.note.gnu.build-id [executable]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, I switched to using this,

build_id = re.match('.*=(.*?),', file_out).groups()[0]
return {
'build_id': build_id,
'prefix_dir': build_id[:2],
'exec_name': build_id[2:]
}


def main():
parser = argparse.ArgumentParser()

parser.add_argument(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add help= arguments to these please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

'--executable-name', dest='exec_name', action='store', required=True)
parser.add_argument(
'--executable-path', dest='exec_path', action='store', required=True)
parser.add_argument(
'--destination-base', dest='dest', action='store', required=True)

parser.add_argument('--stripped', dest='stripped', action='store_true')
parser.add_argument('--unstripped', dest='stripped', action='store_false')
parser.set_defaults(stripped=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can specify the default in the add_argument line itself.


args = parser.parse_args()
assert os.path.exists(args.exec_path)
assert os.path.exists(args.dest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant with required=True.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required=True says that the argument exists, not the path.


parts = GetBuildIdParts(args.exec_path)
dbg_prefix_base = '%s/%s' % (args.dest, parts['prefix_dir'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer os.path.join. Here and elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


if not os.path.exists(dbg_prefix_base):
os.makedirs(dbg_prefix_base)

dbg_suffix = ''
if not args.stripped:
dbg_suffix = '.debug'
dbg_file_path = '%s/%s%s' % (dbg_prefix_base, parts['exec_name'], dbg_suffix)

shutil.copyfile(args.exec_path, dbg_file_path)

# Note this needs to be in sync with debug_symbols.gni
completion_file = '%s/.%s_dbg_success' % (args.dest, args.exec_name)
Touch(completion_file)

return 0


if __name__ == '__main__':
sys.exit(main())
82 changes: 82 additions & 0 deletions tools/fuchsia/debug_symbols.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# The inputs to this template are 'binary_path' and a boolean 'unstripped'.
# If 'unstripped' is specified, we append '.debug' to the symbols name.
template("_copy_debug_symbols") {
assert(defined(invoker.binary_path), "'binary_path' needs to be defined.")

bin_path = invoker.binary_path

_args = []
if (defined(invoker.unstripped) && invoker.unstripped) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd generally about optional invoker arguments. Especially in this case where the template is private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

_args += [ "--unstripped" ]
}

action(target_name) {
testonly = defined(invoker.testonly) && invoker.testonly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, prefer forward_variables_from

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


deps = []
if (defined(invoker.deps)) {
deps = invoker.deps
}

script = "$flutter_root/tools/fuchsia/copy_debug_symbols.py"

sources = [
bin_path,
]

_dest_base =
"${root_out_dir}/flutter-debug-symbols-${target_os}-${target_cpu}"

args = _args + [
"--executable-name",
target_name,
"--executable-path",
rebase_path(bin_path),
"--destination-base",
rebase_path(_dest_base),
]

outputs = [
"${_dest_base}/.${target_name}_success",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the convention is .stamp. Also, why is the file hidden?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was making these hidden to avoid the noise when you try to inspect the directories. .stamp seems to have a special meaning as far as gn is concerned - there is a tool (" "stamp": Tool for creating stamp files") that gn uses to generate these, so I tried to avoid that. If that is the convention, i'm happy to change it to .stamp.

]
}
}

# Takes a binary and generates its debug symbols following
# the Fuchsia packaging convention.
template("debug_symbols") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fuchsia_debug_symbols for clarity. Also, please document the invoker arguments necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

assert(defined(invoker.binary), "'binary' needs to be defined.")
_testonly = defined(invoker.testonly) && invoker.testonly

_deps = []
if (defined(invoker.deps)) {
_deps = invoker.deps
}

bin = invoker.binary

_copy_debug_symbols("_${target_name}_stripped") {
testonly = _testonly
binary_path = rebase_path("${root_out_dir}/$bin")
deps = _deps
}

_copy_debug_symbols("_${target_name}_unstripped") {
testonly = _testonly
binary_path = "${root_out_dir}/exe.unstripped/$bin"
unstripped = true
deps = _deps
}

group(target_name) {
testonly = _testonly
deps = [
":_${target_name}_stripped",
":_${target_name}_unstripped",
]
}
}
14 changes: 13 additions & 1 deletion tools/fuchsia/fuchsia_archive.gni
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("$flutter_root/tools/fuchsia/debug_symbols.gni")

# Creates a Fuchsia archive (.far) file using PM from the Fuchsia SDK.
template("fuchsia_archive") {
assert(defined(invoker.binary), "package must define binary")
Expand Down Expand Up @@ -72,6 +74,13 @@ template("fuchsia_archive") {
},
"json")

_dbg_symbols_target = "${target_name}_dbg_symbols"
debug_symbols(_dbg_symbols_target) {
deps = pkg.deps
testonly = pkg_testonly
binary = invoker.binary
}

pkg_dir_deps = pkg.deps + [ ":$cmx_target" ]

action("${target_name}_dir") {
Expand All @@ -86,7 +95,10 @@ template("fuchsia_archive") {

action(target_name) {
script = "$flutter_root/tools/fuchsia/gen_package.py"
deps = pkg_dir_deps + [ ":${target_name}_dir" ]
deps = pkg_dir_deps + [
":${target_name}_dir",
":${_dbg_symbols_target}",
]
sources = copy_outputs

inputs = []
Expand Down