1# 2# File : vs.py 3# This file is part of RT-Thread RTOS 4# COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License along 17# with this program; if not, write to the Free Software Foundation, Inc., 18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19# 20# Change Logs: 21# Date Author Notes 22# 2015-01-20 Bernard Add copyright information 23# 24 25import os 26import sys 27import string 28import uuid 29import utils 30from xml.etree.ElementTree import SubElement 31from utils import _make_path_relative 32from utils import xml_indent 33 34# Add parent directory to path to import building 35sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 36import building 37 38import xml.etree.ElementTree as etree 39fs_encoding = sys.getfilesystemencoding() 40 41def VS_AddGroup(ProjectFiles, parent, name, files, libs, project_path): 42 Filter = SubElement(parent, 'Filter') 43 Filter.set('Name', name) #set group name to group 44 45 for f in files: 46 fn = f.rfile() 47 name = fn.name 48 path = os.path.dirname(fn.abspath) 49 50 path = _make_path_relative(project_path, path) 51 path = os.path.join(path, name) 52 try: 53 path = path.decode(fs_encoding) 54 except: 55 path = path 56 File = SubElement(Filter, 'File') 57 File.set('RelativePath', path) 58 59 for lib in libs: 60 name = os.path.basename(lib) 61 path = os.path.dirname(lib) 62 63 path = _make_path_relative(project_path, path) 64 path = os.path.join(path, name) 65 66 File = SubElement(Filter, 'File') 67 try: 68 path = path.decode(fs_encoding) 69 except: 70 path = path 71 File.set('RelativePath', path) 72 73def VS_AddHeadFilesGroup(program, elem, project_path): 74 utils.source_ext = [] 75 utils.source_ext = ["h"] 76 for item in program: 77 utils.walk_children(item) 78 utils.source_list.sort() 79 # print utils.source_list 80 81 for f in utils.source_list: 82 path = _make_path_relative(project_path, f) 83 File = SubElement(elem, 'File') 84 try: 85 path = path.decode(fs_encoding) 86 except: 87 path = path 88 File.set('RelativePath', path) 89 90def VSProject(target, script, program): 91 project_path = os.path.dirname(os.path.abspath(target)) 92 93 tree = etree.parse('template_vs2005.vcproj') 94 root = tree.getroot() 95 96 out = open(target, 'w') 97 out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n') 98 99 ProjectFiles = [] 100 101 # add "*.c" files group 102 for elem in tree.iter(tag='Filter'): 103 if elem.attrib['Name'] == 'Source Files': 104 #print elem.tag, elem.attrib 105 break 106 107 for group in script: 108 libs = [] 109 if 'LIBS' in group and group['LIBS']: 110 for item in group['LIBS']: 111 lib_path = '' 112 for path_item in group['LIBPATH']: 113 full_path = os.path.join(path_item, item + '.lib') 114 if os.path.isfile(full_path): # has this library 115 lib_path = full_path 116 117 if lib_path != '': 118 libs.append(lib_path) 119 120 group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], libs, project_path) 121 122 # add "*.h" files group 123 for elem in tree.iter(tag='Filter'): 124 if elem.attrib['Name'] == 'Header Files': 125 break 126 VS_AddHeadFilesGroup(program, elem, project_path) 127 128 # write head include path 129 if 'CPPPATH' in building.Env: 130 cpp_path = building.Env['CPPPATH'] 131 paths = set() 132 for path in cpp_path: 133 inc = _make_path_relative(project_path, os.path.normpath(path)) 134 paths.add(inc) #.replace('\\', '/') 135 136 paths = [i for i in paths] 137 paths.sort() 138 cpp_path = ';'.join(paths) 139 140 # write include path, definitions 141 for elem in tree.iter(tag='Tool'): 142 if elem.attrib['Name'] == 'VCCLCompilerTool': 143 #print elem.tag, elem.attrib 144 break 145 elem.set('AdditionalIncludeDirectories', cpp_path) 146 147 # write cppdefinitons flags 148 if 'CPPDEFINES' in building.Env: 149 CPPDEFINES = building.Env['CPPDEFINES'] 150 definitions = [] 151 if type(CPPDEFINES[0]) == type(()): 152 for item in CPPDEFINES: 153 definitions += [i for i in item] 154 definitions = ';'.join(definitions) 155 else: 156 definitions = ';'.join(building.Env['CPPDEFINES']) 157 elem.set('PreprocessorDefinitions', definitions) 158 # write link flags 159 160 # write lib dependence 161 if 'LIBS' in building.Env: 162 for elem in tree.iter(tag='Tool'): 163 if elem.attrib['Name'] == 'VCLinkerTool': 164 break 165 libs_with_extention = [i+'.lib' for i in building.Env['LIBS']] 166 libs = ' '.join(libs_with_extention) 167 elem.set('AdditionalDependencies', libs) 168 169 # write lib include path 170 if 'LIBPATH' in building.Env: 171 lib_path = building.Env['LIBPATH'] 172 paths = set() 173 for path in lib_path: 174 inc = _make_path_relative(project_path, os.path.normpath(path)) 175 paths.add(inc) #.replace('\\', '/') 176 177 paths = [i for i in paths] 178 paths.sort() 179 lib_paths = ';'.join(paths) 180 elem.set('AdditionalLibraryDirectories', lib_paths) 181 182 xml_indent(root) 183 text = etree.tostring(root, encoding='utf-8') 184 try: 185 text = text.decode(encoding="utf-8") 186 except: 187 text = text 188 out.write(text) 189 out.close() 190