[VS AddIn] Fix default library paths.

Also, add pnacl-finalize step to PNaCl build.

The default library path for all platforms was wrong and
did not include the location of the SDK libraries such
as ppapi_cpp.  There was a bug in the test code that meant
that we didn't catch this since we where not actually linking
any of the test projects to .nexe or .pexe files.

R=binji@chromium.org

Review URL: https://codereview.chromium.org/69343013

git-svn-id: https://nativeclient-sdk.googlecode.com/svn/trunk/src@1567 050acbb0-2703-11df-ab0a-9f3f633ae91d
diff --git a/visual_studio/NativeClientVSAddIn/InstallerResources/NaCl/NaCl.Paths.default.props b/visual_studio/NativeClientVSAddIn/InstallerResources/NaCl/NaCl.Paths.default.props
index 4467973..16533fe 100644
--- a/visual_studio/NativeClientVSAddIn/InstallerResources/NaCl/NaCl.Paths.default.props
+++ b/visual_studio/NativeClientVSAddIn/InstallerResources/NaCl/NaCl.Paths.default.props
@@ -22,8 +22,10 @@
 
     <ExecutablePath Condition="'$(ExecutablePath)' == ''">$(PATH);</ExecutablePath>
     <IncludePath Condition="'$(IncludePath)' == ''">$(VSNaClSDKRoot)include;</IncludePath>
-    <LibraryPath Condition="'$(LibraryPath)' == '' AND '$(TargetArchitecture)'=='i686'">$(VSNaClSDKRoot)lib32;</LibraryPath>
-    <LibraryPath Condition="'$(LibraryPath)' == '' AND '$(TargetArchitecture)'=='x86_64'">$(VSNaClSDKRoot)lib;</LibraryPath>
+    <LibraryPath Condition="'$(LibraryPath)' == '' AND '$(TargetArchitecture)'=='i686'">$(VSNaClSDKRoot)lib\$(ToolchainName)_x86_32\$(Configuration);</LibraryPath>
+    <LibraryPath Condition="'$(LibraryPath)' == '' AND '$(TargetArchitecture)'=='x86_64'">$(VSNaClSDKRoot)lib\$(ToolchainName)_x86_64\$(Configuration);</LibraryPath>
+    <LibraryPath Condition="'$(LibraryPath)' == '' AND '$(TargetArchitecture)'=='arm'">$(VSNaClSDKRoot)lib\$(ToolchainName)_arm\$(Configuration);</LibraryPath>
+    <LibraryPath Condition="'$(LibraryPath)' == '' AND '$(TargetArchitecture)'=='pnacl'">$(VSNaClSDKRoot)lib\pnacl\$(Configuration);</LibraryPath>
 
   </PropertyGroup>
 </Project>
diff --git a/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClCompile.cs b/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClCompile.cs
index 3838764..71725df 100644
--- a/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClCompile.cs
+++ b/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClCompile.cs
@@ -193,7 +193,7 @@
             StringBuilder commandLine = new StringBuilder(GCCUtilities.s_CommandLineLength);
 
             //build command line from components and add required switches
-            string props = xamlParser.Parse(sourceFile, fullOutputName);
+            string props = xamlParser.Parse(sourceFile, fullOutputName, null);
             commandLine.Append(props);
             commandLine.Append(" -c");
 
diff --git a/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClLink.cs b/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClLink.cs
index 61cd7f6..4bb3244 100644
--- a/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClLink.cs
+++ b/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/NaClLink.cs
@@ -77,17 +77,33 @@
             }
             responseFileCmds.Append("-Wl,--end-group ");
 
-            responseFileCmds.Append(xamlParser.Parse(Sources[0], false));
+            responseFileCmds.Append(xamlParser.Parse(Sources[0], false, IsPNaCl() ? ".bc" : null));
 
             return responseFileCmds.ToString();
         }
 
+        private static string PexeToBC(string pexe)
+        {
+            return Path.ChangeExtension(pexe, ".bc");
+        }
+
         private static string PexeToNexe(string pexe, string arch)
         {
             string basename = Path.GetFileNameWithoutExtension(pexe) + "_" + arch + ".nexe";
             return Path.Combine(Path.GetDirectoryName(pexe), basename);
         }
 
