summaryrefslogtreecommitdiffstats
path: root/org.rtems.cdt.toolchain/src/org/rtems/cdt/toolchain/RtemsEnvironmentVariableSupplier.java
blob: 11865ecb4a2f7a3272a6c93c0d18326d238353fc (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**********************************************************************
 * Copyright (c) 2008 RobertF.
 * All rights reserved.
 **********************************************************************/

package org.rtems.cdt.toolchain;

import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable;
import org.eclipse.cdt.managedbuilder.envvar.IConfigurationEnvironmentVariableSupplier;
import org.eclipse.cdt.managedbuilder.envvar.IEnvironmentVariableProvider;
import org.eclipse.cdt.utils.WindowsRegistry;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;

public class RtemsEnvironmentVariableSupplier implements
                                   IConfigurationEnvironmentVariableSupplier {
  public static final String ENV_RTEMS_TOOL_DIR = "RTEMS_TOOL_DIR";
  public static final String ENV_RTEMS_TARGET_TOOL_DIR = "RTEMS_TARGET_TOOL_DIR";
  public static final String ENV_RTEMS_TARGET_CPU_GCC_LIB_DIR = "RTEMS_TARGET_CPU_GCC_LIB_DIR";
  public static final String ENV_RTEMS_INSTALL_DIR = "RTEMS_INSTALL_DIR";
  public static final String ENV_RTEMS_INSTALL_BSP_DIR = "RTEMS_INSTALL_BSP_DIR";

  private static IPath toolPath;            // e.g., /opt/rtems-4.9
  private static IPath targetToolPath;      // e.g., /opt/rtems-4.9/i386-rtems4.9
  private static IPath targetCpuGccLibPath; // e.g., /opt/rtems-4.9/lib/gcc/i386-rtems4.9/4.3.2/mpentiumpro
  private static IPath installPath;         // e.g., /opt/rtems_install
  private static IPath installBspPath;      // e.g., /opt/rtems_install/i386-rtems4.9/pc686/lib

  private static IBuildEnvironmentVariable evBinPath        = null;
  private static IBuildEnvironmentVariable evCIncludePath   = null;
  private static IBuildEnvironmentVariable evLibPath        = null;

  public static boolean toolChainSupported;
  public static boolean isOSWindows;
  public static String  pathSeparator; // ":" on UNIX
  private static String drivePrefix;   // disk drive prefix: C: for Windows a
                                       //                    blank for other OS

  private static class RtemsBuildEnvironmentVariable implements IBuildEnvironmentVariable {
    private final String name;
    private final String value;
    private final int operation;

    public RtemsBuildEnvironmentVariable(String name, String value, int operation) {
      this.name = name;
      this.value = value;
      this.operation = operation;
    }

    public String getName()      { return name; }
    public String getValue()     { return value; }
    public int getOperation()    { return operation; }
    public String getDelimiter() { return pathSeparator; }
  }

  static {
    /* Some useful system properties

      property          Description
      --------------    ---------------------------
      os.name           Operating system name
      os.arch           Operating system architecture
      os.version        Operating system version
      file.separator    File separator ("/" on UNIX)
      path.separator    Path separator (":" on UNIX)
    */
    pathSeparator = System.getProperty("path.separator");

    isOSWindows = false;
    String osName = System.getProperty("os.name");
    if (osName != null) {
        isOSWindows = osName.indexOf("Windows") >= 0;
    }

    drivePrefix = isOSWindows ? "C:" : "";

    toolPath            = getRtemsToolDirectory();
    targetToolPath      = getRtemsTargetToolDirectory();
    targetCpuGccLibPath = getRtemsTargetCpuGccLibDirectory();
    installPath         = getRtemsInstallDirectory();
    installBspPath      = getRtemsInstallBspDirectory();

    toolChainSupported  =   toolPath    != null && targetToolPath != null
                         && installPath != null && installBspPath != null
                         && targetCpuGccLibPath != null;

    StringBuffer binPathBuf = new StringBuffer();
    StringBuffer incPathBuf = new StringBuffer();
    StringBuffer libPathBuf = new StringBuffer();

    if (toolPath != null) {
      binPathBuf = binPathBuf.append(toolPath.append("bin").toOSString());
    }

    if (targetCpuGccLibPath != null) {
      if (libPathBuf.length()>0) libPathBuf.append(pathSeparator);
      libPathBuf = libPathBuf.append(targetCpuGccLibPath.toOSString());


      IPath targetGccVersionPath;
      try {
        targetGccVersionPath = targetCpuGccLibPath.append("..");

        IPath gccIncPath = targetGccVersionPath.append("include");
        if (!gccIncPath.toFile().isDirectory()) {
          targetGccVersionPath = targetCpuGccLibPath;
        }
      } catch (Exception e) {
        targetGccVersionPath = targetCpuGccLibPath;
      }

      if (incPathBuf.length()>0) incPathBuf.append(pathSeparator);
      incPathBuf = incPathBuf.append(targetGccVersionPath.append("include").toOSString());

      incPathBuf.append(pathSeparator);
      incPathBuf = incPathBuf.append(targetGccVersionPath.append("include-fixed").toOSString());

    }

    if (targetToolPath != null) {
      if (binPathBuf.length()>0) binPathBuf.append(pathSeparator);
      binPathBuf = binPathBuf.append(targetToolPath.append("bin").toOSString());

      if (incPathBuf.length()>0) incPathBuf.append(pathSeparator);
      incPathBuf = incPathBuf.append(targetToolPath.append("include").toOSString());

      if (libPathBuf.length()>0) libPathBuf.append(pathSeparator);
      libPathBuf = libPathBuf.append(targetToolPath.append("lib").toOSString());
    }

    if (installPath != null) {
      if (binPathBuf.length()>0) binPathBuf.append(pathSeparator);
      binPathBuf = binPathBuf.append(installPath.append("bin").toOSString());
    }

    if (installBspPath != null) {
      if (binPathBuf.length()>0) binPathBuf.append(pathSeparator);
      binPathBuf = binPathBuf.append(installBspPath.append("build-tools").toOSString());

      IPath bspLibPath = installBspPath.append("lib");
      if (incPathBuf.length()>0) incPathBuf.append(pathSeparator);
      incPathBuf = incPathBuf.append(bspLibPath.append("include").toOSString());

      if (libPathBuf.length()>0) libPathBuf.append(pathSeparator);
      libPathBuf = libPathBuf.append(bspLibPath.toOSString());
    }

    evBinPath = new RtemsBuildEnvironmentVariable("PATH",
                        binPathBuf.toString(),
                        IBuildEnvironmentVariable.ENVVAR_PREPEND);
    evCIncludePath = new RtemsBuildEnvironmentVariable("C_INCLUDE_PATH",
                        incPathBuf.toString(),
                        IBuildEnvironmentVariable.ENVVAR_PREPEND);
    //evCppIncludePath = new RtemsBuildEnvironmentVariable("CPLUS_INCLUDE_PATH",
    //                    incPath.toString(),
    //                    IBuildEnvironmentVariable.ENVVAR_PREPEND);
    evLibPath = new RtemsBuildEnvironmentVariable("LIBRARY_PATH",
                        libPathBuf.toString(),
                        IBuildEnvironmentVariable.ENVVAR_PREPEND);
   }

  public RtemsEnvironmentVariableSupplier() {
  }

  public IBuildEnvironmentVariable getVariable(String variableName,
          IConfiguration configuration, IEnvironmentVariableProvider provider) {
    if (evBinPath != null &&
        variableName.equals(evBinPath.getName())) {
      return evBinPath;

    } else if (evLibPath != null &&
        variableName.equals(evLibPath.getName())) {
      return evLibPath;

    } else if (evCIncludePath != null &&
        variableName.equals(evCIncludePath.getName())) {
      return evCIncludePath;
    }

    return null;
  }

  public IBuildEnvironmentVariable[] getVariables(
      IConfiguration configuration, IEnvironmentVariableProvider provider) {
    return new IBuildEnvironmentVariable[] {
                                    evBinPath, evCIncludePath, evLibPath };
  }

  public static IPath getRtemsToolDirectory() {
      IPath path;

      // Preferred way to get RTEMS Tool directory via environment variable
      String dir = System.getenv(ENV_RTEMS_TOOL_DIR);
      if (dir != null) {
        path = new Path(dir);
        if (path.toFile().isDirectory()) return path;
      }

      // Try to locate RTEMS Tool directory at /opt/rtems
      // User can create such a soft link to point to the latest one
      path = new Path(drivePrefix + "/opt/rtems");
      if (path.toFile().isDirectory()) return path;

      // Try RTEMS 4.9 specific tool bin directory
      path = new Path(drivePrefix + "/opt/rtems-4.9");
      if (path.toFile().isDirectory()) return path;

      // Still not found
      return null;
  }

  public static IPath getRtemsTargetToolDirectory() {
      IPath path;

      // Preferred way to get RTEMS Target Tool directory via environment variable
      String dir = System.getenv(ENV_RTEMS_TARGET_TOOL_DIR);
      if (dir != null) {
        path = new Path(dir);
        if (path.toFile().isDirectory()) return path;
      }

      // Try to locate RTEMS Target Tool directory at /opt/rtems/i386-rtems4.9
      // User can create such a soft link to point to the latest one
      path = new Path(drivePrefix + "/opt/rtems/i386-rtems4.9");
      if (path.toFile().isDirectory()) return path;

      // Try RTEMS 4.9 specific target tool bin directory
      path = new Path(drivePrefix + "/opt/rtems-4.9/i386-rtems4.9");
      if (path.toFile().isDirectory()) return path;

      // Still not found
      return null;
  }

  public static IPath getRtemsTargetCpuGccLibDirectory() {
      IPath path;

      // Preferred way to get RTEMS Target Tool directory via environment variable
      String dir = System.getenv(ENV_RTEMS_TARGET_CPU_GCC_LIB_DIR);
      if (dir != null) {
        path = new Path(dir);
        if (path.toFile().isDirectory()) return path;
      }

      //Try default RTEMS 4.9 Target Tool bin directory
      path = new Path(drivePrefix + "/opt/rtems-4.9/lib/gcc/i386-rtems4.9/4.3.2/mpentiumpro");
      if (path.toFile().isDirectory()) return path;

      // Still not found
      return null;
  }

  public static IPath getRtemsInstallDirectory() {
      IPath path;

      // Preferred way to get RTEMS Install directory via environment variable
      String dir = System.getenv(ENV_RTEMS_INSTALL_DIR);
      if (dir != null) {
        path = new Path(dir);
        if (path.toFile().isDirectory()) return path;
      }

      // Try /opt/rtems_install
      path = new Path(drivePrefix + "/opt/rtems_install");
      if (path.toFile().isDirectory()) return path;

      // Still not found
      return null;
  }

  public static IPath getRtemsInstallBspDirectory() {
      IPath path;

      // Preferred way to get installed target lib directory via environment variable
      String dir = System.getenv(ENV_RTEMS_INSTALL_BSP_DIR);
      if (dir != null) {
        path = new Path(dir);
        if (path.toFile().isDirectory()) return path;
      }

      // Try  /opt/rtems_install/i386-rtems4.9/pc686/lib
      path = new Path(drivePrefix + "/opt/rtems_install/i386-rtems4.9/pc686/lib");
      if (path.toFile().isDirectory()) return path;

      // Still not found
      return null;
  }
  
  public static IPath getRtemsInstallBspLibDirectory() {
    IPath installedBspLibPath = getRtemsInstallBspDirectory();
    
    if (installedBspLibPath != null) {
      installedBspLibPath = installedBspLibPath.append("lib");
    }
    
    return installedBspLibPath;
  }
}