1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied.  See the License for the
15# specific language governing permissions and limitations
16# under the License.
17#
18#
19#
20# transform_config_hw.py -- Generate svn_private_config.h
21# from svn_private_config.hw while editing SVN_BUILD_HOST
22
23
24import os
25import re
26import sys
27import platform
28
29
30def usage_and_exit(msg):
31  if msg:
32    sys.stderr.write('%s\n\n' % msg)
33  sys.stderr.write(
34    'USAGE: %s ARCHITECTURE TEMPLATE_FILE [OUTPUT_FILE]\n'
35    '  stdout will be used if OUTPUT_FILE is not provided.\n'
36    % os.path.basename(sys.argv[0]))
37  sys.stderr.flush()
38  sys.exit(1)
39
40
41_wincpu_map = {
42    'x86': 'x86',
43    'x64': 'x86_64',
44    'amd64': 'x86_64',
45    'x86_64': 'x86_64',
46    'ia64': 'ia64',
47    'powerpc': 'powerpc',
48    'alpha': 'alpha',
49    }
50
51_arch_map = _wincpu_map.copy()
52_arch_map.update({
53    'win32': 'x86',
54    })
55
56_interesting_rx = re.compile(
57    r'^\s*#\s*define\s+SVN_BUILD_HOST\s+(?P<host>"[^"]+")\s*$')
58
59def process_header(input, output, architecture):
60    uname = platform.uname()
61    winver = uname[3]
62    wincpu = _wincpu_map.get(uname[4].lower(), 'unknown')
63    arch = _arch_map.get(architecture.lower(), 'unknown')
64
65    if wincpu == arch:
66        host = '"%s-microsoft-windows%s"' % (arch, winver)
67    else:
68        host = '"%s/%s-microsoft-windows%s"' % (arch, wincpu, winver)
69
70    for line in input.split('\n'):
71        match = _interesting_rx.match(line)
72        if match is not None:
73            line = line.replace(match.group('host'), host)
74        output.write(line + '\n')
75
76
77def main(input_filepath, output, architecture):
78    filename = os.path.basename(input_filepath)
79    input = open(input_filepath, 'r').read()
80
81    output.write(
82        '/* This file is automatically generated from %s.\n'
83        ' * Do not edit this file -- edit the source and rerun gen-make.py */'
84        '\n\n'
85        % (filename,))
86
87    process_header(input, output, architecture)
88
89
90if __name__ == '__main__':
91    if os.name != 'nt':
92        usage_and_exit('This script should only be run on Windows')
93
94    if len(sys.argv) < 3 or len(sys.argv) > 4:
95        usage_and_exit('Incorrect number of arguments')
96
97    architecture = sys.argv[1]
98    input_filepath = sys.argv[2]
99
100    if len(sys.argv) > 3:
101        output_file = open(sys.argv[3], 'w')
102    else:
103        output_file = sys.stdout
104
105    main(input_filepath, output_file, architecture)
106