+        private bool Finalize()
+        {
+            string dirname = Path.GetDirectoryName(GenerateFullPathToTool());
+            string finalize = Path.Combine(dirname, "pnacl-finalize.bat");
+            string cmd = String.Format("\"{0}\" -o \"{1}\"", PexeToBC(OutputFile), OutputFile);
+            if (!OutputCommandLine)
+                Log.LogMessage("pnacl-finalize -> {0}", Path.GetFileName(OutputFile));
+
+            return ExecuteTool(finalize, cmd, string.Empty) == 0;
+        }
+
         private bool Translate(string arch, string pnacl_arch=null)
         {
             if (pnacl_arch == null)
@@ -101,18 +117,17 @@
             if (!OutputCommandLine)
                 Log.LogMessage("pnacl-translate -> {0}", Path.GetFileName(outfile));
 
-            if (ExecuteTool(translateTool, cmd, string.Empty) != 0)
-            {
-                return false;
-            }
-
-            return true;
+            return ExecuteTool(translateTool, cmd, string.Empty) == 0;
         }
 
         public override bool Execute()
         {
-            if (!OutputCommandLine)
-                Log.LogMessage("Linking: {0}", Path.GetFileName(OutputFile));
+            if (!OutputCommandLine) {
+                string filename = OutputFile;
+                if (IsPNaCl())
+                   filename = PexeToBC(OutputFile);
+                Log.LogMessage("Linking: {0}", filename);
+            }
 
             if (!base.Execute())
                 return false;
@@ -128,6 +143,9 @@
         {
             if (IsPNaCl())
             {
+                if (!Finalize())
+                    return false;
+
                 if (TranslateX64 && !Translate("64", "x86-64"))
                     return false;
 
@@ -180,14 +198,6 @@
                 }
                 else
                 {
-                    if (ToolchainName == "glibc")
-                    {
-                        string bindir = Path.GetDirectoryName(NaClLinkerPath);
-                        string tcroot = Path.GetDirectoryName(bindir);
-                        cmd += " -D \"" + Path.Combine(bindir, "x86_64-nacl-objdump.exe") + "\"";
-                        cmd += " -L \"" + Path.Combine(tcroot, "x86_64-nacl", "lib") + "\"";
-                        cmd += " -L \"" + Path.Combine(tcroot, "x86_64-nacl", "lib32") + "\"";
-                    }
                     cmd += " \"" + OutputFile + "\"";
                 }
 
@@ -195,9 +205,7 @@
                     Log.LogMessage("CreateNMF -> {0}", Path.GetFileName(nmfPath));
 
                 if (ExecuteTool("python", string.Empty, cmd) != 0)
-                {
                     return false;
-                }
             }
 
             return true;
@@ -206,9 +214,7 @@
         protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
         {
             if (OutputCommandLine)
-            {
                 Log.LogMessage(MessageImportance.High, pathToTool + "  " + responseFileCommands + " " + commandLineCommands);
-            }
 
             return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
         }
diff --git a/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/XamlParser.cs b/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/XamlParser.cs
index 652cab9..b2b3be1 100644
--- a/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/XamlParser.cs
+++ b/visual_studio/NativeClientVSAddIn/NaCl.Build.CPPTasks/XamlParser.cs
@@ -49,13 +49,17 @@
             };
         }
 
