Skip to content

Commit

Permalink
Manually compile libargon2 instead of using make
Browse files Browse the repository at this point in the history
As the libargon2 Makefile shells out to `uname -s` and `uname -m`, it
passes invalid linker flags when cross-compiling (as we're compiling for
one OS but on a host machine that might use another).

Instead of bending over backwards to work around this, abandon using
their Makefile altogether and instead reproduce the specific logic we
need, i.e.

1. Compile each of the core object files in a position independent way
2. Create a static archive of those object files

This way, we only build what we need and hopefully in a way that is a
static library on as many platforms as possible.
  • Loading branch information
mudge committed Oct 31, 2024
1 parent 99f534a commit 2bec2b8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Argon2::Password.create("opensesame") == "notopensesame" #=> false
* [Native gems](#native-gems)
* [Verifying the gems](#verifying-the-gems)
* [Installing the `ruby` platform gem](#installing-the-ruby-platform-gem)
* [Thanks](#thanks)
* [Contact](#contact)
* [License](#license)
* [Dependencies](#dependencies)
Expand Down Expand Up @@ -215,6 +216,11 @@ You will need a full compiler toolchain for compiling Ruby C extensions (see
Toolchain"](https://nokogiri.org/tutorials/installing_nokogiri.html#appendix-a-the-compiler-toolchain))
plus the toolchain required for compiling the vendored version of Argon2.

## Thanks

* Thanks to [Mike Dalessio](https://github.com/flavorjones) for his advice and
work on [MiniPortile](https://github.com/flavorjones/mini_portile)

## Contact

All issues and suggestions should go to [GitHub
Expand Down
2 changes: 0 additions & 2 deletions ext/argon2id/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

require_relative "recipe"

# Configure cross-compiler
RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"]
$CFLAGS << " -Wextra"

have_header("stdint.h")
Expand Down
46 changes: 30 additions & 16 deletions ext/argon2id/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,46 @@ def configured?
end

def compile
cflags = [ENV["CFLAGS"], "-fPIC"].compact.join(" ")
env = {
"PREFIX" => File.expand_path(port_path),
"LIBRARY_REL" => "lib",
"CC" => gcc_cmd,
"CFLAGS" => cflags
}
# libargon2's Makefile shells out to uname to determine the host platform
# which doesn't work when cross-compiling. Instead, we manually compile
# the necessary source files and generate only the static library in the
# hopes that it is compatible across the most platforms.
cflags = [ENV["CFLAGS"], "-std=c89", "-O3", "-Wall", "-g", "-Iinclude", "-Isrc", "-fPIC", "-pthread"].compact.join(" ")
objs = %w[argon2 core blake2/blake2b thread encoding ref]
objs.each do |obj|
execute("compile-#{File.basename(obj)}", "#{gcc_cmd} #{cflags} -c -o src/#{obj}.o src/#{obj}.c")
end

ar_cmd = ENV["AR"] || "ar"
arflags = ENV["ARFLAGS"] || "rcs"

# Thanks to @flavorjones for this, see
# https://github.com/sparklemotion/nokogiri/blob/e05b9949b794ca94b37a90f2fb2555d99a37daa5/ext/nokogiri/extconf.rb#L1074-L1127
if enable_config("cross-build")
if host.include?("darwin")
env["AR"] = "#{host}-libtool"
env["ARFLAGS"] = "-o"
case host
when /darwin/
ar_cmd = "#{host}-libtool"
arflags = "-o"
when "i686-redhat-linux"
ar_cmd = "i686-redhat-linux-gnu-ar"
when "x86_64-redhat-linux"
ar_cmd = "x86_64-redhat-linux-gnu-ar"
else
env["AR"] = "#{host}-ar"
ar_cmd = "#{host}-ar"
end
end

execute("compile", make_cmd, env: env)
execute("archive", "#{ar_cmd} #{arflags} libargon2.a #{objs.map { |obj| "src/#{obj}.o" }.join(" ")}")
end

def install
return if installed?

execute("install", "#{make_cmd} install", env: {
"PREFIX" => File.expand_path(port_path),
"LIBRARY_REL" => "lib"
})
lib_dir = File.join(port_path, "lib")
include_dir = File.join(port_path, "include")
FileUtils.mkdir_p([lib_dir, include_dir])
FileUtils.cp(File.join(work_path, "libargon2.a"), lib_dir)
FileUtils.cp(File.join(work_path, "include", "argon2.h"), include_dir)
end
end

Expand Down

0 comments on commit 2bec2b8

Please sign in to comment.