View Javadoc
1   package org.codehaus.mojo.natives.msvc;
2   
3   /*
4    * The MIT License
5    * 
6    * Copyright (c) 2004, The Codehaus
7    * 
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
9    * associated documentation files (the "Software"), to deal in the Software without restriction,
10   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
11   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in all copies or
15   * substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22   */
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.codehaus.mojo.natives.NativeBuildException;
30  import org.codehaus.mojo.natives.util.EnvUtil;
31  
32  /**
33   * Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat environment
34   */
35  
36  public class MSVC2008x86EnvFactory
37      extends AbstractMSVCEnvFactory
38  {
39      private static final String VS90COMNTOOLS_ENV_KEY = "VS90COMNTOOLS";
40  
41      protected Map createEnvs()
42          throws NativeBuildException
43      {
44          Map envs = new HashMap();
45  
46          File vsCommonToolDir = this.getCommonToolDirectory();
47  
48          File vsInstallDir = this.getVisualStudioInstallDirectory( vsCommonToolDir );
49  
50          if ( !vsInstallDir.isDirectory() )
51          {
52              throw new NativeBuildException( vsInstallDir.getPath() + " is not a directory." );
53          }
54          envs.put( "VSINSTALLDIR", vsInstallDir.getPath() );
55  
56          File vcInstallDir = new File( vsInstallDir.getPath() + "\\VC" );
57          if ( !vcInstallDir.isDirectory() )
58          {
59              throw new NativeBuildException( vcInstallDir.getPath() + " is not a directory." );
60          }
61          envs.put( "VCINSTALLDIR", vcInstallDir.getPath() );
62  
63          File frameworkDir = new File( getSystemRoot() + "\\Microsoft.NET\\Framework" );
64          envs.put( "FrameworkDir", frameworkDir.getPath() );
65  
66          File windowsSDKDir = new File( "C:\\Program Files" + "\\Microsoft SDKs\\Windows\\v6.0A" );
67          String value =
68              RegQuery.getValue( "REG_SZ", "HKLM\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows", "CurrentInstallFolder" );
69          if ( value != null )
70          {
71              windowsSDKDir = new File( value );
72          }
73  
74          envs.put( "WindowsSdkDir", windowsSDKDir.getPath() );
75  
76          String frameworkVersion = "v2.0.50727";
77          envs.put( "FrameworkVersion", frameworkVersion );
78  
79          String framework35Version = "v3.5";
80          envs.put( "Framework35Version", framework35Version );
81  
82          String devEnvDir = vsCommonToolDir + "\\..\\IDE";
83          envs.put( "DevEnvDir", devEnvDir );
84  
85          // set "PATH=%WindowsSdkDir%bin;%PATH%"
86          // @set PATH=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE;C:\Program Files (x86)\Microsoft
87          // Visual Studio 9.0\VC\BIN;C:\Program Files (x86)\Microsoft Visual Studio
88          // 9.0\Common7\Tools;C:\Windows\Microsoft.NET\Framework\v3.5;C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program
89          // Files (x86)\Microsoft Visual Studio 9.0\VC\VCPackages;%PATH%
90  
91          // setup new PATH
92          String currentPathEnv = System.getProperty( "java.library.path" );
93  
94          String newPathEnv =
95              devEnvDir + ";" + vcInstallDir.getPath() + "\\bin" + ";" + vsCommonToolDir + ";" + frameworkDir + "\\"
96                  + framework35Version + ";" + frameworkDir + "\\" + frameworkVersion + ";" + vcInstallDir.getPath()
97                  + "\\VCPackages" + ";" + windowsSDKDir.getPath() + "\\bin;" + currentPathEnv;
98  
99          envs.put( "PATH", newPathEnv );
100 
101         // setup new INCLUDE PATH
102         // @set INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE;C:\Program Files
103         // (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE;%INCLUDE%
104 
105         String currentIncludeEnv = EnvUtil.getEnv( "INCLUDE" );
106 
107         String newIncludeEnv =
108             vcInstallDir.getPath() + "\\ATLMFC\\INCLUDE;" + vcInstallDir.getPath() + "\\INCLUDE;"
109                 + windowsSDKDir.getPath() + "\\include;" + currentIncludeEnv;
110 
111         envs.put( "INCLUDE", newIncludeEnv );
112 
113         //
114         // setup new LIB PATH
115         // @set LIB=C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Microsoft
116         // Visual Studio 9.0\VC\LIB;%LIB%
117         //
118         String currentLibEnv = EnvUtil.getEnv( "LIB" );
119 
120         String newLibEnv =
121             vcInstallDir.getPath() + "\\ATLMFC\\LIB;" + vcInstallDir.getPath() + "\\LIB;" + windowsSDKDir.getPath()
122                 + "\\LIB;" + currentLibEnv;
123 
124         envs.put( "LIB", newLibEnv );
125 
126         // @set LIBPATH=C:\Windows\Microsoft.NET\Framework\v3.5;C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program
127         // Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Microsoft Visual Studio
128         // 9.0\VC\LIB;%LIBPATH%
129 
130         String currentLibPathEnv = EnvUtil.getEnv( "LIBPATH" );
131 
132         String newLibPathEnv =
133             frameworkDir + "\\" + framework35Version + ";" + frameworkDir + "\\" + frameworkVersion + ";"
134                 + vcInstallDir.getPath() + "\\ATLMFC\\LIB;" + vcInstallDir.getPath() + "\\LIB;" + currentLibPathEnv;
135 
136         envs.put( "LIBPATH", newLibPathEnv );
137 
138         return envs;
139 
140     }
141 
142     private File getCommonToolDirectory()
143         throws NativeBuildException
144     {
145         String envValue = System.getenv( VS90COMNTOOLS_ENV_KEY );
146         if ( envValue == null )
147         {
148             throw new NativeBuildException( "Environment variable: " + VS90COMNTOOLS_ENV_KEY + " not available." );
149         }
150 
151         return new File( envValue );
152     }
153 
154     private File getVisualStudioInstallDirectory( File commonToolDir )
155         throws NativeBuildException
156     {
157         try
158         {
159             return new File( commonToolDir, "../.." ).getCanonicalFile();
160         }
161         catch ( IOException e )
162         {
163             throw new NativeBuildException( "Unable to contruct Visual Studio install directory using: "
164                 + commonToolDir, e );
165         }
166     }
167 
168 }