-        public string Parse(ITaskItem taskItem, bool fullOutputName)
+        public string Parse(ITaskItem taskItem, bool fullOutputName, string outputExtension)
         {
             CommandLineBuilder builder = new CommandLineBuilder();
 
             foreach (string name in taskItem.MetadataNames)
             {
                 string value = taskItem.GetMetadata(name);
+                if (outputExtension != null && name == "OutputFile")
+                {
+                    value = Path.ChangeExtension(value, outputExtension);
+                }
                 if (fullOutputName && name == "ObjectFileName")
                 {
                     if ((File.GetAttributes(value) & FileAttributes.Directory) != 0)
diff --git a/visual_studio/NativeClientVSAddIn/UnitTests/BaseCompileTest.cs b/visual_studio/NativeClientVSAddIn/UnitTests/BaseCompileTest.cs
index 03959c3..be46d2d 100644
--- a/visual_studio/NativeClientVSAddIn/UnitTests/BaseCompileTest.cs
+++ b/visual_studio/NativeClientVSAddIn/UnitTests/BaseCompileTest.cs
@@ -106,16 +106,21 @@
             TestUtilities.SetProjectType(project, projectType, platformName);
         }
 
-        protected void CheckCompile(string platform, bool dll)
+        protected void CheckCompile(string platform)
         {
+            bool ppapi = platform == Strings.PepperPlatformName;
             dte_.Solution.Open(SolutionName_);
-            SetProjectType("Executable", platform);
-            TryCompile("Debug", platform);
-            TryCompile("Release", platform);
+            if (!ppapi)
+            {
+                // PPAPI host build are always either dll's or static libraries.
+                SetProjectType("Application", platform);
+                TryCompile("Debug", platform);
+                TryCompile("Release", platform);
+            }
             SetProjectType("StaticLibrary", platform);
             TryCompile("Debug", platform);
             TryCompile("Release", platform);
-            if (dll)
+            if (ppapi)
             {
                 SetProjectType("DynamicLibrary", platform);
                 TryCompile("Debug", platform);
diff --git a/visual_studio/NativeClientVSAddIn/UnitTests/CompileParallelTest.cs b/visual_studio/NativeClientVSAddIn/UnitTests/CompileParallelTest.cs
index e52a0b4..a86301c 100644
--- a/visual_studio/NativeClientVSAddIn/UnitTests/CompileParallelTest.cs
+++ b/visual_studio/NativeClientVSAddIn/UnitTests/CompileParallelTest.cs
@@ -55,7 +55,7 @@
 
             dte_.Solution.Close(true);
 
-            CheckCompile(platform, false);
+            CheckCompile(platform);
         }
     }
 }
diff --git a/visual_studio/NativeClientVSAddIn/UnitTests/CompileTest.cs b/visual_studio/NativeClientVSAddIn/UnitTests/CompileTest.cs
index dc301c1..629ce3e 100644
--- a/visual_studio/NativeClientVSAddIn/UnitTests/CompileTest.cs
+++ b/visual_studio/NativeClientVSAddIn/UnitTests/CompileTest.cs
@@ -38,7 +38,7 @@
         [TestMethod]
         public void CheckNaCl64Compile()
         {
-            CheckCompile(Strings.NaCl64PlatformName, false);
+            CheckCompile(Strings.NaCl64PlatformName);
         }
 
         /// <summary>
@@ -52,7 +52,7 @@
             {
                 Assert.Inconclusive();
             }
-            CheckCompile(Strings.NaClARMPlatformName, false);
+            CheckCompile(Strings.NaClARMPlatformName);
         }
 
         /// <summary>
@@ -61,7 +61,7 @@
         [TestMethod]
         public void CheckPepperCompile()
         {
-            CheckCompile(Strings.PepperPlatformName, true);
+            CheckCompile(Strings.PepperPlatformName);
         }
 
         /// <summary>
@@ -75,7 +75,7 @@
             {
                 Assert.Inconclusive();
             }
-            CheckCompile(Strings.PNaClPlatformName, false);
+            CheckCompile(Strings.PNaClPlatformName);
         }
 
     }
diff --git a/visual_studio/NativeClientVSAddIn/UnitTests/ProjectSettingsTest.cs b/visual_studio/NativeClientVSAddIn/UnitTests/ProjectSettingsTest.cs
index a01c090..03728ac 100644
--- a/visual_studio/NativeClientVSAddIn/UnitTests/ProjectSettingsTest.cs
+++ b/visual_studio/NativeClientVSAddIn/UnitTests/ProjectSettingsTest.cs
@@ -407,8 +407,8 @@
 
       // VC++ Directories
       page = "ConfigurationDirectories";
-      AllConfigsAssertPropertyContains(page, "IncludePath", @"$(VSNaClSDKRoot)include;", true);
-      AllConfigsAssertPropertyContains(page, "LibraryPath", @"$(VSNaClSDKRoot)lib;", true);
+      AllConfigsAssertPropertyContains(page, "IncludePath", @"$(VSNaClSDKRoot)include;", true);

+      AllConfigsAssertPropertyContains(page, "LibraryPath", @"$(VSNaClSDKRoot)lib\$(ToolchainName)_x86_64\$(Configuration);", true);
 
       // C/C++ General
       page = "CL";
diff --git a/visual_studio/NativeClientVSAddIn/create_package.py b/visual_studio/NativeClientVSAddIn/create_package.py
index 9eac232..eb7ef21 100644
--- a/visual_studio/NativeClientVSAddIn/create_package.py
+++ b/visual_studio/NativeClientVSAddIn/create_package.py
@@ -213,7 +213,7 @@
   # Create PNaCl
   pnacl_replacements = {
       'NaCl64': 'PNaCl',
-      'x86_64': 'i686',
+      'x86_64': 'pnacl',
       '64': '32',
       '.nexe': '.pexe',
       'nacl_link.xml': 'pnacl_link.xml',