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