blob: 85daeb65ba4a0747c2e37b4387433123e61bcf20 [file] [log] [blame]
Googler48859c12016-09-23 17:16:37 +00001# pylint: disable=g-bad-file-header
2# Copyright 2016 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Creates symbolic links for .o files with hashcode.
17
18This script reads the file list containing the input files, creates symbolic
19links with a path-hash appended to their original name (foo.o becomes
20foo_{md5sum}.o), then saves the list of symbolic links to another file.
21
22This is to circumvent a bug in the original libtool that arises when two input
23files have the same base name (even if they are in different directories).
24"""
25
26import hashlib
27import os
28import sys
29
30
31def main():
32 obj_file_list = open(sys.argv[1])
33 hashed_obj_file_list = open(sys.argv[2], 'w')
34
35 for line in obj_file_list:
36 obj_file_path = line.rstrip('\n')
37 hashed_obj_file_path = '%s_%s.o' % (
38 os.path.splitext(obj_file_path)[0],
cparsons49bb7232017-08-23 19:02:52 +020039 hashlib.md5(obj_file_path.encode('utf-8')).hexdigest())
Googler48859c12016-09-23 17:16:37 +000040
41 hashed_obj_file_list.write(hashed_obj_file_path + '\n')
42
43 # Create symlink only if the symlink doesn't exist.
44 if not os.path.exists(hashed_obj_file_path):
45 os.symlink(os.path.basename(obj_file_path), hashed_obj_file_path)
46
47 hashed_obj_file_list.close()
48
49if __name__ == '__main__':
50 main()