patch.lua 6.2 KB

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