TypeStructures clobber other RDS entries
[hiphop-php.git] / hphp / hack / CMakeLists.txt
blob58e5fc76988a92008bb18ac998a361c5773d5975
1 option(BUILD_HACK "True if we should build the Hack typechecker." ON)
3 if (NOT BUILD_HACK)
4   message(STATUS "Skipping hack")
5   return()
6 endif()
8 message(STATUS "Building hack")
10 find_package(LZ4)
11 find_package(LibElf)
13 # native_libraries: values for `-l` flags
14 # lib_paths: values for `-L` flags (directories)
15 # extra_link_opts: opaque options passed to the linker
17 # We need extra_link_opts for:
18 # - static libraries
19 # - anything built from third-party: cmake gives us the link flags
20 unset(extra_include_paths)
21 unset(extra_native_libraries)
22 unset(extra_lib_paths)
23 unset(extra_link_opts)
24 unset(extra_cc_flags)
26 # Allows '#include "hphp/path/to/library/"' paths to start from hphp
27 # project directory  which is consistent with fbmake's include paths.
28 list(APPEND extra_include_paths ${HPHP_HOME})
29 list(APPEND extra_cc_flags -pthread)
31 # Xcode/Ninja generators undefined MAKE
32 if(NOT MAKE)
33   set(MAKE make)
34 endif()
36 if ("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
37   set(DUNE_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/_build")
38   set(OPAM_STAMP_FILE "_build/opam.stamp")
39 else()
40   set(DUNE_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}")
41   set(OPAM_STAMP_FILE "opam.stamp")
42 endif()
43 set(HACK_BUILD_ROOT "${DUNE_BUILD_DIR}/default")
45 get_target_property(OPAM_EXECUTABLE opam IMPORTED_LOCATION)
47 add_custom_command(
48   OUTPUT "${OPAM_STAMP_FILE}"
49   DEPENDS opam opam_setup.sh
50   COMMAND
51     ${CMAKE_CURRENT_SOURCE_DIR}/opam_setup.sh
52     "${OPAM_EXECUTABLE}"
53     "${DUNE_BUILD_DIR}"
54     && cmake -E touch "${OPAM_STAMP_FILE}"
56 add_custom_target(opam_setup DEPENDS "${OPAM_STAMP_FILE}" opam_setup.sh)
58 if (SKIP_OPAM)
59   set(OPAMROOT "~/.opam")
60 else ()
61   set(OPAMROOT "${DUNE_BUILD_DIR}/opam")
62 endif()
64 if(LZ4_FOUND)
65   list(APPEND extra_include_paths ${LZ4_INCLUDE_DIR})
66   get_filename_component(pth ${LZ4_LIBRARY} DIRECTORY)
67   list(APPEND extra_lib_paths ${pth})
68   list(APPEND extra_native_libraries "lz4")
69 else()
70   get_target_property(LZ4_INCLUDE_DIRS lz4 INTERFACE_INCLUDE_DIRECTORIES)
71   list(APPEND extra_include_paths ${LZ4_INCLUDE_DIRS})
72   # If LZ4_FOUND is false either we didn't find lz4 or we found it but it's the
73   # wrong version.  We can't just add the new path and a native_lib because we
74   # can't control the order (and -l won't accept the raw path to the lib).  By
75   # doing it this way we specify the path explicitly.
76   get_target_property(LZ4_LIBS lz4 INTERFACE_LINK_LIBRARIES)
77   list(APPEND extra_link_opts ${LZ4_LIBS})
78 endif()
80 get_target_property(ZSTD_INCLUDE_DIRS zstd INTERFACE_INCLUDE_DIRECTORIES)
81 list(APPEND extra_include_paths ${ZSTD_INCLUDE_DIRS})
82 get_target_property(ZSTD_LIBS zstd INTERFACE_LINK_LIBRARIES)
83 list(APPEND extra_link_opts ${ZSTD_LIBS})
85 if(PC_SQLITE3_FOUND)
86   list(APPEND extra_include_paths ${LIBSQLITE3_INCLUDE_DIR})
87   get_filename_component(pth ${LIBSQLITE3_LIBRARY} DIRECTORY)
88   list(APPEND extra_lib_paths ${pth})
89   list(APPEND extra_native_libraries "sqlite3")
90 else()
91   list(APPEND extra_include_paths "${TP_DIR}/libsqlite3")
92   get_target_property(SQLITE_DIR sqlite3 BINARY_DIR)
93   list(APPEND extra_link_opts "${SQLITE_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}sqlite3${CMAKE_STATIC_LIBRARY_SUFFIX}")
94 endif()
96 get_target_property(RUSTC_EXE rustc LOCATION)
97 get_target_property(CARGO_EXE cargo LOCATION)
98 get_filename_component(RUSTC_BIN_DIR "${RUSTC_EXE}" DIRECTORY)
99 get_filename_component(CARGO_BIN_DIR "${CARGO_EXE}" DIRECTORY)
101 function(invoke_dune name target)
102   add_custom_target(
103       ${name}
104       COMMAND
105         . "${CMAKE_CURRENT_BINARY_DIR}/dev_env.sh" &&
106         opam config exec --
107         $(MAKE) --makefile=Makefile.dune ${target}
108         BYTECODE="${EMIT_OCAML_BYTECODE}"
109       WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
110     )
112   add_dependencies(${name} ocaml rustc cargo zstd)
113   if (NOT SKIP_OPAM)
114     add_dependencies(${name} opam_setup)
115   endif()
116 endfunction()
118 invoke_dune(hack_dune_debug debug)
119 invoke_dune(hack_dune_test test)
120 invoke_dune(hack_dune all)
122 set(CARGO_BUILD "${CMAKE_SOURCE_DIR}/hphp/hack/scripts/build_rust_to_ocaml.sh")
124 add_custom_target(
125   hack_ffi
126   COMMAND
127    . "${CMAKE_CURRENT_BINARY_DIR}/dev_env.sh" &&
128     ${CARGO_BUILD} hack_parser_ffi rust_parser_ffi &&
129     ${CARGO_BUILD} rust_facts_ffi rust_facts_ffi &&
130     ${CARGO_BUILD} compile_ffi compile_ffi
131   COMMENT "Compiling Rust FFI"
134 add_dependencies(hack_ffi rustc cargo)
135 # Not a true dependency, but we need to make sure we don't have two cargo/rustc
136 # processes building the FFI files at the same time; this leads to intermittent
137 # failures, which for some reason are particularly common on our Azure MacOS
138 # build jobs
139 add_dependencies(hack_dune hack_ffi)
141 if (NOT LZ4_FOUND)
142   add_dependencies(hack_dune lz4)
143   add_dependencies(hack_dune_debug lz4)
144   add_dependencies(hack_dune_test lz4)
145   add_dependencies(hack_ffi lz4)
146 endif()
148 if (NOT PC_SQLITE3_FOUND)
149   add_dependencies(hack_dune sqlite3)
150   add_dependencies(hack_dune_debug sqlite3)
151   add_dependencies(hack_dune_test sqlite3)
152   add_dependencies(hack_ffi sqlite3)
153 endif()
155 # Intentionally not using `hack_dune_debug` as it generates output files of a
156 # different format (bytecode instead of raw executables, which is useful if
157 # you're working with Hack, but means that e.g. hhvm can't find
158 # `hh_single_compile` in the source tree. Keep it around, but require it to be
159 # explicitly used
160 add_custom_target(hack ALL DEPENDS hack_dune)
161 add_custom_target(hack_test DEPENDS hack_dune_test)
163 # libcompile_ffi_stubs.a, librust_facts_ffi_stubs.a, and
164 # librust_parser_ffi_stubs.a need to be exposed as library targets to cmake
165 # for other targets (e.g. hhvm) to depend on.
166 set(LIBCOMPILE_PATH "${CMAKE_BINARY_DIR}/hphp/hack/libcompile_ffi_stubs.a")
167 add_library(libcompile_ffi_stubs STATIC IMPORTED GLOBAL)
168 add_dependencies(libcompile_ffi_stubs hack_ffi)
169 set_target_properties(libcompile_ffi_stubs PROPERTIES IMPORTED_LOCATION ${LIBCOMPILE_PATH})
171 set(LIBRUST_PATH "${CMAKE_BINARY_DIR}/hphp/hack/librust_facts_ffi_stubs.a")
172 add_library(librust_facts_ffi_stubs STATIC IMPORTED GLOBAL)
173 add_dependencies(librust_facts_ffi_stubs hack_ffi)
174 set_target_properties(librust_facts_ffi_stubs PROPERTIES IMPORTED_LOCATION ${LIBRUST_PATH})
176 set(LIBRUST_PARSER_PATH "${CMAKE_BINARY_DIR}/hphp/hack/librust_parser_ffi_stubs.a")
177 add_library(librust_parser_ffi_stubs STATIC IMPORTED GLOBAL)
178 add_dependencies(librust_parser_ffi_stubs hack_ffi)
179 set_target_properties(librust_parser_ffi_stubs PROPERTIES IMPORTED_LOCATION ${LIBRUST_PARSER_PATH})
181 configure_file(dev_env.sh.in dev_env.sh ESCAPE_QUOTES @ONLY)
183 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_client
184   DESTINATION bin
185   COMPONENT dev)
187 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_server
188   DESTINATION bin
189   COMPONENT dev)
191 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hackfmt
192   DESTINATION bin
193   COMPONENT dev)
195 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_parse
196   DESTINATION bin
197   COMPONENT dev)
199 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_single_compile
200   DESTINATION bin
201   COMPONENT dev)