1 package org.codehaus.mojo.natives.msvc; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.Map; 6 7 import org.codehaus.mojo.natives.AbstractEnvFactory; 8 import org.codehaus.mojo.natives.NativeBuildException; 9 import org.codehaus.mojo.natives.util.EnvUtil; 10 import org.codehaus.plexus.util.FileUtils; 11 import org.codehaus.plexus.util.cli.CommandLineUtils; 12 import org.codehaus.plexus.util.cli.Commandline; 13 import org.codehaus.plexus.util.cli.DefaultConsumer; 14 import org.codehaus.plexus.util.cli.StreamConsumer; 15 16 public abstract class AbstractMSVCEnvFactory 17 extends AbstractEnvFactory 18 { 19 20 protected static String getProgramFiles() 21 { 22 return EnvUtil.getEnv( "ProgramFiles", "ProgramFiles", "C:\\Program Files" ); 23 } 24 25 protected static String getProgramFilesX86() 26 { 27 return EnvUtil.getEnv( "ProgramFiles(x86)", "ProgramFiles", getProgramFiles() ); 28 } 29 30 protected static String getSystemRoot() 31 { 32 return EnvUtil.getEnv( "SystemRoot", "SystemRoot", "C:\\WINDOWS" ); 33 } 34 35 protected Map createEnvs( String commonToolEnvKey, String platform ) 36 throws NativeBuildException 37 { 38 39 File tmpEnvExecFile = null; 40 try 41 { 42 File vsCommonToolDir = this.getCommonToolDirectory( commonToolEnvKey ); 43 44 File vsInstallDir = this.getVisualStudioInstallDirectory( vsCommonToolDir ); 45 46 if ( !vsInstallDir.isDirectory() ) 47 { 48 throw new NativeBuildException( vsInstallDir.getPath() + " is not a directory." ); 49 } 50 51 tmpEnvExecFile = this.createEnvWrapperFile( vsInstallDir, platform ); 52 53 Commandline cl = new Commandline(); 54 cl.setExecutable( tmpEnvExecFile.getAbsolutePath() ); 55 56 EnvStreamConsumer stdout = new EnvStreamConsumer(); 57 StreamConsumer stderr = new DefaultConsumer(); 58 59 CommandLineUtils.executeCommandLine( cl, stdout, stderr ); 60 61 return stdout.getParsedEnv(); 62 } 63 catch ( Exception e ) 64 { 65 throw new NativeBuildException( "Unable to retrieve env", e ); 66 } 67 finally 68 { 69 if ( tmpEnvExecFile != null ) 70 { 71 tmpEnvExecFile.delete(); 72 } 73 } 74 75 } 76 77 private File getCommonToolDirectory( String commonToolEnvKey ) 78 throws NativeBuildException 79 { 80 String envValue = System.getenv( commonToolEnvKey ); 81 if ( envValue == null ) 82 { 83 throw new NativeBuildException( "Environment variable: " + commonToolEnvKey + " not available." ); 84 } 85 86 return new File( envValue ); 87 } 88 89 private File getVisualStudioInstallDirectory( File commonToolDir ) 90 throws NativeBuildException 91 { 92 try 93 { 94 return new File( commonToolDir, "../.." ).getCanonicalFile(); 95 } 96 catch ( IOException e ) 97 { 98 throw new NativeBuildException( "Unable to contruct Visual Studio install directory using: " 99 + commonToolDir, e ); 100 } 101 } 102 103 private File createEnvWrapperFile( File vsInstallDir, String platform ) 104 throws IOException 105 { 106 107 File tmpFile = File.createTempFile( "msenv", ".bat" ); 108 109 StringBuffer buffer = new StringBuffer(); 110 buffer.append( "@echo off\r\n" ); 111 buffer.append( "call \"" ).append( vsInstallDir ).append( "\"" ).append( "\\VC\\vcvarsall.bat " + platform 112 + "\n\r" ); 113 buffer.append( "echo " + EnvStreamConsumer.START_PARSING_INDICATOR ).append( "\r\n" ); 114 buffer.append( "set\n\r" ); 115 116 FileUtils.fileWrite( tmpFile.getAbsolutePath(), buffer.toString() ); 117 118 return tmpFile; 119 } 120 121 }