@@ -26,7 +26,7 @@ desc "Compile binary extension"
26
26
task :compile do
27
27
require 'tempfile'
28
28
Dir . chdir ( Dir . tmpdir ) do
29
- sh "ruby #{ File . join ( __dir__ , 'ext' , 'extconf.rb' ) } "
29
+ sh "ruby ' #{ File . join ( __dir__ , 'ext' , 'extconf.rb' ) } ' "
30
30
end
31
31
end
32
32
@@ -56,15 +56,206 @@ task :render_git_revision do
56
56
`git fetch --tags >/dev/null 2>&1`
57
57
`git describe --long --always HEAD` . strip
58
58
end
59
- File . write ( File . join ( __dir__ , 'ext' , 'revisions.rb' ) , <<~REVISIONS )
60
- cmake_flags << "-DEXT_GIT_REVISION=#{ library_revision } "
61
- cmake_flags << "-DCOUCHBASE_CXX_CLIENT_GIT_REVISION=#{ core_revision } "
62
- cmake_flags << "-DCOUCHBASE_CXX_CLIENT_GIT_DESCRIBE=#{ core_describe } "
63
- REVISIONS
59
+ File . open ( File . join ( __dir__ , "ext" , "cache" , "extconf_include.rb" ) , "a+" ) do |io |
60
+ io . puts ( <<~REVISIONS )
61
+ cmake_flags << "-DEXT_GIT_REVISION=#{ library_revision } "
62
+ cmake_flags << "-DCOUCHBASE_CXX_CLIENT_GIT_REVISION=#{ core_revision } "
63
+ cmake_flags << "-DCOUCHBASE_CXX_CLIENT_GIT_DESCRIBE=#{ core_describe } "
64
+ REVISIONS
65
+ end
66
+ end
67
+
68
+ def which ( name , extra_locations = [ ] )
69
+ ENV . fetch ( "PATH" , "" )
70
+ . split ( File ::PATH_SEPARATOR )
71
+ . prepend ( *extra_locations )
72
+ . select { |path | File . directory? ( path ) }
73
+ . map { |path | [ path , name ] . join ( File ::SEPARATOR ) + RbConfig ::CONFIG [ "EXEEXT" ] }
74
+ . find { |file | File . executable? ( file ) }
75
+ end
76
+
77
+ desc "Download and cache dependencies of C++ core"
78
+ task :cache_cxx_dependencies do
79
+ require "tempfile"
80
+ require "rubygems/package"
81
+
82
+ output_dir = Dir . mktmpdir ( "cxx_output_" )
83
+ output_tarball = File . join ( output_dir , "cache.tar" )
84
+ cpm_cache_dir = Dir . mktmpdir ( "cxx_cache_" )
85
+ cxx_core_build_dir = Dir . mktmpdir ( "cxx_build_" )
86
+ cxx_core_source_dir = File . join ( __dir__ , "ext" , "couchbase" )
87
+ cc = ENV . fetch ( "CB_CC" , nil )
88
+ cxx = ENV . fetch ( "CB_CXX" , nil )
89
+ ar = ENV . fetch ( "CB_AR" , nil )
90
+
91
+ cmake_extra_locations = [ ]
92
+ if RUBY_PLATFORM . match? ( /mswin|mingw/ )
93
+ cmake_extra_locations = [
94
+ 'C:\Program Files\CMake\bin' ,
95
+ 'C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ,
96
+ 'C:\Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ,
97
+ ]
98
+ local_app_data = ENV . fetch ( "LOCALAPPDATA" , "#{ Dir . home } \\ AppData\\ Local" )
99
+ cmake_extra_locations . unshift ( "#{ local_app_data } \\ CMake\\ bin" ) if File . directory? ( local_app_data )
100
+ cc = RbConfig ::CONFIG [ "CC" ]
101
+ cxx = RbConfig ::CONFIG [ "CXX" ]
102
+ end
103
+ cmake = which ( "cmake" , cmake_extra_locations ) || which ( "cmake3" , cmake_extra_locations )
104
+ cmake_flags = [
105
+ "-S#{ cxx_core_source_dir } " ,
106
+ "-B#{ cxx_core_build_dir } " ,
107
+ "-DCOUCHBASE_CXX_CLIENT_BUILD_TESTS=OFF" ,
108
+ "-DCOUCHBASE_CXX_CLIENT_BUILD_TOOLS=OFF" ,
109
+ "-DCOUCHBASE_CXX_CLIENT_BUILD_DOCS=OFF" ,
110
+ "-DCOUCHBASE_CXX_CLIENT_STATIC_BORINGSSL=ON" ,
111
+ "-DCPM_DOWNLOAD_ALL=ON" ,
112
+ "-DCPM_USE_NAMED_CACHE_DIRECTORIES=ON" ,
113
+ "-DCPM_USE_LOCAL_PACKAGES=OFF" ,
114
+ "-DCPM_SOURCE_CACHE=#{ cpm_cache_dir } " ,
115
+ ]
116
+ cmake_flags << "-DCMAKE_C_COMPILER=#{ cc } " if cc
117
+ cmake_flags << "-DCMAKE_CXX_COMPILER=#{ cxx } " if cxx
118
+ cmake_flags << "-DCMAKE_AR=#{ ar } " if ar
119
+
120
+ puts ( "-----> run cmake to dowload all depenencies (#{ cmake } )" )
121
+ sh ( cmake , *cmake_flags , verbose : true )
122
+
123
+ puts ( "-----> create archive with whitelisted sources: #{ output_tarball } " )
124
+ File . open ( output_tarball , "w+b" ) do |file |
125
+ Gem ::Package ::TarWriter . new ( file ) do |writer |
126
+ chdir ( cxx_core_build_dir , verbose : true ) do
127
+ [ "mozilla-ca-bundle.sha256" , "mozilla-ca-bundle.crt" ] . each do |path |
128
+ writer . add_file ( path , 0o660 ) { |io | io . write ( File . binread ( path ) ) }
129
+ end
130
+ end
131
+ chdir ( cpm_cache_dir , verbose : true ) do
132
+ third_party_sources = Dir [
133
+ "cpm/*.cmake" ,
134
+ "asio/*/LICENSE*" ,
135
+ "asio/*/asio/COPYING" ,
136
+ "asio/*/asio/asio/include/*.hpp" ,
137
+ "asio/*/asio/asio/include/asio/**/*.[hi]pp" ,
138
+ "boringssl/*/boringssl/**/*.{cc,h,c,asm,S}" ,
139
+ "boringssl/*/boringssl/**/CMakeLists.txt" ,
140
+ "boringssl/*/boringssl/LICENSE" ,
141
+ "fmt/*/fmt/CMakeLists.txt" ,
142
+ "fmt/*/fmt/ChangeLog.rst" ,
143
+ "fmt/*/fmt/LICENSE.rst" ,
144
+ "fmt/*/fmt/README.rst" ,
145
+ "fmt/*/fmt/include/**/*" ,
146
+ "fmt/*/fmt/src/**/*" ,
147
+ "fmt/*/fmt/support/cmake/**/*" ,
148
+ "gsl/*/gsl/CMakeLists.txt" ,
149
+ "gsl/*/gsl/GSL.natvis" ,
150
+ "gsl/*/gsl/LICENSE*" ,
151
+ "gsl/*/gsl/ThirdPartyNotices.txt" ,
152
+ "gsl/*/gsl/cmake/*" ,
153
+ "gsl/*/gsl/include/**/*" ,
154
+ "hdr_histogram/*/hdr_histogram/*.pc.in" ,
155
+ "hdr_histogram/*/hdr_histogram/CMakeLists.txt" ,
156
+ "hdr_histogram/*/hdr_histogram/COPYING.txt" ,
157
+ "hdr_histogram/*/hdr_histogram/LICENSE.txt" ,
158
+ "hdr_histogram/*/hdr_histogram/cmake/*" ,
159
+ "hdr_histogram/*/hdr_histogram/config.cmake.in" ,
160
+ "hdr_histogram/*/hdr_histogram/include/**/*" ,
161
+ "hdr_histogram/*/hdr_histogram/src/**/*" ,
162
+ "json/*/json/CMakeLists.txt" ,
163
+ "json/*/json/LICENSE*" ,
164
+ "json/*/json/external/PEGTL/.cmake/*" ,
165
+ "json/*/json/external/PEGTL/CMakeLists.txt" ,
166
+ "json/*/json/external/PEGTL/LICENSE*" ,
167
+ "json/*/json/external/PEGTL/include/**/*" ,
168
+ "json/*/json/include/**/*" ,
169
+ "llhttp/*/llhttp/*.pc.in" ,
170
+ "llhttp/*/llhttp/CMakeLists.txt" ,
171
+ "llhttp/*/llhttp/LICENSE*" ,
172
+ "llhttp/*/llhttp/include/*.h" ,
173
+ "llhttp/*/llhttp/src/*.c" ,
174
+ "snappy/*/snappy/CMakeLists.txt" ,
175
+ "snappy/*/snappy/COPYING" ,
176
+ "snappy/*/snappy/cmake/*" ,
177
+ "snappy/*/snappy/snappy-c.{h,cc}" ,
178
+ "snappy/*/snappy/snappy-internal.h" ,
179
+ "snappy/*/snappy/snappy-sinksource.{h,cc}" ,
180
+ "snappy/*/snappy/snappy-stubs-internal.{h,cc}" ,
181
+ "snappy/*/snappy/snappy-stubs-public.h.in" ,
182
+ "snappy/*/snappy/snappy.{h,cc}" ,
183
+ "spdlog/*/spdlog/CMakeLists.txt" ,
184
+ "spdlog/*/spdlog/LICENSE" ,
185
+ "spdlog/*/spdlog/cmake/*" ,
186
+ "spdlog/*/spdlog/include/**/*" ,
187
+ "spdlog/*/spdlog/src/**/*" ,
188
+ ] . grep_v ( /crypto_test_data.cc/ )
189
+
190
+ # we don't want to fail if git is not available
191
+ cpm_cmake_path = third_party_sources . grep ( /cpm.*\. cmake$/ ) . first
192
+ File . write ( cpm_cmake_path , File . read ( cpm_cmake_path ) . gsub ( "Git REQUIRED" , "Git" ) )
193
+
194
+ third_party_sources
195
+ . select { |path | File . file? ( path ) }
196
+ . each { |path | writer . add_file ( path , 0o660 ) { |io | io . write ( File . binread ( path ) ) } }
197
+ end
198
+ end
199
+ end
200
+
201
+ rm_rf ( cxx_core_build_dir , verbose : true )
202
+ rm_rf ( cpm_cache_dir , verbose : true )
203
+
204
+ untar = [ "tar" , "-x" ]
205
+ untar << "--force-local" unless RUBY_PLATFORM . match? ( /darwin/ )
206
+
207
+ puts ( "-----> verify that tarball works as a cache for CPM" )
208
+ cxx_core_build_dir = Dir . mktmpdir ( "cxx_build_" )
209
+ cpm_cache_dir = Dir . mktmpdir ( "cxx_cache_" )
210
+ chdir ( cpm_cache_dir , verbose : true ) do
211
+ sh ( *untar , "-f" , output_tarball , verbose : true )
212
+ end
213
+
214
+ cmake_flags = [
215
+ "-S#{ cxx_core_source_dir } " ,
216
+ "-B#{ cxx_core_build_dir } " ,
217
+ "-DCOUCHBASE_CXX_CLIENT_BUILD_TESTS=OFF" ,
218
+ "-DCOUCHBASE_CXX_CLIENT_BUILD_TOOLS=OFF" ,
219
+ "-DCOUCHBASE_CXX_CLIENT_BUILD_DOCS=OFF" ,
220
+ "-DCOUCHBASE_CXX_CLIENT_STATIC_BORINGSSL=ON" ,
221
+ "-DCPM_DOWNLOAD_ALL=OFF" ,
222
+ "-DCPM_USE_NAMED_CACHE_DIRECTORIES=ON" ,
223
+ "-DCPM_USE_LOCAL_PACKAGES=OFF" ,
224
+ "-DCPM_SOURCE_CACHE=#{ cpm_cache_dir } " ,
225
+ "-DCOUCHBASE_CXX_CLIENT_EMBED_MOZILLA_CA_BUNDLE_ROOT=#{ cpm_cache_dir } " ,
226
+ ]
227
+ cmake_flags << "-DCMAKE_C_COMPILER=#{ cc } " if cc
228
+ cmake_flags << "-DCMAKE_CXX_COMPILER=#{ cxx } " if cxx
229
+ cmake_flags << "-DCMAKE_AR=#{ ar } " if ar
230
+
231
+ sh ( cmake , *cmake_flags , verbose : true )
232
+
233
+ rm_rf ( cxx_core_build_dir , verbose : true )
234
+ rm_rf ( cpm_cache_dir , verbose : true )
235
+
236
+ cache_dir = File . join ( __dir__ , "ext" , "cache" )
237
+ rm_rf ( cache_dir , verbose : true )
238
+ abort ( "unable to remove #{ cache_dir } " ) if File . directory? ( cache_dir )
239
+ mkdir_p ( cache_dir , verbose : true )
240
+ chdir ( cache_dir , verbose : true ) do
241
+ sh ( *untar , "-f" , output_tarball , verbose : true )
242
+ end
243
+ rm_rf ( output_dir , verbose : true )
244
+
245
+ extconf_include = File . join ( cache_dir , "extconf_include.rb" )
246
+ File . open ( extconf_include , "w+" ) do |io |
247
+ io . puts ( <<~CACHE_FLAGS )
248
+ cmake_flags << "-DCPM_DOWNLOAD_ALL=OFF"
249
+ cmake_flags << "-DCPM_USE_NAMED_CACHE_DIRECTORIES=ON"
250
+ cmake_flags << "-DCPM_USE_LOCAL_PACKAGES=OFF"
251
+ cmake_flags << "-DCPM_SOURCE_CACHE=\# {File.expand_path('cache', __dir__)}"
252
+ cmake_flags << "-DCOUCHBASE_CXX_CLIENT_EMBED_MOZILLA_CA_BUNDLE_ROOT=\# {File.expand_path('cache', __dir__)}"
253
+ CACHE_FLAGS
254
+ end
64
255
end
65
256
66
257
desc "Build the package"
67
- task :build => : render_git_revision
258
+ task :build => [ :cache_cxx_dependencies , : render_git_revision]
68
259
69
260
RuboCop ::RakeTask . new
70
261
0 commit comments