h2inc.ps1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) .NET Foundation and Contributors
  4. #
  5. # All rights reserved.
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # 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
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. # C to MASM include file translator
  25. # This is replacement for the deprecated h2inc tool that used to be part of VS.
  26. #
  27. # The use of [console]::WriteLine (instead of Write-Output) is intentional.
  28. # PowerShell 2.0 (installed by default on Windows 7) wraps lines written with
  29. # Write-Output at whatever column width is being used by the current terminal,
  30. # even when output is being redirected to a file. We can't have this behavior
  31. # because it will cause the generated file to be malformed.
  32. #
  33. Function ProcessFile($filePath) {
  34. [console]::WriteLine("; File start: $filePath")
  35. Get-Content $filePath | ForEach-Object {
  36. if ($_ -match "^\s*#\spragma") {
  37. # Ignore pragmas
  38. return
  39. }
  40. if ($_ -match "^\s*#\s*include\s*`"(.*)`"")
  41. {
  42. # Expand includes.
  43. ProcessFile(Join-Path (Split-Path -Parent $filePath) $Matches[1])
  44. return
  45. }
  46. if ($_ -match "^\s*#define\s+(\S+)\s*(.*)")
  47. {
  48. # Augment #defines with their MASM equivalent
  49. $name = $Matches[1]
  50. $value = $Matches[2]
  51. # Note that we do not handle multiline constants
  52. # Strip comments from value
  53. $value = $value -replace "//.*", ""
  54. $value = $value -replace "/\*.*\*/", ""
  55. # Strip whitespaces from value
  56. $value = $value -replace "\s+$", ""
  57. # ignore #defines with arguments
  58. if ($name -notmatch "\(") {
  59. $HEX_NUMBER_PATTERN = "\b0x(\w+)\b"
  60. $DECIMAL_NUMBER_PATTERN = "(-?\b\d+\b)"
  61. if ($value -match $HEX_NUMBER_PATTERN -or $value -match $DECIMAL_NUMBER_PATTERN) {
  62. $value = $value -replace $HEX_NUMBER_PATTERN, "0`$1h" # Convert hex constants
  63. $value = $value -replace $DECIMAL_NUMBER_PATTERN, "`$1t" # Convert dec constants
  64. [console]::WriteLine("$name EQU $value")
  65. } else {
  66. [console]::WriteLine("$name TEXTEQU <$value>")
  67. }
  68. }
  69. }
  70. # [console]::WriteLine("$_")
  71. }
  72. [console]::WriteLine("; File end: $filePath")
  73. }
  74. ProcessFile $args[0]