Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenSSL: improve MinGW support on Windows #6079

Merged
merged 21 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4a58502
openssl: enhance test
Doekin Dec 27, 2024
4556226
openssl: improve cross-build support on Windows
Doekin Dec 28, 2024
17ea9ac
openssl: Remove always-true `if` condition
Doekin Dec 29, 2024
abe12e9
openssl: handle mismatched Perl
Doekin Dec 29, 2024
79e98d4
openssl: fix Lua script
Doekin Dec 29, 2024
79c6f04
openssl: fix removing DLL import libraries unexpectedly
Doekin Dec 29, 2024
0e4066b
openssl: fix BSD platform
Doekin Dec 29, 2024
d5e12bc
openssl: fix wrong config
Doekin Dec 29, 2024
661f083
openssl: skip building tests
Doekin Dec 29, 2024
2121619
openssl: improve Perl dependency handling for Windows
Doekin Dec 29, 2024
2cf58d8
openssl: fix overlong make recipe
Doekin Dec 29, 2024
43c7fb9
openssl: trim trailing whitespace
Doekin Dec 29, 2024
0de6b83
openssl: improve Perl platform compatibility warning message
Doekin Dec 29, 2024
aeecc86
openssl: ensure ARFLAGS is not overridden with an empty string
Doekin Dec 30, 2024
a7144ca
openssl: restore the check for precompiled packages
Doekin Dec 30, 2024
409ef37
openssl: add LLVM resource compiler support for MinGW
Doekin Dec 30, 2024
6d5fbd8
openssl: remove temporary file after use
Doekin Dec 31, 2024
d6a3f76
openssl: add try-catch to configuration and Makefile patch functions
Doekin Dec 31, 2024
c86fa6e
openssl: improve compatibility with older versions
Doekin Dec 31, 2024
dbe0b01
openssl: add warning for building versions earlier than 1.1.1
Doekin Dec 31, 2024
977b69c
openssl: improve version checks
Doekin Dec 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions packages/o/openssl/configure/patch.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
function _fix_overlong_make_recipe(package)
-- In the MSYS environment, the make recipe can be too long to execute.
-- This patch is adapted from OpenSSL 3.
-- For more details, see: https://github.com/openssl/openssl/issues/12116
io.gsub("Configurations/00-base-templates.conf", -- replace default AR

"DEFAULTS%s-=>%s-{" ..
"(.-)"..
[[AR%s-=>%s-"%S-"%s-,]].. -- AR => "ar",
"(.-)}",

"DEFAULTS => {"..
"%1"..
[[AR => "(unused)",]] ..
"%2}")
io.gsub("Configurations/00-base-templates.conf", -- replace default ARFLAGS

"DEFAULTS%s-=>%s-{" ..
"(.-)"..
[[ARFLAGS%s-=>%s-"%S-"%s-,]].. -- ARFLAGS => "r",
"(.-)}",

"DEFAULTS => {"..
"%1"..
[[ARFLAGS => "(unused)",]] ..
"%2}")
io.gsub("Configurations/00-base-templates.conf", -- replace BASE_unix ARFLAGS

"BASE_unix%s-=>%s-{" ..
"(.-)"..
[[ARFLAGS%s-=>%s-"%S-"%s-,]].. -- ARFLAGS => "r",
"(.-)}",

"BASE_unix => {"..
"%1"..
[[ARFLAGS => "qc",]] ..
"%2}")
io.gsub("Configurations/unix-Makefile.tmpl", -- insert fill_lines function

"(sub%s-dependmagic%s-{)" ..
"(.-)"..
"}%s-'';",

"%1"..
"%2"..
"}\n"..
[[
sub fill_lines {
my $item_sep = shift; # string
my $line_length = shift; # number of chars

my @result = ();
my $resultpos = 0;

foreach (@_) {
my $fill_line = $result[$resultpos] // '';
my $newline =
($fill_line eq '' ? '' : $fill_line . $item_sep) . $_;

if (length($newline) > $line_length) {
# If this is a single item and the intended result line
# is empty, we put it there anyway
if ($fill_line eq '') {
$result[$resultpos++] = $newline;
} else {
$result[++$resultpos] = $_;
}
} else {
$result[$resultpos] = $newline;
}
}
return @result;
}
]]..
[['';]])

