Skip to content

Commit

Permalink
Provide precompiled gem for x86_64-linux
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso committed Jan 7, 2025
1 parent 9136dee commit 77783e7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 73 deletions.
119 changes: 57 additions & 62 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ jobs:
platform:
- "x64-mingw32"
- "x64-mingw-ucrt"
name: cross-compile-windows
- "x86_64-linux-gnu"
name: cross-compile
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
Expand All @@ -34,10 +35,10 @@ jobs:
uses: actions/cache@v4
with:
path: ports
key: cross-compiled-v4-${{ matrix.platform }}-${{ hashFiles('**/.ports_versions') }}
key: cross-compiled-v5-${{ matrix.platform }}-${{ hashFiles('**/.ports_versions') }}
restore-keys: |
cross-compiled-v4-${{ matrix.platform }}-${{ hashFiles('**/.ports_versions') }}
cross-compiled-v4-${{ matrix.platform }}-
cross-compiled-v5-${{ matrix.platform }}-${{ hashFiles('**/.ports_versions') }}
cross-compiled-v5-${{ matrix.platform }}-
- name: Build gem
shell: bash
Expand Down Expand Up @@ -123,18 +124,9 @@ jobs:

- name: Install native gem and restore cross-compiled code from it
shell: pwsh
run: |
$rubyArchitecture = (ruby -e "puts RbConfig::CONFIG['arch']").Trim()
$gemVersion = (Get-Content VERSION).Trim()
$gemToUnpack = "./tiny_tds-$gemVersion-$rubyArchitecture.gem"
Write-Host "Looking to unpack $gemToUnpack"
gem unpack --target ./tmp "$gemToUnpack"
# Restore precompiled code
$source = (Resolve-Path ".\tmp\tiny_tds-$gemVersion-$rubyArchitecture\lib\tiny_tds").Path
$destination = (Resolve-Path ".\lib\tiny_tds").Path
Get-ChildItem $source -Recurse -Exclude "*.rb" | Copy-Item -Destination {Join-Path $destination $_.FullName.Substring($source.length)}
run: "& ./test/bin/restore-from-native-gem.ps1"
env:
RUBY_ARCHITECTURE: "x64-mingw32"

- name: Setup MSSQL
uses: rails-sqlserver/setup-mssql@v1
Expand Down Expand Up @@ -247,18 +239,9 @@ jobs:

- name: Install native gem and restore cross-compiled code from it
shell: pwsh
run: |
$rubyArchitecture = (ruby -e "puts RbConfig::CONFIG['arch']").Trim()
$gemVersion = (Get-Content VERSION).Trim()
$gemToUnpack = "./tiny_tds-$gemVersion-$rubyArchitecture.gem"
Write-Host "Looking to unpack $gemToUnpack"
gem unpack --target ./tmp "$gemToUnpack"
# Restore precompiled code
$source = (Resolve-Path ".\tmp\tiny_tds-$gemVersion-$rubyArchitecture\lib\tiny_tds").Path
$destination = (Resolve-Path ".\lib\tiny_tds").Path
Get-ChildItem $source -Recurse -Exclude "*.rb" | Copy-Item -Destination {Join-Path $destination $_.FullName.Substring($source.length)}
run: "& ./test/bin/restore-from-native-gem.ps1"
env:
RUBY_ARCHITECTURE: "x64-mingw-ucrt"

- name: Setup MSSQL
uses: rails-sqlserver/setup-mssql@v1
Expand Down Expand Up @@ -330,44 +313,59 @@ jobs:
ruby -e "require 'tiny_tds'; puts TinyTds::Gem.root_path"
exit $LASTEXITCODE
compile-native-ports:
runs-on: ubuntu-22.04
name: cross-compile-linux
steps:
- uses: actions/checkout@v4
install-linux:
needs:
- cross-compile
strategy:
fail-fast: false
matrix:
platform:
- "x86_64-linux-gnu"

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.4
bundler-cache: true
ruby-version:
- "2.7"
- "3.0"
- "3.1"
- "3.2"
- "3.3"
- "3.4"

- name: Write used versions into file
run: bundle exec rake ports:version_file
name: install-linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Cache ports
uses: actions/cache@v4
- name: Download precompiled gem
uses: actions/download-artifact@v4
with:
path: ports
key: native-v3-${{ hashFiles('**/.ports_versions') }}
restore-keys: |
native-v3-${{ hashFiles('* */.ports_versions') }}
native-v3-
name: gem-${{ matrix.platform }}
path: precompiled/gems

- name: Build required libraries
run: |
bundle exec rake ports
- run: |
docker run --rm -v $PWD/precompiled:/precompiled -w /precompiled \
${{ matrix.docker_platform }} ruby:${{ matrix.ruby }}${{ matrix.docker_tag }} \
sh -c "
gem update --system &&
gem install --no-document ./tiny_tds-$(cat VERSION)-${{ matrix.platform }}.gem &&
ruby -e "require 'tiny_tds'; puts TinyTds::Gem.root_path"
"
test-linux:
needs:
- compile-native-ports
- cross-compile
name: test-linux
strategy:
fail-fast: false
matrix:
force-encryption:
- false
- true

mssql-version:
- 2017
- 2019
- 2022

ruby-version:
- "2.7"
- "3.0"
Expand All @@ -385,27 +383,24 @@ jobs:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true

- name: Write used versions into file
run: |
bundle exec rake ports:version_file
- name: Cache ports
uses: actions/cache@v4
- name: Download precompiled gem
uses: actions/download-artifact@v4
with:
path: ports
key: native-v3-${{ hashFiles('**/.ports_versions') }}
fail-on-cache-miss: true
name: gem-x86_64-linux-gnu

