-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Rakefile
131 lines (109 loc) · 5 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true
require File.expand_path('../../lib/rdkafka/version', __FILE__)
require "digest"
require "fileutils"
require "open-uri"
task :default => :clean do
# For nix users, nix can't locate the file paths because the packages it's requiring aren't managed by the system but are
# managed by nix itself, so using the normal file paths doesn't work for nix users.
#
# Mini_portile causes an issue because it's dependencies are downloaded on the fly and therefore don't exist/aren't
# accessible in the nix environment
if ENV.fetch('RDKAFKA_EXT_PATH', '').empty?
# Download and compile librdkafka if RDKAFKA_EXT_PATH is not set
require "mini_portile2"
recipe = MiniPortile.new("librdkafka", Rdkafka::LIBRDKAFKA_VERSION)
# Use default homebrew openssl if we're on mac and the directory exists
# and each of flags is not empty
if recipe.host&.include?("darwin") && system("which brew &> /dev/null") && Dir.exist?("#{homebrew_prefix = %x(brew --prefix openssl).strip}")
ENV["CPPFLAGS"] = "-I#{homebrew_prefix}/include" unless ENV["CPPFLAGS"]
ENV["LDFLAGS"] = "-L#{homebrew_prefix}/lib" unless ENV["LDFLAGS"]
end
releases = File.expand_path(File.join(File.dirname(__FILE__), '../dist'))
recipe.files << {
:url => "file://#{releases}/librdkafka_#{Rdkafka::LIBRDKAFKA_VERSION}.tar.gz",
:sha256 => Rdkafka::LIBRDKAFKA_SOURCE_SHA256
}
recipe.configure_options = ["--host=#{recipe.host}"]
recipe.patch_files = Dir[File.join(releases, 'patches', "*.patch")].sort
# Disable using libc regex engine in favor of the embedded one
# The default regex engine of librdkafka does not always work exactly as most of the users
# would expect, hence this flag allows for changing it to the other one
if ENV.key?('RDKAFKA_DISABLE_REGEX_EXT')
recipe.configure_options << '--disable-regex-ext'
end
recipe.cook
# Move dynamic library we're interested in
if recipe.host.include?('darwin')
from_extension = '1.dylib'
to_extension = 'dylib'
else
from_extension = 'so.1'
to_extension = 'so'
end
lib_path = File.join(File.dirname(__FILE__), "ports/#{recipe.host}/librdkafka/#{Rdkafka::LIBRDKAFKA_VERSION}/lib/librdkafka.#{from_extension}")
FileUtils.mv(lib_path, File.join(File.dirname(__FILE__), "librdkafka.#{to_extension}"))
# Cleanup files created by miniportile we don't need in the gem
FileUtils.rm_rf File.join(File.dirname(__FILE__), "tmp")
FileUtils.rm_rf File.join(File.dirname(__FILE__), "ports")
else
# Otherwise, copy existing libraries to ./ext
if ENV['RDKAFKA_EXT_PATH'].nil? || ENV['RDKAFKA_EXT_PATH'].empty?
raise "RDKAFKA_EXT_PATH must be set in your nix config when running under nix"
end
files = [
File.join(ENV['RDKAFKA_EXT_PATH'], 'lib', 'librdkafka.dylib'),
File.join(ENV['RDKAFKA_EXT_PATH'], 'lib', 'librdkafka.so')
]
files.each { |ext| FileUtils.cp(ext, File.dirname(__FILE__)) if File.exist?(ext) }
end
end
task :clean do
FileUtils.rm_f File.join(File.dirname(__FILE__), "librdkafka.dylib")
FileUtils.rm_f File.join(File.dirname(__FILE__), "librdkafka.so")
FileUtils.rm_rf File.join(File.dirname(__FILE__), "ports")
FileUtils.rm_rf File.join(File.dirname(__FILE__), "tmp")
end
namespace :dist do
task :dir do
ENV["RDKAFKA_DIST_PATH"] ||= File.expand_path(File.join(File.dirname(__FILE__), '..', 'dist'))
end
task :file => "dist:dir" do
ENV["RDKAFKA_DIST_FILE"] ||= File.join(ENV["RDKAFKA_DIST_PATH"], "librdkafka_#{Rdkafka::LIBRDKAFKA_VERSION}.tar.gz")
end
task :clean => "dist:file" do
Dir.glob(File.join("#{ENV['RDKAFKA_DIST_PATH']}", "*")).each do |filename|
next if filename.include? ENV["RDKAFKA_DIST_FILE"]
FileUtils.rm_rf filename
end
end
task :download => "dist:file" do
version = Rdkafka::LIBRDKAFKA_VERSION
librdkafka_download = "https://codeload.github.com/confluentinc/librdkafka/tar.gz/v#{version}"
URI.open(librdkafka_download) do |file|
filename = ENV["RDKAFKA_DIST_FILE"]
data = file.read
if Digest::SHA256.hexdigest(data) != Rdkafka::LIBRDKAFKA_SOURCE_SHA256
raise "SHA256 does not match downloaded file"
end
File.write(filename, data)
end
end
task :update => %w[dist:download dist:clean]
end
namespace :build do
desc "Build librdkafka at the given git sha or tag"
task :git, [:ref] do |task, args|
ref = args[:ref]
version = "git-#{ref}"
recipe = MiniPortile.new("librdkafka", version)
recipe.files << "https://github.com/confluentinc/librdkafka/archive/#{ref}.tar.gz"
recipe.configure_options = ["--host=#{recipe.host}","--enable-static", "--enable-zstd"]
recipe.patch_files = Dir[File.join(releases, 'patches', "*.patch")].sort
recipe.cook
ext = recipe.host.include?("darwin") ? "dylib" : "so"
lib = File.expand_path("ports/#{recipe.host}/librdkafka/#{version}/lib/librdkafka.#{ext}", __dir__)
# Copy will copy the content, following any symlinks
FileUtils.cp(lib, __dir__)
end
end