Handle trait use conflict resolution
[hiphop-php.git] / hphp / hack / CMakeLists.txt
blobe97c50127be1fdf913c3a06a96e3fbf0dbedc473
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 if(PC_SQLITE3_FOUND)
81   list(APPEND extra_include_paths ${LIBSQLITE3_INCLUDE_DIR})
82   get_filename_component(pth ${LIBSQLITE3_LIBRARY} DIRECTORY)
83   list(APPEND extra_lib_paths ${pth})
84   list(APPEND extra_native_libraries "sqlite3")
85 else()
86   list(APPEND extra_include_paths "${TP_DIR}/libsqlite3")
87   get_target_property(SQLITE_DIR sqlite3 BINARY_DIR)
88   list(APPEND extra_link_opts "${SQLITE_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}sqlite3${CMAKE_STATIC_LIBRARY_SUFFIX}")
89 endif()
91 get_target_property(RUSTC_EXE rustc LOCATION)
92 get_target_property(CARGO_EXE cargo LOCATION)
93 get_filename_component(RUSTC_BIN_DIR "${RUSTC_EXE}" DIRECTORY)
94 get_filename_component(CARGO_BIN_DIR "${CARGO_EXE}" DIRECTORY)
96 function(invoke_dune name target)
97   add_custom_target(
98       ${name}
99       COMMAND
100         . "${CMAKE_CURRENT_BINARY_DIR}/dev_env.sh" &&
101         opam config exec --
102         $(MAKE) --makefile=Makefile.dune ${target}
103         BYTECODE="${EMIT_OCAML_BYTECODE}"
104       WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
105     )
107   add_dependencies(${name} ocaml rustc cargo)
108   if (NOT SKIP_OPAM)
109     add_dependencies(${name} opam_setup)
110   endif()
111 endfunction()
113 invoke_dune(hack_dune_debug debug)
114 invoke_dune(hack_dune_test test)
115 invoke_dune(hack_dune all)
117 if (NOT LZ4_FOUND)
118   add_dependencies(hack_dune lz4)
119   add_dependencies(hack_dune_debug lz4)
120   add_dependencies(hack_dune_test lz4)
121 endif()
123 if (NOT PC_SQLITE3_FOUND)
124   add_dependencies(hack_dune sqlite3)
125   add_dependencies(hack_dune_debug sqlite3)
126   add_dependencies(hack_dune_test sqlite3)
127 endif()
129 # Intentionally not using `hack_dune_debug` as it generates output files of a
130 # different format (bytecode instead of raw executables, which is useful if
131 # you're working with Hack, but means that e.g. hhvm can't find
132 # `hh_single_compile` in the source tree. Keep it around, but require it to be
133 # explicitly used
134 add_custom_target(hack ALL DEPENDS hack_dune)
135 add_custom_target(hack_test DEPENDS hack_dune_test)
137 configure_file(dev_env.sh.in dev_env.sh ESCAPE_QUOTES @ONLY)
139 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_client
140   DESTINATION bin
141   COMPONENT dev)
143 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_server
144   DESTINATION bin
145   COMPONENT dev)
147 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hackfmt
148   DESTINATION bin
149   COMPONENT dev)
151 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_parse
152   DESTINATION bin
153   COMPONENT dev)
155 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bin/hh_single_compile
156   DESTINATION bin
157   COMPONENT dev)