1#!/usr/bin/env python3 2 3"""Converts a .exports file to a format consumable by linkers. 4 5An .exports file is a file with one exported symbol per line. 6This script converts a .exports file into a format that linkers 7can understand: 8- It prepends a `_` to each line for use with -exported_symbols_list for Darwin 9- It writes a .def file for use with /DEF: for Windows 10- It writes a linker script for use with --version-script elsewhere 11""" 12 13import argparse 14import sys 15 16 17def main(): 18 parser = argparse.ArgumentParser( 19 description=__doc__, 20 formatter_class=argparse.RawDescriptionHelpFormatter) 21 parser.add_argument("--format", required=True, choices=("linux", "mac", "win")) 22 parser.add_argument("source") 23 parser.add_argument("output") 24 args = parser.parse_args() 25 26 symbols = open(args.source).readlines() 27 28 if args.format == "linux": 29 output_lines = ( 30 [ 31 "LLVM_0 {\n", 32 " global:\n", 33 ] 34 + [" %s;\n" % s.rstrip() for s in symbols] 35 + [" local:\n", " *;\n", "};\n"] 36 ) 37 elif args.format == "mac": 38 output_lines = ["_" + s for s in symbols] 39 else: 40 assert args.format == "win" 41 output_lines = ["EXPORTS\n"] + [" " + s for s in symbols] 42 43 open(args.output, "w").writelines(output_lines) 44 45 46if __name__ == "__main__": 47 sys.exit(main()) 48