1 import java.io.File;
   2 import java.io.FileNotFoundException;
   3 import java.io.IOException;
   4 import java.io.PrintWriter;
   5 import java.io.UnsupportedEncodingException;
   6 import java.nio.file.FileSystems;
   7 import java.util.Iterator;
   8 import java.util.LinkedList;
   9 import java.util.UUID;
  10 import java.util.Vector;
  11 
  12 public class WinGammaPlatformVC10 extends WinGammaPlatformVC7 {
  13 
  14 
  15    LinkedList <String>filters = new LinkedList<String>();
  16    LinkedList <String[]>filterDeps = new LinkedList<String[]>();
  17    
  18     @Override
  19     protected String getProjectExt() {
  20         return ".vcxproj";
  21     }
  22 
  23     @Override
  24     public void writeProjectFile(String projectFileName, String projectName,
  25             Vector<BuildConfig> allConfigs) throws IOException {
  26         System.out.println();
  27         System.out.print("    Writing .vcxproj file: " + projectFileName);
  28 
  29         String projDir = Util.normalize(new File(projectFileName).getParent());
  30 
  31         printWriter = new PrintWriter(projectFileName, "UTF-8");
  32         printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  33         startTag("Project",
  34                 "DefaultTargets", "Build",
  35                 "ToolsVersion", "4.0",
  36                 "xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
  37         startTag("ItemGroup",
  38                 "Label", "ProjectConfigurations");
  39         for (BuildConfig cfg : allConfigs) {
  40             startTag("ProjectConfiguration",
  41                     "Include", cfg.get("Name"));
  42             tagData("Configuration", cfg.get("Id"));
  43             tagData("Platform", cfg.get("PlatformName"));
  44             endTag();
  45         }
  46         endTag();
  47 
  48         startTag("PropertyGroup", "Label", "Globals");
  49         tagData("ProjectGuid", "{8822CB5C-1C41-41C2-8493-9F6E1994338B}");
  50         tag("SccProjectName");
  51         tag("SccLocalPath");
  52         endTag();
  53 
  54         tag("Import", "Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  55 
  56         for (BuildConfig cfg : allConfigs) {
  57             startTag(cfg, "PropertyGroup", "Label", "Configuration");
  58             tagData("ConfigurationType", "DynamicLibrary");
  59             tagData("UseOfMfc", "false");
  60             endTag();
  61         }
  62 
  63         tag("Import", "Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  64         startTag("ImportGroup", "Label", "ExtensionSettings");
  65         endTag();
  66         for (BuildConfig cfg : allConfigs) {
  67             startTag(cfg, "ImportGroup", "Label", "PropertySheets");
  68             tag("Import",
  69                     "Project", "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props",
  70                     "Condition", "exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')",
  71                     "Label", "LocalAppDataPlatform");
  72             endTag();
  73         }
  74 
  75         tag("PropertyGroup", "Label", "UserMacros");
  76 
  77         startTag("PropertyGroup");
  78         tagData("_ProjectFileVersion", "10.0.30319.1");
  79         for (BuildConfig cfg : allConfigs) {
  80             tagData(cfg, "OutDir", cfg.get("OutputDir") + Util.sep);
  81             tagData(cfg, "IntDir", cfg.get("OutputDir") + Util.sep);
  82             tagData(cfg, "LinkIncremental", "false");
  83         }
  84         for (BuildConfig cfg : allConfigs) {
  85             tagData(cfg, "CodeAnalysisRuleSet", "AllRules.ruleset");
  86             tag(cfg, "CodeAnalysisRules");
  87             tag(cfg, "CodeAnalysisRuleAssemblies");
  88         }
  89         endTag();
  90 
  91         for (BuildConfig cfg : allConfigs) {
  92             startTag(cfg, "ItemDefinitionGroup");
  93             startTag("ClCompile");
  94             tagV(cfg.getV("CompilerFlags"));
  95             endTag();
  96 
  97             startTag("Link");
  98             tagV(cfg.getV("LinkerFlags"));
  99             endTag();
 100 
 101             startTag("PostBuildEvent");
 102             tagData("Message", BuildConfig.getFieldString(null, "PostbuildDescription"));
 103             tagData("Command", cfg.expandFormat(BuildConfig.getFieldString(null, "PostbuildCommand").replace("\t", "\r\n")));
 104             endTag();
 105 
 106             startTag("PreLinkEvent");
 107             tagData("Message", BuildConfig.getFieldString(null, "PrelinkDescription"));
 108             tagData("Command", cfg.expandFormat(BuildConfig.getFieldString(null, "PrelinkCommand").replace("\t", "\r\n")));
 109             endTag();
 110 
 111             endTag();
 112         }
 113 
 114         writeFiles(allConfigs, projDir);
 115 
 116         tag("Import", "Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
 117         startTag("ImportGroup", "Label", "ExtensionTargets");
 118         endTag();
 119 
 120         endTag();
 121         printWriter.close();
 122         System.out.println("    Done.");
 123 
 124         writeFilterFile(projectFileName, projectName, allConfigs, projDir);
 125         writeUserFile(projectFileName, allConfigs);
 126     }
 127 
 128 
 129     private void writeUserFile(String projectFileName, Vector<BuildConfig> allConfigs) throws FileNotFoundException, UnsupportedEncodingException {
 130         String userFileName = projectFileName + ".user";
 131         if (new File(userFileName).exists()) {
 132             return;
 133         }
 134         System.out.print("    Writing .vcxproj.user file: " + userFileName);
 135         printWriter = new PrintWriter(userFileName, "UTF-8");
 136 
 137         printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
 138         startTag("Project",
 139                 "ToolsVersion", "4.0",
 140                 "xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
 141 
 142         for (BuildConfig cfg : allConfigs) {
 143             startTag(cfg, "PropertyGroup");
 144             tagData("LocalDebuggerCommand", "$(TargetDir)/hotspot.exe");
 145             endTag();
 146         }
 147 
 148         endTag();
 149         printWriter.close();
 150         System.out.println("    Done.");
 151     }
 152     
 153     public void addFilter(String rPath) {
 154        filters.add(rPath);      
 155     }
 156     
 157     public void addFilterDependency(String fileLoc, String filter) {
 158       filterDeps.add(new String[] {fileLoc, filter});      
 159     }
 160  
 161     private void writeFilterFile(String projectFileName, String projectName,
 162             Vector<BuildConfig> allConfigs, String base) throws FileNotFoundException, UnsupportedEncodingException {
 163         String filterFileName = projectFileName + ".filters";
 164         System.out.print("    Writing .vcxproj.filters file: " + filterFileName);
 165         printWriter = new PrintWriter(filterFileName, "UTF-8");
 166 
 167         printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
 168         startTag("Project",
 169                 "ToolsVersion", "4.0",
 170                 "xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");
 171 
 172         startTag("ItemGroup");
 173         for (String filter : filters) {
 174            startTag("Filter", "Include",filter);
 175            UUID uuid = UUID.randomUUID();
 176            tagData("UniqueIdentifier", "{" + uuid.toString() + "}");
 177            endTag();     
 178         }
 179         startTag("Filter", "Include", "Resource Files");
 180         UUID uuid = UUID.randomUUID();
 181         tagData("UniqueIdentifier", "{" + uuid.toString() + "}");
 182         tagData("Extensions", "ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe");
 183         endTag();
 184         endTag();
 185         
 186         //TODO - do I need to split cpp and hpp files?
 187         
 188         // then all files
 189         startTag("ItemGroup");
 190         for (String[] dep : filterDeps) {
 191            String tagName = getFileTagFromSuffix(dep[0]);
 192            
 193            startTag(tagName, "Include", dep[0]);
 194            tagData("Filter", dep[1]);
 195            endTag();    
 196         }
 197         endTag();
 198         
 199         endTag();
 200         printWriter.close();
 201         System.out.println("    Done.");
 202     }
 203 
 204     public String getFileTagFromSuffix(String fileName) {
 205        if (fileName.endsWith(".cpp")) {
 206           return"ClCompile";
 207        } else if (fileName.endsWith(".c")) {
 208           return "ClCompile";
 209        } else if (fileName.endsWith(".hpp")) {
 210           return"ClInclude";
 211        } else if (fileName.endsWith(".h")) {
 212           return "ClInclude";
 213        } else {
 214           return"None";
 215        }
 216     }
 217 
 218     void writeFiles(Vector<BuildConfig> allConfigs, String projDir) {
 219        // This code assummes there are no config specific includes.
 220        startTag("ItemGroup");
 221        
 222        String sourceBase = BuildConfig.getFieldString(null, "SourceBase");
 223 
 224        // Use first config for all global absolute includes.
 225        BuildConfig baseConfig = allConfigs.firstElement();
 226        Vector<String> rv = new Vector<String>();
 227 
 228        // Then use first config for all relative includes 
 229        Vector<String> ri = new Vector<String>();        
 230        baseConfig.collectRelevantVectors(ri, "RelativeSrcInclude");
 231        for (String f : ri) {
 232           rv.add(sourceBase + Util.sep + f);
 233        }        
 234 
 235        baseConfig.collectRelevantVectors(rv, "AbsoluteSrcInclude");     
 236 
 237        handleIncludes(rv, allConfigs);     
 238 
 239        endTag();
 240     }
 241 
 242     // Will visit file tree for each include
 243     private void handleIncludes(Vector<String> includes, Vector<BuildConfig> allConfigs) {
 244        for (String path : includes)  {
 245           FileTreeCreatorVC10 ftc = new FileTreeCreatorVC10(FileSystems.getDefault().getPath(path) , allConfigs, this);
 246           try {
 247              ftc.writeFileTree();
 248           } catch (IOException e) {
 249              e.printStackTrace();
 250           }
 251        }
 252     }
 253     
 254     String buildCond(BuildConfig cfg) {
 255         return "'$(Configuration)|$(Platform)'=='"+cfg.get("Name")+"'";
 256     }
 257 
 258     void tagV(Vector<String> v) {
 259         Iterator<String> i = v.iterator();
 260         while(i.hasNext()) {
 261             String name = i.next();
 262             String data = i.next();
 263             tagData(name, data);
 264         }
 265     }
 266 
 267     void tagData(BuildConfig cfg, String name, String data) {
 268         tagData(name, data, "Condition", buildCond(cfg));
 269     }
 270 
 271     void tag(BuildConfig cfg, String name, String... attrs) {
 272         String[] ss = new String[attrs.length + 2];
 273         ss[0] = "Condition";
 274         ss[1] = buildCond(cfg);
 275         System.arraycopy(attrs, 0, ss, 2, attrs.length);
 276 
 277         tag(name, ss);
 278     }
 279 
 280     void startTag(BuildConfig cfg, String name, String... attrs) {
 281         String[] ss = new String[attrs.length + 2];
 282         ss[0] = "Condition";
 283         ss[1] = buildCond(cfg);
 284         System.arraycopy(attrs, 0, ss, 2, attrs.length);
 285 
 286         startTag(name, ss);
 287     }
 288 
 289 }
 290 
 291 class CompilerInterfaceVC10 extends CompilerInterface {
 292 
 293     @Override
 294     Vector getBaseCompilerFlags(Vector defines, Vector includes, String outDir) {
 295         Vector rv = new Vector();
 296 
 297         addAttr(rv, "AdditionalIncludeDirectories", Util.join(";", includes));
 298         addAttr(rv, "PreprocessorDefinitions",
 299                 Util.join(";", defines).replace("\\\"", "\""));
 300         addAttr(rv, "PrecompiledHeaderFile", "precompiled.hpp");
 301         addAttr(rv, "PrecompiledHeaderOutputFile", outDir+Util.sep+"vm.pch");
 302         addAttr(rv, "AssemblerListingLocation", outDir);
 303         addAttr(rv, "ObjectFileName", outDir+Util.sep);
 304         addAttr(rv, "ProgramDataBaseFileName", outDir+Util.sep+"jvm.pdb");
 305         // Set /nologo option
 306         addAttr(rv, "SuppressStartupBanner", "true");
 307         // Surpass the default /Tc or /Tp.
 308         addAttr(rv, "CompileAs", "Default");
 309         // Set /W3 option.
 310         addAttr(rv, "WarningLevel", "Level3");
 311         // Set /WX option,
 312         addAttr(rv, "TreatWarningAsError", "true");
 313         // Set /GS option
 314         addAttr(rv, "BufferSecurityCheck", "false");
 315         // Set /Zi option.
 316         addAttr(rv, "DebugInformationFormat", "ProgramDatabase");
 317         // Set /Yu option.
 318         addAttr(rv, "PrecompiledHeader", "Use");
 319         // Set /EHsc- option
 320         addAttr(rv, "ExceptionHandling", "");
 321 
 322         addAttr(rv, "MultiProcessorCompilation", "true");
 323 
 324         return rv;
 325     }
 326 
 327     @Override
 328     Vector getDebugCompilerFlags(String opt) {
 329         Vector rv = new Vector();
 330 
 331         // Set /On option
 332         addAttr(rv, "Optimization", opt);
 333         // Set /FR option.
 334         addAttr(rv, "BrowseInformation", "true");
 335         addAttr(rv, "BrowseInformationFile", "$(IntDir)");
 336         // Set /MD option.
 337         addAttr(rv, "RuntimeLibrary", "MultiThreadedDLL");
 338         // Set /Oy- option
 339         addAttr(rv, "OmitFramePointers", "false");
 340 
 341         return rv;
 342     }
 343 
 344     @Override
 345     Vector getProductCompilerFlags() {
 346         Vector rv = new Vector();
 347 
 348         // Set /O2 option.
 349         addAttr(rv, "Optimization", "MaxSpeed");
 350         // Set /Oy- option
 351         addAttr(rv, "OmitFramePointers", "false");
 352         // Set /Ob option.  1 is expandOnlyInline
 353         addAttr(rv, "InlineFunctionExpansion", "OnlyExplicitInline");
 354         // Set /GF option.
 355         addAttr(rv, "StringPooling", "true");
 356         // Set /MD option. 2 is rtMultiThreadedDLL
 357         addAttr(rv, "RuntimeLibrary", "MultiThreadedDLL");
 358         // Set /Gy option
 359         addAttr(rv, "FunctionLevelLinking", "true");
 360 
 361         return rv;
 362     }
 363 
 364     @Override
 365     Vector getBaseLinkerFlags(String outDir, String outDll, String platformName) {
 366         Vector rv = new Vector();
 367 
 368         addAttr(rv, "AdditionalOptions",
 369                 "/export:JNI_GetDefaultJavaVMInitArgs " +
 370                 "/export:JNI_CreateJavaVM " +
 371                 "/export:JVM_FindClassFromBootLoader "+
 372                 "/export:JNI_GetCreatedJavaVMs "+
 373                 "/export:jio_snprintf /export:jio_printf "+
 374                 "/export:jio_fprintf /export:jio_vfprintf "+
 375                 "/export:jio_vsnprintf "+
 376                 "/export:JVM_GetVersionInfo "+
 377                 "/export:JVM_GetThreadStateNames "+
 378                 "/export:JVM_GetThreadStateValues "+
 379                 "/export:JVM_InitAgentProperties");
 380         addAttr(rv, "AdditionalDependencies", "kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;Wsock32.lib;winmm.lib;psapi.lib");
 381         addAttr(rv, "OutputFile", outDll);
 382         addAttr(rv, "SuppressStartupBanner", "true");
 383         addAttr(rv, "ModuleDefinitionFile", outDir+Util.sep+"vm.def");
 384         addAttr(rv, "ProgramDatabaseFile", outDir+Util.sep+"jvm.pdb");
 385         addAttr(rv, "SubSystem", "Windows");
 386         addAttr(rv, "BaseAddress", "0x8000000");
 387         addAttr(rv, "ImportLibrary", outDir+Util.sep+"jvm.lib");
 388 
 389         if(platformName.equals("Win32")) {
 390             addAttr(rv, "TargetMachine", "MachineX86");
 391         } else {
 392             addAttr(rv, "TargetMachine", "MachineX64");
 393         }
 394 
 395         // We always want the /DEBUG option to get full symbol information in the pdb files
 396         addAttr(rv, "GenerateDebugInformation", "true");
 397 
 398         return rv;
 399     }
 400 
 401     @Override
 402     Vector getDebugLinkerFlags() {
 403         Vector rv = new Vector();
 404 
 405         // Empty now that /DEBUG option is used by all configs
 406 
 407         return rv;
 408     }
 409 
 410     @Override
 411     Vector getProductLinkerFlags() {
 412         Vector rv = new Vector();
 413 
 414         // Set /OPT:REF option.
 415         addAttr(rv, "OptimizeReferences", "true");
 416         // Set /OPT:ICF option.
 417         addAttr(rv, "EnableCOMDATFolding", "true");
 418 
 419         return rv;
 420     }
 421 
 422     @Override
 423     void getAdditionalNonKernelLinkerFlags(Vector rv) {
 424         extAttr(rv, "AdditionalOptions", " /export:AsyncGetCallTrace");
 425     }
 426 
 427     @Override
 428     String getOptFlag() {
 429         return "MaxSpeed";
 430     }
 431 
 432     @Override
 433     String getNoOptFlag() {
 434         return "Disabled";
 435     }
 436 
 437     @Override
 438     String makeCfgName(String flavourBuild, String platform) {
 439         return  flavourBuild + "|" + platform;
 440     }
 441 }