- name: Build gem
run: |
bundle exec rake build
- name: Install native gem and restore cross-compiled code from it
shell: pwsh
run: "& ./test/bin/restore-from-native-gem.ps1"
env:
RUBY_ARCHITECTURE: "x86_64-linux-gnu"

- name: Setup MSSQL
uses: rails-sqlserver/setup-mssql@v1
with:
components: sqlcmd,sqlengine
version: ${{ matrix.mssql-version }}
sa-password: "c0MplicatedP@ssword"
force-encryption: ${{ matrix.force-encryption }}

- name: Setup MSSQL database
run: |
Expand Down
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ SPEC = Gem::Specification.load(File.expand_path('../tiny_tds.gemspec', __FILE__)

CrossLibrary = Struct.new :platform, :openssl_config, :toolchain
CrossLibraries = [
['x64-mingw-ucrt', 'mingw64', 'x86_64-w64-mingw32'],
['x64-mingw32', 'mingw64', 'x86_64-w64-mingw32'],
['x64-mingw-ucrt', 'mingw64'],
['x64-mingw32', 'mingw64'],
['x86_64-linux-gnu', 'linux-x86_64'],
].map do |platform, openssl_config, toolchain|
CrossLibrary.new platform, openssl_config, toolchain
end
Expand Down
18 changes: 9 additions & 9 deletions ext/tiny_tds/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
require 'mini_portile2'

openssl_platform = with_config("openssl-platform")
toolchain = with_config("toolchain")

class BuildRecipe < MiniPortile
def initialize(name, version, files)
Expand All @@ -29,11 +28,11 @@ def initialize(name, version, files)

# this will yield all ports into the same directory, making our path configuration for the linker easier
def port_path
"#{@target}/#{host}"
"#{@target}/#{RUBY_PLATFORM}"
end

def tmp_path
"tmp/#{host}/ports/#{@name}/#{@version}"
"tmp/#{RUBY_PLATFORM}/ports/#{@name}/#{@version}"
end

def cook_and_activate
Expand Down Expand Up @@ -70,14 +69,11 @@ def install
end

recipe.openssl_platform = openssl_platform
recipe.host = toolchain
recipe.cook_and_activate
end

libiconv_recipe = BuildRecipe.new("libiconv", ICONV_VERSION, [ICONV_SOURCE_URI]).tap do |recipe|
recipe.configure_options << "CFLAGS=-fPIC" if RUBY_PLATFORM =~ /linux/
recipe.host = toolchain

recipe.cook_and_activate
end

Expand All @@ -88,17 +84,16 @@ def install
# removing one or the other leads to build failures in any case of FreeTDS
recipe.configure_options << "CFLAGS=-fPIC" if RUBY_PLATFORM =~ /linux/
recipe.configure_options << "LDFLAGS=-L#{openssl_recipe.path}/lib"
recipe.configure_options << "LIBS=-liconv -lssl -lcrypto #{"-lwsock32 -lgdi32 -lws2_32 -lcrypt32" if RUBY_PLATFORM =~ /mingw|mswin/}"
recipe.configure_options << "LIBS=-liconv -lssl -lcrypto #{"-lwsock32 -lgdi32 -lws2_32 -lcrypt32" if RUBY_PLATFORM =~ /mingw|mswin/} #{"-ldl -lpthread" if RUBY_PLATFORM =~ /linux/}"
recipe.configure_options << "CPPFLAGS=-I#{openssl_recipe.path}/include"

recipe.configure_options << "OPENSSL_CFLAGS=-L#{openssl_recipe.path}/lib"
recipe.configure_options << "OPENSSL_LIBS=-lssl -lcrypto #{"-lwsock32 -lgdi32 -lws2_32 -lcrypt32" if RUBY_PLATFORM =~ /mingw|mswin/}"
recipe.configure_options << "OPENSSL_LIBS=-lssl -lcrypto #{"-lwsock32 -lgdi32 -lws2_32 -lcrypt32" if RUBY_PLATFORM =~ /mingw|mswin/} #{"-ldl -lpthread" if RUBY_PLATFORM =~ /linux/}"

recipe.configure_options << "--with-openssl=#{openssl_recipe.path}"
recipe.configure_options << "--with-libiconv-prefix=#{libiconv_recipe.path}"
recipe.configure_options << "--disable-odbc"

recipe.host = toolchain
recipe.cook_and_activate
end

Expand All @@ -121,6 +116,11 @@ def install
find_library("wsock32", nil)
end

if RUBY_PLATFORM =~ /linux/
find_library("dl", nil)
find_library("pthread", nil)
end

find_library("crypto", nil)
find_library("ssl", nil)
find_library("iconv", "libiconv_open")
Expand Down
10 changes: 10 additions & 0 deletions test/bin/restore-from-native-gem.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$gemVersion = (Get-Content VERSION).Trim()
$gemToUnpack = "./tiny_tds-$gemVersion-$env:RUBY_ARCHITECTURE.gem"

Write-Host "Looking to unpack $gemToUnpack"
gem unpack --target ./tmp "$gemToUnpack"

# Restore precompiled code
$source = (Resolve-Path ".\tmp\tiny_tds-$gemVersion-$env:RUBY_ARCHITECTURE\lib\tiny_tds").Path
$destination = (Resolve-Path ".\lib\tiny_tds").Path
Get-ChildItem $source -Recurse -Exclude "*.rb" | Copy-Item -Destination {Join-Path $destination $_.FullName.Substring($source.length)}

0 comments on commit 77783e7

Please sign in to comment.