1+ # frozen_string_literal: true
2+
13# Copyright 2020-2021 Couchbase, Inc.
24#
35# Licensed under the Apache License, Version 2.0 (the "License");
1214# See the License for the specific language governing permissions and
1315# limitations under the License.
1416
17+ require 'English'
1518require "mkmf"
1619require "tempfile"
1720
2629 require "ruby_installer/runtime"
2730 # ridk install 1
2831 # ridk install 3
29- # ridk exec pacman --sync --noconfirm mingw-w64-ucrt-x86_64-ninja mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-toolchain mingw-w64-ucrt-x86_64-go mingw-w64-ucrt-x86_64-nasm mingw-w64-ucrt-x86_64-ccache
32+ # ridk exec pacman --sync --noconfirm \
33+ # mingw-w64-ucrt-x86_64-ninja \
34+ # mingw-w64-ucrt-x86_64-cmake \
35+ # mingw-w64-ucrt-x86_64-toolchain \
36+ # mingw-w64-ucrt-x86_64-go \
37+ # mingw-w64-ucrt-x86_64-nasm \
38+ # mingw-w64-ucrt-x86_64-ccache
3039 ENV [ "CB_STATIC_BORINGSSL" ] = "true"
3140 ENV [ "CB_STATIC_STDLIB" ] = "true"
3241 ENV [ "GOROOT" ] = File . join ( RubyInstaller ::Runtime . msys2_installation . msys_path , "ucrt64/lib/go" )
33- else
3442end
3543
44+ pp ENV . select { |k , _ | k =~ /^CB_|^DEBUG$/ } . merge ( "SDK_VERSION" => SDK_VERSION )
45+
46+ system ( "cat /usr/local/include/openssl/opensslv.h" ) if File . exist? ( "/usr/local/include/openssl/opensslv.h" )
47+ exit ( 1 )
48+
3649def which ( name , extra_locations = [ ] )
3750 ENV . fetch ( "PATH" , "" )
3851 . split ( File ::PATH_SEPARATOR )
@@ -53,28 +66,24 @@ def check_version(name, extra_locations = [])
5366end
5467
5568cmake_extra_locations = [ ]
56- if RUBY_PLATFORM =~ /mswin|mingw/
69+ if RUBY_PLATFORM . match? ( /mswin|mingw/ )
5770 cmake_extra_locations = [
5871 'C:\Program Files\CMake\bin' ,
5972 'C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ,
6073 'C:\Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ,
6174 ]
62- local_app_data = ENV . fetch ( "LOCALAPPDATA" , "#{ ENV . fetch ( "HOME" ) } \\ AppData\\ Local" )
63- if File . directory? ( local_app_data )
64- cmake_extra_locations . unshift ( "#{ local_app_data } \\ CMake\\ bin" )
65- end
75+ local_app_data = ENV . fetch ( "LOCALAPPDATA" , "#{ Dir . home } \\ AppData\\ Local" )
76+ cmake_extra_locations . unshift ( "#{ local_app_data } \\ CMake\\ bin" ) if File . directory? ( local_app_data )
6677end
6778cmake , version = check_version ( "cmake" , cmake_extra_locations )
68- if version [ /cmake version (\d +\. \d +)/ , 1 ] . to_f < 3.15
69- cmake , version = check_version ( "cmake3" )
70- end
79+ cmake , version = check_version ( "cmake3" ) if version [ /cmake version (\d +\. \d +)/ , 1 ] . to_f < 3.15
7180abort "ERROR: CMake is required to build couchbase extension." unless cmake
7281puts "-- #{ version } , #{ cmake } "
7382
7483def sys ( *cmd )
7584 puts "-- #{ Dir . pwd } "
7685 puts "-- #{ cmd . join ( ' ' ) } "
77- system ( *cmd ) || abort ( "failed to execute command: #{ $? . inspect } \n #{ cmd . join ( ' ' ) } " )
86+ system ( *cmd ) || abort ( "failed to execute command: #{ $CHILD_STATUS . inspect } \n #{ cmd . join ( ' ' ) } " )
7887end
7988
8089build_type = ENV [ "DEBUG" ] ? "Debug" : "Release"
@@ -98,7 +107,6 @@ def sys(*cmd)
98107 cmake_flags << "-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
99108end
100109
101-
102110extconf_include = File . expand_path ( "cache/extconf_include.rb" , __dir__ )
103111if File . exist? ( extconf_include )
104112 puts "-- include extra cmake options from #{ extconf_include } "
@@ -132,18 +140,18 @@ def sys(*cmd)
132140cmake_flags << "-DENABLE_SANITIZER_THREAD=ON" if ENV [ "CB_TSAN" ]
133141cmake_flags << "-DENABLE_SANITIZER_UNDEFINED_BEHAVIOUR=ON" if ENV [ "CB_UBSAN" ]
134142
135- cc = ENV [ "CB_CC" ]
136- cxx = ENV [ "CB_CXX" ]
137- ar = ENV [ "CB_AR" ]
143+ cc = ENV . fetch ( "CB_CC" , nil )
144+ cxx = ENV . fetch ( "CB_CXX" , nil )
145+ ar = ENV . fetch ( "CB_AR" , nil )
138146
139- if RbConfig ::CONFIG [ "target_os" ] =~ / mingw/
147+ if RbConfig ::CONFIG [ "target_os" ] . include? ( ' mingw' )
140148 require "ruby_installer/runtime"
141149 RubyInstaller ::Runtime . enable_dll_search_paths
142150 RubyInstaller ::Runtime . enable_msys_apps
143151 cc = RbConfig ::CONFIG [ "CC" ]
144152 cxx = RbConfig ::CONFIG [ "CXX" ]
145153 cmake_flags << "-G Ninja"
146- cmake_flags << "-DRUBY_LIBRUBY=#{ File . basename ( RbConfig ::CONFIG [ " LIBRUBY_SO" ] , ".#{ RbConfig ::CONFIG [ " SOEXT" ] } " ) } "
154+ cmake_flags << "-DRUBY_LIBRUBY=#{ File . basename ( RbConfig ::CONFIG [ ' LIBRUBY_SO' ] , ".#{ RbConfig ::CONFIG [ ' SOEXT' ] } " ) } "
147155end
148156
149157cmake_flags << "-DCMAKE_C_COMPILER=#{ cc } " if cc
@@ -163,26 +171,25 @@ def sys(*cmd)
163171if ENV [ "CB_CREATE_BUILD_DIR_LINK" ]
164172 links = [
165173 File . expand_path ( File . join ( project_path , ".." , "build" ) ) ,
166- File . expand_path ( File . join ( project_path , "build" ) )
174+ File . expand_path ( File . join ( project_path , "build" ) ) ,
167175 ]
168176 links . each do |link |
169177 next if link == build_dir
178+
170179 FileUtils . ln_sf ( build_dir , link , verbose : true )
171180 end
172181end
173- Dir . chdir ( build_dir ) do
182+ Dir . chdir ( build_dir ) do # rubocop:disable ThreadSafety/DirChdir
174183 puts "-- build #{ build_type } extension #{ SDK_VERSION } for ruby #{ RUBY_VERSION } -#{ RUBY_PATCHLEVEL } -#{ RUBY_PLATFORM } "
175184 sys ( cmake , *cmake_flags , "-B#{ build_dir } " , "-S#{ project_path } " )
176185 number_of_jobs = ( ENV [ "CB_NUMBER_OF_JOBS" ] || 4 ) . to_s
177- sys ( cmake , "--build" , build_dir , "--parallel" , number_of_jobs , "--verbose" )
186+ sys ( cmake , "--build" , build_dir , "--parallel" , number_of_jobs , "--verbose" )
178187end
179188extension_name = "libcouchbase.#{ RbConfig ::CONFIG [ 'SOEXT' ] || RbConfig ::CONFIG [ 'DLEXT' ] } "
180189extension_path = File . expand_path ( File . join ( build_dir , extension_name ) )
181190abort "ERROR: failed to build extension in #{ extension_path } " unless File . file? ( extension_path )
182- extension_name . gsub! ( /\. dylib/ , '.bundle' )
183- if RbConfig ::CONFIG [ "target_os" ] =~ /mingw/
184- extension_name . gsub! ( /\. dll$/ , '.so' )
185- end
191+ extension_name . gsub! ( '.dylib' , '.bundle' )
192+ extension_name . gsub! ( /\. dll$/ , '.so' ) if RbConfig ::CONFIG [ "target_os" ] . include? ( 'mingw' )
186193install_path = File . expand_path ( File . join ( __dir__ , ".." , "lib" , "couchbase" , extension_name ) )
187194puts "-- copy extension to #{ install_path } "
188195FileUtils . cp ( extension_path , install_path , verbose : true )
0 commit comments