source: rtems-eclipse-plug-in/org.rtems.cdt.toolchain2/org/rtems/cdt/Storage.java @ f4f1f44

Last change on this file since f4f1f44 was f4f1f44, checked in by Sebastian Huber <sebastian.huber@…>, on 12/04/08 at 11:26:34

Added preferences for Cygwin, MinGW and MSYS paths on windows.

  • Property mode set to 100644
File size: 9.8 KB
Line 
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
13package org.rtems.cdt;
14
15import java.io.BufferedReader;
16import java.io.File;
17import java.io.IOException;
18import java.io.InputStream;
19import java.io.InputStreamReader;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23
24import org.eclipse.cdt.build.core.scannerconfig.CfgInfoContext;
25import org.eclipse.cdt.build.internal.core.scannerconfig.CfgDiscoveredPathManager;
26import org.eclipse.cdt.core.model.CoreModel;
27import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
28import org.eclipse.cdt.core.settings.model.ICProjectDescription;
29import org.eclipse.cdt.managedbuilder.core.IConfiguration;
30import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
31import org.eclipse.core.resources.IProject;
32import org.eclipse.core.runtime.CoreException;
33import org.eclipse.core.runtime.IPath;
34import org.eclipse.core.runtime.Path;
35import org.eclipse.core.runtime.Platform;
36import org.eclipse.core.runtime.QualifiedName;
37
38public class Storage {
39        private static final String OPTION_SEPARATOR = "\0";
40       
41        private static final String VALUE_START_TOKEN = "\t";
42       
43        private static final int EXPECT_OPTION = 0;
44       
45        private static final int EXPECT_COMMAND = 1;
46       
47        private static final int EXPECT_KEY = 2;
48       
49        private static final int TOOL_COMPLETE = 3;
50
51        public static String getPreference( String key) {
52                return Activator.getDefault().getPreferenceStore().getString( key);
53        }
54       
55        public static String getPristineProperty( IProject project, String key) {
56                String value = null;
57               
58                try {
59                        value = project.getPersistentProperty( new QualifiedName( "", key));
60                } catch (CoreException e) {
61                        e.printStackTrace();
62                }
63               
64                return value;
65        }
66       
67        public static String getProperty( IProject project, String key) {
68                String value = getPristineProperty( project, key);
69               
70                if (value == null) {
71                        if (key.startsWith( Constants.TOOL_KEY_PREFIX)) {
72                                changePlatform( project, Constants.PLATFORM_DEFAULT);
73                        } else {
74                                value = getPreference( key);
75                                setProperty( project, key, value);
76                        }
77                }
78               
79                return value;
80        }
81       
82        public static void setProperty( IProject project, String key, String value) {
83                try {
84                        project.setPersistentProperty( new QualifiedName( "", key), value);
85                } catch (CoreException e) {
86                        e.printStackTrace();
87                }
88        }
89       
90        public static IConfiguration [] getConfigurations( IProject project) {
91                ICProjectDescription pd = CoreModel.getDefault().getProjectDescription( project);
92               
93                ICConfigurationDescription cds [] = pd.getConfigurations();
94                IConfiguration cfgs [] = new IConfiguration [cds.length];
95                for (int i = 0; i < cds.length; ++i) {
96                        cfgs [i] = ManagedBuildManager.getConfigurationForDescription( cds [i]);
97                }
98
99                return cfgs;
100        }
101       
102        public static IConfiguration getActiveConfiguration( IProject project) {
103                ICProjectDescription pd = CoreModel.getDefault().getProjectDescription( project);
104               
105                ICConfigurationDescription cd = pd.getActiveConfiguration();
106                IConfiguration cfg = ManagedBuildManager.getConfigurationForDescription( cd);
107               
108                return cfg;
109        }
110       
111        public static String prependToPath( String path, String part) {
112                if (path == null || path.isEmpty()) {
113                        return part;
114                } else {
115                        return part + Constants.PATH_SEPARATOR + path;
116                }
117        }
118       
119        public static String prependToPathByPreference( String path, String key) {
120                String basePath = getPreference( key);
121               
122                if (basePath != null) {
123                        IPath part = new Path( basePath).append( "bin");
124                       
125                        return prependToPath( path, part.toOSString());
126                }
127               
128                return path;
129        }
130       
131        public static String prependToPathByProperty( IProject project, String path, String key) {
132                String basePath = getProperty( project, key);
133               
134                if (basePath != null) {
135                        IPath part = new Path( basePath).append( "bin");
136                       
137                        return prependToPath( path, part.toOSString());
138                }
139               
140                return path;
141        }
142       
143        public static void clearPlatform( IProject project) {
144                setProperty( project, Constants.PLATFORM_KEY, null);
145       
146                // Delete discovered paths for all configurations of the project
147                for (IConfiguration cfg : getConfigurations( project)) {
148                        CfgDiscoveredPathManager.getInstance().removeDiscoveredInfo(
149                                project,
150                                new CfgInfoContext( cfg)
151                        );
152                }
153        }
154       
155        public static String getPlatform( IProject project) {
156                return getPristineProperty( project, Constants.PLATFORM_KEY);
157        }
158       
159        public static void changePlatform( IProject project, String newPlatform) {
160                String platform = getPlatform( project);
161               
162                // Check if we have already the requested platform
163                if (platform != null && platform == newPlatform) {
164                        // Nothing to do
165                        return;
166                }
167               
168                // Set new platform
169                setProperty( project, Constants.PLATFORM_KEY, newPlatform);
170               
171                // Update path prepends
172                String path = null;
173                if (Platform.getOS().equals( Platform.OS_WIN32)) {
174                        if (newPlatform.equals( Constants.PLATFORM_CYGWIN)) {
175                                path = prependToPathByPreference( path, Constants.CYGWIN_PATH_KEY);
176                        } else {
177                                path = prependToPathByPreference( path, Constants.MINGW_PATH_KEY);
178                                path = prependToPathByPreference( path, Constants.MSYS_PATH_KEY);
179                        }
180                }
181                path = prependToPathByProperty( project, path, Constants.BASE_PATH_KEY);
182                setProperty( project, Constants.PATH_PREPEND_KEY, path);
183               
184                // Update tools
185                updateTools( project, newPlatform);
186        }
187       
188        private static void updateTools( IProject project, String platform) {
189                String bspPath = getProperty( project, Constants.BSP_PATH_KEY);
190                IPath make = new Path( "make");
191
192                // Translate path if necessary
193                if (Platform.getOS().equals( Platform.OS_WIN32)) {
194                        if (platform.equals( Constants.PLATFORM_CYGWIN)) {
195                                String s [] = bspPath.split( ":");
196                                if (s.length > 0) {
197                                        bspPath = bspPath.replaceFirst( "^" + s [0] + ":", "/cygdrive/" + s [0]);
198                                }
199                        }
200                        bspPath = bspPath.replaceAll( "\\\\", "/");
201                }
202       
203                // Create make process builder
204                ProcessBuilder pb = new ProcessBuilder();
205               
206                // Change working directory to the Makefile location
207                pb.directory( Activator.getDefault().getMakefileLocation().toFile());
208               
209                // Update path environment variable
210                Map<String, String> env = pb.environment();
211                String path = env.get( Constants.PATH_VARIABLE_NAME);
212                String part = getProperty( project, Constants.PATH_PREPEND_KEY);
213                path = Storage.prependToPath( path, part);
214                env.put( Constants.PATH_VARIABLE_NAME, path);
215               
216                // On windows we have to search for the make program in the new path environment
217                if (Platform.getOS().equals( Platform.OS_WIN32)) {
218                        String parts [] = path.split( Constants.PATH_SEPARATOR);
219                        for (String p : parts) {
220                                IPath makeCandidate = new Path( p).append( "make.exe");
221                                File file = new File( makeCandidate.toOSString());
222                                if (file.exists()) {
223                                        make = makeCandidate;
224                                        break;
225                                }
226                        }
227                }
228               
229                // Set command line
230                pb.command(
231                        make.toOSString(),
232                        Constants.BSP_PATH_MAKE_VARIABLE + "=" + bspPath
233                );
234               
235                // Start make process and parse its output
236                Process p = null;
237                try {
238                        p = pb.start();
239                    InputStream is = p.getInputStream();
240                    BufferedReader br = new BufferedReader( new InputStreamReader( is));
241                    String line = br.readLine();
242                    String key = null;
243                    String command = null;
244                    List<String> options = new LinkedList<String>();
245                    int state = EXPECT_KEY;
246                    while (line != null) {
247                        switch (state) {
248                                case EXPECT_OPTION:
249                                        if (line.startsWith( VALUE_START_TOKEN)) {
250                                                options.add( line.substring( 1));
251                                        } else {
252                                                state = TOOL_COMPLETE;
253                                                continue;
254                                        }
255                                        break;
256                                case EXPECT_COMMAND:
257                                        if (line.startsWith( VALUE_START_TOKEN)) {
258                                                command = line.substring( 1);
259                                                state = EXPECT_OPTION;
260                                        } else {
261                                                throw new IOException( "Unexpected line format");
262                                        }
263                                        break;
264                                case EXPECT_KEY:
265                                        if (line.length() > Constants.TOOL_KEY_PREFIX.length()) {
266                                                key = line;
267                                                state = EXPECT_COMMAND;
268                                        } else {
269                                                throw new IOException( "Unexpected line format");
270                                        }
271                                        break;
272                                case TOOL_COMPLETE:
273                                        updateTool( project, key, command, options);
274                                        options.clear();
275                                        state = EXPECT_KEY;
276                                        continue;
277                                default:
278                                        throw new IOException( "Unexpected state");
279                        }
280                        line = br.readLine();
281                    }
282                    if (state == EXPECT_OPTION) {
283                        updateTool( project, key, command, options);
284                    }
285                } catch (IOException e) {
286                        e.printStackTrace();
287                } finally {
288                        while (true) {
289                                try {
290                                        p.waitFor();
291                                        break;
292                                } catch (InterruptedException e) {
293                                        continue;
294                                }
295                        }
296                }
297        }
298       
299        private static void updateTool( IProject project, String toolKey, String command, List<String> options) {
300                List<String> filteredOptions = new LinkedList<String>();
301               
302                // Filter options
303                if (toolKey.startsWith( Constants.COMPILER_KEY_PREFIX) || toolKey.startsWith( Constants.LINKER_KEY_PREFIX)) {
304                        for (String option : options) {
305                                if (!(option.isEmpty() || option.trim().matches( "^-c|-O[0123s]|-g|-W[\\w-]*$"))) {
306                                        filteredOptions.add( option);
307                                }
308                        }
309                } else {
310                        filteredOptions = options;
311                }
312               
313                // Transform filtered option list into option string value
314                String optionsValue = new String();
315                if (!options.isEmpty()) {
316                        optionsValue = filteredOptions.get( 0);
317                        filteredOptions.remove( 0);
318                }
319                for (String option : filteredOptions) {
320                        optionsValue += OPTION_SEPARATOR + option;
321                }
322               
323                // Set properties
324                setProperty( project, toolKey, command);
325                setProperty( project, toolKey + Constants.TOOL_OPTIONS_KEY_POSTFIX, optionsValue);
326        }
327       
328        public static String [] getToolOptions( IProject project, String toolKey) {
329                String optionsValue = getProperty( project, toolKey + Constants.TOOL_OPTIONS_KEY_POSTFIX);
330               
331                return optionsValue.split( OPTION_SEPARATOR);
332        }
333       
334        private Storage() {
335                // Do nothing
336        }
337}
Note: See TracBrowser for help on using the repository browser.