Skip to content

Commit 26dba3c

Browse files
Nix support (#363)
* nix support for rakefile * nix support for rakefile * frozen string literal * comment about nix * add check for rd-kafka ext if defaulting to nix, move comment --------- Co-authored-by: Maciej Mensfeld <[email protected]>
1 parent 9a5ae2f commit 26dba3c

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

ext/Rakefile

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
11
# frozen_string_literal: true
22

33
require File.expand_path('../../lib/rdkafka/version', __FILE__)
4-
require "mini_portile2"
54
require "fileutils"
65
require "open-uri"
76

87
task :default => :clean do
9-
# Download and compile librdkafka
10-
recipe = MiniPortile.new("librdkafka", Rdkafka::LIBRDKAFKA_VERSION)
8+
# For nix users, nix can't locate the file paths because the packages it's requiring aren't managed by the system but are
9+
# managed by nix itself, so using the normal file paths doesn't work for nix users.
10+
#
11+
# Mini_portile causes an issue because it's dependencies are downloaded on the fly and therefore don't exist/aren't
12+
# accessible in the nix environment
13+
if ENV.fetch('RDKAFKA_EXT_PATH', '').empty?
14+
# Download and compile librdkafka if RDKAFKA_EXT_PATH is not set
15+
require "mini_portile2"
16+
recipe = MiniPortile.new("librdkafka", Rdkafka::LIBRDKAFKA_VERSION)
1117

12-
# Use default homebrew openssl if we're on mac and the directory exists
13-
# and each of flags is not empty
14-
if recipe.host&.include?("darwin") && system("which brew &> /dev/null") && Dir.exist?("#{homebrew_prefix = %x(brew --prefix openssl).strip}")
15-
ENV["CPPFLAGS"] = "-I#{homebrew_prefix}/include" unless ENV["CPPFLAGS"]
16-
ENV["LDFLAGS"] = "-L#{homebrew_prefix}/lib" unless ENV["LDFLAGS"]
17-
end
18+
# Use default homebrew openssl if we're on mac and the directory exists
19+
# and each of flags is not empty
20+
if recipe.host&.include?("darwin") && system("which brew &> /dev/null") && Dir.exist?("#{homebrew_prefix = %x(brew --prefix openssl).strip}")
21+
ENV["CPPFLAGS"] = "-I#{homebrew_prefix}/include" unless ENV["CPPFLAGS"]
22+
ENV["LDFLAGS"] = "-L#{homebrew_prefix}/lib" unless ENV["LDFLAGS"]
23+
end
1824

19-
recipe.files << {
20-
:url => "https://codeload.github.com/confluentinc/librdkafka/tar.gz/v#{Rdkafka::LIBRDKAFKA_VERSION}",
21-
:sha256 => Rdkafka::LIBRDKAFKA_SOURCE_SHA256
22-
}
23-
recipe.configure_options = ["--host=#{recipe.host}"]
24-
recipe.cook
25-
# Move dynamic library we're interested in
26-
if recipe.host.include?('darwin')
27-
from_extension = '1.dylib'
28-
to_extension = 'dylib'
25+
recipe.files << {
26+
:url => "https://codeload.github.com/edenhill/librdkafka/tar.gz/v#{Rdkafka::LIBRDKAFKA_VERSION}",
27+
:sha256 => Rdkafka::LIBRDKAFKA_SOURCE_SHA256
28+
}
29+
recipe.configure_options = ["--host=#{recipe.host}"]
30+
recipe.cook
31+
# Move dynamic library we're interested in
32+
if recipe.host.include?('darwin')
33+
from_extension = '1.dylib'
34+
to_extension = 'dylib'
35+
else
36+
from_extension = 'so.1'
37+
to_extension = 'so'
38+
end
39+
lib_path = File.join(File.dirname(__FILE__), "ports/#{recipe.host}/librdkafka/#{Rdkafka::LIBRDKAFKA_VERSION}/lib/librdkafka.#{from_extension}")
40+
FileUtils.mv(lib_path, File.join(File.dirname(__FILE__), "librdkafka.#{to_extension}"))
41+
# Cleanup files created by miniportile we don't need in the gem
42+
FileUtils.rm_rf File.join(File.dirname(__FILE__), "tmp")
43+
FileUtils.rm_rf File.join(File.dirname(__FILE__), "ports")
2944
else
30-
from_extension = 'so.1'
31-
to_extension = 'so'
45+
# Otherwise, copy existing libraries to ./ext
46+
if ENV['RDKAFKA_EXT_PATH'].nil? || ENV['RDKAFKA_EXT_PATH'].empty?
47+
raise "RDKAFKA_EXT_PATH must be set in your nix config when running under nix"
48+
end
49+
files = [
50+
File.join(ENV['RDKAFKA_EXT_PATH'], 'lib', 'librdkafka.dylib'),
51+
File.join(ENV['RDKAFKA_EXT_PATH'], 'lib', 'librdkafka.so')
52+
]
53+
files.each { |ext| FileUtils.cp(ext, File.dirname(__FILE__)) if File.exist?(ext) }
3254
end
33-
lib_path = File.join(File.dirname(__FILE__), "ports/#{recipe.host}/librdkafka/#{Rdkafka::LIBRDKAFKA_VERSION}/lib/librdkafka.#{from_extension}")
34-
FileUtils.mv(lib_path, File.join(File.dirname(__FILE__), "librdkafka.#{to_extension}"))
35-
# Cleanup files created by miniportile we don't need in the gem
36-
FileUtils.rm_rf File.join(File.dirname(__FILE__), "tmp")
37-
FileUtils.rm_rf File.join(File.dirname(__FILE__), "ports")
3855
end
3956

4057
task :clean do

0 commit comments

Comments
 (0)