From 1269c01c4c2e048f31378b4d18bf42e45a0ebf0b Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 25 Mar 2021 01:25:32 -0700 Subject: [PATCH 01/12] Use mini_portile2 to build libmagic This will make it easier to use system libraries as Nokogiri does. --- ext/magic/extconf.rb | 119 ++++++++++++++++++++++++++++++++----------- ruby-magic.gemspec | 2 + 2 files changed, 91 insertions(+), 30 deletions(-) diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index 0a44868..f355462 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -1,42 +1,101 @@ # frozen_string_literal: true require 'mkmf' -require 'digest' -require 'open-uri' - -LIBMAGIC_TAG = '5.39'.freeze - -workdir = Dir.pwd -libdir = File.join(workdir, 'file-' + LIBMAGIC_TAG) -gemdir = File.expand_path(File.join(__dir__, '../..')) -gem_ext_dir = File.join(gemdir, 'lib', 'ext') -gem_include_dir = File.join(gem_ext_dir, 'include') -gem_lib_dir = File.join(gem_ext_dir, 'lib') -build_lib_dir = File.join(libdir, 'src', '.libs') - -expected_sha256 = 'f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1' -filename = "#{workdir}/file.tar.gz" - -unless File.exist?(filename) - File.open(filename, 'wb') do |target_file| - URI.open("https://fossies.org/linux/misc/file-#{LIBMAGIC_TAG}.tar.gz", "User-Agent" => "RubyMagic/#{RUBY_DESCRIPTION}") do |read_file| - target_file.write(read_file.read) + +LIBMAGIC_TAG = '5.39' +LIBIMAGE_SHA256 = 'f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1' + +# helpful constants +PACKAGE_ROOT_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')) + +# The gem version constraint in the Rakefile is not respected at install time. +# Keep this version in sync with the one in the Rakefile ! +REQUIRED_MINI_PORTILE_VERSION = "~> 2.5.0" + +def process_recipe(name, version, static_p, cross_p) + require 'rubygems' + gem('mini_portile2', REQUIRED_MINI_PORTILE_VERSION) + require 'mini_portile2' + message("Using mini_portile version #{MiniPortile::VERSION}\n") + + MiniPortile.new(name, version).tap do |recipe| + # Prefer host_alias over host in order to use i586-mingw32msvc as + # correct compiler prefix for cross build, but use host if not set. + recipe.host = RbConfig::CONFIG["host_alias"].empty? ? RbConfig::CONFIG["host"] : RbConfig::CONFIG["host_alias"] + recipe.target = File.join(PACKAGE_ROOT_DIR, "ports") + recipe.configure_options << "--libdir=#{File.join(recipe.path, 'lib')}" + + yield recipe + + env = Hash.new do |hash, key| + hash[key] = (ENV[key]).to_s + end + + recipe.configure_options.flatten! + + if static_p + recipe.configure_options += [ + "--disable-shared", + "--enable-static", + ] + env["CFLAGS"] = concat_flags(env["CFLAGS"], "-fPIC") + else + recipe.configure_options += [ + "--enable-shared", + "--disable-static", + ] + end + + if cross_p + recipe.configure_options += [ + "--target=#{recipe.host}", + "--host=#{recipe.host}", + ] +>>>>>>> 6ec2828 (Use mini_portile2 to build libmagic) end - end - checksum = Digest::SHA256.hexdigest(File.read(filename)) + recipe.configure_options += env.map do |key, value| + "#{key}=#{value.strip}" + end - if checksum != expected_sha256 - raise "SHA256 of #{filename} does not match: got #{checksum}, expected #{expected_sha256}" + recipe.cook + recipe.activate end end -system("tar -xzf #{filename}") || raise('ERROR') -system("cd #{libdir} && ./configure --prefix=#{gem_ext_dir} && make install") || raise('ERROR') +def truffle? + ::RUBY_ENGINE == 'truffleruby' +end + +def concat_flags(*args) + args.compact.join(" ") +end + +def config_static? + default_static = !truffle? + enable_config("static", default_static) +end + +def config_cross_build? + enable_config("cross-build") +end + +message "Building ruby-magic using packaged libraries.\n" -$LOCAL_LIBS << '-lmagic' -$LIBPATH << gem_lib_dir -$CFLAGS << " -I #{libdir}/src" +static_p = config_static? +message "Static linking is #{static_p ? 'enabled' : 'disabled'}.\n" +cross_build_p = config_cross_build? +message "Cross build is #{cross_build_p ? 'enabled' : 'disabled'}.\n" + +process_recipe('libmagic', LIBMAGIC_TAG, static_p, cross_build_p) do |recipe| + recipe.files = [{ + url: "https://fossies.org/linux/misc/file-#{recipe.version}.tar.gz", + sha256: LIBIMAGE_SHA256 + }] + + $LDFLAGS += " -L#{recipe.path}/lib -lz -lbz2 " + $CFLAGS << " -I#{recipe.path}/include" +end def darwin? RbConfig::CONFIG['target_os'] =~ /darwin/ @@ -188,7 +247,7 @@ def windows? have_func(f) end -dir_config('magic', [gem_include_dir], [gem_lib_dir]) +dir_config('magic') create_header create_makefile('magic/magic') diff --git a/ruby-magic.gemspec b/ruby-magic.gemspec index e3b7acb..c7f202b 100644 --- a/ruby-magic.gemspec +++ b/ruby-magic.gemspec @@ -49,4 +49,6 @@ Gem::Specification.new do |s| s.cert_chain = [ 'kwilczynski-public.pem' ] s.signing_key = signing_key if File.exist?(signing_key) + + s.add_runtime_dependency("mini_portile2", "~> 2.5.0") # keep version in sync with extconf.rb end From 69758e2fc7bad331862d9e12745a34234651f68e Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 26 Mar 2021 12:59:17 +0530 Subject: [PATCH 02/12] Make static builds work This commit builds a static library for libmagic and links the `magic.so` against it. That makes it a little easier to clean up files that are installed by the library. --- ext/magic/extconf.rb | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index f355462..9a757b1 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -34,13 +34,13 @@ def process_recipe(name, version, static_p, cross_p) recipe.configure_options.flatten! if static_p - recipe.configure_options += [ + recipe.configure_options = [ "--disable-shared", "--enable-static", ] env["CFLAGS"] = concat_flags(env["CFLAGS"], "-fPIC") else - recipe.configure_options += [ + recipe.configure_options = [ "--enable-shared", "--disable-static", ] @@ -51,7 +51,6 @@ def process_recipe(name, version, static_p, cross_p) "--target=#{recipe.host}", "--host=#{recipe.host}", ] ->>>>>>> 6ec2828 (Use mini_portile2 to build libmagic) end recipe.configure_options += env.map do |key, value| @@ -80,6 +79,14 @@ def config_cross_build? enable_config("cross-build") end +def darwin? + RbConfig::CONFIG['target_os'] =~ /darwin/ +end + +def windows? + RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/ +end + message "Building ruby-magic using packaged libraries.\n" static_p = config_static? @@ -87,22 +94,23 @@ def config_cross_build? cross_build_p = config_cross_build? message "Cross build is #{cross_build_p ? 'enabled' : 'disabled'}.\n" -process_recipe('libmagic', LIBMAGIC_TAG, static_p, cross_build_p) do |recipe| +libmagic_recipe = process_recipe('libmagic', LIBMAGIC_TAG, static_p, cross_build_p) do |recipe| recipe.files = [{ - url: "https://fossies.org/linux/misc/file-#{recipe.version}.tar.gz", + url: "https://ruby-magic.s3.eu-central-1.amazonaws.com/file-#{recipe.version}.tar.gz", sha256: LIBIMAGE_SHA256 }] - - $LDFLAGS += " -L#{recipe.path}/lib -lz -lbz2 " - $CFLAGS << " -I#{recipe.path}/include" end -def darwin? - RbConfig::CONFIG['target_os'] =~ /darwin/ -end +$LIBPATH = [File.join(libmagic_recipe.path, 'lib')] +$CFLAGS << " -I#{File.join(libmagic_recipe.path, 'include')} " -def windows? - RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/ +if static_p + ENV['PKG_CONFIG_PATH'] = "#{libmagic_recipe.path}/lib/pkgconfig" + # mkmf appends -- to the first option + $LIBS += " " + pkg_config('libmagic', 'libs --static') + $LDFLAGS.gsub!('-lmagic', '') + $LIBS.gsub!('-lmagic', '') + $LIBS += " " + File.join(libmagic_recipe.path, 'lib', "libmagic.#{$LIBEXT}") end if ENV['CC'] From e5b17e45a99432aaea0545dcdbef255fecf2069a Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 26 Mar 2021 07:51:59 -0700 Subject: [PATCH 03/12] Add --help and clean step --- ext/magic/extconf.rb | 108 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 6 deletions(-) diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index 9a757b1..9f3814f 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true +require 'find' require 'mkmf' +require 'pathname' LIBMAGIC_TAG = '5.39' LIBIMAGE_SHA256 = 'f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1' @@ -12,6 +14,41 @@ # Keep this version in sync with the one in the Rakefile ! REQUIRED_MINI_PORTILE_VERSION = "~> 2.5.0" +MAGIC_HELP_MESSAGE = <<~HELP + USAGE: ruby #{$0} [options] + + Flags that are always valid: + + --disable-clean + Do not clean out intermediate files after successful build. + + Flags only used when building and using the packaged libraries: + + --disable-static + Do not statically link packaged libraries, instead use shared libraries. + + --enable-cross-build + Enable cross-build mode. (You probably do not want to set this manually.) + + + Environment variables used: + + CC + Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']` + + CPPFLAGS + If this string is accepted by the C preprocessor, add it to the flags passed to the C preprocessor + + CFLAGS + If this string is accepted by the compiler, add it to the flags passed to the compiler + + LDFLAGS + If this string is accepted by the linker, add it to the flags passed to the linker + + LIBS + Add this string to the flags passed to the linker +HELP + def process_recipe(name, version, static_p, cross_p) require 'rubygems' gem('mini_portile2', REQUIRED_MINI_PORTILE_VERSION) @@ -62,12 +99,11 @@ def process_recipe(name, version, static_p, cross_p) end end -def truffle? - ::RUBY_ENGINE == 'truffleruby' -end - -def concat_flags(*args) - args.compact.join(" ") +# +# utility functions +# +def config_clean? + enable_config('clean', true) end def config_static? @@ -87,6 +123,54 @@ def windows? RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/ end +def truffle? + ::RUBY_ENGINE == 'truffleruby' +end + +def concat_flags(*args) + args.compact.join(" ") +end + +def do_help + print(MAGIC_HELP_MESSAGE) + exit!(0) +end + +def do_clean + root = Pathname(PACKAGE_ROOT_DIR) + pwd = Pathname(Dir.pwd) + + # Skip if this is a development work tree + unless (root + '.git').exist? + message("Cleaning files only used during build.\n") + + # (root + 'tmp') cannot be removed at this stage because + # libmagic.so is yet to be copied to lib. + + # clean the ports build directory + Pathname.glob(pwd.join('tmp', '*', 'ports')) do |dir| + FileUtils.rm_rf(dir, verbose: true) + end + + FileUtils.rm_rf(root + 'ports' + 'archives', verbose: true) + + if config_static? + # Remove everything but share/ directory + Find.find(root + 'ports').each do |filename| + FileUtils.rm_f(filename, verbose: true) unless filename.include?('/share') + end + end + end + + exit!(0) +end + +# +# main +# +do_help if arg_config('--help') +do_clean if arg_config('--clean') + message "Building ruby-magic using packaged libraries.\n" static_p = config_static? @@ -259,3 +343,15 @@ def windows? create_header create_makefile('magic/magic') + +if config_clean? + # Do not clean if run in a development work tree. + File.open('Makefile', 'at') do |mk| + mk.print(<<~EOF) + + all: clean-ports + clean-ports: $(DLLIB) + \t-$(Q)$(RUBY) $(srcdir)/extconf.rb --clean --#{static_p ? 'enable' : 'disable'}-static + EOF + end +end From 933457c740784f0b181237ced3c0a5453fdc2c61 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 26 Mar 2021 07:54:39 -0700 Subject: [PATCH 04/12] Add additional configure options --disable-dependency-tracking: As mentioned in https://www.gnu.org/software/automake/manual/html_node/Dependency-Tracking.html, is useless for one-time builds. --disable-silent-rules: As mentioned in https://www.gnu.org/software/automake/manual/html_node/Automake-Silent-Rules.html, this flag makes it possible to see what `make` is doing with every step. --enable-fsect-man5: Enable file formats in man section 5 --- ext/magic/extconf.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index 9f3814f..4558516 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -70,14 +70,20 @@ def process_recipe(name, version, static_p, cross_p) recipe.configure_options.flatten! + recipe.configure_options = [ + "--disable-silent-rules", + "--disable-dependency-tracking", + "--enable-fsect-man5" + ] + if static_p - recipe.configure_options = [ + recipe.configure_options += [ "--disable-shared", "--enable-static", ] env["CFLAGS"] = concat_flags(env["CFLAGS"], "-fPIC") else - recipe.configure_options = [ + recipe.configure_options += [ "--enable-shared", "--disable-static", ] From 4cd2f69831001a5f11b3b552fc969cba8151f2d0 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 26 Mar 2021 11:07:35 -0700 Subject: [PATCH 05/12] Add Travis CI builds with --disable-static This will ensure the build works with linking libmagic as a static and shared library. --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 36e1690..f3c386a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,8 @@ env: matrix: - COMPILER=gcc - COMPILER=clang + - COMPILER=gcc RAKE_COMPILER_OPTIONS="-- --disable-static" + - COMPILER=clang RAKE_COMPILER_OPTIONS="-- --disable-static" matrix: fast_finish: true @@ -45,5 +47,5 @@ before_script: - bundle exec rake clean script: - - bundle exec rake compile + - bundle exec rake compile $RAKE_COMPILER_OPTIONS - bundle exec rake test From d15e8d5c83cfd82e648d5d0480b887ddbc3f952a Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 27 Mar 2021 00:13:47 +0530 Subject: [PATCH 06/12] Support building with system library Using --use-system-libraries with link against the installed libmagic. magic.h is needed (e.g. via libmagic-dev). --- .travis.yml | 3 ++ ext/magic/extconf.rb | 75 +++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index f3c386a..be34b2f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,8 @@ env: - COMPILER=clang - COMPILER=gcc RAKE_COMPILER_OPTIONS="-- --disable-static" - COMPILER=clang RAKE_COMPILER_OPTIONS="-- --disable-static" + - COMPILER=gcc RAKE_COMPILER_OPTIONS="-- --use-system-libraries" + - COMPILER=clang RAKE_COMPILER_OPTIONS="-- --use-system-libraries" matrix: fast_finish: true @@ -40,6 +42,7 @@ before_install: - bundle config set --local path 'vendor/bundle' - bundle config set --local without 'development' - export CC="$(which $COMPILER)" + - if [[ $RAKE_COMPILER_OPTIONS =~ use-system-libraries ]]; then sudo apt update && sudo apt install libmagic1 libmagic-dev; fi before_script: - export CFLAGS="-I${PREFIX}/include" LDFLAGS="-L${PREFIX}/lib" diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index 4558516..1b61896 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -19,6 +19,15 @@ Flags that are always valid: + --use-system-libraries + --enable-system-libraries + Use system libraries instead of building and using the packaged libraries. + + --disable-system-libraries + Use the packaged libraries, and ignore the system libraries. This is the default on most + platforms, and overrides `--use-system-libraries` and the environment variable + `RB_MAGIC_USE_SYSTEM_LIBRARIES`. + --disable-clean Do not clean out intermediate files after successful build. @@ -30,6 +39,18 @@ --enable-cross-build Enable cross-build mode. (You probably do not want to set this manually.) + Flags only used when using system libraries: + + Related to libmagic: + + --with-magic-dir=DIRECTORY + Look for libmagic headers and library in DIRECTORY. + + --with-magic-lib=DIRECTORY + Look for libmagic library in DIRECTORY. + + --with-magic-include=DIRECTORY + Look for libmagic headers in DIRECTORY. Environment variables used: @@ -121,6 +142,12 @@ def config_cross_build? enable_config("cross-build") end +def config_system_libraries? + enable_config("system-libraries", ENV.key?("RB_MAGIC_USE_SYSTEM_LIBRARIES")) do |_, default| + arg_config('--use-system-libraries', default) + end +end + def darwin? RbConfig::CONFIG['target_os'] =~ /darwin/ end @@ -177,30 +204,36 @@ def do_clean do_help if arg_config('--help') do_clean if arg_config('--clean') -message "Building ruby-magic using packaged libraries.\n" +if config_system_libraries? + message "Building ruby-magic using system libraries.\n" -static_p = config_static? -message "Static linking is #{static_p ? 'enabled' : 'disabled'}.\n" -cross_build_p = config_cross_build? -message "Cross build is #{cross_build_p ? 'enabled' : 'disabled'}.\n" + dir_config('magic') +else + message "Building ruby-magic using packaged libraries.\n" -libmagic_recipe = process_recipe('libmagic', LIBMAGIC_TAG, static_p, cross_build_p) do |recipe| - recipe.files = [{ - url: "https://ruby-magic.s3.eu-central-1.amazonaws.com/file-#{recipe.version}.tar.gz", - sha256: LIBIMAGE_SHA256 - }] -end + static_p = config_static? + message "Static linking is #{static_p ? 'enabled' : 'disabled'}.\n" + cross_build_p = config_cross_build? + message "Cross build is #{cross_build_p ? 'enabled' : 'disabled'}.\n" -$LIBPATH = [File.join(libmagic_recipe.path, 'lib')] -$CFLAGS << " -I#{File.join(libmagic_recipe.path, 'include')} " + libmagic_recipe = process_recipe('libmagic', LIBMAGIC_TAG, static_p, cross_build_p) do |recipe| + recipe.files = [{ + url: "https://ruby-magic.s3.eu-central-1.amazonaws.com/file-#{recipe.version}.tar.gz", + sha256: LIBIMAGE_SHA256 + }] + end -if static_p - ENV['PKG_CONFIG_PATH'] = "#{libmagic_recipe.path}/lib/pkgconfig" - # mkmf appends -- to the first option - $LIBS += " " + pkg_config('libmagic', 'libs --static') - $LDFLAGS.gsub!('-lmagic', '') - $LIBS.gsub!('-lmagic', '') - $LIBS += " " + File.join(libmagic_recipe.path, 'lib', "libmagic.#{$LIBEXT}") + $LIBPATH = [File.join(libmagic_recipe.path, 'lib')] + $CFLAGS << " -I#{File.join(libmagic_recipe.path, 'include')} " + + if static_p + ENV['PKG_CONFIG_PATH'] = "#{libmagic_recipe.path}/lib/pkgconfig" + # mkmf appends -- to the first option + $LIBS += " " + pkg_config('libmagic', 'libs --static') + $LDFLAGS.gsub!('-lmagic', '') + $LIBS.gsub!('-lmagic', '') + $LIBS += " " + File.join(libmagic_recipe.path, 'lib', "libmagic.#{$LIBEXT}") + end end if ENV['CC'] @@ -345,8 +378,6 @@ def do_clean have_func(f) end -dir_config('magic') - create_header create_makefile('magic/magic') From a2e1ef463c4fd8927074df5317f1b9a03fd82971 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 26 Mar 2021 20:36:02 -0700 Subject: [PATCH 07/12] Update Travis CI to use Ubuntu 18.04 (bionic) Ubuntu 16.04 (xenial) will reach end-of-life in April 2021, and it has a more recent version of libmagic (5.32 vs. 5.25). --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index be34b2f..e0bf3fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: ruby -dist: xenial +dist: bionic cache: bundler: true From d01a319576d86bcb4ed846a3bb26d81a7cdb9f91 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 26 Mar 2021 21:07:03 -0700 Subject: [PATCH 08/12] Improve cleanup step in build Explicitly remove the paths we can clean up, leaving the share directory. --- ext/magic/extconf.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index 1b61896..cc471f2 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -187,10 +187,14 @@ def do_clean FileUtils.rm_rf(root + 'ports' + 'archives', verbose: true) - if config_static? - # Remove everything but share/ directory - Find.find(root + 'ports').each do |filename| - FileUtils.rm_f(filename, verbose: true) unless filename.include?('/share') + # Remove everything but share/ directory + remove_paths = %w[bin include] + remove_paths << 'lib' if config_static? + + Pathname.glob(File.join(root, 'ports', '*', 'libmagic', '*')) do |dir| + remove_paths.each do |path| + remove_dir = File.join(dir, path) + FileUtils.rm_rf(remove_dir, verbose: true) end end end From a2d8b93b25ab7bf15a058f99efd842e30dfdeb54 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 28 Mar 2021 05:39:40 +0530 Subject: [PATCH 09/12] Fix test failing when MAGIC path set to list of paths With system libraries, the default MAGIC path may be set to a list of paths. For example, in Debian and Ubuntu, the path is `/etc/magic:/usr/share/misc/magic`. This causes libmagic to do further scans and activates checking of parsed files. We now update the test to reflect this behavior. --- test/test_magic.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/test_magic.rb b/test/test_magic.rb index 9303b15..f3ab0d2 100644 --- a/test/test_magic.rb +++ b/test/test_magic.rb @@ -55,7 +55,13 @@ def test_magic_new_instance end def test_magic_new_instance_default_flags - assert_equal(0, @magic.flags) + # If libmagic needs to search more than one path, it will enable the CHECK flag. + # See https://github.com/kwilczynski/ruby-magic/pull/5#issuecomment-808686480 + if @magic.paths.length > 1 + assert_equal(Magic::CHECK, @magic.flags) + else + assert_equal(Magic::NONE, @magic.flags) + end end def test_magic_new_with_block From dfe76a5bcea965ec3f6e8c8c67c8646324e8ddd6 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 28 Mar 2021 06:37:07 -0700 Subject: [PATCH 10/12] Add rpath and drop -lmagic substituions rpath will help enusre we don't link against the static library in case there isone. The -lmagic substitutions weren't actually helping because mkmf have_library was always adding its own -lmagic option. --- ext/magic/extconf.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/magic/extconf.rb b/ext/magic/extconf.rb index cc471f2..1f6fe90 100644 --- a/ext/magic/extconf.rb +++ b/ext/magic/extconf.rb @@ -229,13 +229,12 @@ def do_clean $LIBPATH = [File.join(libmagic_recipe.path, 'lib')] $CFLAGS << " -I#{File.join(libmagic_recipe.path, 'include')} " + $LDFLAGS += " -Wl,-rpath,#{libmagic_recipe.path}/lib" if static_p ENV['PKG_CONFIG_PATH'] = "#{libmagic_recipe.path}/lib/pkgconfig" # mkmf appends -- to the first option $LIBS += " " + pkg_config('libmagic', 'libs --static') - $LDFLAGS.gsub!('-lmagic', '') - $LIBS.gsub!('-lmagic', '') $LIBS += " " + File.join(libmagic_recipe.path, 'lib', "libmagic.#{$LIBEXT}") end end From d279cb436178556b334073aca75d17e73c7f3d59 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 29 Mar 2021 10:51:53 -0700 Subject: [PATCH 11/12] Add more files to `rake clobber` list --- Rakefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 825990f..c9bdc60 100644 --- a/Rakefile +++ b/Rakefile @@ -11,7 +11,11 @@ CLEAN.include FileList['**/*{.o,.so,.bundle,.log}'], FileList['**/Makefile'] CLOBBER.include FileList['lib/**/*.so'], - FileList['doc/**/*'] + FileList['doc/**/*'], + FileList['tmp/'], + FileList['ext/magic/extconf.h'], + FileList['ext/magic/tmp'], + FileList['ports/'] gem = eval File.read('ruby-magic.gemspec') From 6aab08309e3b40b093b9f0ab09e04c793e080b79 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 31 Mar 2021 00:52:21 -0700 Subject: [PATCH 12/12] Remove unnecessary exports in Travis CI config All the necessary paths are configured in extconf.rb already. There is no need to add extra paths. --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e0bf3fd..29c401a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,8 +45,6 @@ before_install: - if [[ $RAKE_COMPILER_OPTIONS =~ use-system-libraries ]]; then sudo apt update && sudo apt install libmagic1 libmagic-dev; fi before_script: - - export CFLAGS="-I${PREFIX}/include" LDFLAGS="-L${PREFIX}/lib" - - export LD_LIBRARY_PATH="${PREFIX}/lib" - bundle exec rake clean script: