Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 1 | # 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 | |
laszlocsomor | f11c6bc | 2018-07-05 01:58:06 -0700 | [diff] [blame] | 18 | This script reads the file list containing the input files, creates |
| 19 | symbolic links with a path-hash appended to their original name (foo.o |
| 20 | becomes foo_{md5sum}.o), then saves the list of symbolic links to another |
| 21 | file. |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 22 | |
laszlocsomor | f11c6bc | 2018-07-05 01:58:06 -0700 | [diff] [blame] | 23 | This is to circumvent a bug in the original libtool that arises when two |
| 24 | input files have the same base name (even if they are in different |
| 25 | directories). |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 26 | """ |
| 27 | |
| 28 | import hashlib |
| 29 | import os |
| 30 | import sys |
| 31 | |
| 32 | |
| 33 | def main(): |
laszlocsomor | f11c6bc | 2018-07-05 01:58:06 -0700 | [diff] [blame] | 34 | with open(sys.argv[1]) as obj_file_list: |
| 35 | with open(sys.argv[2], 'w') as hashed_obj_file_list: |
| 36 | for line in obj_file_list: |
| 37 | obj_file_path = line.rstrip('\n') |
| 38 | hashed_obj_file_path = '%s_%s.o' % ( |
| 39 | os.path.splitext(obj_file_path)[0], |
| 40 | hashlib.md5(obj_file_path.encode('utf-8')).hexdigest()) |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 41 | |
laszlocsomor | f11c6bc | 2018-07-05 01:58:06 -0700 | [diff] [blame] | 42 | hashed_obj_file_list.write(hashed_obj_file_path + '\n') |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 43 | |
laszlocsomor | f11c6bc | 2018-07-05 01:58:06 -0700 | [diff] [blame] | 44 | # Create symlink only if the symlink doesn't exist. |
| 45 | if not os.path.exists(hashed_obj_file_path): |
| 46 | os.symlink(os.path.basename(obj_file_path), |
| 47 | hashed_obj_file_path) |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 48 | |
Googler | 48859c1 | 2016-09-23 17:16:37 +0000 | [diff] [blame] | 49 | |
| 50 | if __name__ == '__main__': |
| 51 | main() |