xref: /f-stack/dpdk/buildtools/map_to_win.py (revision 2d9fd380)
1*2d9fd380Sjfb8856606#!/usr/bin/env python3
2*2d9fd380Sjfb8856606# SPDX-License-Identifier: BSD-3-Clause
3*2d9fd380Sjfb8856606# Copyright(c) 2019 Intel Corporation
4*2d9fd380Sjfb8856606
5*2d9fd380Sjfb8856606import sys
6*2d9fd380Sjfb8856606from os.path import dirname, basename, join, exists
7*2d9fd380Sjfb8856606
8*2d9fd380Sjfb8856606
9*2d9fd380Sjfb8856606def is_function_line(ln):
10*2d9fd380Sjfb8856606    return ln.startswith('\t') and ln.endswith(';\n') and ":" not in ln
11*2d9fd380Sjfb8856606
12*2d9fd380Sjfb8856606# MinGW keeps the original .map file but replaces per_lcore* to __emutls_v.per_lcore*
13*2d9fd380Sjfb8856606def create_mingw_map_file(input_map, output_map):
14*2d9fd380Sjfb8856606    with open(input_map) as f_in, open(output_map, 'w') as f_out:
15*2d9fd380Sjfb8856606        f_out.writelines([lines.replace('per_lcore', '__emutls_v.per_lcore') for lines in f_in.readlines()])
16*2d9fd380Sjfb8856606
17*2d9fd380Sjfb8856606def main(args):
18*2d9fd380Sjfb8856606    if not args[1].endswith('version.map') or \
19*2d9fd380Sjfb8856606            not args[2].endswith('exports.def') and \
20*2d9fd380Sjfb8856606            not args[2].endswith('mingw.map'):
21*2d9fd380Sjfb8856606        return 1
22*2d9fd380Sjfb8856606
23*2d9fd380Sjfb8856606    if args[2].endswith('mingw.map'):
24*2d9fd380Sjfb8856606        create_mingw_map_file(args[1], args[2])
25*2d9fd380Sjfb8856606        return 0
26*2d9fd380Sjfb8856606
27*2d9fd380Sjfb8856606# special case, allow override if an def file already exists alongside map file
28*2d9fd380Sjfb8856606    override_file = join(dirname(args[1]), basename(args[2]))
29*2d9fd380Sjfb8856606    if exists(override_file):
30*2d9fd380Sjfb8856606        with open(override_file) as f_in:
31*2d9fd380Sjfb8856606            functions = f_in.readlines()
32*2d9fd380Sjfb8856606
33*2d9fd380Sjfb8856606# generate def file from map file.
34*2d9fd380Sjfb8856606# This works taking indented lines only which end with a ";" and which don't
35*2d9fd380Sjfb8856606# have a colon in them, i.e. the lines defining functions only.
36*2d9fd380Sjfb8856606    else:
37*2d9fd380Sjfb8856606        with open(args[1]) as f_in:
38*2d9fd380Sjfb8856606            functions = [ln[:-2] + '\n' for ln in sorted(f_in.readlines())
39*2d9fd380Sjfb8856606                         if is_function_line(ln)]
40*2d9fd380Sjfb8856606            functions = ["EXPORTS\n"] + functions
41*2d9fd380Sjfb8856606
42*2d9fd380Sjfb8856606    with open(args[2], 'w') as f_out:
43*2d9fd380Sjfb8856606        f_out.writelines(functions)
44*2d9fd380Sjfb8856606    return 0
45*2d9fd380Sjfb8856606
46*2d9fd380Sjfb8856606
47*2d9fd380Sjfb8856606if __name__ == "__main__":
48*2d9fd380Sjfb8856606    sys.exit(main(sys.argv))
49