Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar by removing 2 redundent forward declaration
[simgrid.git] / tools / stack-cleaner / as
1 #!/usr/bin/env python
2 # Wrapper around the real `as` which adds filtering capabilities.
3
4 import os
5 import re
6 import subprocess
7 import sys
8 import tempfile
9
10 # Process the arguments:
11 # * build the argument array for calling the real assembler;
12 # * get the input file name.
13 args = []
14 input_filename = None
15 args.append('as')
16 i = 1
17 while i < len(sys.argv):
18     if sys.argv[i] == '-o' or sys.argv[i] == '-I':
19         args.append(sys.argv[i])
20         if i + 1 >= len(sys.argv):
21             sys.stderr.write("Missing argument\n")
22             sys.exit(1)
23         args.append(sys.argv[i + 1])
24         i = i + 1
25     elif re.match('^-', sys.argv[i][0]):
26         args.append(sys.argv[i])
27     elif input_filename:
28         sys.stdout.write("Too many input files\n")
29         sys.exit(1)
30     else:
31         input_filename = sys.argv[i]
32     i = i + 1
33 if input_filename == None:
34     sys.stderr.write("Missing input file\n")
35     sys.exit(1)
36
37 temp_file, temp_filename = tempfile.mkstemp(suffix=".s", prefix="as_wrapper")
38 try:
39     # Generate temporary file with modified assembly code:
40     script_file = os.path.join(
41         os.path.dirname(sys.argv[0]), "clean-stack-filter")
42     input_file = os.open(input_filename, os.O_RDONLY)
43     status = subprocess.call([script_file], stdin=input_file, stdout=temp_file)
44     os.close(input_file)
45     if status != 0:
46         sys.stderr.write("Filtering the assembly code failed.\n")
47         sys.exit(status)
48
49     # Call the real assembler on this modified assembly code:
50     args.append(temp_filename)
51     sys.exit(subprocess.call(args))
52 finally:
53     os.remove(temp_filename)