io.gsub("Configurations/unix-Makefile.tmpl", -- change the way we handle dependencies

"sub%s-libobj2shlib%s-{" ..
"(.-)"..
[[my%s-%$objs.-;]].. -- my $objs = join(" ", @objs);
"(.-)}",

"sub libobj2shlib {"..
"%1"..
[[my $objs =
join(" \\\n\t\t", fill_lines(' ', $COLUMNS - 16, @objs));]] ..
"%2}")
io.gsub("Configurations/unix-Makefile.tmpl", -- change the way we handle dependencies

"sub%s-libobj2shlib%s-{" ..
"(.-)"..
[[my%s-%$deps.-;]].. -- my $deps = join(" ", @objs, @defs, @deps);
"(.-)}",

"sub libobj2shlib {"..
"%1"..
[[my @fulldeps = (@objs, @defs, @deps);
my $fulldeps =
join(" \\\n" . ' ' x (length($full) + 2),
fill_lines(' ', $COLUMNS - length($full) - 2, @fulldeps));]] ..
"%2}")
io.gsub("Configurations/unix-Makefile.tmpl",

"sub%s-libobj2shlib%s-{" ..
"(.-)"..
[[%$target:%s-%$deps]].. -- $target: $deps
"(.-)}",

"sub libobj2shlib {"..
"%1"..
[[$target: $fulldeps]] ..
"%2}")
io.gsub("Configurations/unix-Makefile.tmpl",

"sub%s-obj2lib%s-{" ..
"(.-)"..
[[my%s-%$objs.-;]].. -- my $objs = join(" ", @objs);
"(.-)}",

"sub obj2lib {"..
"%1"..
[[my $deps = join(" \\\n" . ' ' x (length($lib) + 2),
fill_lines(' ', $COLUMNS - length($lib) - 2, @objs));
my $max_per_call = 250;
my @objs_grouped;
push @objs_grouped, join(" ", splice @objs, 0, $max_per_call) while @objs;
my $fill_lib =
join("\n\t", (map { "\$(AR) \$(ARFLAGS) $lib$libext $_" } @objs_grouped));]] ..
"%2}")
io.gsub("Configurations/unix-Makefile.tmpl",

"sub%s-obj2lib%s-{" ..
"(.-)"..
[[%$lib%$libext:.-]].. -- $lib$libext: $objs
"EOF",

"sub obj2lib {"..
"%1"..
"$lib$libext: $deps\n" ..
'\t' .. [[\$(RM) $lib$libext]] ..'\n' ..
'\t' .. [[$fill_lib]] ..'\n' ..
'\t' .. [[\$(RANLIB) \$\@ || echo Never mind.]] .. '\n' ..
"EOF")
end

function _remove_unused_pod_usage(package)
-- Perl in "Git for Windows" lacks Pod::Usage, which is only used for help messages in the Configure script.
-- It is not needed for the build and can be safely removed to avoid errors from the missing module.
if package:version():le("1.1.0") then
return
end
io.replace("Configure", "use Pod::Usage;", "", {plain = true})
io.replace("Configure", "pod2usage.-;", "")
end

function _replace_NUL_with_null(package)
-- The Configure script uses "NUL" to redirect output on Windows when checking NASM.
-- Creating a file named "NUL" can cause issues because "NUL" is a reserved name in Windows.
if package:version():le("1.1.0") then
return
end
io.replace("Configurations/10-main.conf", "NUL", "null", {plain = true})
end

function main(package)
_remove_unused_pod_usage(package)
_replace_NUL_with_null(package)
_fix_overlong_make_recipe(package)
end
36 changes: 36 additions & 0 deletions packages/o/openssl/makefile/patch.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function _patch_for_llvm_rc(package, opt)
if package:is_plat("mingw") and package:has_tool("mrc", "llvm_rc", "llvm-rc", "rc") then
local cc = package:build_getenv("cc")
local cflags = opt.buildenvs.CFLAGS
local tmpfile = path.unix(os.tmpfile() .. ".c")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to remove tmpfile after running this script

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

io.writefile(tmpfile, "int main(void) { return 0; }\n")
local compile_out, compile_err = try {function() return os.iorun(format("%s -v %s %s", cc, cflags, tmpfile)) end}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use lua table as argv and call os.iorunv(cc, argv)

Copy link
Contributor Author

@Doekin Doekin Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encountered an issue where the CFLAGS might be set to -m64 --target=x86_64-w64-windows-gnu, resulting in a concatenated command like clang "-v" "-m64 --target=x86_64-w64-windows-gnu" "a.c". This causes the following error:

clang-19: error: unknown argument: '-m64 --target=x86_64-w64-windows-gnu'
clang version 19.1.6 (https://github.com/llvm/llvm-project.git e21dc4bd5474d04b8e62d7331362edcc5648d7e5)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: D:/Applications/Scoop/apps/mingw-mstorsjo-llvm-ucrt/19.1.6-20241217/bin

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because buildenvs.CFLAGS has been merged to string. not lua table.

os.tryrm(tmpfile)
local include_dirs = {}
local in_include_section = false
for _, verbose_command in ipairs({compile_out, compile_err}) do
if verbose_command then
for line in verbose_command:gmatch("[^\r\n]+") do
if line:find("#include.*search") then
in_include_section = true
elseif line:find("End.*search") then
in_include_section = false
elseif in_include_section and not line:find("#include.*search") then
table.insert(include_dirs, line:match("^%s*(.-)%s*$"))
end
end
end
end
local include_directive = ""
for _, include_dir in ipairs(include_dirs) do
include_directive = include_directive .. format([[ -I "%s"]], include_dir)
end
-- $(RC) $(RCFLAGS) -o $@ libcrypto.rc => $(RC) -I inc_dir -I inc_dir -FO $@ libcrypto.rc
io.gsub("Makefile", [[%$%(RC%).-%$@%s+(%S+)]], format("$(RC) %s -FO $@ ", include_directive).."%1")
io.gsub("Makefile", "(%S+).res.o", "%1.res")
end
end

function main(package, opt)
try {function() return _patch_for_llvm_rc(package, opt) end}
end
Loading
Loading