dart-sdk/runtime/tools/bin_to_assembly.py
Wink Saville bbdb7860f6 [build]: Build on Arch Linux with python 2.7-3.8
This change allow builds to succeed on Arch Linux with python 2.7, 3.7.4
and 3.8. Otherwise it fails with python 3.7.4 and 3.8.

- bin_to_assembly.py

  Using ord(byte) here is an error on python 3.x use a conditional
  expression instead.

- create_archive.py

  Using ord(byte) here is an error on python 3.x use a conditional
  expression instead.

  Use `as inst` syntax which is required by 3.x

- create_string_literal.py
- gen_library_src_paths.py

  Use `as inst` syntax which is required by 3.x

- make_version.py

  print >> sys.stderr syntax is not supported by 3.x use file=sys.stderr
  and add `from __future__ import print_function`

  vmhash.update requires a string use encode('utf-8')

  versopm+cc+text.replace requires a str so use `decode`.

Test: No added tests as there are no behavioral changes.
Change-Id: Id9b0a3187d06ad2b2dce3cf2244144393caddc9a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/124211
Commit-Queue: Alexander Thomas <athom@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2019-11-10 21:09:58 +00:00

134 lines
5.1 KiB
Python
Executable file

#!/usr/bin/env python
#
# Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
# Generates an assembly source file the defines a symbol with the bytes from
# a given file.
import os
import sys
from optparse import OptionParser
def Main():
parser = OptionParser()
parser.add_option(
"--output",
action="store",
type="string",
help="output assembly file name")
parser.add_option(
"--input", action="store", type="string", help="input binary blob file")
parser.add_option("--symbol_name", action="store", type="string")
parser.add_option("--executable", action="store_true", default=False)
parser.add_option("--target_os", action="store", type="string")
parser.add_option("--size_symbol_name", action="store", type="string")
parser.add_option("--target_arch", action="store", type="string")
(options, args) = parser.parse_args()
if not options.output:
sys.stderr.write("--output not specified\n")
parser.print_help()
return -1
if not options.input:
sys.stderr.write("--input not specified\n")
parser.print_help()
return -1
if not os.path.isfile(options.input):
sys.stderr.write("input file does not exist: %s\n" % options.input)
parser.print_help()
return -1
if not options.symbol_name:
sys.stderr.write("--symbol_name not specified\n")
parser.print_help()
return -1
if not options.target_os:
sys.stderr.write("--target_os not specified\n")
parser.print_help()
return -1
with open(options.output, "w") as output_file:
if options.target_os in ["mac", "ios"]:
if options.executable:
output_file.write(".text\n")
else:
output_file.write(".const\n")
output_file.write(".global _%s\n" % options.symbol_name)
output_file.write(".balign 32\n")
output_file.write("_%s:\n" % options.symbol_name)
elif options.target_os in ["win"]:
output_file.write("ifndef _ML64_X64\n")
output_file.write(".model flat, C\n")
output_file.write("endif\n")
if options.executable:
output_file.write(".code\n")
else:
output_file.write(".const\n")
output_file.write("public %s\n" % options.symbol_name)
output_file.write("%s label byte\n" % options.symbol_name)
else:
if options.executable:
output_file.write(".text\n")
output_file.write(".type %s STT_FUNC\n" % options.symbol_name)
else:
output_file.write(".section .rodata\n")
output_file.write(".type %s STT_OBJECT\n" % options.symbol_name)
output_file.write(".global %s\n" % options.symbol_name)
output_file.write(".balign 32\n")
output_file.write("%s:\n" % options.symbol_name)
size = 0
with open(options.input, "rb") as input_file:
if options.target_os in ["win"]:
for byte in input_file.read():
output_file.write("byte %d\n" % (byte if isinstance(byte, int) else ord(byte)))
size += 1
else:
for byte in input_file.read():
output_file.write(".byte %d\n" % (byte if isinstance(byte, int) else ord(byte)))
size += 1
if options.target_os not in ["mac", "ios", "win"]:
output_file.write(".size {0}, .-{0}\n".format(options.symbol_name))
if options.size_symbol_name:
if not options.target_arch:
sys.stderr.write("--target_arch not specified\n")
parser.print_help()
return -1
is64bit = 0
if options.target_arch:
if options.target_arch in ["arm64", "x64"]:
is64bit = 1
if options.target_os in ["win"]:
output_file.write("public %s\n" % options.size_symbol_name)
output_file.write("%s label byte\n" % options.size_symbol_name)
if (is64bit == 1):
output_file.write("qword %d\n" % size)
else:
output_file.write("dword %d\n" % size)
else:
if options.target_os in ["mac", "ios"]:
output_file.write(
".global _%s\n" % options.size_symbol_name)
output_file.write("_%s:\n" % options.size_symbol_name)
else:
output_file.write(".global %s\n" % options.size_symbol_name)
output_file.write("%s:\n" % options.size_symbol_name)
if (is64bit == 1):
output_file.write(".quad %d\n" % size)
else:
output_file.write(".long %d\n" % size)
if options.target_os in ["win"]:
output_file.write("end\n")
return 0
if __name__ == "__main__":
sys.exit(Main())