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\vcvarsx86_adm64.bat environment
34   */
35  
36  public class MSVC2008x86AMD64EnvFactory
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\\x86_amd64;" + vcInstallDir.getPath() + "\\bin" + ";"
96                  + vsCommonToolDir + ";" + frameworkDir + "\\" + framework35Version + ";" + frameworkDir + "\\"
97                  + frameworkVersion + ";" + vcInstallDir.getPath() + "\\VCPackages" + ";" + windowsSDKDir.getPath()
98                  + "\\bin;" + currentPathEnv;
99  
100         envs.put( "PATH", newPathEnv );
101 
102         // setup new INCLUDE PATH
103         // @set INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE;C:\Program Files
104         // (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE;%INCLUDE%
105 
106         String currentIncludeEnv = EnvUtil.getEnv( "INCLUDE" );
107 
108         String newIncludeEnv =
109             vcInstallDir.getPath() + "\\ATLMFC\\INCLUDE;" + vcInstallDir.getPath() + "\\INCLUDE;"
110                 + windowsSDKDir.getPath() + "\\include;" + currentIncludeEnv;
111 
112         envs.put( "INCLUDE", newIncludeEnv );
113 
114         //
115         // setup new LIB PATH
116         //
117         String currentLibEnv = EnvUtil.getEnv( "LIB" );
118 
119         String newLibEnv =
120             vcInstallDir.getPath() + "\\ATLMFC\\LIB\\amd64;" + vcInstallDir.getPath() + "\\LIB\\amd64;"
121                 + windowsSDKDir.getPath() + "\\LIB\\x64;" + currentLibEnv;
122 
123         envs.put( "LIB", newLibEnv );
124 
125         // @set LIBPATH=C:\Windows\Microsoft.NET\Framework\v3.5;C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program
126         // Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;C:\Program Files (x86)\Microsoft Visual Studio
127         // 9.0\VC\LIB;%LIBPATH%
128 
129         String currentLibPathEnv = EnvUtil.getEnv( "LIBPATH" );
130 
131         String newLibPathEnv =
132             frameworkDir + "\\" + framework35Version + ";" + frameworkDir + "\\" + frameworkVersion + ";"
133                 + vcInstallDir.getPath() + "\\ATLMFC\\LIB\\amd64;" + vcInstallDir.getPath() + "\\LIB\\amd64;"
134                 + 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 }