ruby-magic 0.0.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # File Magic in Ruby
2
+
3
+ [![Build Status](https://travis-ci.com/kwilczynski/ruby-magic.svg)](https://travis-ci.com/kwilczynski/ruby-magic)
4
+ [![Documentation Status](https://inch-ci.org/github/kwilczynski/ruby-magic.svg)](https://inch-ci.org/github/kwilczynski/ruby-magic)
5
+ [![Gem Version](https://badge.fury.io/rb/ruby-magic.svg)](http://badge.fury.io/rb/ruby-magic)
6
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
+
8
+ ## Introduction
9
+
10
+ Provides simple interface to [libmagic][1] for Ruby Programming Language.
11
+
12
+ ## Table of Contents
13
+
14
+ 1. [Contributing](#contributing)
15
+ 2. [Versioning](#versioning)
16
+ 3. [Author](#author)
17
+ 4. [Contributors](#contributors)
18
+ 4. [Copyright](#copyright)
19
+ 5. [License](#license)
20
+
21
+ ## Contributing
22
+
23
+ See the [CONTRIBUTING.md](CONTRIBUTING.md) for best practices and instructions
24
+ on setting up your development environment.
25
+
26
+ ## Versioning
27
+
28
+ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
29
+ For the versions available, see the tags on this repository.
30
+
31
+ ## Author
32
+
33
+ Krzysztof Wilczyński (<kw@linux.com>)
34
+
35
+ ## Contributors
36
+
37
+ See the [CONTRIBUTORS.md](CONTRIBUTORS.md) file for a list of contributors to
38
+ the project.
39
+
40
+ ## Copyright
41
+
42
+ Copyright 2013-2021 Krzysztof Wilczyński
43
+
44
+ ## License
45
+
46
+ This project is licensed under the terms of the Apache License, Version 2.0 license.
47
+ To view the full license see the included [LICENSE](LICENSE) file.
48
+
49
+ [1]: https://en.wikipedia.org/wiki/File_(command)
data/dependencies.yml ADDED
@@ -0,0 +1,93 @@
1
+ libmagic:
2
+ version: "5.39"
3
+ sha256: "f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1"
4
+ # Manually verified checksum:
5
+ #
6
+ # $ gpg --verify file-5.39.tar.gz.asc file-5.39.tar.gz
7
+ # gpg: Signature made Mon Jun 15 02:06:08 2020 CEST
8
+ # gpg: using DSA key 0x71112AB16CB33B3A
9
+ # gpg: Good signature from "Christos Zoulas (personal key) <christos@zoulas.com>" [unknown]
10
+ # gpg: aka "Christos Zoulas (NetBSD key) <christos@netbsd.org>" [unknown]
11
+ # gpg: WARNING: This key is not certified with a trusted signature!
12
+ # gpg: There is no indication that the signature belongs to the owner.
13
+ # Primary key fingerprint: BE04 995B A8F9 0ED0 C0C1 76C4 7111 2AB1 6CB3 3B3A
14
+ #
15
+ # Using this PGP signature:
16
+ #
17
+ # -----BEGIN PGP PUBLIC KEY BLOCK-----
18
+ # mQGiBEg60Q8RBAC89+Oyi+iU1dMftAqXKh9Ml2PDCRk+Q6gwSY2BwmnKrPYvLSx9/YApjDp4
19
+ # BzhjaMCiDtDkbVDO3JuLQqUsCUWRJr9UxgfkQPMx3obkgoyCCvUcG5eK9CHeztjz5UK3jOzD
20
+ # 939UtupUXs+5z4dK1oAFFEL3sV6m5O4cuHyeWpjLCwCg9LYwmG2wIB65/2619W2kRMShDwkD
21
+ # /1KweB5mmXdQCDpzSg02Q4kGOzWmN2Dpv5kvKd9Lj/qxkj5PFOJ56Rmm04K0lPE6AnDgvEeu
22
+ # SqO8pkA8OY5IQNIjlgg3tshoMBRdsj9JLKv67smQXy01eQmadJWoiIZFuSLX7nrRLm3x3i1K
23
+ # 4/tcKorXoD5QBgOWaYFzkOcQ5jrwA/9tn0MEkkVqez88xGhOjq6jooRWrP2lhNWb+Q7ayEvW
24
+ # 9Wm9CYS0dXxlCCJfqBOc+cZo8iVVbqiOzwAQ7uAAUv8azuaeRWpOD7MSjkXjYtkPwZevEd6x
25
+ # FKODaB2BDO4WMlxO9mp3Q/gPfrM41v9n33tYsAzQMW7c9W5mX/XH2Cv/57QyQ2hyaXN0b3Mg
26
+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H2awLOg6p8at2NJCn56AkldK1PyVtL0Qvl7nEOe+lcULqfLNEbGEgzev+ZK0v6So6lA2t
82
+ # i8derF5XkIMzCFdjcC1reZvfwNnxd/pQzuMfZOfPjPD6OKaBEWnWNZscU/ucNkgQpv+M2hCA
83
+ # paC3NaNiXl0TWsDp7981WSWnf6YZRTjLKyObdGmpo7Z+C9uVu2DbRlUiRAD3azBo4t03EZVB
84
+ # XMo/1opuqxyU6FLVW0mxLI2wh2M1Jt/RAvXLm5/Z9zGnPqwAObxkA518e71QXyM+GQ+M8Kyp
85
+ # PHr00oC+32PrKnFADI3s1oK5zMX0tjXb0voAgXTWNFsmi+efj7A3wt6G15ai70pOSTuFl9kb
86
+ # BqmCI4NmD5wV+uIR/jcmTf7jZKX5QPjDSJddJ5EeAsPXtur0wFZ9JwPM2FyNaeL92BjRYQPW
87
+ # C9vRtAUjJa2PSLofMDaEs+VD76WRI3E8kPIJiFeSqOLinclo1e41q3nyj5JBnwg50CwshW7g
88
+ # w/bU+IbzL0eZFjJ7ftJ+Xi/vlGtfbxjuKuhyD2PM3DNf9uXmcIV4ldEJzwhw2KmITwQYEQIA
89
+ # DwIbDAUCV7RsRgUJIkWdEAAKCRBxESqxbLM7OiJUAJ4y0+A//pOrp3rjo28Uyt34U+qRqwCe
90
+ # NqiZtKbSqpUgcLmKNgot+mSEAmmITwQYEQIADwUCSDrSMwIbDAUJDwmcAAAKCRBxESqxbLM7
91
+ # OnaqAJ0cUAs8+EPXqRYYbdu6ROINhExMkwCgpupi6uEG+b4yo6M+VlcQY7gl2DE=
92
+ # =XrDo
93
+ # -----END PGP PUBLIC KEY BLOCK-----
data/ext/magic/common.h CHANGED
@@ -1,23 +1,3 @@
1
- /* :enddoc: */
2
-
3
- /*
4
- * common.h
5
- *
6
- * Copyright 2013-2014 Krzysztof Wilczynski
7
- *
8
- * Licensed under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License.
10
- * You may obtain a copy of the License at
11
- *
12
- * http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software
15
- * distributed under the License is distributed on an "AS IS" BASIS,
16
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- * See the License for the specific language governing permissions and
18
- * limitations under the License.
19
- */
20
-
21
1
  #if !defined(_COMMON_H)
22
2
  #define _COMMON_H 1
23
3
 
@@ -29,62 +9,153 @@
29
9
  # define _BSD_SOURCE 1
30
10
  #endif
31
11
 
12
+ #if !defined(_DEFAULT_SOURCE)
13
+ # define _DEFAULT_SOURCE 1
14
+ #endif
15
+
32
16
  #if defined(__cplusplus)
33
17
  extern "C" {
34
18
  #endif
35
19
 
36
20
  #include <stdio.h>
37
21
  #include <stdlib.h>
22
+ #include <limits.h>
38
23
  #include <string.h>
39
24
  #include <unistd.h>
40
25
  #include <fcntl.h>
41
26
  #include <errno.h>
42
27
  #include <assert.h>
43
- #include <locale.h>
44
28
  #include <sys/stat.h>
45
- #include <magic.h>
46
- #include <ruby.h>
47
-
48
- #if !defined(EPERM)
49
- # define EPERM 1
50
- #endif
51
-
52
- #if !defined(EFAULT)
53
- # define EFAULT 14
54
- #endif
55
29
 
56
- #if !defined(EINVAL)
57
- # define EINVAL 22
58
- #endif
59
-
60
- #if !defined(ENOSYS)
61
- # define ENOSYS 38
62
- #endif
63
-
64
- #if !defined(ANYARGS)
65
- # if defined(__cplusplus)
66
- # define ANYARGS ...
67
- # else
68
- # define ANYARGS
69
- # endif
70
- #endif
71
-
72
- #if defined(UNUSED)
73
- # undef(UNUSED)
74
- #endif
75
-
76
- #define UNUSED(x) (void)(x)
77
-
78
- #if !defined(HAVE_MAGIC_VERSION) || MAGIC_VERSION < 517
79
- # define HAVE_BROKEN_MAGIC 1
80
- #endif
30
+ #include <magic.h>
81
31
 
82
- extern int errno;
32
+ #include <ruby.h>
33
+ #include <ruby/version.h>
34
+
35
+ #if defined(HAVE_RUBY_IO_H)
36
+ # include <ruby/io.h>
37
+ #else
38
+ # include <rubyio.h>
39
+ #endif /* HAVE_RUBY_IO_H */
40
+
41
+ #define BIT(n) (1 << (n))
42
+
43
+ #if !defined(UNUSED)
44
+ # define UNUSED(x) (void)(x)
45
+ #endif /* UNUSED */
46
+
47
+ #define ARRAY_SIZE(a) (int)(sizeof(a) / sizeof((a)[0]))
48
+
49
+ #if defined(F_DUPFD_CLOEXEC)
50
+ # define HAVE_F_DUPFD_CLOEXEC 1
51
+ #endif /* F_DUPFD_CLOEXEC */
52
+
53
+ #if defined(O_CLOEXEC)
54
+ # define HAVE_O_CLOEXEC 1
55
+ #endif /* O_CLOEXEC */
56
+
57
+ #if defined(POSIX_CLOSE_RESTART)
58
+ # define HAVE_POSIX_CLOSE_RESTART 1
59
+ #endif /* POSIX_CLOSE_RESTART */
60
+
61
+ #define MAGIC_EXTENSION_SEPARATOR "/"
62
+ #define MAGIC_CONTINUE_SEPARATOR "\n- "
63
+
64
+ #if !defined(MAGIC_SYSTEM_LIBRARIES)
65
+ # define MAGIC_STATUS_CHECK(x) x
66
+ #else
67
+ # define MAGIC_STATUS_CHECK(x) 1
68
+ #endif /* MAGIC_SYSTEM_LIBRARIES */
69
+
70
+ #if !defined(MAGIC_CUSTOM_CHECK_TYPE)
71
+ # define MAGIC_CHECK_RUBY_TYPE(o, t) Check_Type((o), (t))
72
+ #else
73
+ # define MAGIC_CHECK_RUBY_TYPE(o, t) magic_check_ruby_type((o), (t))
74
+ #endif /* MAGIC_CUSTOM_CHECK_TYPE */
75
+
76
+ #if !defined(MAGIC_NO_CHECK_CSV)
77
+ # define MAGIC_NO_CHECK_CSV 0
78
+ #endif /* MAGIC_NO_CHECK_CSV */
79
+
80
+ #if !defined(MAGIC_NO_CHECK_JSON)
81
+ # define MAGIC_NO_CHECK_JSON 0
82
+ #endif /* MAGIC_NO_CHECK_JSON */
83
+
84
+ #define DATA_P(x) (RB_TYPE_P((x), T_DATA))
85
+ #define BOOLEAN_P(x) (RB_TYPE_P((x), T_TRUE) || RB_TYPE_P((x), T_FALSE))
86
+ #define STRING_P(x) (RB_TYPE_P((x), T_STRING))
87
+ #define ARRAY_P(x) (RB_TYPE_P((x), T_ARRAY))
88
+ #define FILE_P(x) (RB_TYPE_P((x), T_FILE))
89
+
90
+ #define RVAL2CBOOL(x) (RTEST(x))
91
+ #define CBOOL2RVAL(x) ((x) ? Qtrue : Qfalse)
92
+
93
+ #define RVAL2CSTR(x) (NIL_P(x) ? NULL : StringValueCStr(x))
94
+ #define CSTR2RVAL(x) ((x) == NULL ? Qnil : rb_str_new2(x))
95
+
96
+ #define RARRAY_EMPTY rb_ary_new()
97
+ #define RARRAY_EMPTY_P(a) (RARRAY_LEN(a) == 0)
98
+ #define RARRAY_FIRST(a) (RARRAY_EMPTY_P(a) ? Qnil : rb_ary_entry((a), 0))
99
+
100
+ #if RUBY_API_VERSION_CODE > 20700
101
+ # define RVALUE_TYPE enum ruby_value_type
102
+ #else
103
+ # define RVALUE_TYPE int
104
+ #endif /* RUBY_API_VERSION_CODE > 20700 */
105
+
106
+ #if RUBY_API_VERSION_CODE >= 20700
107
+ # define HAVE_RUBY_GC_COMPACT 1
108
+ # define MAGIC_GC_MARK(x) rb_gc_mark_movable((x))
109
+ #else
110
+ # define MAGIC_GC_MARK(x) rb_gc_mark((x))
111
+ #endif /* RUBY_API_VERSION_CODE >= 20700 */
112
+
113
+ #define CLASS_NAME(o) (NIL_P((o)) ? "nil" : rb_obj_classname((o)))
114
+
115
+ #if !defined(T_INTEGER)
116
+ # define T_INTEGER rb_cInteger
117
+ #endif /* T_INTEGER */
118
+
119
+ #if !defined(HAVE_RB_IO_T)
120
+ # define rb_io_t OpenFile
121
+ #endif /* HAVE_RB_IO_T */
122
+
123
+ #if !defined(GetReadFile)
124
+ # define FPTR_TO_FD(p) ((p)->fd)
125
+ #else
126
+ # define FPTR_TO_FD(p) (fileno(GetReadFile(p)))
127
+ #endif /* GetReadFile */
128
+
129
+ #define NOGVL_FUNCTION (VALUE (*)(void *))
130
+
131
+ #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && \
132
+ defined(HAVE_RUBY_THREAD_H) && HAVE_RUBY_THREAD_H
133
+ # include <ruby/thread.h>
134
+ # define NOGVL(f, d) \
135
+ rb_thread_call_without_gvl((f), (d), RUBY_UBF_IO, NULL)
136
+ #elif defined(HAVE_RB_THREAD_BLOCKING_REGION)
137
+ # define NOGVL(f, d) \
138
+ rb_thread_blocking_region(NOGVL_FUNCTION(f), (d), RUBY_UBF_IO, NULL)
139
+ #else
140
+ # include <rubysig.h>
141
+ static inline VALUE
142
+ fake_blocking_region(VALUE (*f)(ANYARGS), void *data)
143
+ {
144
+ VALUE rv;
145
+
146
+ TRAP_BEG;
147
+ rv = f(data);
148
+ TRAP_END;
149
+
150
+ return rv;
151
+ }
152
+ # define NOGVL(f, d) \
153
+ fake_blocking_region(NOGVL_FUNCTION(f), (d))
154
+ #endif /* HAVE_RB_THREAD_CALL_WITHOUT_GVL
155
+ HAVE_RUBY_THREAD_H */
83
156
 
84
157
  #if defined(__cplusplus)
85
158
  }
86
159
  #endif
87
160
 
88
161
  #endif /* _COMMON_H */
89
-
90
- /* vim: set ts=8 sw=4 sts=2 et : */
data/ext/magic/extconf.rb CHANGED
@@ -1,115 +1,455 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- # :enddoc:
3
+ require 'find'
4
+ require 'mkmf'
5
+ require 'pathname'
6
+
7
+ # helpful constants
8
+ PACKAGE_ROOT_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
9
+
10
+ # The gem version constraint in the Rakefile is not respected at install time.
11
+ # Keep this version in sync with the one in the Rakefile !
12
+ REQUIRED_MINI_PORTILE_VERSION = "~> 2.5.0"
13
+
14
+ MAGIC_HELP_MESSAGE = <<~HELP
15
+ USAGE: ruby #{$0} [options]
16
+
17
+ Flags that are always valid:
18
+
19
+ --use-system-libraries
20
+ --enable-system-libraries
21
+ Use system libraries instead of building and using the packaged libraries.
22
+
23
+ --disable-system-libraries
24
+ Use the packaged libraries, and ignore the system libraries. This is the default on most
25
+ platforms, and overrides `--use-system-libraries` and the environment variable
26
+ `MAGIC_USE_SYSTEM_LIBRARIES`.
27
+
28
+ --disable-clean
29
+ Do not clean out intermediate files after successful build.
30
+
31
+ Flags only used when building and using the packaged libraries:
32
+
33
+ --disable-static
34
+ Do not statically link packaged libraries, instead use shared libraries.
35
+
36
+ --enable-cross-build
37
+ Enable cross-build mode. (You probably do not want to set this manually.)
38
+
39
+ Flags only used when using system libraries:
40
+
41
+ Related to libmagic:
42
+
43
+ --with-magic-dir=DIRECTORY
44
+ Look for libmagic headers and library in DIRECTORY.
45
+
46
+ --with-magic-lib=DIRECTORY
47
+ Look for libmagic library in DIRECTORY.
48
+
49
+ --with-magic-include=DIRECTORY
50
+ Look for libmagic headers in DIRECTORY.
51
+
52
+ Environment variables used:
53
+
54
+ CC
55
+ Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']`
56
+
57
+ CPPFLAGS
58
+ If this string is accepted by the C preprocessor, add it to the flags passed to the C preprocessor
59
+
60
+ CFLAGS
61
+ If this string is accepted by the compiler, add it to the flags passed to the compiler
62
+
63
+ LDFLAGS
64
+ If this string is accepted by the linker, add it to the flags passed to the linker
65
+
66
+ LIBS
67
+ Add this string to the flags passed to the linker
68
+ HELP
69
+
70
+ def process_recipe(name, version, static_p, cross_p)
71
+ require 'rubygems'
72
+ gem('mini_portile2', REQUIRED_MINI_PORTILE_VERSION)
73
+ require 'mini_portile2'
74
+ message("Using mini_portile version #{MiniPortile::VERSION}\n")
75
+
76
+ MiniPortile.new(name, version).tap do |recipe|
77
+ # Prefer host_alias over host in order to use i586-mingw32msvc as
78
+ # correct compiler prefix for cross build, but use host if not set.
79
+ recipe.host = RbConfig::CONFIG["host_alias"].empty? ? RbConfig::CONFIG["host"] : RbConfig::CONFIG["host_alias"]
80
+ recipe.target = File.join(PACKAGE_ROOT_DIR, "ports")
81
+ recipe.patch_files = Dir[File.join(PACKAGE_ROOT_DIR, "patches", name, "*.patch")].sort
82
+ recipe.configure_options << "--libdir=#{File.join(recipe.path, 'lib')}"
83
+
84
+ yield recipe
85
+
86
+ env = Hash.new do |hash, key|
87
+ hash[key] = (ENV[key]).to_s
88
+ end
89
+
90
+ recipe.configure_options.flatten!
91
+
92
+ recipe.configure_options.delete_if do |option|
93
+ case option
94
+ when /\A(\w+)=(.*)\z/
95
+ env[Regexp.last_match(1)] = if env.key?(Regexp.last_match(1))
96
+ concat_flags(env[Regexp.last_match(1)], Regexp.last_match(2))
97
+ else
98
+ Regexp.last_match(2)
99
+ end
100
+ true
101
+ else
102
+ false
103
+ end
104
+ end
105
+
106
+ recipe.configure_options = [
107
+ "--disable-silent-rules",
108
+ "--disable-dependency-tracking",
109
+ "--enable-fsect-man5"
110
+ ]
111
+
112
+ if static_p
113
+ recipe.configure_options += [
114
+ "--disable-shared",
115
+ "--enable-static",
116
+ ]
117
+ env["CFLAGS"] = concat_flags(env["CFLAGS"], "-fPIC")
118
+ else
119
+ recipe.configure_options += [
120
+ "--enable-shared",
121
+ "--disable-static",
122
+ ]
123
+ end
124
+
125
+ if cross_p
126
+ recipe.configure_options += [
127
+ "--target=#{recipe.host}",
128
+ "--host=#{recipe.host}",
129
+ ]
130
+ end
131
+
132
+ if RbConfig::CONFIG['target_cpu'] == 'universal'
133
+ %w[CFLAGS LDFLAGS].each do |key|
134
+ unless env[key].include?('-arch')
135
+ env[key] = concat_flags(env[key], RbConfig::CONFIG['ARCH_FLAG'])
136
+ end
137
+ end
138
+ end
139
+
140
+ recipe.configure_options += env.map do |key, value|
141
+ "#{key}=#{value.strip}"
142
+ end
143
+
144
+ checkpoint = "#{recipe.target}/#{recipe.name}-#{recipe.version}-#{recipe.host}.installed"
145
+
146
+ if File.exist?(checkpoint)
147
+ message("Building Ruby Magic with a packaged version of #{name}-#{version}.\n")
148
+ else
149
+ message(<<~EOM)
150
+ ---------- IMPORTANT NOTICE ----------
151
+ Building Ruby Magic with a packaged version of #{name}-#{version}.
152
+ Configuration options: #{recipe.configure_options.shelljoin}
153
+ EOM
154
+
155
+ unless recipe.patch_files.empty?
156
+ message("The following patches are being applied:\n")
157
+
158
+ recipe.patch_files.each do |patch|
159
+ message(" - %s\n" % File.basename(patch))
160
+ end
161
+ end
162
+
163
+ recipe.cook
164
+
165
+ FileUtils.touch(checkpoint)
166
+ end
167
+
168
+ recipe.activate
169
+ end
170
+ end
4
171
 
5
172
  #
6
- # extconf.rb
7
- #
8
- # Copyright 2013-2014 Krzysztof Wilczynski
9
- #
10
- # Licensed under the Apache License, Version 2.0 (the "License");
11
- # you may not use this file except in compliance with the License.
12
- # You may obtain a copy of the License at
173
+ # utility functions
13
174
  #
14
- # http://www.apache.org/licenses/LICENSE-2.0
175
+ def config_clean?
176
+ enable_config('clean', true)
177
+ end
178
+
179
+ def config_static?
180
+ default_static = !truffle?
181
+ enable_config("static", default_static)
182
+ end
183
+
184
+ def config_cross_build?
185
+ enable_config("cross-build")
186
+ end
187
+
188
+ def config_system_libraries?
189
+ enable_config("system-libraries", ENV.key?("MAGIC_USE_SYSTEM_LIBRARIES")) do |_, default|
190
+ arg_config('--use-system-libraries', default)
191
+ end
192
+ end
193
+
194
+ def darwin?
195
+ RbConfig::CONFIG['target_os'] =~ /darwin/
196
+ end
197
+
198
+ def windows?
199
+ RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/
200
+ end
201
+
202
+ def truffle?
203
+ ::RUBY_ENGINE == 'truffleruby'
204
+ end
205
+
206
+ def concat_flags(*args)
207
+ args.compact.join(" ")
208
+ end
209
+
210
+ def do_help
211
+ print(MAGIC_HELP_MESSAGE)
212
+ exit!(0)
213
+ end
214
+
215
+ def do_clean
216
+ root = Pathname(PACKAGE_ROOT_DIR)
217
+ pwd = Pathname(Dir.pwd)
218
+
219
+ # Skip if this is a development work tree
220
+ unless (root + '.git').exist?
221
+ message("Cleaning files only used during build.\n")
222
+
223
+ # (root + 'tmp') cannot be removed at this stage because
224
+ # libmagic.so is yet to be copied to lib.
225
+
226
+ # clean the ports build directory
227
+ Pathname.glob(pwd.join('tmp', '*', 'ports')) do |dir|
228
+ FileUtils.rm_rf(dir, verbose: true)
229
+ end
230
+
231
+ FileUtils.rm_rf(root + 'ports' + 'archives', verbose: true)
232
+
233
+ # Remove everything but share/ directory
234
+ remove_paths = %w[bin include]
235
+ remove_paths << 'lib' if config_static?
236
+
237
+ Pathname.glob(File.join(root, 'ports', '*', 'libmagic', '*')) do |dir|
238
+ remove_paths.each do |path|
239
+ remove_dir = File.join(dir, path)
240
+ FileUtils.rm_rf(remove_dir, verbose: true)
241
+ end
242
+ end
243
+ end
244
+
245
+ exit!(0)
246
+ end
247
+
15
248
  #
16
- # Unless required by applicable law or agreed to in writing, software
17
- # distributed under the License is distributed on an "AS IS" BASIS,
18
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
- # See the License for the specific language governing permissions and
20
- # limitations under the License.
249
+ # main
21
250
  #
251
+ do_help if arg_config('--help')
252
+ do_clean if arg_config('--clean')
22
253
 
23
- require 'mkmf'
254
+ if ENV['CC']
255
+ RbConfig::CONFIG['CC'] = RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC']
256
+ end
257
+
258
+ ENV['CC'] = RbConfig::CONFIG['CC']
259
+
260
+ append_cflags(ENV["CFLAGS"].split) if ENV["CFLAGS"]
261
+ append_cppflags(ENV["CPPFLAGS"].split) if ENV["CPPFLAGS"]
262
+ append_ldflags(ENV["LDFLAGS"].split) if ENV["LDFLAGS"]
263
+
264
+ $LIBS = concat_flags($LIBS, ENV["LIBS"]) if ENV["LIBS"]
265
+
266
+ if truffle?
267
+ $CPPFLAGS += " -DMAGIC_CUSTOM_CHECK_TYPE"
268
+ end
269
+
270
+ if config_system_libraries?
271
+ message "Building ruby-magic using system libraries.\n"
272
+
273
+ $CPPFLAGS += " -DMAGIC_SYSTEM_LIBRARIES"
274
+
275
+ dir_config('magic')
276
+ else
277
+ message "Building ruby-magic using packaged libraries.\n"
278
+
279
+ require 'yaml'
280
+ dependencies = YAML.load_file(File.join(PACKAGE_ROOT_DIR, "dependencies.yml"))
24
281
 
25
- RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
282
+ static_p = config_static?
283
+ message "Static linking is #{static_p ? 'enabled' : 'disabled'}.\n"
284
+ cross_build_p = config_cross_build?
285
+ message "Cross build is #{cross_build_p ? 'enabled' : 'disabled'}.\n"
286
+
287
+ libmagic_recipe = process_recipe('libmagic', dependencies["libmagic"]["version"], static_p, cross_build_p) do |recipe|
288
+ recipe.files = [{
289
+ url: "https://ruby-magic.s3.nl-ams.scw.cloud/file-#{recipe.version}.tar.gz",
290
+ sha256: dependencies["libmagic"]["sha256"],
291
+ }]
292
+ end
293
+
294
+ $LIBPATH = [File.join(libmagic_recipe.path, 'lib')]
295
+ $CFLAGS << " -I#{File.join(libmagic_recipe.path, 'include')} "
296
+ $LDFLAGS += " -Wl,-rpath,#{libmagic_recipe.path}/lib"
297
+
298
+ if static_p
299
+ ENV['PKG_CONFIG_PATH'] = "#{libmagic_recipe.path}/lib/pkgconfig"
300
+ # mkmf appends -- to the first option
301
+ $LIBS += " " + pkg_config('libmagic', 'libs --static')
302
+ $LIBS += " " + File.join(libmagic_recipe.path, 'lib', "libmagic.#{$LIBEXT}")
303
+ end
304
+ end
305
+
306
+ $CFLAGS += ' -std=c99'
307
+
308
+ if RbConfig::CONFIG['CC'] =~ /gcc/
309
+ $CFLAGS += ' -O3' unless $CFLAGS =~ /-O\d/
310
+ end
311
+
312
+ %w[
313
+ -Wcast-qual
314
+ -Wwrite-strings
315
+ -Wconversion
316
+ -Wmissing-noreturn
317
+ -Winline
318
+ ].select do |flag|
319
+ try_link('int main(void) { return 0; }', flag)
320
+ end.each do |flag|
321
+ $CFLAGS += " " + flag
322
+ end
26
323
 
27
- $LDFLAGS << " %s" % ENV['LDFLAGS'] if ENV['LDFLAGS']
324
+ unless darwin?
325
+ $LDFLAGS += ' -Wl,--as-needed -Wl,--exclude-libs,ALL'
326
+ end
28
327
 
29
- $CFLAGS << " %s" % ENV['CFLAGS'] if ENV['CFLAGS']
30
- $CFLAGS << ' -std=c99 -g -Wall -Wextra -pedantic'
328
+ if windows?
329
+ $LDFLAGS += ' -static-libgcc'
330
+ end
31
331
 
32
332
  unless have_header('ruby.h')
33
- abort <<-EOS
333
+ abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
334
+ You appear to be missing Ruby development libraries and/or header
335
+ files. You can install missing compile-time dependencies in one of
336
+ the following ways:
337
+
338
+ - Debian / Ubuntu
339
+
340
+ apt-get install ruby-dev
341
+
342
+ - Red Hat / CentOS / Fedora
34
343
 
35
- You appear to be missing Ruby development libraries and/or header
36
- files. You can install missing compile-time dependencies in one of
37
- the following ways:
344
+ yum install ruby-devel or dnf install ruby-devel
38
345
 
39
- - Debian / Ubuntu
346
+ - Mac OS X (Darwin)
40
347
 
41
- apt-get install ruby-dev
348
+ brew install ruby (for Homebrew, see https://brew.sh)
349
+ port install ruby2.6 (for MacPorts, see https://www.macports.org)
42
350
 
43
- - Red Hat / CentOS / Fedora
351
+ - OpenBSD / NetBSD
44
352
 
45
- yum install ruby-devel
353
+ pkg_add ruby (for pkgsrc, see https://www.pkgsrc.org)
46
354
 
355
+ - FreeBSD
47
356
 
48
- Alternatively, you can use either of the following Ruby version
49
- managers in order to install Ruby locally (for your user only)
50
- and/or system-wide:
357
+ pkg install ruby (for FreeBSD Ports, see https://www.freebsd.org/ports)
51
358
 
52
- - Ruby Version Manager (for RVM, see http://rvm.io/)
53
- - Ruby Environment (for rbenv, see http://github.com/sstephenson/rbenv)
359
+ Alternatively, you can use either of the following Ruby version
360
+ managers in order to install Ruby locally (for your user only)
361
+ and/or system-wide:
54
362
 
363
+ - Ruby Version Manager (for RVM, see https://rvm.io)
364
+ - Ruby Environment (for rbenv, see https://github.com/sstephenson/rbenv)
365
+ - Change Ruby (for chruby, see https://github.com/postmodern/chruby)
366
+
367
+ More information about how to install Ruby on various platforms
368
+ available at the following web site:
369
+
370
+ https://www.ruby-lang.org/en/documentation/installation
55
371
  EOS
56
372
  end
57
373
 
374
+ have_func('rb_thread_call_without_gvl')
375
+ have_func('rb_thread_blocking_region')
376
+
58
377
  unless have_header('magic.h')
59
- abort <<-EOS
378
+ abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
379
+ You appear to be missing libmagic(3) library and/or necessary header
380
+ files. You can install missing compile-time dependencies in one of
381
+ the following ways:
60
382
 
61
- You appear to be missing libmagic(3) library and/or necessary header
62
- files. You can install missing compile-time dependencies in one of
63
- the following ways:
383
+ - Debian / Ubuntu
64
384
 
65
- - Debian / Ubuntu
385
+ apt-get install libmagic-dev
66
386
 
67
- apt-get install libmagic-dev
387
+ - Red Hat / CentOS / Fedora
68
388
 
69
- - Red Hat / CentOS / Fedora
389
+ yum install file-devel or dns install file-devel
70
390
 
71
- yum install file-devel
391
+ - Mac OS X (Darwin)
72
392
 
73
- - Mac OS X (Darwin)
393
+ brew install libmagic (for Homebrew, see https://brew.sh)
394
+ port install libmagic (for MacPorts, see https://www.macports.org)
74
395
 
75
- brew install libmagic (for Homebrew, see http://brew.sh/)
76
- port install libmagic (for MacPorts, see http://www.macports.org/)
396
+ - OpenBSD / NetBSD
77
397
 
398
+ pkg_add file (for pkgsrc, see https://www.pkgsrc.org)
78
399
 
79
- Alternatively, you can download recent release of the file(1) package
80
- from the following web site and attempt to compile libmagic(3) manually:
400
+ - FreeBSD
81
401
 
82
- http://www.darwinsys.com/file/
402
+ pkg install file (for FreeBSD Ports, see https://www.freebsd.org/ports)
83
403
 
404
+ Alternatively, you can download recent release of the file(1) package
405
+ from the following web site and attempt to compile libmagic(3) manually:
406
+
407
+ https://www.darwinsys.com/file
84
408
  EOS
85
409
  end
86
410
 
87
- unless have_library('magic', 'magic_getpath')
88
- abort <<-EOS
89
-
90
- Your version of libmagic(3) appears to be too old. Please, consider
91
- upgrading to at least version 5.09 or newer if possible ...
411
+ have_library('magic')
92
412
 
93
- For more information about file(1) command and libmagic(3) please
94
- visit the following web site:
413
+ unless have_func('magic_getpath')
414
+ abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
415
+ Your version of libmagic(3) appears to be too old.
95
416
 
96
- http://www.darwinsys.com/file/
417
+ Please, consider upgrading to at least version 5.29 or newer,
418
+ if possible. For more information about file(1) command and
419
+ libmagic(3) please visit the following web site:
97
420
 
421
+ https://www.darwinsys.com/file
98
422
  EOS
99
423
  end
100
424
 
101
- have_func('magic_version', 'magic.h')
102
-
103
- have_func('utime', ['utime.h', 'sys/types.h'])
104
- have_func('utimes', 'sys/time.h')
425
+ have_func('magic_getflags')
105
426
 
106
- have_func('rb_thread_call_without_gvl')
107
- have_func('rb_thread_blocking_region')
427
+ %w[
428
+ utime.h
429
+ sys/types.h
430
+ sys/time.h
431
+ ].each do |h|
432
+ have_header(h)
433
+ end
108
434
 
109
- dir_config('magic')
435
+ %w[
436
+ utime
437
+ utimes
438
+ ].each do |f|
439
+ have_func(f)
440
+ end
110
441
 
111
442
  create_header
112
443
  create_makefile('magic/magic')
113
444
 
114
- # vim: set ts=2 sw=2 sts=2 et :
115
- # encoding: utf-8
445
+ if config_clean?
446
+ # Do not clean if run in a development work tree.
447
+ File.open('Makefile', 'at') do |mk|
448
+ mk.print(<<~EOF)
449
+
450
+ all: clean-ports
451
+ clean-ports: $(DLLIB)
452
+ \t-$(Q)$(RUBY) $(srcdir)/extconf.rb --clean --#{static_p ? 'enable' : 'disable'}-static
453
+ EOF
454
+ end
455
+ end