xref: /netbsd-src/external/bsd/tcpdump/dist/cmake/Modules/FindCRYPTO.cmake (revision c41df9f6167ea7cd2f761f0a97783c8267cb8847)
1d881c474Schristos#
2d881c474Schristos# Try to find libcrypto.
3d881c474Schristos#
4d881c474Schristos
5*c41df9f6Schristos#
6*c41df9f6Schristos# Were we told where to look for libcrypto?
7*c41df9f6Schristos#
8*c41df9f6Schristosif(NOT CRYPTO_ROOT)
9*c41df9f6Schristos  #
10*c41df9f6Schristos  # No.
11*c41df9f6Schristos  #
12*c41df9f6Schristos  # First, try looking for it with pkg-config, if we have it.
13*c41df9f6Schristos  #
14*c41df9f6Schristos  find_package(PkgConfig)
15d881c474Schristos
16*c41df9f6Schristos  #
17*c41df9f6Schristos  # Homebrew's pkg-config does not, by default, look for
18*c41df9f6Schristos  # pkg-config files for packages it has installed.
19*c41df9f6Schristos  # Furthermore, at least for OpenSSL, they appear to be
20*c41df9f6Schristos  # dumped in package-specific directories whose paths are
21*c41df9f6Schristos  # not only package-specific but package-version-specific.
22*c41df9f6Schristos  #
23*c41df9f6Schristos  # So the only way to find openssl is to get the value of
24*c41df9f6Schristos  # PKG_CONFIG_PATH from "brew --env openssl" and add that
25*c41df9f6Schristos  # to PKG_CONFIG_PATH.  (No, we can't just assume it's under
26*c41df9f6Schristos  # /usr/local; Homebrew have conveniently chosen to put it
27*c41df9f6Schristos  # under /opt/homebrew on ARM.)
28*c41df9f6Schristos  #
29*c41df9f6Schristos  # That's the nice thing about Homebrew - it makes things easier!
30*c41df9f6Schristos  # Thanks!
31*c41df9f6Schristos  #
32*c41df9f6Schristos  find_program(BREW brew)
33*c41df9f6Schristos  if(BREW)
34*c41df9f6Schristos    #
35*c41df9f6Schristos    # We have Homebrew.
36*c41df9f6Schristos    # Get the pkg-config directory for openssl.
37*c41df9f6Schristos    #
38*c41df9f6Schristos    execute_process(COMMAND "${BREW}" "--env" "--plain" "openssl"
39*c41df9f6Schristos      RESULT_VARIABLE BREW_RESULT
40*c41df9f6Schristos      OUTPUT_VARIABLE BREW_OUTPUT
41*c41df9f6Schristos      OUTPUT_STRIP_TRAILING_WHITESPACE
42*c41df9f6Schristos    )
43*c41df9f6Schristos    if(BREW_RESULT EQUAL 0)
44*c41df9f6Schristos      #
45*c41df9f6Schristos      # brew --env --plain openssl succeeded.
46*c41df9f6Schristos      # Split its output into a list, one entry per line.
47*c41df9f6Schristos      #
48*c41df9f6Schristos      string(REGEX MATCHALL "[^\n\r]+" BREW_OUTPUT_LINES "${BREW_OUTPUT}")
49*c41df9f6Schristos
50*c41df9f6Schristos      #
51*c41df9f6Schristos      # Find the line that begins with "PKG_CONFIG_PATH: ", and extract
52*c41df9f6Schristos      # the path following that.
53*c41df9f6Schristos      #
54*c41df9f6Schristos      foreach(LINE IN LISTS BREW_OUTPUT_LINES)
55*c41df9f6Schristos        if(LINE MATCHES "PKG_CONFIG_PATH: \(.*\)")
56*c41df9f6Schristos          string(REGEX REPLACE "PKG_CONFIG_PATH: \(.*\)"
57*c41df9f6Schristos              "\\1" OPENSSL_PKGCONFIG_DIR
58*c41df9f6Schristos              ${LINE})
59*c41df9f6Schristos        endif()
60*c41df9f6Schristos      endforeach()
61*c41df9f6Schristos    endif()
62*c41df9f6Schristos  endif()
63*c41df9f6Schristos
64*c41df9f6Schristos  #
65*c41df9f6Schristos  # Save the current value of the PKG_CONFIG_PATH environment
66*c41df9f6Schristos  # variable.
67*c41df9f6Schristos  #
68*c41df9f6Schristos  set(SAVE_PKG_CONFIG_PATH $ENV{PKG_CONFIG_PATH})
69*c41df9f6Schristos
70*c41df9f6Schristos  #
71*c41df9f6Schristos  # If we got an additional pkg-config directory from Homebrew, add
72*c41df9f6Schristos  # it to the PKG_CONFIG_PATH environment variable.
73*c41df9f6Schristos  #
74*c41df9f6Schristos  if(OPENSSL_PKGCONFIG_DIR)
75*c41df9f6Schristos    set(ENV{PKG_CONFIG_PATH} "${OPENSSL_PKGCONFIG_DIR}:$ENV{PKG_CONFIG_PATH}")
76*c41df9f6Schristos  endif()
77*c41df9f6Schristos
78*c41df9f6Schristos  #
79*c41df9f6Schristos  # Use pkg-config to find libcrypto.
80*c41df9f6Schristos  #
81*c41df9f6Schristos  pkg_check_modules(CRYPTO libcrypto)
82*c41df9f6Schristos
83*c41df9f6Schristos  #
84*c41df9f6Schristos  # Revert the change to PKG_CONFIG_PATH.
85*c41df9f6Schristos  #
86*c41df9f6Schristos  set(ENV{PKG_CONFIG_PATH} "${SAVE_PKG_CONFIG_PATH}")
87*c41df9f6Schristos
88*c41df9f6Schristos  #
89*c41df9f6Schristos  # Did pkg-config find it?
90*c41df9f6Schristos  #
91*c41df9f6Schristos  if(CRYPTO_FOUND)
92*c41df9f6Schristos    #
93*c41df9f6Schristos    # This "helpfully" supplies CRYPTO_LIBRARIES as a bunch of
94*c41df9f6Schristos    # library names - not paths - and CRYPTO_LIBRARY_DIRS as
95*c41df9f6Schristos    # a bunch of directories.
96*c41df9f6Schristos    #
97*c41df9f6Schristos    # CMake *really* doesn't like the notion of specifying "here are
98*c41df9f6Schristos    # the directories in which to look for libraries" except in
99*c41df9f6Schristos    # find_library() calls; it *really* prefers using full paths to
100*c41df9f6Schristos    # library files, rather than library names.
101*c41df9f6Schristos    #
102*c41df9f6Schristos    # Find the libraries and add their full paths.
103*c41df9f6Schristos    #
104*c41df9f6Schristos    set(CRYPTO_LIBRARY_FULLPATHS)
105*c41df9f6Schristos    foreach(_lib IN LISTS CRYPTO_LIBRARIES)
106*c41df9f6Schristos      #
107*c41df9f6Schristos      # Try to find this library, so we get its full path.
108*c41df9f6Schristos      #
109*c41df9f6Schristos      find_library(_libfullpath ${_lib} HINTS ${CRYPTO_LIBRARY_DIRS})
110*c41df9f6Schristos      list(APPEND CRYPTO_LIBRARY_FULLPATHS ${_libfullpath})
111*c41df9f6Schristos    endforeach()
112*c41df9f6Schristos    set(CRYPTO_LIBRARIES "${CRYPTO_LIBRARY_FULLPATHS}")
113*c41df9f6Schristos  else()
114*c41df9f6Schristos    #
115*c41df9f6Schristos    # No.  If we have Homebrew installed, see if it's in Homebrew.
116*c41df9f6Schristos    #
117*c41df9f6Schristos    if(BREW)
118*c41df9f6Schristos      #
119*c41df9f6Schristos      # The brew man page lies when it speaks of
120*c41df9f6Schristos      # $BREW --prefix --installed <formula>
121*c41df9f6Schristos      # outputting nothing.  In Homebrew 3.3.16,
122*c41df9f6Schristos      # it produces output regardless of whether
123*c41df9f6Schristos      # the formula is installed or not, so we
124*c41df9f6Schristos      # send the standard output and error to
125*c41df9f6Schristos      # the bit bucket.
126*c41df9f6Schristos      #
127*c41df9f6Schristos      # libcrypto isn't a formula, openssl is a formula.
128*c41df9f6Schristos      #
129*c41df9f6Schristos      execute_process(COMMAND "${BREW}" "--prefix" "--installed" "openssl"
130*c41df9f6Schristos        RESULT_VARIABLE BREW_RESULT
131*c41df9f6Schristos        OUTPUT_QUIET
132*c41df9f6Schristos      )
133*c41df9f6Schristos      if(BREW_RESULT EQUAL 0)
134*c41df9f6Schristos        #
135*c41df9f6Schristos        # Yes.  Get the include directory and library
136*c41df9f6Schristos        # directory.  (No, we can't just assume it's
137*c41df9f6Schristos        # under /usr/local; Homebrew have conveniently
138*c41df9f6Schristos        # chosen to put it under /opt/homebrew on ARM.)
139*c41df9f6Schristos        #
140*c41df9f6Schristos        execute_process(COMMAND "${BREW}" "--prefix" "openssl"
141*c41df9f6Schristos          RESULT_VARIABLE BREW_RESULT
142*c41df9f6Schristos          OUTPUT_VARIABLE OPENSSL_PATH
143*c41df9f6Schristos          OUTPUT_STRIP_TRAILING_WHITESPACE
144*c41df9f6Schristos        )
145*c41df9f6Schristos        set(CRYPTO_INCLUDE_DIRS "${OPENSSL_PATH}/include")
146*c41df9f6Schristos
147*c41df9f6Schristos        #
148*c41df9f6Schristos        # Search for the libcrypto library under lib.
149*c41df9f6Schristos        #
150*c41df9f6Schristos        find_library(CRYPTO_LIBRARIES crypto
151*c41df9f6Schristos            PATHS "${OPENSSL_PATH}/lib"
152*c41df9f6Schristos            NO_DEFAULT_PATH)
153*c41df9f6Schristos      endif()
154*c41df9f6Schristos    endif()
155*c41df9f6Schristos  endif()
156*c41df9f6Schristosendif()
157*c41df9f6Schristos
158*c41df9f6Schristos#
159*c41df9f6Schristos# Have we found it with pkg-config or Homebrew?
160*c41df9f6Schristos#
161*c41df9f6Schristosif(NOT CRYPTO_INCLUDE_DIRS)
162*c41df9f6Schristos  #
163*c41df9f6Schristos  # No.
164*c41df9f6Schristos  # Try to find the openss/evp.h header.
165*c41df9f6Schristos  # We search for that header to make sure that it's installed (if
166*c41df9f6Schristos  # it's just a shared library for the benefit of existing
167*c41df9f6Schristos  # programs, that's not useful).
168*c41df9f6Schristos  #
169*c41df9f6Schristos  find_path(CRYPTO_INCLUDE_DIRS openssl/evp.h)
170*c41df9f6Schristos
171*c41df9f6Schristos  #
172*c41df9f6Schristos  # Try to find the library.
173*c41df9f6Schristos  #
174*c41df9f6Schristos  find_library(CRYPTO_LIBRARIES crypto)
175*c41df9f6Schristosendif()
176d881c474Schristos
177d881c474Schristosinclude(FindPackageHandleStandardArgs)
178d881c474Schristosfind_package_handle_standard_args(CRYPTO
179d881c474Schristos  DEFAULT_MSG
180*c41df9f6Schristos  CRYPTO_INCLUDE_DIRS
181*c41df9f6Schristos  CRYPTO_LIBRARIES
182d881c474Schristos)
183d881c474Schristos
184d881c474Schristosmark_as_advanced(
185*c41df9f6Schristos  CRYPTO_INCLUDE_DIRS
186*c41df9f6Schristos  CRYPTO_LIBRARIES
187d881c474Schristos)
188