Module: FormulaCellarChecks

Included in:
FormulaInstaller, Homebrew::FormulaAuditor
Defined in:
brew/Library/Homebrew/formula_cellar_checks.rb,
brew/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb,
brew/Library/Homebrew/extend/os/linux/formula_cellar_checks.rb

Constant Summary collapse

VALID_LIBRARY_EXTENSIONS =
%w[.a .jnilib .la .o .so .jar .prl .pm .sh].freeze

Instance Method Summary collapse

Instance Method Details

#audit_installedObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 168

def audit_installed
  @new_formula ||= false

  problem_if_output(check_manpages)
  problem_if_output(check_infopages)
  problem_if_output(check_jars)
  problem_if_output(check_non_libraries) if @new_formula
  problem_if_output(check_non_executables(formula.bin))
  problem_if_output(check_generic_executables(formula.bin))
  problem_if_output(check_non_executables(formula.sbin))
  problem_if_output(check_generic_executables(formula.sbin))
  problem_if_output(check_easy_install_pth(formula.lib))
  problem_if_output(check_elisp_dirname(formula.share, formula.name))
  problem_if_output(check_elisp_root(formula.share, formula.name))
end

#check_easy_install_pth(lib) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 118

def check_easy_install_pth(lib)
  pth_found = Dir["#{lib}/python{2.7,3}*/site-packages/easy-install.pth"].map { |f| File.dirname(f) }
  return if pth_found.empty?

  <<~EOS
    easy-install.pth files were found
    These .pth files are likely to cause link conflicts. Please invoke
    setup.py using Language::Python.setup_install_args.
    The offending files are:
      #{pth_found * "\n        "}
  EOS
end

#check_elisp_dirname(share, name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 131

def check_elisp_dirname(share, name)
  return unless (share/"emacs/site-lisp").directory?
  # Emacs itself can do what it wants
  return if name == "emacs"

  bad_dir_name = (share/"emacs/site-lisp").children.any? do |child|
    child.directory? && child.basename.to_s != name
  end

  return unless bad_dir_name

  <<~EOS
    Emacs Lisp files were installed into the wrong site-lisp subdirectory.
    They should be installed into:
      #{share}/emacs/site-lisp/#{name}
  EOS
end

#check_elisp_root(share, name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 149

def check_elisp_root(share, name)
  return unless (share/"emacs/site-lisp").directory?
  # Emacs itself can do what it wants
  return if name == "emacs"

  elisps = (share/"emacs/site-lisp").children.select { |file| %w[.el .elc].include? file.extname }
  return if elisps.empty?

  <<~EOS
    Emacs Lisp files were linked directly to #{HOMEBREW_PREFIX}/share/emacs/site-lisp
    This may cause conflicts with other packages.
    They should instead be installed into:
      #{share}/emacs/site-lisp/#{name}

    The offending files are:
      #{elisps * "\n        "}
  EOS
end

#check_env_path(bin) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 6

def check_env_path(bin)
  # warn the user if stuff was installed outside of their PATH
  return unless bin.directory?
  return if bin.children.empty?

  prefix_bin = (HOMEBREW_PREFIX/bin.basename)
  return unless prefix_bin.directory?

  prefix_bin = prefix_bin.realpath
  return if ORIGINAL_PATHS.include? prefix_bin

  <<~EOS
    #{prefix_bin} is not in your PATH
    You can amend this by altering your #{Utils::Shell.profile} file.
  EOS
end

#check_generic_executables(bin) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 100

def check_generic_executables(bin)
  return unless bin.directory?

  generic_names = %w[run service start stop]
  generics = bin.children.select { |g| generic_names.include? g.basename.to_s }
  return if generics.empty?

  <<~EOS
    Generic binaries were installed to "#{bin}"
    Binaries with generic names are likely to conflict with other software,
    and suggest that this software should be installed to "libexec" and then
    symlinked as needed.

    The offending files are:
      #{generics * "\n        "}
  EOS
end

#check_infopagesObject



34
35
36
37
38
39
40
41
42
43
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 34

def check_infopages
  # Check for info pages that aren't in share/info
  return unless (formula.prefix/"info").directory?

  <<~EOS
    A top-level "info" directory was found
    Homebrew suggests that info pages live under share.
    This can often be fixed by passing "--infodir=\#{info}" to configure.
  EOS
end

#check_jarsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 45

def check_jars
  return unless formula.lib.directory?

  jars = formula.lib.children.select { |g| g.extname == ".jar" }
  return if jars.empty?

  <<~EOS
    JARs were installed to "#{formula.lib}"
    Installing JARs to "lib" can cause conflicts between packages.
    For Java software, it is typically better for the formula to
    install to "libexec" and then symlink or wrap binaries into "bin".
    See "activemq", "jruby", etc. for examples.
    The offending files are:
      #{jars * "\n        "}
  EOS
end

#check_linkageObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'brew/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb', line 68

