1# Copyright 2016 Google Inc. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS-IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14from typing import List 15 16 17def generate_makefile(cpp_files: List[str], executable_name: str, compile_command: str, link_command: str, link_command_suffix: str): 18 assert executable_name + '.cpp' in cpp_files, '%s.cpp in %s' % (executable_name, cpp_files) 19 20 link_rule_template = """ 21{executable_name}: {object_files} 22\t{link_command} {object_files} -o {executable_name} {link_command_suffix} 23 24{executable_name}_ram.txt: {object_files_ram_txt} 25\t(cat {object_files_ram_txt}; /bin/time -v {link_command} {object_files} -o {executable_name}.tmp {link_command_suffix} 2>&1 | fgrep 'Maximum resident set size' | sed 's|.*: ||') >{executable_name}_ram.txt 26""" 27 compile_rule_template = """ 28{name}.o: {name}.cpp 29\t{compile_command} -c {name}.cpp -o {name}.o 30 31{name}.o_ram.txt: {name}.cpp 32\t/bin/time -v {compile_command} -c {name}.cpp -o {name}.o 2>&1 | fgrep 'Maximum resident set size' | sed 's|.*: ||' >{name}.o_ram.txt 33""" 34 35 clean_rule_template = """ 36clean: 37\trm -f {object_files} {dep_files} {executable_name} {executable_name}_ram.txt {object_files_ram_txt} 38""" 39 40 dep_file_deps = """ 41%.d: ; 42""" 43 44 dep_files_includes_template = """ 45include {dep_files} 46""" 47 48 compile_rules = [] 49 object_files = [] 50 object_files_ram_txt = [] 51 dep_files = [] 52 for cpp_file in cpp_files: 53 assert cpp_file.endswith('.cpp') 54 source = cpp_file[:-len('.cpp')] 55 56 compile_rule = compile_rule_template.format( 57 name=source, 58 compile_command=compile_command) 59 compile_rules.append(compile_rule) 60 object_files.append('%s.o' % source) 61 object_files_ram_txt.append('%s.o_ram.txt' % source) 62 dep_files.append('%s.d' % source) 63 64 link_rule = link_rule_template.format( 65 object_files=' '.join(object_files), 66 object_files_ram_txt=' '.join(object_files_ram_txt), 67 link_command=link_command, 68 link_command_suffix=link_command_suffix, 69 executable_name=executable_name) 70 71 clean_rule = clean_rule_template.format( 72 object_files=' '.join(object_files), 73 object_files_ram_txt=' '.join(object_files_ram_txt), 74 executable_name=executable_name, 75 dep_files=' '.join(dep_files)) 76 77 dep_files_includes = dep_files_includes_template.format(dep_files=' '.join(dep_files)) 78 79 # We put the link rule first so that it's the default Make target. 80 return link_rule + ''.join(compile_rules) + clean_rule + dep_file_deps + dep_files_includes 81