summaryrefslogtreecommitdiffstats
path: root/org.rtems.cdt.toolchain2/org/rtems/cdt/Storage.java
blob: 22be33351bb546c776a6f5a95f72e5c9d8d3fecb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (c) 2008
 * Embedded Brains GmbH
 * Obere Lagerstr. 30
 * D-82178 Puchheim
 * Germany
 * rtems@embedded-brains.de
 *
 * The license and distribution terms for this file may be found in the file
 * LICENSE in this distribution or at http://www.rtems.com/license/LICENSE.
 */

package org.rtems.cdt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.rtems.cdt.Activator;

public class Storage {
	private static final String OPTION_SEPARATOR = "\0";
	
	private static final String VALUE_START_TOKEN = "\t";
	
	private static final int EXPECT_OPTION = 0;
	
	private static final int EXPECT_COMMAND = 1;
	
	private static final int EXPECT_KEY = 2;
	
	private static final int TOOL_COMPLETE = 3;

	public static String getPreference( String key) {
		return Activator.getDefault().getPreferenceStore().getString( key);
	}
	
	public static String getPristineProperty( IProject project, String key) {
		String value = null;
		
		try {
			value = project.getPersistentProperty( new QualifiedName( "", key));
		} catch (CoreException e) {
			e.printStackTrace();
		}
		
		return value;
	}
	
	public static String getProperty( IProject project, String key) {
		String value = getPristineProperty( project, key);
		
		if (value == null) {
			if (key.startsWith( Constants.TOOL_KEY_PREFIX)) {
				updateTools( project);
			} else {
				value = Activator.getDefault().getPreferenceStore().getString( key);
				setProperty( project, key, value);
			}
		}
		
		return value;
	}
	
	public static void setProperty( IProject project, String key, String value) {
		try {
			project.setPersistentProperty( new QualifiedName( "", key), value);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	public static void updateTools( IProject project) {
		// Create make process builder
		ProcessBuilder pb = new ProcessBuilder( "make");

		// Provide RTEMS_MAKEFILE_PATH environment variable
		Map<String, String> env = pb.environment();
		env.put( "RTEMS_MAKEFILE_PATH", getProperty( project, Constants.BSP_PATH_KEY));
		
		// Change working directory to the Makefile location
		pb.directory( Activator.getDefault().getMakefileLocation().toFile());
		
		// Start make process and parse its output
		Process p = null;
		try {
			p = pb.start();
		    InputStream is = p.getInputStream();
		    BufferedReader br = new BufferedReader( new InputStreamReader( is));
		    String line = br.readLine();
		    String key = null;
		    String command = null;
		    List<String> options = new LinkedList<String>();
		    int state = EXPECT_KEY;
		    while (line != null) {
		    	switch (state) {
		    		case EXPECT_OPTION:
		    			if (line.startsWith( VALUE_START_TOKEN)) {
		    				options.add( line.substring( 1));
		    			} else {
		    				state = TOOL_COMPLETE;
		    				continue;
		    			}
		    			break;
		    		case EXPECT_COMMAND:
		    			if (line.startsWith( VALUE_START_TOKEN)) {
		    				command = line.substring( 1);
		    				state = EXPECT_OPTION;
		    			} else {
		    				throw new IOException( "Unexpected line format");
		    			}
		    			break;
		    		case EXPECT_KEY:
		    			if (line.length() > Constants.TOOL_KEY_PREFIX.length()) {
		    				key = line;
		    				state = EXPECT_COMMAND;
		    			} else {
		    				throw new IOException( "Unexpected line format");
		    			}
		    			break;
		    		case TOOL_COMPLETE:
				    	updateTool( project, key, command, options);
		    			options.clear();
		    			state = EXPECT_KEY;
		    			continue;
		    		default:
		    			throw new IOException( "Unexpected state");
		    	}
		    	line = br.readLine();
		    }
		    if (state == EXPECT_OPTION) {
		    	updateTool( project, key, command, options);
		    }
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			while (true) {
				try {
					p.waitFor();
					break;
				} catch (InterruptedException e) {
					continue;
				}
			}
		}
	}
	
	private static void updateTool( IProject project, String toolKey, String command, List<String> options) {
		List<String> filteredOptions = new LinkedList<String>();
		
		// Filter options
		if (toolKey.startsWith( Constants.COMPILER_KEY_PREFIX) || toolKey.startsWith( Constants.LINKER_KEY_PREFIX)) {
			for (String option : options) {
				if (!(option.isEmpty() || option.trim().matches( "^-c|-O[0123s]|-g|-W[\\w-]*$"))) {
					filteredOptions.add( option);
				}
			}
		} else {
			filteredOptions = options;
		}
		
		// Transform filtered option list into option string value
		String optionsValue = new String();
		if (!options.isEmpty()) {
			optionsValue = filteredOptions.get( 0);
			filteredOptions.remove( 0);
		}
		for (String option : filteredOptions) {
			optionsValue += OPTION_SEPARATOR + option;
		}
		
		// Set properties
		setProperty( project, toolKey, command);
		setProperty( project, toolKey + Constants.TOOL_OPTIONS_KEY_POSTFIX, optionsValue);
	}
	
	public static String [] getToolOptions( IProject project, String toolKey) {
		String optionsValue = getProperty( project, toolKey + Constants.TOOL_OPTIONS_KEY_POSTFIX);
		
		return optionsValue.split( OPTION_SEPARATOR);
	}
	
	private Storage() {
		// Do nothing
	}
}