def check_linkage
  return unless formula.prefix.directory?

  keg = Keg.new(formula.prefix)

  CacheStoreDatabase.use(:linkage) do |db|
    checker = LinkageChecker.new(keg, formula, cache_db: db)
    next unless checker.broken_library_linkage?

    output = <<~EOS
      #{formula} has broken dynamic library links:
        #{checker.display_test_output}
    EOS

    tab = Tab.for_keg(keg)
    if tab.poured_from_bottle
      output += <<~EOS
        Rebuild this from source with:
          brew reinstall --build-from-source #{formula}
        If that's successful, file an issue#{formula.tap ? " here:\n  #{formula.tap.issues_url}" : "."}
      EOS
    end
    problem_if_output output
  end
end

#check_manpagesObject



23
24
25
26
27
28
29
30
31
32
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 23

def check_manpages
  # Check for man pages that aren't in share/man
  return unless (formula.prefix/"man").directory?

  <<~EOS
    A top-level "man" directory was found
    Homebrew requires that man pages live under share.
    This can often be fixed by passing "--mandir=\#{man}" to configure.
  EOS
end

#check_non_executables(bin) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 87

def check_non_executables(bin)
  return unless bin.directory?

  non_exes = bin.children.select { |g| g.directory? || !g.executable? }
  return if non_exes.empty?

  <<~EOS
    Non-executables were installed to "#{bin}"
    The offending files are:
      #{non_exes * "\n  "}
  EOS
end

#check_non_librariesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 69

def check_non_libraries
  return unless formula.lib.directory?

  non_libraries = formula.lib.children.reject do |g|
    next true if g.directory?

    valid_library_extension? g
  end
  return if non_libraries.empty?

  <<~EOS
    Non-libraries were installed to "#{formula.lib}"
    Installing non-libraries to "lib" is discouraged.
    The offending files are:
      #{non_libraries * "\n        "}
  EOS
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'brew/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb', line 32

def check_openssl_links
  return unless formula.prefix.directory?

  keg = Keg.new(formula.prefix)
  system_openssl = keg.mach_o_files.select do |obj|
    dlls = obj.dynamically_linked_libraries
    dlls.any? { |dll| %r{/usr/lib/lib(crypto|ssl|tls)\..*dylib}.match dll }
  end
  return if system_openssl.empty?

  <<~EOS
    object files were linked against system openssl
    These object files were linked against the deprecated system OpenSSL or
    the system's private LibreSSL.
    Adding `depends_on "openssl"` to the formula may help.
      #{system_openssl * "\n        "}
  EOS
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'brew/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb', line 51

def check_python_framework_links(lib)
  python_modules = Pathname.glob lib/"python*/site-packages/**/*.so"
  framework_links = python_modules.select do |obj|
    dlls = obj.dynamically_linked_libraries
    dlls.any? { |dll| /Python\.framework/.match dll }
  end
  return if framework_links.empty?

  <<~EOS
    python modules have explicit framework links
    These python extension modules were linked directly to a Python
    framework binary. They should be linked with -undefined dynamic_lookup
    instead of -lpython or -framework Python.
      #{framework_links * "\n        "}
  EOS
end

#check_shadowed_headersObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'brew/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb', line 7

def check_shadowed_headers
  return if ["libtool", "subversion", "berkeley-db"].any? do |formula_name|
    formula.name.start_with?(formula_name)
  end

  return if formula.name =~ Version.formula_optionally_versioned_regex(:php)

  return if MacOS.version < :mavericks && formula.name.start_with?("postgresql")
  return if MacOS.version < :yosemite  && formula.name.start_with?("memcached")

  return if formula.keg_only? || !formula.include.directory?

  files  = relative_glob(formula.include, "**/*.h")
  files &= relative_glob("#{MacOS.sdk_path}/usr/include", "**/*.h")
  files.map! { |p| File.join(formula.include, p) }

  return if files.empty?

  <<~EOS
    Header files that shadow system header files were installed to "#{formula.include}"
    The offending files are:
      #{files * "\n        "}
  EOS
end

#generic_audit_installedObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 183

def audit_installed
  @new_formula ||= false

  problem_if_output(check_manpages)
  problem_if_output(check_infopages)
  problem_if_output(check_jars)
  problem_if_output(check_non_libraries) if @new_formula
  problem_if_output(check_non_executables(formula.bin))
  problem_if_output(check_generic_executables(formula.bin))
  problem_if_output(check_non_executables(formula.sbin))
  problem_if_output(check_generic_executables(formula.sbin))
  problem_if_output(check_easy_install_pth(formula.lib))
  problem_if_output(check_elisp_dirname(formula.share, formula.name))
  problem_if_output(check_elisp_root(formula.share, formula.name))
end

#generic_valid_library_extension?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 67

def valid_library_extension?(filename)
  VALID_LIBRARY_EXTENSIONS.include? filename.extname
end

#valid_library_extension?(filename) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'brew/Library/Homebrew/formula_cellar_checks.rb', line 64

def valid_library_extension?(filename)
  VALID_LIBRARY_EXTENSIONS.include? filename.extname
end