Skip to content

Instantly share code, notes, and snippets.

@sailfish009
Created August 12, 2020 08:48
Show Gist options
  • Save sailfish009/8470dced2621ae8633f8d5ba30563728 to your computer and use it in GitHub Desktop.
Save sailfish009/8470dced2621ae8633f8d5ba30563728 to your computer and use it in GitHub Desktop.
https://forums.gentoo.org/viewtopic-t-1108872-start-0.html
# Python3+ instead of python2.7
# by Pavel Kabikov aka bitMan
--- chromium-81.0.4044.26/build/toolchain/gcc_solink_wrapper.py 2020-02-20 03:32:08.000000000 +0300
+++ /tmp/gcc_solink_wrapper.py 2020-02-27 00:45:25.526917216 +0300
@@ -11,6 +11,7 @@
"""
import argparse
+import io
import os
import subprocess
import sys
@@ -18,12 +19,19 @@
import wrapper_utils
+def read_as_utf8(fileno):
+ fp = io.open(fileno, mode="r", encoding="utf-8", closefd=False)
+ content = fp.read()
+ fp.close()
+ return content
+
def CollectSONAME(args):
"""Replaces: readelf -d $sofile | grep SONAME"""
toc = ''
readelf = subprocess.Popen(wrapper_utils.CommandToRun(
[args.readelf, '-d', args.sofile]), stdout=subprocess.PIPE, bufsize=-1)
- for line in readelf.stdout:
+ # print(readelf.stdout)
+ for line in read_as_utf8(readelf.stdout.fileno()):
if 'SONAME' in line:
toc += line
return readelf.wait(), toc
@@ -35,7 +43,7 @@
nm = subprocess.Popen(wrapper_utils.CommandToRun([
args.nm, '--format=posix', '-g', '-D', args.sofile]),
stdout=subprocess.PIPE, bufsize=-1)
- for line in nm.stdout:
+ for line in read_as_utf8(nm.stdout.fileno()):
toc += ' '.join(line.split(' ', 2)[:2]) + '\n'
return nm.wait(), toc
@sailfish009
Copy link
Author

#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Embeds Chrome user data files in C++ code."""

import base64
import optparse
import os
# import StringIO
import sys
import zipfile

import cpp_source

# try:
#     from StringIO import StringIO
# except ImportError:
#     from io import StringIO
from io import BytesIO


def main():
  parser = optparse.OptionParser()
  parser.add_option(
      '', '--directory', type='string', default='.',
      help='Path to directory where the cc/h  file should be created')
  options, args = parser.parse_args()

  global_string_map = {}
  # string_buffer = StringIO.StringIO()
  # string_buffer = StringIO()
  string_buffer = BytesIO()

  zipper = zipfile.ZipFile(string_buffer, 'w')
  for f in args:
    zipper.write(f, os.path.basename(f), zipfile.ZIP_STORED)
  zipper.close()
  global_string_map['kAutomationExtension'] = base64.b64encode(
      string_buffer.getvalue())
  string_buffer.close()

  cpp_source.WriteSource('embedded_automation_extension',
                         'chrome/test/chromedriver/chrome',
                         options.directory, global_string_map)


if __name__ == '__main__':
  sys.exit(main())

@sailfish009
Copy link
Author

# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Writes C++ header/cc source files for embedding resources into C++."""
import datetime
import os
import sys
def WriteSource(base_name,
                dir_from_src,
                output_dir,
                global_string_map):
  """Writes C++ header/cc source files for the given map of string variables.
  Args:
      base_name: The basename of the file, without the extension.
      dir_from_src: Path from src to the directory that will contain the file,
          using forward slashes.
      output_dir: Directory to output the sources to.
      global_string_map: Map of variable names to their string values. These
          variables will be available as globals.
  """
  copyright_header_template = (
      '// Copyright 2018 The Chromium Authors. All rights reserved.\n'
      '// Use of this source code is governed by a BSD-style license '
      'that can be\n'
      '// found in the LICENSE file.\n\n'
      '// This file was generated by running:\n'
      '//     %s')
  copyright_header = copyright_header_template % (' '.join(sys.argv))
  # Write header file.
  externs = []
  for name in global_string_map.keys():
    externs += ['extern const char %s[];' % name]
  temp = '_'.join(dir_from_src.split('/') + [base_name])
  define = temp.upper() + '_H_'
  header = '\n'.join([
      copyright_header,
      '',
      '#ifndef ' + define,
      '#define ' + define,
      '',
      '\n'.join(externs),
      '',
      '#endif  // ' + define])
  header += '\n'
  with open(os.path.join(output_dir, base_name + '.h'), 'w') as f:
    f.write(header)
  # Write cc file.
  def EscapeLine(line):
    return line.replace('\\', '\\\\').replace('"', '\\"')
  definitions = []
  for name, contents in global_string_map.items():
    lines = []
    if '\n' not in contents:
      lines = ['    "%s"' % EscapeLine(contents)]
    else:
      for line in contents.split('\n'):
        lines += ['    "%s\\n"' % EscapeLine(line)]
    definitions += ['const char %s[] =\n%s;' % (name, '\n'.join(lines))]
  cc = '\n'.join([
      copyright_header,
      '',
      '#include "%s"' % (dir_from_src + '/' + base_name + '.h'),
      '',
      '\n'.join(definitions)])
  cc += '\n'
  with open(os.path.join(output_dir, base_name + '.cc'), 'w') as f:
    f.write(cc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment