llvm-mt: Merge manifest namespaces.

mt.exe performs a tree merge where certain element nodes are combined
into one.  This introduces the possibility of xml namespaces conflicting
with each other.  The original mt.exe has a hierarchy whereby certain
namespace names can override others, and nodes that would then end up in
ambigious namespaces have their namespaces explicitly defined.  This
namespace handles this merging process.

llvm-svn: 311215
diff --git a/llvm/test/tools/llvm-mt/Inputs/assembly_identity.manifest b/llvm/test/tools/llvm-mt/Inputs/assembly_identity.manifest
new file mode 100644
index 0000000..2090be1
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/Inputs/assembly_identity.manifest
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<assembly xmlns="urn:schemas-microsoft-com:asm.v2" manifestVersion="1.0">
+  <dependency>
+    <dependentAssembly>
+      <assemblyIdentity foo="bar" name="identity1" version="1.2.3.4"></assemblyIdentity>
+    </dependentAssembly>
+  </dependency>
+</assembly>
diff --git a/llvm/test/tools/llvm-mt/Inputs/compatibility.manifest b/llvm/test/tools/llvm-mt/Inputs/compatibility.manifest
new file mode 100644
index 0000000..4a5d3bd
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/Inputs/compatibility.manifest
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <application>
+    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+      <supportedOS Id="FooOS"/>
+      <supportedOS Id="BarOS"/>
+    </compatibility>
+  </application>
+</assembly>
diff --git a/llvm/test/tools/llvm-mt/Inputs/expected_big.manifest b/llvm/test/tools/llvm-mt/Inputs/expected_big.manifest
new file mode 100644
index 0000000..1d88447
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/Inputs/expected_big.manifest
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
+<ms_asmv1:trustInfo xmlns="urn:schemas-microsoft-com:asm.v3" xmlns:ms_asmv1="urn:schemas-microsoft-com:asm.v1">
+  <ms_asmv1:security>
+    <ms_asmv1:requestedPrivileges>
+      <ms_asmv1:requestedExecutionLevel level="trust1" ms_asmv1:access="false"></ms_asmv1:requestedExecutionLevel>
+    </ms_asmv1:requestedPrivileges>
+  </ms_asmv1:security>
+</ms_asmv1:trustInfo>
+<ms_asmv2:dependency xmlns:ms_asmv2="urn:schemas-microsoft-com:asm.v2">
+  <ms_asmv2:dependentAssembly>
+    <ms_asmv2:assemblyIdentity foo="bar" name="identity1" version="1.2.3.4"></assemblyIdentity>
+  </ms_asmv2:dependentAssembly>
+</ms_asmv2:dependency>
+<dependency>
+  <dependentAssembly>
+    <assemblyIdentity bar="foo" name="my assembly" version="5"></assemblyIdentity>
+  </dependentAssembly>
+</dependency>
+<application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <supportedOS Id="FooOS"/>
+    <supportedOS Id="BarOS"/>
+  </compatibility>
+  <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
+    <dpiAware>true/pm</dpiAware>
+  </asmv3:windowsSettings>
+</application>
+</assembly>
diff --git a/llvm/test/tools/llvm-mt/Inputs/trust_and_identity.manifest b/llvm/test/tools/llvm-mt/Inputs/trust_and_identity.manifest
new file mode 100644
index 0000000..40c1b14
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/Inputs/trust_and_identity.manifest
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
+
+  <!--random comment -->
+
+  <dependency>
+    <dependentAssembly>
+      <assemblyIdentity bar="foo" name="my assembly" version="5"></assemblyIdentity>
+    </dependentAssembly>
+  </dependency>
+
+  <trustInfo>
+    <security>
+      <requestedPrivileges>
+        <requestedExecutionLevel level="trust1" access="false"/>
+      </requestedPrivileges>
+    </security>
+  </trustInfo>
+
+</assembly>
diff --git a/llvm/test/tools/llvm-mt/Inputs/trust_info.manifest b/llvm/test/tools/llvm-mt/Inputs/trust_info.manifest
new file mode 100644
index 0000000..114a118
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/Inputs/trust_info.manifest
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
+<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
+  <security>
+    <requestedPrivileges>
+      <requestedExecutionLevel level="trust1"></requestedExecutionLevel>
+    </requestedPrivileges>
+  </security>
+</trustInfo></assembly>
diff --git a/llvm/test/tools/llvm-mt/Inputs/windows_settings.manifest b/llvm/test/tools/llvm-mt/Inputs/windows_settings.manifest
new file mode 100644
index 0000000..20e4adc
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/Inputs/windows_settings.manifest
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<assembly xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings" manifestVersion="1.0">
+ <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
+    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
+      <dpiAware>true/pm</dpiAware>
+    </asmv3:windowsSettings>
+  </asmv3:application>
+</assembly>
diff --git a/llvm/test/tools/llvm-mt/big_merge.test b/llvm/test/tools/llvm-mt/big_merge.test
new file mode 100644
index 0000000..b16cc87
--- /dev/null
+++ b/llvm/test/tools/llvm-mt/big_merge.test
@@ -0,0 +1,39 @@
+REQUIRES: libxml2
+UNSUPPORTED: windows
+
+RUN: llvm-mt /manifest %p/Inputs/trust_info.manifest \
+RUN:   /manifest %p/Inputs/assembly_identity.manifest \
+RUN:   /manifest %p/Inputs/trust_and_identity.manifest \
+RUN:   /manifest %p/Inputs/compatibility.manifest \
+RUN:   /manifest  %p/Inputs/windows_settings.manifest /out:%t
+RUN: FileCheck %s -input-file=%t
+
+CHECK:      <?xml version="1.0" encoding="UTF-8"?>
+CHECK-NEXT: <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
+CHECK-NEXT: <ms_asmv1:trustInfo xmlns="urn:schemas-microsoft-com:asm.v3" xmlns:ms_asmv1="urn:schemas-microsoft-com:asm.v1">
+CHECK-NEXT:   <ms_asmv1:security>
+CHECK-NEXT:     <ms_asmv1:requestedPrivileges>
+CHECK-NEXT:       <ms_asmv1:requestedExecutionLevel level="trust1" ms_asmv1:access="false"/>
+CHECK-NEXT:     </ms_asmv1:requestedPrivileges>
+CHECK-NEXT:   </ms_asmv1:security>
+CHECK-NEXT: </ms_asmv1:trustInfo>
+CHECK-NEXT: <ms_asmv2:dependency xmlns:ms_asmv2="urn:schemas-microsoft-com:asm.v2">
+CHECK-NEXT:   <ms_asmv2:dependentAssembly>
+CHECK-NEXT:     <ms_asmv2:assemblyIdentity foo="bar" name="identity1" version="1.2.3.4"/>
+CHECK-NEXT:   </ms_asmv2:dependentAssembly>
+CHECK-NEXT: </ms_asmv2:dependency>
+CHECK-NEXT: <dependency>
+CHECK-NEXT:   <dependentAssembly>
+CHECK-NEXT:     <assemblyIdentity bar="foo" name="my assembly" version="5"/>
+CHECK-NEXT:   </dependentAssembly>
+CHECK-NEXT: </dependency>
+CHECK-NEXT: <application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
+CHECK-NEXT:   <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+CHECK-NEXT:     <supportedOS Id="FooOS"/>
+CHECK-NEXT:     <supportedOS Id="BarOS"/>
+CHECK-NEXT:   </compatibility>
+CHECK-NEXT:   <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
+CHECK-NEXT:     <dpiAware>true/pm</dpiAware>
+CHECK-NEXT:   </asmv3:windowsSettings>
+CHECK-NEXT: </application>
+CHECK-NEXT: </assembly>
diff --git a/llvm/test/tools/llvm-mt/help.test b/llvm/test/tools/llvm-mt/help.test
index 190e90a..c42273a 100644
--- a/llvm/test/tools/llvm-mt/help.test
+++ b/llvm/test/tools/llvm-mt/help.test
@@ -1,3 +1,7 @@
 RUN: llvm-mt /h | FileCheck %s -check-prefix=HELP
 
 HELP:      OVERVIEW: Manifest Tool
+
+RUN: not llvm-mt /foo 2>&1 >/dev/null | FileCheck %s -check-prefix=INVALID
+
+INVALID: llvm-mt error: invalid option /foo
diff --git a/llvm/test/tools/llvm-mt/simple_merge.test b/llvm/test/tools/llvm-mt/simple_merge.test
index 13b7808..3748ae7 100644
--- a/llvm/test/tools/llvm-mt/simple_merge.test
+++ b/llvm/test/tools/llvm-mt/simple_merge.test
@@ -5,7 +5,7 @@
 RUN:   %p/Inputs/additional.manifest /out:%t
 RUN: FileCheck %s -input-file=%t
 
-CHECK:      <?xml version="1.0"?>
+CHECK:      <?xml version="1.0" encoding="UTF-8"?>
 CHECK-NEXT: <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
 CHECK-NEXT:   <trustInfo>
 CHECK-NEXT:     <security>
diff --git a/llvm/test/tools/llvm-mt/single_file.test b/llvm/test/tools/llvm-mt/single_file.test
index 9db22c3..6b1429b 100644
--- a/llvm/test/tools/llvm-mt/single_file.test
+++ b/llvm/test/tools/llvm-mt/single_file.test
@@ -4,7 +4,7 @@
 RUN: llvm-mt /manifest %p/Inputs/test_manifest.manifest /out:%t
 RUN: FileCheck %s --input-file=%t
 
-CHECK:      <?xml version="1.0"?>
+CHECK:      <?xml version="1.0" encoding="UTF-8"?>
 CHECK-NEXT: <assembly xmlns="urn:schemas-microsoft-com:asm.v1">
 CHECK-NEXT:   <trustInfo>
 CHECK-NEXT:     <security>
diff --git a/llvm/test/tools/llvm-mt/xml_error.test b/llvm/test/tools/llvm-mt/xml_error.test
index 762a664..40537cd 100644
--- a/llvm/test/tools/llvm-mt/xml_error.test
+++ b/llvm/test/tools/llvm-mt/xml_error.test
@@ -11,8 +11,8 @@
 
 EMPTY: llvm-mt error: attempted to merge empty manifest
 
-RUN: llvm-mt /inputresource:foo.res /manifest \
-RUN:   %p/Inputs/test_manifest.manifest | FileCheck %s \
-RUN:    -check-prefix=NOT_SUPPORTED
+RUN: llvm-mt /inputresource:foo.res \
+RUN:   /manifest %p/Inputs/test_manifest.manifest \
+RUN:   /out:%t | FileCheck %s -check-prefix=NOT_SUPPORTED
 
 NOT_SUPPORTED: llvm-mt: ignoring unsupported 'inputresource:' option