|
| 1 | +import re |
| 2 | +import os |
| 3 | +import copy |
| 4 | + |
| 5 | +from os.path import relpath, join, exists |
| 6 | +from os import makedirs |
| 7 | + |
| 8 | +from tools.export.makefile import Makefile, GccArm, Armc5, IAR |
| 9 | + |
| 10 | + |
| 11 | +class Netbeans(Makefile): |
| 12 | + """Generic Netbeans project. Intended to be subclassed by classes that |
| 13 | + specify a type of Makefile. |
| 14 | + """ |
| 15 | + |
| 16 | + @property |
| 17 | + def flags(self): |
| 18 | + """Returns a dictionary of toolchain flags. |
| 19 | + Keys of the dictionary are: |
| 20 | + cxx_flags - c++ flags |
| 21 | + c_flags - c flags |
| 22 | + ld_flags - linker flags |
| 23 | + asm_flags - assembler flags |
| 24 | + common_flags - common options |
| 25 | +
|
| 26 | + The difference from the parent function is that it does not |
| 27 | + add macro definitions, since they are passed separately. |
| 28 | + """ |
| 29 | + |
| 30 | + config_header = self.toolchain.get_config_header() |
| 31 | + flags = {key + "_flags": copy.deepcopy(value) for key, value |
| 32 | + in self.toolchain.flags.iteritems()} |
| 33 | + if config_header: |
| 34 | + config_header = relpath(config_header, |
| 35 | + self.resources.file_basepath[config_header]) |
| 36 | + flags['c_flags'] += self.toolchain.get_config_option(config_header) |
| 37 | + flags['cxx_flags'] += self.toolchain.get_config_option( |
| 38 | + config_header) |
| 39 | + return flags |
| 40 | + |
| 41 | + def generate(self): |
| 42 | + """Generate Makefile, configurations.xml & project.xml Netbeans project file |
| 43 | + """ |
| 44 | + super(Netbeans, self).generate() |
| 45 | + |
| 46 | + defines = [] # list of tuples ('D'/'U', [key, value]) (value is optional) |
| 47 | + forced_includes = [] # list of strings |
| 48 | + sources = [] # list of strings |
| 49 | + headers = [] # list of strings |
| 50 | + |
| 51 | + next_is_include = False |
| 52 | + for f in self.flags['c_flags'] + self.flags['cxx_flags']: |
| 53 | + f = f.strip() |
| 54 | + if next_is_include: |
| 55 | + forced_includes.append(f) |
| 56 | + next_is_include = False |
| 57 | + continue |
| 58 | + if f.startswith('-D'): |
| 59 | + defines.append(('D', f[2:].split('=', 1))) |
| 60 | + elif f.startswith('-U'): |
| 61 | + defines.append(('U', [f[2:]])) |
| 62 | + elif f == "-include": |
| 63 | + next_is_include = True |
| 64 | + |
| 65 | + # Convert all Backslashes to Forward Slashes |
| 66 | + self.resources.win_to_unix() |
| 67 | + |
| 68 | + for r_type in ['c_sources', 's_sources', 'cpp_sources']: |
| 69 | + sources.extend(getattr(self.resources, r_type)) |
| 70 | + |
| 71 | + for r_type in ['headers']: |
| 72 | + headers.extend(getattr(self.resources, r_type)) |
| 73 | + |
| 74 | + starting_dot = re.compile(r'(^[.]/|^[.]$)') |
| 75 | + sources = [starting_dot.sub('', field) for field in sources] |
| 76 | + headers = [starting_dot.sub('', field) for field in headers] |
| 77 | + include_paths = [starting_dot.sub('', field) for field in self.resources.inc_dirs] |
| 78 | + |
| 79 | + headers.sort() |
| 80 | + sources.sort() |
| 81 | + |
| 82 | + headers_output = create_netbeans_file_list(headers) |
| 83 | + sources_output = create_netbeans_file_list(sources) |
| 84 | + ctx = { |
| 85 | + 'name': self.project_name, |
| 86 | + 'elf_location': join('BUILD', self.project_name) + '.elf', |
| 87 | + 'c_symbols': self.toolchain.get_symbols(), |
| 88 | + 'asm_symbols': self.toolchain.get_symbols(True), |
| 89 | + 'c_flags': self.flags['c_flags'], |
| 90 | + 'cxx_flags': self.flags['cxx_flags'], |
| 91 | + 'ld_flags': self.flags['ld_flags'], |
| 92 | + 'asm_flags': self.flags['asm_flags'], |
| 93 | + 'common_flags': self.flags['common_flags'], |
| 94 | + 'target': self.target, |
| 95 | + 'include_paths': include_paths, |
| 96 | + 'sources': sources, |
| 97 | + 'headers': headers, |
| 98 | + 'headers_folder': headers_output, |
| 99 | + 'sources_folder': sources_output, |
| 100 | + 'load_exe': str(self.LOAD_EXE).lower() |
| 101 | + } |
| 102 | + |
| 103 | + if not exists(join(self.export_dir, 'nbproject')): |
| 104 | + makedirs(join(self.export_dir, 'nbproject')) |
| 105 | + |
| 106 | + self.gen_file('nb/configurations.tmpl', ctx, 'nbproject/configurations.xml') |
| 107 | + self.gen_file('nb/project.tmpl', ctx, 'nbproject/project.xml') |
| 108 | + self.gen_file('nb/mbedignore.tmpl', ctx, '.mbedignore') |
| 109 | + |
| 110 | + |
| 111 | +def create_netbeans_file_list(file_list): |
| 112 | + output = [] |
| 113 | + prev_dir = '' |
| 114 | + folder_count = 1 |
| 115 | + for idx, item in enumerate(file_list): |
| 116 | + cur_dir = os.path.dirname(item) |
| 117 | + dir_temp = os.path.normpath(cur_dir) |
| 118 | + prev_dir_temp = os.path.normpath(prev_dir) |
| 119 | + dir_list = dir_temp.split(os.sep) |
| 120 | + prev_dir_list = prev_dir_temp.split(os.sep) |
| 121 | + dir_depth = len(dir_list) |
| 122 | + |
| 123 | + # print 'PREV_DIR: ' + str(prev_dir_list) |
| 124 | + |
| 125 | + # print 'CURR_DIR: ' + str(dir_list) |
| 126 | + |
| 127 | + # Current File is in Directory: Compare the given dir with previous Dir |
| 128 | + if cur_dir and prev_dir != cur_dir: |
| 129 | + # evaluate all matched items (from current and previous list) |
| 130 | + matched = [] |
| 131 | + for element in dir_list: |
| 132 | + if element in prev_dir_list: |
| 133 | + matched.append(element) |
| 134 | + |
| 135 | + # calculate difference between matched and length |
| 136 | + diff = dir_depth - len(matched) |
| 137 | + |
| 138 | + # print 'MATCHED: ' + str(matched) |
| 139 | + |
| 140 | + # if previous dir was not root |
| 141 | + if prev_dir != '': |
| 142 | + # if the elements count is not equal we calculate the difference |
| 143 | + if len(dir_list) != len(prev_dir_list): |
| 144 | + dir_depth_prev = len(prev_dir_list) |
| 145 | + delta = dir_depth_prev - len(matched) |
| 146 | + |
| 147 | + for i in range(dir_depth_prev - delta, dir_depth_prev): |
| 148 | + output.append('</logicalFolder>') |
| 149 | + |
| 150 | + # if the elements count is equal, we subtract the matched length from the total length |
| 151 | + else: |
| 152 | + for i in range(len(matched), len(dir_list)): |
| 153 | + output.append('</logicalFolder>') |
| 154 | + |
| 155 | + for i in range(dir_depth - diff, dir_depth): |
| 156 | + output.append('<logicalFolder name="f' + str(folder_count) + '" displayName="' + str( |
| 157 | + dir_list[i]) + '" projectFiles="true">') |
| 158 | + folder_count += 1 |
| 159 | + |
| 160 | + # Current File is in root |
| 161 | + else: |
| 162 | + # Close Tag if we are in root and the previous dir wasn't |
| 163 | + if cur_dir == '' and prev_dir != '': |
| 164 | + for i in range(0, len(prev_dir_list)): |
| 165 | + output.append('</logicalFolder>') |
| 166 | + |
| 167 | + # Save the Current Dir |
| 168 | + prev_dir = cur_dir |
| 169 | + output.append('<itemPath>' + str(item) + '</itemPath>') |
| 170 | + # if last iteration close all open tags |
| 171 | + if idx == len(file_list) - 1 and cur_dir != '': |
| 172 | + for i in range(0, len(dir_list)): |
| 173 | + output.append('</logicalFolder>') |
| 174 | + |
| 175 | + return output |
| 176 | + |
| 177 | + |
| 178 | +class NetbeansGcc(Netbeans, GccArm): |
| 179 | + LOAD_EXE = True |
| 180 | + NAME = "Netbeans-GCC-ARM" |
| 181 | + |
| 182 | + |
| 183 | +class NetbeansArmc5(Netbeans, Armc5): |
| 184 | + LOAD_EXE = False |
| 185 | + NAME = "Netbeans-Armc5" |
| 186 | + |
| 187 | + |
| 188 | +class NetbeansIAR(Netbeans, IAR): |
| 189 | + LOAD_EXE = True |
| 190 | + NAME = "Netbeans-IAR" |
0 commit comments