blob: c58ac9cb7349e11a5dd1acda9818621bb4baeac6 [file] [log] [blame]
Peter Zotovb20073c2014-12-01 19:50:23 +00001# CMake build rules for the OCaml language.
2# Assumes FindOCaml is used.
3# http://ocaml.org/
4#
5# Example usage:
6#
7# add_ocaml_library(pkg_a OCAML mod_a OCAMLDEP pkg_b C mod_a_stubs PKG ctypes LLVM core)
8#
9# Unnamed parameters:
10#
11# * Library name.
12#
13# Named parameters:
14#
15# OCAML OCaml module names. Imply presence of a corresponding .ml and .mli files.
16# OCAMLDEP Names of libraries this library depends on.
17# C C stub sources. Imply presence of a corresponding .c file.
18# CFLAGS Additional arguments passed when compiling C stubs.
19# PKG Names of ocamlfind packages this library depends on.
20# LLVM Names of LLVM libraries this library depends on.
21# NOCOPY Do not automatically copy sources (.c, .ml, .mli) from the source directory,
22# e.g. if they are generated.
23#
24
25function(add_ocaml_library name)
26 CMAKE_PARSE_ARGUMENTS(ARG "NOCOPY" "" "OCAML;OCAMLDEP;C;CFLAGS;PKG;LLVM" ${ARGN})
27
28 set(src ${CMAKE_CURRENT_SOURCE_DIR})
29 set(bin ${CMAKE_CURRENT_BINARY_DIR})
30
31 set(ocaml_pkgs)
32 foreach( ocaml_pkg ${ARG_PKG} )
33 list(APPEND ocaml_pkgs "-package" "${ocaml_pkg}")
34 endforeach()
35
36 set(sources)
37
38 set(ocaml_inputs)
39
40 set(ocaml_outputs "${bin}/${name}.cma")
41 if( ARG_C )
42 list(APPEND ocaml_outputs
43 "${bin}/lib${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
44 if ( BUILD_SHARED_LIBS )
45 list(APPEND ocaml_outputs
46 "${bin}/dll${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
47 endif()
48 endif()
49 if( HAVE_OCAMLOPT )
50 list(APPEND ocaml_outputs
51 "${bin}/${name}.cmxa"
52 "${bin}/${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
53 endif()
54
Peter Zotov0bd445012014-12-30 03:24:07 +000055 set(ocaml_flags "-lstdc++" "-ldopt" "-L${LLVM_LIBRARY_DIR}"
Peter Zotov1f00ac92015-01-13 09:47:59 +000056 "-ccopt" "-L\\$CAMLORIGIN/.."
57 "-ccopt" "-Wl,-rpath,\\$CAMLORIGIN/.."
Peter Zotovb20073c2014-12-01 19:50:23 +000058 ${ocaml_pkgs})
59
60 foreach( ocaml_dep ${ARG_OCAMLDEP} )
61 get_target_property(dep_ocaml_flags "ocaml_${ocaml_dep}" OCAML_FLAGS)
62 list(APPEND ocaml_flags ${dep_ocaml_flags})
63 endforeach()
64
65 if( NOT BUILD_SHARED_LIBS )
66 list(APPEND ocaml_flags "-custom")
67 endif()
68
69 explicit_map_components_to_libraries(llvm_libs ${ARG_LLVM})
70 foreach( llvm_lib ${llvm_libs} )
71 list(APPEND ocaml_flags "-l${llvm_lib}" )
72 endforeach()
73
74 get_property(system_libs TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS)
75 foreach(system_lib ${system_libs})
76 list(APPEND ocaml_flags "-l${system_lib}" )
77 endforeach()
78
79 string(REPLACE ";" " " ARG_CFLAGS "${ARG_CFLAGS}")
80 set(c_flags "${ARG_CFLAGS} ${LLVM_DEFINITIONS}")
81 foreach( include_dir ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR} )
82 set(c_flags "${c_flags} -I${include_dir}")
83 endforeach()
84
85 foreach( ocaml_file ${ARG_OCAML} )
86 list(APPEND sources "${ocaml_file}.mli" "${ocaml_file}.ml")
87
88 list(APPEND ocaml_inputs "${bin}/${ocaml_file}.mli" "${bin}/${ocaml_file}.ml")
89
90 list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmi" "${bin}/${ocaml_file}.cmo")
91 if( HAVE_OCAMLOPT )
92 list(APPEND ocaml_outputs
93 "${bin}/${ocaml_file}.cmx"
94 "${bin}/${ocaml_file}${CMAKE_C_OUTPUT_EXTENSION}")
95 endif()
96 endforeach()
97
98 foreach( c_file ${ARG_C} )
99 list(APPEND sources "${c_file}.c")
100
101 list(APPEND c_inputs "${bin}/${c_file}.c")
102 list(APPEND c_outputs "${bin}/${c_file}${CMAKE_C_OUTPUT_EXTENSION}")
103 endforeach()
104
105 if( NOT ARG_NOCOPY )
106 foreach( source ${sources} )
107 add_custom_command(
108 OUTPUT "${bin}/${source}"
109 COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${src}/${source}" "${bin}"
110 DEPENDS "${src}/${source}"
111 COMMENT "Copying ${source} to build area")
112 endforeach()
113 endif()
114
115 foreach( c_input ${c_inputs} )
116 get_filename_component(basename "${c_input}" NAME_WE)
117 add_custom_command(
118 OUTPUT "${basename}${CMAKE_C_OUTPUT_EXTENSION}"
119 COMMAND "${OCAMLFIND}" "ocamlc" "-c" "${c_input}" -ccopt ${c_flags}
120 DEPENDS "${c_input}"
121 COMMENT "Building OCaml stub object file ${basename}${CMAKE_C_OUTPUT_EXTENSION}"
122 VERBATIM)
123 endforeach()
124
125 set(ocaml_params)
126 foreach( ocaml_input ${ocaml_inputs} ${c_outputs})
127 get_filename_component(filename "${ocaml_input}" NAME)
128 list(APPEND ocaml_params "${filename}")
129 endforeach()
130
131 if( APPLE )
132 set(ocaml_rpath "@executable_path/../../lib")
133 elseif( UNIX )
134 set(ocaml_rpath "\\$ORIGIN/../../lib")
135 endif()
136 list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}")
137
138 add_custom_command(
139 OUTPUT ${ocaml_outputs}
140 COMMAND "${OCAMLFIND}" "ocamlmklib" "-o" "${name}" ${ocaml_flags} ${ocaml_params}
141 DEPENDS ${ocaml_inputs} ${c_outputs}
142 COMMENT "Building OCaml library ${name}"
143 VERBATIM)
144
145 add_custom_command(
146 OUTPUT "${bin}/${name}.odoc"
147 COMMAND "${OCAMLFIND}" "ocamldoc"
148 "-I" "${bin}"
Peter Zotov0bd445012014-12-30 03:24:07 +0000149 "-I" "${LLVM_LIBRARY_DIR}/ocaml/"
Peter Zotovb20073c2014-12-01 19:50:23 +0000150 "-dump" "${bin}/${name}.odoc"
151 ${ocaml_pkgs} ${ocaml_inputs}
152 DEPENDS ${ocaml_inputs}
153 COMMENT "Building OCaml documentation for ${name}"
154 VERBATIM)
155
156 add_custom_target("ocaml_${name}" ALL DEPENDS ${ocaml_outputs} "${bin}/${name}.odoc")
157
158 set_target_properties("ocaml_${name}" PROPERTIES
159 OCAML_FLAGS "-I;${bin}")
160 set_target_properties("ocaml_${name}" PROPERTIES
161 OCAML_ODOC "${bin}/${name}.odoc")
162
163 foreach( ocaml_dep ${ARG_OCAMLDEP} )
164 add_dependencies("ocaml_${name}" "ocaml_${ocaml_dep}")
165 endforeach()
166
167 foreach( llvm_lib ${llvm_libs} )
168 add_dependencies("ocaml_${name}" "${llvm_lib}")
169 endforeach()
170
171 set(install_files)
172 set(install_shlibs)
173 foreach( ocaml_output ${ocaml_outputs} )
174 get_filename_component(ext "${ocaml_output}" EXT)
175
176 if( NOT (ext STREQUAL ".cmo" OR
177 ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR
178 ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) )
179 list(APPEND install_files "${ocaml_output}")
180 elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX)
181 list(APPEND install_shlibs "${ocaml_output}")
182 endif()
183 endforeach()
184
185 install(FILES ${install_files}
186 DESTINATION lib/ocaml)
187 install(FILES ${install_shlibs}
188 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
189 GROUP_READ GROUP_EXECUTE
190 WORLD_READ WORLD_EXECUTE
191 DESTINATION lib/ocaml)
192
193 foreach( install_file ${install_files} ${install_shlibs} )
194 get_filename_component(filename "${install_file}" NAME)
195 add_custom_command(TARGET "ocaml_${name}" POST_BUILD
196 COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${install_file}"
Peter Zotov0bd445012014-12-30 03:24:07 +0000197 "${LLVM_LIBRARY_DIR}/ocaml/"
Peter Zotovb20073c2014-12-01 19:50:23 +0000198 COMMENT "Copying OCaml library component ${filename} to intermediate area"
199 VERBATIM)
200 endforeach()
201endfunction()