Add StringInterner to FuncBuilder
[hiphop-php.git] / CMake / HPHPFindLibs.cmake
bloba82fb4bb7bb4fac431b4c46be412bfee964d9ad1
2 #   +----------------------------------------------------------------------+
3 #   | HipHop for PHP                                                       |
4 #   +----------------------------------------------------------------------+
5 #   | Copyright (c) 2010 Facebook, Inc. (http://www.facebook.com)          |
6 #   | Copyright (c) 1997-2010 The PHP Group                                |
7 #   +----------------------------------------------------------------------+
8 #   | This source file is subject to version 3.01 of the PHP license,      |
9 #   | that is bundled with this package in the file LICENSE, and is        |
10 #   | available through the world-wide-web at the following url:           |
11 #   | http://www.php.net/license/3_01.txt                                  |
12 #   | If you did not receive a copy of the PHP license and are unable to   |
13 #   | obtain it through the world-wide-web, please send a note to          |
14 #   | license@php.net so we can mail you a copy immediately.               |
15 #   +----------------------------------------------------------------------+
18 include(CheckFunctionExists)
20 # libdl
21 find_package(LibDL)
22 if (LIBDL_INCLUDE_DIRS)
23   add_definitions("-DHAVE_LIBDL")
24   include_directories(${LIBDL_INCLUDE_DIRS})
25   if (LIBDL_NEEDS_UNDERSCORE)
26     add_definitions("-DLIBDL_NEEDS_UNDERSCORE")
27   endif()
28 endif()
30 # google-glog
31 find_package(Glog REQUIRED)
32 if (GLOG_STATIC)
33   add_definitions("-DGOOGLE_GLOG_DLL_DECL=")
34 endif()
35 include_directories(${GLOG_INCLUDE_DIR})
37 # inotify checks
38 find_package(Libinotify)
39 if (LIBINOTIFY_INCLUDE_DIR)
40   include_directories(${LIBINOTIFY_INCLUDE_DIR})
41 endif()
43 # pcre checks
44 find_package(PCRE)
45 include_directories(${PCRE_INCLUDE_DIR})
47 # libevent checks
48 find_package(LibEvent REQUIRED)
49 include_directories(${LIBEVENT_INCLUDE_DIR})
51 set(CMAKE_REQUIRED_LIBRARIES "${LIBEVENT_LIB}")
52 CHECK_FUNCTION_EXISTS("evhttp_bind_socket_with_fd" HAVE_CUSTOM_LIBEVENT)
53 if(HAVE_CUSTOM_LIBEVENT)
54         message("Using custom LIBEVENT")
55         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_CUSTOM_LIBEVENT")
56 endif()
57 set(CMAKE_REQUIRED_LIBRARIES)
59 # libXed
60 if (ENABLE_XED)
61   find_package(LibXed)
62   if (LibXed_FOUND)
63     include_directories(${LibXed_INCLUDE_DIR})
64   endif()
65   add_definitions("-DHAVE_LIBXED")
66 else()
67   message(STATUS "XED is disabled")
68 endif()
70 # CURL checks
71 find_package(CURL REQUIRED)
72 include_directories(${CURL_INCLUDE_DIR})
73 if (CURL_STATIC)
74   add_definitions("-DCURL_STATICLIB")
75 endif()
77 set(CMAKE_REQUIRED_LIBRARIES "${CURL_LIBRARIES}")
78 CHECK_FUNCTION_EXISTS("curl_multi_select" HAVE_CURL_MULTI_SELECT)
79 CHECK_FUNCTION_EXISTS("curl_multi_wait" HAVE_CURL_MULTI_WAIT)
80 if (HAVE_CURL_MULTI_SELECT)
81   add_definitions("-DHAVE_CURL_MULTI_SELECT")
82 endif()
83 if (HAVE_CURL_MULTI_WAIT)
84   add_definitions("-DHAVE_CURL_MULTI_WAIT")
85 endif()
86 set(CMAKE_REQUIRED_LIBRARIES)
88 # LibXML2 checks
89 find_package(LibXml2 REQUIRED)
90 include_directories(${LIBXML2_INCLUDE_DIR})
91 add_definitions(${LIBXML2_DEFINITIONS})
93 # libsqlite3
94 find_package(LibSQLite REQUIRED)
95 if (LIBSQLITE3_INCLUDE_DIR)
96   include_directories(${LIBSQLITE3_INCLUDE_DIR})
97 endif ()
99 # fastlz
100 find_package(FastLZ)
101 if (FASTLZ_INCLUDE_DIR)
102   include_directories(${FASTLZ_INCLUDE_DIR})
103 endif()
105 # ICU
106 find_package(ICU REQUIRED)
107 if (ICU_FOUND)
108   if (ICU_VERSION VERSION_LESS "4.2")
109     unset(ICU_FOUND CACHE)
110     unset(ICU_INCLUDE_DIRS CACHE)
111     unset(ICU_LIBRARIES CACHE)
112     message(FATAL_ERROR "ICU is too old, found ${ICU_VERSION} and we need 4.2")
113   endif ()
114   include_directories(${ICU_INCLUDE_DIRS})
115   if (ICU_STATIC)
116     add_definitions("-DU_EXPORT=")
117     add_definitions("-DU_IMPORT=")
118   endif()
119   # Everything is either in the `icu61` namespace or `icu` namespace, depending
120   # on another definition. There's an implicit `using namespace WHATEVER;` in
121   # ICU4c < 61.1, but now that's opt-in rather than opt-out.
122   add_definitions("-DU_USING_ICU_NAMESPACE=1")
123 endif (ICU_FOUND)
125 # jemalloc/tmalloc and profiler
126 if (USE_GOOGLE_HEAP_PROFILER OR USE_GOOGLE_CPU_PROFILER)
127   FIND_LIBRARY(GOOGLE_PROFILER_LIB profiler)
128   FIND_PATH(GOOGLE_PROFILER_INCLUDE_DIR NAMES google/profiler.h)
129   if (GOOGLE_PROFILER_INCLUDE_DIR)
130     include_directories(${GOOGLE_PROFILER_INCLUDE_DIR})
131   endif()
132   if (GOOGLE_PROFILER_LIB)
133     message(STATUS "Found Google profiler: ${GOOGLE_PROFILER_LIB}")
134     if (USE_GOOGLE_CPU_PROFILER)
135       set(GOOGLE_CPU_PROFILER_ENABLED 1)
136     endif()
137   else()
138     message(STATUS "Can't find Google profiler")
139   endif()
140 endif()
142 if (USE_GOOGLE_HEAP_PROFILER AND GOOGLE_PROFILER_LIB)
143   FIND_LIBRARY(GOOGLE_TCMALLOC_FULL_LIB tcmalloc)
144   if (GOOGLE_TCMALLOC_FULL_LIB)
145     message(STATUS "Found full tcmalloc: ${GOOGLE_TCMALLOC_FULL_LIB}")
146     set(GOOGLE_HEAP_PROFILER_ENABLED 1)
147     set(GOOGLE_TCMALLOC_ENABLED 1)
148   else()
149     message(STATUS "Can't find full tcmalloc - heap profiling is disabled")
150   endif()
151 endif()
153 if(USE_JEMALLOC AND NOT GOOGLE_TCMALLOC_ENABLED)
154   add_definitions(-DUSE_JEMALLOC=1)
155   set(JEMALLOC_ENABLED 1)
156 else()
157   add_definitions(-DNO_JEMALLOC=1)
158 endif()
160 if (USE_TCMALLOC AND NOT JEMALLOC_ENABLED AND NOT GOOGLE_TCMALLOC_ENABLED)
161   FIND_LIBRARY(GOOGLE_TCMALLOC_MIN_LIB tcmalloc_minimal)
162   if (GOOGLE_TCMALLOC_MIN_LIB)
163     message(STATUS "Found minimal tcmalloc: ${GOOGLE_TCMALLOC_MIN_LIB}")
164     set(GOOGLE_TCMALLOC_ENABLED 1)
165   else()
166     message(STATUS "Can't find minimal tcmalloc")
167   endif()
168 endif()
170 if (GOOGLE_TCMALLOC_ENABLED)
171   add_definitions(-DGOOGLE_TCMALLOC=1)
172 else()
173   add_definitions(-DNO_TCMALLOC=1)
174 endif()
175 if (GOOGLE_HEAP_PROFILER_ENABLED)
176   add_definitions(-DGOOGLE_HEAP_PROFILER=1)
177 endif()
178 if (GOOGLE_CPU_PROFILER_ENABLED)
179   add_definitions(-DGOOGLE_CPU_PROFILER=1)
180 endif()
182 # HHProf
183 if (JEMALLOC_ENABLED AND ENABLE_HHPROF)
184   add_definitions(-DENABLE_HHPROF=1)
185 endif()
187 # tbb libs
188 find_package(TBB REQUIRED)
189 if (${TBB_INTERFACE_VERSION} LESS 5005)
190   unset(TBB_FOUND CACHE)
191   unset(TBB_INCLUDE_DIRS CACHE)
192   unset(TBB_LIBRARIES CACHE)
193   message(FATAL_ERROR "TBB is too old, please install at least 3.0(5005), preferably 4.0(6000) or higher")
194 endif()
195 include_directories(${TBB_INCLUDE_DIRS})
196 link_directories(${TBB_LIBRARY_DIRS})
198 # OpenSSL libs
199 find_package(OpenSSL REQUIRED)
200 include_directories(${OPENSSL_INCLUDE_DIR})
202 # LibreSSL explicitly refuses to support RAND_egd()
203 SET(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
204 SET(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
205 INCLUDE(CheckCXXSourceCompiles)
206 CHECK_CXX_SOURCE_COMPILES("#include <openssl/rand.h>
207 int main() {
208   return RAND_egd(\"/dev/null\");
209 }" OPENSSL_HAVE_RAND_EGD)
210 if (NOT OPENSSL_HAVE_RAND_EGD)
211   add_definitions("-DOPENSSL_NO_RAND_EGD")
212 endif()
213 CHECK_CXX_SOURCE_COMPILES("#include <openssl/ssl.h>
214 int main() {
215   return SSL_set_alpn_protos(nullptr, nullptr, 0);
216 }" OPENSSL_HAVE_ALPN)
217 SET(CMAKE_REQUIRED_INCLUDES)
218 SET(CMAKE_REQUIRED_LIBRARIES)
221 # ZLIB
222 find_package(ZLIB REQUIRED)
223 include_directories(${ZLIB_INCLUDE_DIR})
225 # libpthreads
226 find_package(PThread REQUIRED)
227 include_directories(${LIBPTHREAD_INCLUDE_DIRS})
228 if (LIBPTHREAD_STATIC)
229   add_definitions("-DPTW32_STATIC_LIB")
230 endif()
232 OPTION(
233   NON_DISTRIBUTABLE_BUILD
234   "Use libraries which may result in a binary that can not be legally distributed"
235   OFF
238 # Either Readline or Editline (for hphpd)
239 if(NON_DISTRIBUTABLE_BUILD)
240   find_package(Readline)
241 endif()
242 if (NOT READLINE_INCLUDE_DIR)
243   find_package(Editline)
244 endif()
246 if (NON_DISTRIBUTABLE_BUILD AND READLINE_INCLUDE_DIR)
247   if (READLINE_STATIC)
248     add_definitions("-DREADLINE_STATIC")
249   endif()
250   include_directories(${READLINE_INCLUDE_DIR})
251 elseif (EDITLINE_INCLUDE_DIRS)
252   add_definitions("-DUSE_EDITLINE")
253   include_directories(${EDITLINE_INCLUDE_DIRS})
254 else()
255   message(FATAL_ERROR "Could not find Readline or Editline")
256 endif()
258 if (NOT WINDOWS)
259   find_package(LibDwarf REQUIRED)
260   include_directories(${LIBDWARF_INCLUDE_DIRS})
261   if (LIBDWARF_CONST_NAME)
262     add_definitions("-DLIBDWARF_CONST_NAME")
263   endif()
264   if (LIBDWARF_USE_INIT_C)
265     add_definitions("-DLIBDWARF_USE_INIT_C")
266   endif()
268   find_package(LibElf REQUIRED)
269   include_directories(${LIBELF_INCLUDE_DIRS})
270   if (ELF_GETSHDRSTRNDX)
271     add_definitions("-DHAVE_ELF_GETSHDRSTRNDX")
272   endif()
273 endif()
275 FIND_LIBRARY(CRYPT_LIB NAMES xcrypt crypt crypto)
276 if (LINUX OR FREEBSD)
277   FIND_LIBRARY (RT_LIB rt)
278 endif()
280 if (LINUX)
281   FIND_LIBRARY (CAP_LIB cap)
283   if (NOT CAP_LIB)
284     message(FATAL_ERROR "You need to install libcap")
285   endif()
286 endif()
288 if (LINUX OR APPLE)
289   FIND_LIBRARY (DL_LIB dl)
290   FIND_LIBRARY (RESOLV_LIB resolv)
291 endif()
293 if (FREEBSD)
294   FIND_LIBRARY (EXECINFO_LIB execinfo)
295   if (NOT EXECINFO_LIB)
296     message(FATAL_ERROR "You need to install libexecinfo")
297   endif()
298 endif()
300 if (APPLE)
301   find_library(KERBEROS_LIB NAMES gssapi_krb5)
302 endif()
304 # This is required by Homebrew's libc. See
305 # https://github.com/facebook/hhvm/pull/5728#issuecomment-124290712
306 # for more info.
307 find_package(Libpam)
308 if (PAM_INCLUDE_PATH)
309   include_directories(${PAM_INCLUDE_PATH})
310 endif()
312 include_directories(${HPHP_HOME}/hphp)
314 macro(hphp_link target)
315   if (${ARGC} GREATER 1)
316     set(VISIBILITY "${ARGV1}")
317   else ()
318     # We actually want PUBLIC, but specifying PUBLIC is an error if there is
319     # another target_link_libraries(${target} ) without PUBLIC/PRIVATE/INTERFACE
320     # anywhere else, so keep this for now for backwards compatibility
321     set(VISIBILITY "")
322   endif ()
323   # oniguruma must be linked first for MacOS's linker to do the right thing -
324   # that's handled in HPHPSetup.cmake
325   #
326   # That only handles linking - we still need to make sure that:
327   # - oniguruma is built first, if needed (so we have the header files)
328   # - we build with the header files in the include path
329   if(APPLE)
330     if (NOT "${VISIBILITY}" STREQUAL "INTERFACE")
331       add_dependencies(${target} ${VISIBILITY} onig)
332       target_include_directories(${target} PRIVATE $<TARGET_PROPERTY:onig,INTERFACE_INCLUDE_DIRECTORIES>)
333     endif ()
334   else()
335     # Otherwise, the linker does the right thing, which sometimes means putting it after things that use it
336     target_link_libraries(${target} ${VISIBILITY} onig)
337   endif()
339   if (LIBDL_LIBRARIES)
340     target_link_libraries(${target} ${VISIBILITY} ${LIBDL_LIBRARIES})
341   endif ()
343   if (JEMALLOC_ENABLED)
344     target_link_libraries(${target} ${VISIBILITY} jemalloc)
345   endif ()
347   if (GOOGLE_HEAP_PROFILER_ENABLED OR GOOGLE_CPU_PROFILER_ENABLED)
348     target_link_libraries(${target} ${VISIBILITY} ${GOOGLE_PROFILER_LIB})
349   endif()
351   if (GOOGLE_HEAP_PROFILER_ENABLED)
352     target_link_libraries(${target} ${VISIBILITY} ${GOOGLE_TCMALLOC_FULL_LIB})
353   elseif (GOOGLE_TCMALLOC_ENABLED)
354     target_link_libraries(${target} ${VISIBILITY} ${GOOGLE_TCMALLOC_MIN_LIB})
355   endif()
357   target_link_libraries(${target} ${VISIBILITY} libsodium)
359   target_link_libraries(${target} ${VISIBILITY} ${PCRE_LIBRARY})
360   target_link_libraries(${target} ${VISIBILITY} ${ICU_DATA_LIBRARIES} ${ICU_I18N_LIBRARIES} ${ICU_LIBRARIES})
361   target_link_libraries(${target} ${VISIBILITY} ${LIBEVENT_LIB})
362   target_link_libraries(${target} ${VISIBILITY} ${CURL_LIBRARIES})
363   target_link_libraries(${target} ${VISIBILITY} glog::glog)
364   if (LIBJSONC_LIBRARY)
365     target_link_libraries(${target} ${VISIBILITY} ${LIBJSONC_LIBRARY})
366   endif()
368   if (LIBINOTIFY_LIBRARY)
369     target_link_libraries(${target} ${VISIBILITY} ${LIBINOTIFY_LIBRARY})
370   endif()
372   if (LINUX)
373     target_link_libraries(${target} ${VISIBILITY} ${CAP_LIB})
374   endif()
376   if (LINUX OR APPLE)
377     target_link_libraries(${target} ${VISIBILITY} ${RESOLV_LIB})
378     target_link_libraries(${target} ${VISIBILITY} ${DL_LIB})
379   endif()
381   if (FREEBSD)
382     target_link_libraries(${target} ${VISIBILITY} ${EXECINFO_LIB})
383   endif()
385   if (APPLE)
386     target_link_libraries(${target} ${VISIBILITY} ${LIBINTL_LIBRARIES})
387     target_link_libraries(${target} ${VISIBILITY} ${KERBEROS_LIB})
388   endif()
390   if (PAM_LIBRARY)
391     target_link_libraries(${target} ${VISIBILITY} ${PAM_LIBRARY})
392   endif()
394   if (LIBPTHREAD_LIBRARIES)
395     target_link_libraries(${target} ${VISIBILITY} ${LIBPTHREAD_LIBRARIES})
396   endif()
398   target_link_libraries(${target} ${VISIBILITY} ${TBB_LIBRARIES})
399   target_link_libraries(${target} ${VISIBILITY} ${OPENSSL_LIBRARIES})
400   target_link_libraries(${target} ${VISIBILITY} ${ZLIB_LIBRARIES})
402   target_link_libraries(${target} ${VISIBILITY} ${LIBXML2_LIBRARIES})
404   target_link_libraries(${target} ${VISIBILITY} ${LBER_LIBRARIES})
406   if (CRYPT_LIB)
407     target_link_libraries(${target} ${VISIBILITY} ${CRYPT_LIB})
408   endif()
410   if (LINUX OR FREEBSD)
411     target_link_libraries(${target} ${VISIBILITY} ${RT_LIB})
412   endif()
414   if (LIBSQLITE3_FOUND AND LIBSQLITE3_LIBRARY)
415     target_link_libraries(${target} ${VISIBILITY} ${LIBSQLITE3_LIBRARY})
416   else()
417     target_link_libraries(${target} ${VISIBILITY} sqlite3)
418   endif()
420   target_link_libraries(${target} ${VISIBILITY} lz4)
421   target_link_libraries(${target} ${VISIBILITY} libzip)
423   if (PCRE_LIBRARY)
424     target_link_libraries(${target} ${VISIBILITY} ${PCRE_LIBRARY})
425   else()
426     target_link_libraries(${target} ${VISIBILITY} pcre)
427   endif()
429   if (LIBFASTLZ_LIBRARY)
430     target_link_libraries(${target} ${VISIBILITY} ${LIBFASTLZ_LIBRARY})
431   else()
432     target_link_libraries(${target} ${VISIBILITY} fastlz)
433   endif()
435   target_link_libraries(${target} ${VISIBILITY} timelib)
436   target_link_libraries(${target} ${VISIBILITY} folly)
437   target_link_libraries(${target} ${VISIBILITY} jemalloc)
438   target_link_libraries(${target} ${VISIBILITY} wangle)
439   target_link_libraries(${target} ${VISIBILITY} brotli)
440   target_link_libraries(${target} ${VISIBILITY} hhbc_ast_header)
441   target_link_libraries(${target} ${VISIBILITY} compiler_ffi)
442   target_link_libraries(${target} ${VISIBILITY} parser_ffi)
443   target_link_libraries(${target} ${VISIBILITY} hhvm_types_ffi)
444   target_link_libraries(${target} ${VISIBILITY} hhvm_hhbc_defs_ffi)
446   if (NOT MSVC)
447     target_link_libraries(${target} ${VISIBILITY} afdt)
448   endif()
449   target_link_libraries(${target} ${VISIBILITY} mbfl)
451   if (EDITLINE_LIBRARIES)
452     target_link_libraries(${target} ${VISIBILITY} ${EDITLINE_LIBRARIES})
453   elseif (READLINE_LIBRARY)
454     target_link_libraries(${target} ${VISIBILITY} ${READLINE_LIBRARY})
455   endif()
457   if (MSVC)
458     target_link_libraries(${target} ${VISIBILITY} dbghelp.lib dnsapi.lib)
459   endif()
461 # Check whether atomic operations require -latomic or not
462 # See https://github.com/facebook/hhvm/issues/5217
463   include(CheckCXXSourceCompiles)
464   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
465   set(CMAKE_REQUIRED_FLAGS "-std=c++1y")
466   CHECK_CXX_SOURCE_COMPILES("
467 #include <atomic>
468 #include <iostream>
469 #include <stdint.h>
470 int main() {
471     struct Test { int64_t val1; int64_t val2; };
472     std::atomic<Test> s;
473     // Do this to stop modern compilers from optimizing away the libatomic
474     // calls in release builds, making this test always pass in release builds,
475     // and incorrectly think that HHVM doesn't need linking against libatomic.
476     bool (std::atomic<Test>::* volatile x)(void) const =
477       &std::atomic<Test>::is_lock_free;
478     std::cout << (s.*x)() << std::endl;
480   " NOT_REQUIRE_ATOMIC_LINKER_FLAG)
482   if(NOT "${NOT_REQUIRE_ATOMIC_LINKER_FLAG}")
483       message(STATUS "-latomic is required to link hhvm")
484       find_library(ATOMIC_LIBRARY NAMES atomic libatomic.so.1)
485       if (ATOMIC_LIBRARY STREQUAL "ATOMIC_LIBRARY-NOTFOUND")
486         # -latomic should be available for gcc even when libatomic.so.1 is not
487         # in the library search path
488         target_link_libraries(${target} ${VISIBILITY} atomic)
489       else()
490         target_link_libraries(${target} ${VISIBILITY} ${ATOMIC_LIBRARY})
491       endif()
492   endif()
493   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
495   if (ENABLE_XED)
496     if (LibXed_FOUND)
497         target_link_libraries(${target} ${VISIBILITY} ${LibXed_LIBRARY})
498     else()
499         target_link_libraries(${target} ${VISIBILITY} xed)
500     endif()
501   endif()
503   if (${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang") # using Clang
504     if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
505       target_link_libraries(${target} ${VISIBILITY} c++fs)
506     endif()
507   elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
508     if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1)
509       target_link_libraries(${target} ${VISIBILITY} stdc++fs)
510     endif()
511   endif()
512 endmacro()