-
Notifications
You must be signed in to change notification settings - Fork 51
/
Rakefile
265 lines (238 loc) · 10 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# frozen_string_literal: true
# Copyright 2020-2021 Couchbase, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "bundler/gem_tasks"
require "rake/testtask"
require "rubocop/rake_task"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end
desc "Compile binary extension"
task :compile do
require 'tempfile'
Dir.chdir(Dir.tmpdir) do # rubocop:disable ThreadSafety/DirChdir
sh "ruby '#{File.join(__dir__, 'ext', 'extconf.rb')}'"
end
end
desc "Generate YARD documentation"
task :doc do
require "couchbase/version"
input_dir = File.join(__dir__, "lib")
output_dir = File.join(__dir__, "doc", "couchbase-ruby-client-#{Couchbase::VERSION[:sdk]}")
rm_rf output_dir
sh "bundle exec yard doc --no-progress --hide-api private --output-dir #{output_dir} #{input_dir} --main README.md"
puts "#{File.realpath(output_dir)}/index.html"
end
desc "An alias for documentation generation task"
task :docs => :doc
desc "Display stats on undocumented things"
task :undocumented => :doc do
sh "yard stats --list-undoc --compact"
end
desc "Encode git revision into 'ext/extconf.rb' template (dependency of 'build' task)"
task :render_git_revision do
library_revision = Dir.chdir(__dir__) { `git rev-parse HEAD`.strip } # rubocop:disable ThreadSafety/DirChdir
core_revision = Dir.chdir(File.join(__dir__, 'ext', 'couchbase')) { `git rev-parse HEAD`.strip } # rubocop:disable ThreadSafety/DirChdir
core_describe = Dir.chdir(File.join(__dir__, 'ext', 'couchbase')) do # rubocop:disable ThreadSafety/DirChdir
`git fetch --tags >/dev/null 2>&1`
`git describe --long --always HEAD`.strip
end
File.open(File.join(__dir__, "ext", "cache", "extconf_include.rb"), "a+") do |io|
io.puts(<<~REVISIONS)
cmake_flags << "-DEXT_GIT_REVISION=#{library_revision}"
cmake_flags << "-DCOUCHBASE_CXX_CLIENT_GIT_REVISION=#{core_revision}"
cmake_flags << "-DCOUCHBASE_CXX_CLIENT_GIT_DESCRIBE=#{core_describe}"
REVISIONS
end
end
def which(name, extra_locations = [])
ENV.fetch("PATH", "")
.split(File::PATH_SEPARATOR)
.prepend(*extra_locations)
.select { |path| File.directory?(path) }
.map { |path| [path, name].join(File::SEPARATOR) + RbConfig::CONFIG["EXEEXT"] }
.find { |file| File.executable?(file) }
end
desc "Download and cache dependencies of C++ core"
task :cache_cxx_dependencies do
require "tempfile"
require "rubygems/package"
output_dir = Dir.mktmpdir("cxx_output_")
output_tarball = File.join(output_dir, "cache.tar")
cpm_cache_dir = Dir.mktmpdir("cxx_cache_")
cxx_core_build_dir = Dir.mktmpdir("cxx_build_")
cxx_core_source_dir = File.join(__dir__, "ext", "couchbase")
cc = ENV.fetch("CB_CC", nil)
cxx = ENV.fetch("CB_CXX", nil)
ar = ENV.fetch("CB_AR", nil)
cmake_extra_locations = []
if RUBY_PLATFORM.match?(/mswin|mingw/)
cmake_extra_locations = [
'C:\Program Files\CMake\bin',
'C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin',
'C:\Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin',
]
local_app_data = ENV.fetch("LOCALAPPDATA", "#{Dir.home}\\AppData\\Local")
cmake_extra_locations.unshift("#{local_app_data}\\CMake\\bin") if File.directory?(local_app_data)
cc = RbConfig::CONFIG["CC"]
cxx = RbConfig::CONFIG["CXX"]
end
cmake = which("cmake", cmake_extra_locations) || which("cmake3", cmake_extra_locations)
cmake_flags = [
"-S#{cxx_core_source_dir}",
"-B#{cxx_core_build_dir}",
"-DCOUCHBASE_CXX_CLIENT_BUILD_TESTS=OFF",
"-DCOUCHBASE_CXX_CLIENT_BUILD_TOOLS=OFF",
"-DCOUCHBASE_CXX_CLIENT_BUILD_DOCS=OFF",
"-DCOUCHBASE_CXX_CLIENT_STATIC_BORINGSSL=ON",
"-DCPM_DOWNLOAD_ALL=ON",
"-DCPM_USE_NAMED_CACHE_DIRECTORIES=ON",
"-DCPM_USE_LOCAL_PACKAGES=OFF",
"-DCPM_SOURCE_CACHE=#{cpm_cache_dir}",
]
cmake_flags << "-DCMAKE_C_COMPILER=#{cc}" if cc
cmake_flags << "-DCMAKE_CXX_COMPILER=#{cxx}" if cxx
cmake_flags << "-DCMAKE_AR=#{ar}" if ar
puts("-----> run cmake to dowload all depenencies (#{cmake})")
sh(cmake, *cmake_flags, verbose: true)
puts("-----> create archive with whitelisted sources: #{output_tarball}")
File.open(output_tarball, "w+b") do |file|
Gem::Package::TarWriter.new(file) do |writer|
chdir(cxx_core_build_dir, verbose: true) do
["mozilla-ca-bundle.sha256", "mozilla-ca-bundle.crt"].each do |path|
writer.add_file(path, 0o660) { |io| io.write(File.binread(path)) }
end
end
chdir(cpm_cache_dir, verbose: true) do
third_party_sources = Dir[
"cpm/*.cmake",
"asio/*/LICENSE*",
"asio/*/asio/COPYING",
"asio/*/asio/asio/include/*.hpp",
"asio/*/asio/asio/include/asio/**/*.[hi]pp",
"asio/*/asio/asio/src/*.cpp",
"boringssl/*/boringssl/**/*.{cc,h,c,asm,S}",
"boringssl/*/boringssl/**/CMakeLists.txt",
"boringssl/*/boringssl/LICENSE",
"fmt/*/fmt/CMakeLists.txt",
"fmt/*/fmt/ChangeLog.md",
"fmt/*/fmt/LICENSE",
"fmt/*/fmt/README.md",
"fmt/*/fmt/include/**/*",
"fmt/*/fmt/src/**/*",
"fmt/*/fmt/support/cmake/**/*",
"gsl/*/gsl/CMakeLists.txt",
"gsl/*/gsl/GSL.natvis",
"gsl/*/gsl/LICENSE*",
"gsl/*/gsl/ThirdPartyNotices.txt",
"gsl/*/gsl/cmake/*",
"gsl/*/gsl/include/**/*",
"hdr_histogram/*/hdr_histogram/*.pc.in",
"hdr_histogram/*/hdr_histogram/CMakeLists.txt",
"hdr_histogram/*/hdr_histogram/COPYING.txt",
"hdr_histogram/*/hdr_histogram/LICENSE.txt",
"hdr_histogram/*/hdr_histogram/cmake/*",
"hdr_histogram/*/hdr_histogram/config.cmake.in",
"hdr_histogram/*/hdr_histogram/include/**/*",
"hdr_histogram/*/hdr_histogram/src/**/*",
"json/*/json/CMakeLists.txt",
"json/*/json/LICENSE*",
"json/*/json/external/PEGTL/.cmake/*",
"json/*/json/external/PEGTL/CMakeLists.txt",
"json/*/json/external/PEGTL/LICENSE*",
"json/*/json/external/PEGTL/include/**/*",
"json/*/json/include/**/*",
"llhttp/*/llhttp/*.pc.in",
"llhttp/*/llhttp/CMakeLists.txt",
"llhttp/*/llhttp/LICENSE*",
"llhttp/*/llhttp/include/*.h",
"llhttp/*/llhttp/src/*.c",
"snappy/*/snappy/CMakeLists.txt",
"snappy/*/snappy/COPYING",
"snappy/*/snappy/cmake/*",
"snappy/*/snappy/snappy-c.{h,cc}",
"snappy/*/snappy/snappy-internal.h",
"snappy/*/snappy/snappy-sinksource.{h,cc}",
"snappy/*/snappy/snappy-stubs-internal.{h,cc}",
"snappy/*/snappy/snappy-stubs-public.h.in",
"snappy/*/snappy/snappy.{h,cc}",
"spdlog/*/spdlog/CMakeLists.txt",
"spdlog/*/spdlog/LICENSE",
"spdlog/*/spdlog/cmake/*",
"spdlog/*/spdlog/include/**/*",
"spdlog/*/spdlog/src/**/*",
].grep_v(/crypto_test_data.cc|googletest/)
# we don't want to fail if git is not available
cpm_cmake_path = third_party_sources.grep(/cpm.*\.cmake$/).first
File.write(cpm_cmake_path, File.read(cpm_cmake_path).gsub("Git REQUIRED", "Git"))
third_party_sources
.select { |path| File.file?(path) }
.each { |path| writer.add_file(path, 0o660) { |io| io.write(File.binread(path)) } }
end
end
end
rm_rf(cxx_core_build_dir, verbose: true)
rm_rf(cpm_cache_dir, verbose: true)
untar = ["tar", "-x"]
untar << "--force-local" unless RUBY_PLATFORM.include?('darwin')
puts("-----> verify that tarball works as a cache for CPM")
cxx_core_build_dir = Dir.mktmpdir("cxx_build_")
cpm_cache_dir = Dir.mktmpdir("cxx_cache_")
chdir(cpm_cache_dir, verbose: true) do
sh(*untar, "-f", output_tarball, verbose: true)
end
cmake_flags = [
"-S#{cxx_core_source_dir}",
"-B#{cxx_core_build_dir}",
"-DCOUCHBASE_CXX_CLIENT_BUILD_TESTS=OFF",
"-DCOUCHBASE_CXX_CLIENT_BUILD_TOOLS=OFF",
"-DCOUCHBASE_CXX_CLIENT_BUILD_DOCS=OFF",
"-DCOUCHBASE_CXX_CLIENT_STATIC_BORINGSSL=ON",
"-DCPM_DOWNLOAD_ALL=OFF",
"-DCPM_USE_NAMED_CACHE_DIRECTORIES=ON",
"-DCPM_USE_LOCAL_PACKAGES=OFF",
"-DCPM_SOURCE_CACHE=#{cpm_cache_dir}",
"-DCOUCHBASE_CXX_CLIENT_EMBED_MOZILLA_CA_BUNDLE_ROOT=#{cpm_cache_dir}",
]
cmake_flags << "-DCMAKE_C_COMPILER=#{cc}" if cc
cmake_flags << "-DCMAKE_CXX_COMPILER=#{cxx}" if cxx
cmake_flags << "-DCMAKE_AR=#{ar}" if ar
sh(cmake, *cmake_flags, verbose: true)
rm_rf(cxx_core_build_dir, verbose: true)
rm_rf(cpm_cache_dir, verbose: true)
cache_dir = File.join(__dir__, "ext", "cache")
rm_rf(cache_dir, verbose: true)
abort("unable to remove #{cache_dir}") if File.directory?(cache_dir)
mkdir_p(cache_dir, verbose: true)
chdir(cache_dir, verbose: true) do
sh(*untar, "-f", output_tarball, verbose: true)
end
rm_rf(output_dir, verbose: true)
extconf_include = File.join(cache_dir, "extconf_include.rb")
File.open(extconf_include, "w+") do |io|
io.puts(<<~CACHE_FLAGS)
cmake_flags << "-DCPM_DOWNLOAD_ALL=OFF"
cmake_flags << "-DCPM_USE_NAMED_CACHE_DIRECTORIES=ON"
cmake_flags << "-DCPM_USE_LOCAL_PACKAGES=OFF"
cmake_flags << "-DCPM_SOURCE_CACHE=\#{File.expand_path('cache', __dir__)}"
cmake_flags << "-DCOUCHBASE_CXX_CLIENT_EMBED_MOZILLA_CA_BUNDLE_ROOT=\#{File.expand_path('cache', __dir__)}"
CACHE_FLAGS
end
end
desc "Build the package"
task :build => [:cache_cxx_dependencies, :render_git_revision]
RuboCop::RakeTask.new
load "task/grpc.rake"