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+
3646def which ( name , extra_locations = [ ] )
3747 ENV . fetch ( "PATH" , "" )
3848 . split ( File ::PATH_SEPARATOR )
@@ -53,28 +63,24 @@ def check_version(name, extra_locations = [])
5363end
5464
5565cmake_extra_locations = [ ]
56- if RUBY_PLATFORM =~ /mswin|mingw/
66+ if RUBY_PLATFORM . match? ( /mswin|mingw/ )
5767 cmake_extra_locations = [
5868 'C:\Program Files\CMake\bin' ,
5969 'C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ,
6070 'C:\Program Files\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ,
6171 ]
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
72+ local_app_data = ENV . fetch ( "LOCALAPPDATA" , "#{ Dir . home } \\ AppData\\ Local" )
73+ cmake_extra_locations . unshift ( "#{ local_app_data } \\ CMake\\ bin" ) if File . directory? ( local_app_data )
6674end
6775cmake , 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
76+ cmake , version = check_version ( "cmake3" ) if version [ /cmake version (\d +\. \d +)/ , 1 ] . to_f < 3.15
7177abort "ERROR: CMake is required to build couchbase extension." unless cmake
7278puts "-- #{ version } , #{ cmake } "
7379
7480def sys ( *cmd )
7581 puts "-- #{ Dir . pwd } "
7682 puts "-- #{ cmd . join ( ' ' ) } "
77- system ( *cmd ) || abort ( "failed to execute command: #{ $? . inspect } \n #{ cmd . join ( ' ' ) } " )
83+ system ( *cmd ) || abort ( "failed to execute command: #{ $CHILD_STATUS . inspect } \n #{ cmd . join ( ' ' ) } " )
7884end
7985
8086build_type = ENV [ "DEBUG" ] ? "Debug" : "Release"
@@ -98,7 +104,6 @@ def sys(*cmd)
98104 cmake_flags << "-DCMAKE_POLICY_VERSION_MINIMUM=3.5"
99105end
100106
101-
102107extconf_include = File . expand_path ( "cache/extconf_include.rb" , __dir__ )
103108if File . exist? ( extconf_include )
104109 puts "-- include extra cmake options from #{ extconf_include } "
@@ -132,18 +137,18 @@ def sys(*cmd)
132137cmake_flags << "-DENABLE_SANITIZER_THREAD=ON" if ENV [ "CB_TSAN" ]
133138cmake_flags << "-DENABLE_SANITIZER_UNDEFINED_BEHAVIOUR=ON" if ENV [ "CB_UBSAN" ]
134139
135- cc = ENV [ "CB_CC" ]
136- cxx = ENV [ "CB_CXX" ]
137- ar = ENV [ "CB_AR" ]
140+ cc = ENV . fetch ( "CB_CC" , nil )
141+ cxx = ENV . fetch ( "CB_CXX" , nil )
142+ ar = ENV . fetch ( "CB_AR" , nil )
138143
139- if RbConfig ::CONFIG [ "target_os" ] =~ / mingw/
144+ if RbConfig ::CONFIG [ "target_os" ] . include? ( ' mingw' )
140145 require "ruby_installer/runtime"
141146 RubyInstaller ::Runtime . enable_dll_search_paths
142147 RubyInstaller ::Runtime . enable_msys_apps
143148 cc = RbConfig ::CONFIG [ "CC" ]
144149 cxx = RbConfig ::CONFIG [ "CXX" ]
145150 cmake_flags << "-G Ninja"
146- cmake_flags << "-DRUBY_LIBRUBY=#{ File . basename ( RbConfig ::CONFIG [ " LIBRUBY_SO" ] , ".#{ RbConfig ::CONFIG [ " SOEXT" ] } " ) } "
151+ cmake_flags << "-DRUBY_LIBRUBY=#{ File . basename ( RbConfig ::CONFIG [ ' LIBRUBY_SO' ] , ".#{ RbConfig ::CONFIG [ ' SOEXT' ] } " ) } "
147152end
148153
149154cmake_flags << "-DCMAKE_C_COMPILER=#{ cc } " if cc
@@ -163,26 +168,25 @@ def sys(*cmd)
163168if ENV [ "CB_CREATE_BUILD_DIR_LINK" ]
164169 links = [
165170 File . expand_path ( File . join ( project_path , ".." , "build" ) ) ,
166- File . expand_path ( File . join ( project_path , "build" ) )
171+ File . expand_path ( File . join ( project_path , "build" ) ) ,
167172 ]
168173 links . each do |link |
169174 next if link == build_dir
175+
170176 FileUtils . ln_sf ( build_dir , link , verbose : true )
171177 end
172178end
173- Dir . chdir ( build_dir ) do
179+ Dir . chdir ( build_dir ) do # rubocop:disable ThreadSafety/DirChdir
174180 puts "-- build #{ build_type } extension #{ SDK_VERSION } for ruby #{ RUBY_VERSION } -#{ RUBY_PATCHLEVEL } -#{ RUBY_PLATFORM } "
175181 sys ( cmake , *cmake_flags , "-B#{ build_dir } " , "-S#{ project_path } " )
176182 number_of_jobs = ( ENV [ "CB_NUMBER_OF_JOBS" ] || 4 ) . to_s
177- sys ( cmake , "--build" , build_dir , "--parallel" , number_of_jobs , "--verbose" )
183+ sys ( cmake , "--build" , build_dir , "--parallel" , number_of_jobs , "--verbose" )
178184end
179185extension_name = "libcouchbase.#{ RbConfig ::CONFIG [ 'SOEXT' ] || RbConfig ::CONFIG [ 'DLEXT' ] } "
180186extension_path = File . expand_path ( File . join ( build_dir , extension_name ) )
181187abort "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
188+ extension_name . gsub! ( '.dylib' , '.bundle' )
189+ extension_name . gsub! ( /\. dll$/ , '.so' ) if RbConfig ::CONFIG [ "target_os" ] . include? ( 'mingw' )
186190install_path = File . expand_path ( File . join ( __dir__ , ".." , "lib" , "couchbase" , extension_name ) )
187191puts "-- copy extension to #{ install_path } "
188192FileUtils . cp ( extension_path , install_path , verbose : true )
0 commit comments