patch.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. function _fix_overlong_make_recipe(package)
  2. -- In the MSYS environment, the make recipe can be too long to execute.
  3. -- This patch is adapted from OpenSSL 3.
  4. -- For more details, see: https://github.com/openssl/openssl/issues/12116
  5. io.gsub("Configurations/00-base-templates.conf", -- replace default AR
  6. "DEFAULTS%s-=>%s-{" ..
  7. "(.-)"..
  8. [[AR%s-=>%s-"%S-"%s-,]].. -- AR => "ar",
  9. "(.-)}",
  10. "DEFAULTS => {"..
  11. "%1"..
  12. [[AR => "(unused)",]] ..
  13. "%2}")
  14. io.gsub("Configurations/00-base-templates.conf", -- replace default ARFLAGS
  15. "DEFAULTS%s-=>%s-{" ..
  16. "(.-)"..
  17. [[ARFLAGS%s-=>%s-"%S-"%s-,]].. -- ARFLAGS => "r",
  18. "(.-)}",
  19. "DEFAULTS => {"..
  20. "%1"..
  21. [[ARFLAGS => "(unused)",]] ..
  22. "%2}")
  23. io.gsub("Configurations/00-base-templates.conf", -- replace BASE_unix ARFLAGS
  24. "BASE_unix%s-=>%s-{" ..
  25. "(.-)"..
  26. [[ARFLAGS%s-=>%s-"%S-"%s-,]].. -- ARFLAGS => "r",
  27. "(.-)}",
  28. "BASE_unix => {"..
  29. "%1"..
  30. [[ARFLAGS => "qc",]] ..
  31. "%2}")
  32. io.gsub("Configurations/unix-Makefile.tmpl", -- insert fill_lines function
  33. "(sub%s-dependmagic%s-{)" ..
  34. "(.-)"..
  35. "}%s-'';",
  36. "%1"..
  37. "%2"..
  38. "}\n"..
  39. [[
  40. sub fill_lines {
  41. my $item_sep = shift; # string
  42. my $line_length = shift; # number of chars
  43. my @result = ();
  44. my $resultpos = 0;
  45. foreach (@_) {
  46. my $fill_line = $result[$resultpos] // '';
  47. my $newline =
  48. ($fill_line eq '' ? '' : $fill_line . $item_sep) . $_;
  49. if (length($newline) > $line_length) {
  50. # If this is a single item and the intended result line
  51. # is empty, we put it there anyway
  52. if ($fill_line eq '') {
  53. $result[$resultpos++] = $newline;
  54. } else {
  55. $result[++$resultpos] = $_;
  56. }
  57. } else {
  58. $result[$resultpos] = $newline;
  59. }
  60. }
  61. return @result;
  62. }
  63. ]]..
  64. [['';]])
  65. io.gsub("Configurations/unix-Makefile.tmpl", -- change the way we handle dependencies
  66. "sub%s-libobj2shlib%s-{" ..
  67. "(.-)"..
  68. [[my%s-%$objs.-;]].. -- my $objs = join(" ", @objs);
  69. "(.-)}",
  70. "sub libobj2shlib {"..
  71. "%1"..
  72. [[my $objs =
  73. join(" \\\n\t\t", fill_lines(' ', $COLUMNS - 16, @objs));]] ..
  74. "%2}")
  75. io.gsub("Configurations/unix-Makefile.tmpl", -- change the way we handle dependencies
  76. "sub%s-libobj2shlib%s-{" ..
  77. "(.-)"..
  78. [[my%s-%$deps.-;]].. -- my $deps = join(" ", @objs, @defs, @deps);
  79. "(.-)}",
  80. "sub libobj2shlib {"..
  81. "%1"..
  82. [[my @fulldeps = (@objs, @defs, @deps);
  83. my $fulldeps =
  84. join(" \\\n" . ' ' x (length($full) + 2),
  85. fill_lines(' ', $COLUMNS - length($full) - 2, @fulldeps));]] ..
  86. "%2}")
  87. io.gsub("Configurations/unix-Makefile.tmpl",
  88. "sub%s-libobj2shlib%s-{" ..
  89. "(.-)"..
  90. [[%$target:%s-%$deps]].. -- $target: $deps
  91. "(.-)}",
  92. "sub libobj2shlib {"..
  93. "%1"..
  94. [[$target: $fulldeps]] ..
  95. "%2}")
  96. io.gsub("Configurations/unix-Makefile.tmpl",
  97. "sub%s-obj2lib%s-{" ..
  98. "(.-)"..
  99. [[my%s-%$objs.-;]].. -- my $objs = join(" ", @objs);
  100. "(.-)}",
  101. "sub obj2lib {"..
  102. "%1"..
  103. [[my $deps = join(" \\\n" . ' ' x (length($lib) + 2),
  104. fill_lines(' ', $COLUMNS - length($lib) - 2, @objs));
  105. my $max_per_call = 250;
  106. my @objs_grouped;
  107. push @objs_grouped, join(" ", splice @objs, 0, $max_per_call) while @objs;
  108. my $fill_lib =
  109. join("\n\t", (map { "\$(AR) \$(ARFLAGS) $lib$libext $_" } @objs_grouped));]] ..
  110. "%2}")
  111. io.gsub("Configurations/unix-Makefile.tmpl",
  112. "sub%s-obj2lib%s-{" ..
  113. "(.-)"..
  114. [[%$lib%$libext:.-]].. -- $lib$libext: $objs
  115. "EOF",
  116. "sub obj2lib {"..
  117. "%1"..
  118. "$lib$libext: $deps\n" ..
  119. '\t' .. [[\$(RM) $lib$libext]] ..'\n' ..
  120. '\t' .. [[$fill_lib]] ..'\n' ..
  121. '\t' .. [[\$(RANLIB) \$\@ || echo Never mind.]] .. '\n' ..
  122. "EOF")
  123. end
  124. function _remove_unused_pod_usage(package)
  125. -- Perl in "Git for Windows" lacks Pod::Usage, which is only used for help messages in the Configure script.
  126. -- It is not needed for the build and can be safely removed to avoid errors from the missing module.
  127. if package:version():le("1.1.0") then
  128. return
  129. end
  130. io.replace("Configure", "use Pod::Usage;", "", {plain = true})
  131. io.replace("Configure", "pod2usage.-;", "")
  132. end
  133. function _replace_NUL_with_null(package)
  134. -- The Configure script uses "NUL" to redirect output on Windows when checking NASM.
  135. -- Creating a file named "NUL" can cause issues because "NUL" is a reserved name in Windows.
  136. if package:version():le("1.1.0") then
  137. return
  138. end
  139. io.replace("Configurations/10-main.conf", "NUL", "null", {plain = true})
  140. end
  141. function main(package)
  142. _remove_unused_pod_usage(package)
  143. _replace_NUL_with_null(package)
  144. _fix_overlong_make_recipe(package)
  145. end