From 8e15e8e60a4aa3cc356a325796ab3d8f50c7a37a Mon Sep 17 00:00:00 2001 From: Rick Ohnemus Date: Thu, 15 Sep 2022 15:49:26 -0700 Subject: [PATCH 1/4] Don't use default include and library directories with dir_config Using default directories in publicly available gems can cause problems because the directories are used when building unless all of them are overridden. For this gem it means running "gem install" with "--with-magic-dir=..." and "--with-gnurx-dir=..." (yes, even if gnurx isn't installed on the system). Kind of annoying to have to look at a gem's extconf.rb to see what dir_config calls are being made and what needs to be overridden because of defaults that conflict with how ruby was configured/installed. The above discovery was prompted by having libmagic installed in both the system default location and a non-standard location (that ruby uses for a bunch of other third-party libraries). The following warning was issued when trying to use the gem unless the gem was installed with both '--with-magic-dir=..." and "-with-gnurx-dir=...": FileMagic v0.7.3: compiled magic version [5.40] does not match with shared library magic version [5.43] --- ext/filemagic/extconf.rb | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/ext/filemagic/extconf.rb b/ext/filemagic/extconf.rb index 158cea4..5b39163 100644 --- a/ext/filemagic/extconf.rb +++ b/ext/filemagic/extconf.rb @@ -1,24 +1,10 @@ require 'mkmf' -HEADER_DIRS = [ - '/opt/local/include', # MacPorts - '/usr/local/include', # compiled from source and Homebrew - '/opt/homebrew/include', # compiled from source and Homebrew (ARM based Macs) - '/usr/include', # system -] - -LIB_DIRS = [ - '/opt/local/lib', # MacPorts - '/usr/local/lib', # compiled from source and Homebrew - '/opt/homebrew/lib', # compiled from source and Homebrew (ARM based Macs) - '/usr/lib', # system -] - $CFLAGS << ' -Wall' if ENV['WALL'] $LDFLAGS << ' -static-libgcc' if RUBY_PLATFORM =~ /cygwin|mingw|mswin/ -dir_config('magic', HEADER_DIRS, LIB_DIRS) -dir_config('gnurx', HEADER_DIRS, LIB_DIRS) +dir_config('magic') +dir_config('gnurx') have_library('gnurx') From 0f80e2b67ec48135d9457655f902b2ad83a02343 Mon Sep 17 00:00:00 2001 From: Rick Ohnemus Date: Mon, 19 Sep 2022 16:58:35 -0700 Subject: [PATCH 2/4] Backward compatibility with older Mac changes. This is slightly different from the older changes because it allows --with-magic-* to override the defaults in this file. --- ext/filemagic/extconf.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/ext/filemagic/extconf.rb b/ext/filemagic/extconf.rb index 5b39163..c13d658 100644 --- a/ext/filemagic/extconf.rb +++ b/ext/filemagic/extconf.rb @@ -1,10 +1,32 @@ require 'mkmf' +hdr_dirs = lib_dirs = nil + +if RbConfig::CONFIG['host_os'] =~ /darwin/ + unless with_config('magic-dir') || with_config('magic-include') + hdr_dirs = [ + '/opt/local/include', # MacPorts + '/usr/local/include', # compiled from source and Homebrew + '/opt/homebrew/include', # compiled from source and Homebrew (ARM based Macs) + '/usr/include', # system + ] + end + + unless with_config('magic-dir') || with_config('magic-lib') + lib_dirs = [ + '/opt/local/lib', # MacPorts + '/usr/local/lib', # compiled from source and Homebrew + '/opt/homebrew/lib', # compiled from source and Homebrew (ARM based Macs) + '/usr/lib', # system + ] + end +end + $CFLAGS << ' -Wall' if ENV['WALL'] $LDFLAGS << ' -static-libgcc' if RUBY_PLATFORM =~ /cygwin|mingw|mswin/ -dir_config('magic') -dir_config('gnurx') +dir_config('magic', hdr_dirs, lib_dirs) +dir_config('gnurx', hdr_dirs, lib_dirs) have_library('gnurx') @@ -13,5 +35,5 @@ have_header('file/patchlevel.h') create_makefile('filemagic/ruby_filemagic') else - abort '*** ERROR: missing required library to compile this module' + abort '*** ERROR: missing required header and/or library to compile this module' end From c246f674f545ba3b752e7ca1a03a69d3594a550d Mon Sep 17 00:00:00 2001 From: Rick Ohnemus Date: Thu, 7 Dec 2023 09:51:12 -0800 Subject: [PATCH 3/4] Revert back to original changes without darwin checks. Looked at MANY gems and very few set paths for headers and/or libraries. The vast majority expect --with-* options to be used when installing gems that use things in non-standard places. Old commit log entry: Don't use default include and library directories with dir_config Using default directories in publicly available gems can cause problems because the directories are used when building unless all of them are overridden. For this gem it means running "gem install" with "--with-magic-dir=..." and "--with-gnurx-dir=..." (yes, even if gnurx isn't installed on the system). Kind of annoying to have to look at a gem's extconf.rb to see what dir_config calls are being made and what needs to be overridden because of defaults that conflict with how ruby was configured/installed. The above discovery was prompted by having libmagic installed in both the system default location and a non-standard location (that ruby uses for a bunch of other third-party libraries). The following warning was issued when trying to use the gem unless the gem was installed with both '--with-magic-dir=..." and "-with-gnurx-dir=...": FileMagic v0.7.3: compiled magic version [5.40] does not match with shared library magic version [5.43] --- ext/filemagic/extconf.rb | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/ext/filemagic/extconf.rb b/ext/filemagic/extconf.rb index c13d658..29dfedb 100644 --- a/ext/filemagic/extconf.rb +++ b/ext/filemagic/extconf.rb @@ -1,32 +1,10 @@ require 'mkmf' -hdr_dirs = lib_dirs = nil - -if RbConfig::CONFIG['host_os'] =~ /darwin/ - unless with_config('magic-dir') || with_config('magic-include') - hdr_dirs = [ - '/opt/local/include', # MacPorts - '/usr/local/include', # compiled from source and Homebrew - '/opt/homebrew/include', # compiled from source and Homebrew (ARM based Macs) - '/usr/include', # system - ] - end - - unless with_config('magic-dir') || with_config('magic-lib') - lib_dirs = [ - '/opt/local/lib', # MacPorts - '/usr/local/lib', # compiled from source and Homebrew - '/opt/homebrew/lib', # compiled from source and Homebrew (ARM based Macs) - '/usr/lib', # system - ] - end -end - $CFLAGS << ' -Wall' if ENV['WALL'] $LDFLAGS << ' -static-libgcc' if RUBY_PLATFORM =~ /cygwin|mingw|mswin/ -dir_config('magic', hdr_dirs, lib_dirs) -dir_config('gnurx', hdr_dirs, lib_dirs) +dir_config('magic') +dir_config('gnurx') have_library('gnurx') From 41cb1a16c752d6baa3c792b44af5d9a3a5df740f Mon Sep 17 00:00:00 2001 From: Rick Ohnemus Date: Thu, 7 Dec 2023 09:52:02 -0800 Subject: [PATCH 4/4] Separate error messages for missing header and library --- ext/filemagic/extconf.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ext/filemagic/extconf.rb b/ext/filemagic/extconf.rb index 29dfedb..59f949c 100644 --- a/ext/filemagic/extconf.rb +++ b/ext/filemagic/extconf.rb @@ -6,12 +6,15 @@ dir_config('magic') dir_config('gnurx') -have_library('gnurx') - -if have_library('magic', 'magic_open') && have_header('magic.h') - have_func('magic_version') - have_header('file/patchlevel.h') - create_makefile('filemagic/ruby_filemagic') +if have_library('magic', 'magic_open') + if have_header('magic.h') + have_library('gnurx') + have_func('magic_version') + have_header('file/patchlevel.h') + create_makefile('filemagic/ruby_filemagic') + else + abort '*** ERROR: missing magic.h (required to compile this module)' + end else - abort '*** ERROR: missing required header and/or library to compile this module' + abort '*** ERROR: missing magic library (required to build this module)' end