1 | /* |
---|
2 | * Copyright (c) 2008 |
---|
3 | * Embedded Brains GmbH |
---|
4 | * Obere Lagerstr. 30 |
---|
5 | * D-82178 Puchheim |
---|
6 | * Germany |
---|
7 | * rtems@embedded-brains.de |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be found in the file |
---|
10 | * LICENSE in this distribution or at http://www.rtems.com/license/LICENSE. |
---|
11 | */ |
---|
12 | |
---|
13 | package org.rtems.cdt; |
---|
14 | |
---|
15 | import java.io.BufferedReader; |
---|
16 | import java.io.IOException; |
---|
17 | import java.io.InputStream; |
---|
18 | import java.io.InputStreamReader; |
---|
19 | import java.util.LinkedList; |
---|
20 | import java.util.List; |
---|
21 | import java.util.Map; |
---|
22 | |
---|
23 | import org.eclipse.core.resources.IProject; |
---|
24 | import org.eclipse.core.runtime.CoreException; |
---|
25 | import org.eclipse.core.runtime.IPath; |
---|
26 | import org.eclipse.core.runtime.Path; |
---|
27 | import org.eclipse.core.runtime.QualifiedName; |
---|
28 | import org.rtems.cdt.Activator; |
---|
29 | |
---|
30 | public class Storage { |
---|
31 | private static final String OPTION_SEPARATOR = "\0"; |
---|
32 | |
---|
33 | private static final String VALUE_START_TOKEN = "\t"; |
---|
34 | |
---|
35 | private static final int EXPECT_OPTION = 0; |
---|
36 | |
---|
37 | private static final int EXPECT_COMMAND = 1; |
---|
38 | |
---|
39 | private static final int EXPECT_KEY = 2; |
---|
40 | |
---|
41 | private static final int TOOL_COMPLETE = 3; |
---|
42 | |
---|
43 | public static String getPreference( String key) { |
---|
44 | return Activator.getDefault().getPreferenceStore().getString( key); |
---|
45 | } |
---|
46 | |
---|
47 | public static String getPristineProperty( IProject project, String key) { |
---|
48 | String value = null; |
---|
49 | |
---|
50 | try { |
---|
51 | value = project.getPersistentProperty( new QualifiedName( "", key)); |
---|
52 | } catch (CoreException e) { |
---|
53 | e.printStackTrace(); |
---|
54 | } |
---|
55 | |
---|
56 | return value; |
---|
57 | } |
---|
58 | |
---|
59 | public static String getProperty( IProject project, String key) { |
---|
60 | String value = getPristineProperty( project, key); |
---|
61 | |
---|
62 | if (value == null) { |
---|
63 | if (key.startsWith( Constants.TOOL_KEY_PREFIX)) { |
---|
64 | updateTools( project); |
---|
65 | } else { |
---|
66 | value = Activator.getDefault().getPreferenceStore().getString( key); |
---|
67 | setProperty( project, key, value); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | return value; |
---|
72 | } |
---|
73 | |
---|
74 | public static void setProperty( IProject project, String key, String value) { |
---|
75 | try { |
---|
76 | project.setPersistentProperty( new QualifiedName( "", key), value); |
---|
77 | } catch (CoreException e) { |
---|
78 | e.printStackTrace(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | public static void updateTools( IProject project) { |
---|
83 | // Create make process builder |
---|
84 | ProcessBuilder pb = new ProcessBuilder( "make"); |
---|
85 | |
---|
86 | // Provide RTEMS_MAKEFILE_PATH environment variable |
---|
87 | Map<String, String> env = pb.environment(); |
---|
88 | env.put( "RTEMS_MAKEFILE_PATH", getProperty( project, Constants.BSP_PATH_KEY)); |
---|
89 | |
---|
90 | // Change working directory to the Makefile location |
---|
91 | pb.directory( Activator.getDefault().getMakefileLocation().toFile()); |
---|
92 | |
---|
93 | // Start make process and parse its output |
---|
94 | Process p = null; |
---|
95 | try { |
---|
96 | p = pb.start(); |
---|
97 | InputStream is = p.getInputStream(); |
---|
98 | BufferedReader br = new BufferedReader( new InputStreamReader( is)); |
---|
99 | String line = br.readLine(); |
---|
100 | String key = null; |
---|
101 | String command = null; |
---|
102 | List<String> options = new LinkedList<String>(); |
---|
103 | int state = EXPECT_KEY; |
---|
104 | while (line != null) { |
---|
105 | switch (state) { |
---|
106 | case EXPECT_OPTION: |
---|
107 | if (line.startsWith( VALUE_START_TOKEN)) { |
---|
108 | options.add( line.substring( 1)); |
---|
109 | } else { |
---|
110 | state = TOOL_COMPLETE; |
---|
111 | continue; |
---|
112 | } |
---|
113 | break; |
---|
114 | case EXPECT_COMMAND: |
---|
115 | if (line.startsWith( VALUE_START_TOKEN)) { |
---|
116 | command = line.substring( 1); |
---|
117 | state = EXPECT_OPTION; |
---|
118 | } else { |
---|
119 | throw new IOException( "Unexpected line format"); |
---|
120 | } |
---|
121 | break; |
---|
122 | case EXPECT_KEY: |
---|
123 | if (line.length() > Constants.TOOL_KEY_PREFIX.length()) { |
---|
124 | key = line; |
---|
125 | state = EXPECT_COMMAND; |
---|
126 | } else { |
---|
127 | throw new IOException( "Unexpected line format"); |
---|
128 | } |
---|
129 | break; |
---|
130 | case TOOL_COMPLETE: |
---|
131 | updateTool( project, key, command, options); |
---|
132 | options.clear(); |
---|
133 | state = EXPECT_KEY; |
---|
134 | continue; |
---|
135 | default: |
---|
136 | throw new IOException( "Unexpected state"); |
---|
137 | } |
---|
138 | line = br.readLine(); |
---|
139 | } |
---|
140 | if (state == EXPECT_OPTION) { |
---|
141 | updateTool( project, key, command, options); |
---|
142 | } |
---|
143 | } catch (IOException e) { |
---|
144 | e.printStackTrace(); |
---|
145 | } finally { |
---|
146 | while (true) { |
---|
147 | try { |
---|
148 | p.waitFor(); |
---|
149 | break; |
---|
150 | } catch (InterruptedException e) { |
---|
151 | continue; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | private static void updateTool( IProject project, String toolKey, String command, List<String> options) { |
---|
158 | List<String> filteredOptions = new LinkedList<String>(); |
---|
159 | |
---|
160 | // Filter options |
---|
161 | if (toolKey.startsWith( Constants.COMPILER_KEY_PREFIX) || toolKey.startsWith( Constants.LINKER_KEY_PREFIX)) { |
---|
162 | for (String option : options) { |
---|
163 | if (!(option.isEmpty() || option.trim().matches( "^-c|-O[0123s]|-g|-W[\\w-]*$"))) { |
---|
164 | filteredOptions.add( option); |
---|
165 | } |
---|
166 | } |
---|
167 | } else { |
---|
168 | filteredOptions = options; |
---|
169 | } |
---|
170 | |
---|
171 | // Transform filtered option list into option string value |
---|
172 | String optionsValue = new String(); |
---|
173 | if (!options.isEmpty()) { |
---|
174 | optionsValue = filteredOptions.get( 0); |
---|
175 | filteredOptions.remove( 0); |
---|
176 | } |
---|
177 | for (String option : filteredOptions) { |
---|
178 | optionsValue += OPTION_SEPARATOR + option; |
---|
179 | } |
---|
180 | |
---|
181 | // Set properties |
---|
182 | setProperty( project, toolKey, command); |
---|
183 | setProperty( project, toolKey + Constants.TOOL_OPTIONS_KEY_POSTFIX, optionsValue); |
---|
184 | } |
---|
185 | |
---|
186 | public static String [] getToolOptions( IProject project, String toolKey) { |
---|
187 | String optionsValue = getProperty( project, toolKey + Constants.TOOL_OPTIONS_KEY_POSTFIX); |
---|
188 | |
---|
189 | return optionsValue.split( OPTION_SEPARATOR); |
---|
190 | } |
---|
191 | |
---|
192 | private Storage() { |
---|
193 | // Do nothing |
---|
194 | } |
---|
195 | } |
---|