blob: 4babaf68ef9683a5a0ec27f5db41846c84b94001 [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
55 set(ocaml_flags "-lstdc++" "-ldopt" "-L${LLVM_LIBRARY_OUTPUT_INTDIR}"
56 ${ocaml_pkgs})
57
58 foreach( ocaml_dep ${ARG_OCAMLDEP} )
59 get_target_property(dep_ocaml_flags "ocaml_${ocaml_dep}" OCAML_FLAGS)
60 list(APPEND ocaml_flags ${dep_ocaml_flags})
61 endforeach()
62
63 if( NOT BUILD_SHARED_LIBS )
64 list(APPEND ocaml_flags "-custom")
65 endif()
66
67 explicit_map_components_to_libraries(llvm_libs ${ARG_LLVM})
68 foreach( llvm_lib ${llvm_libs} )
69 list(APPEND ocaml_flags "-l${llvm_lib}" )
70 endforeach()
71
72 get_property(system_libs TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS)
73 foreach(system_lib ${system_libs})
74 list(APPEND ocaml_flags "-l${system_lib}" )
75 endforeach()
76
77 string(REPLACE ";" " " ARG_CFLAGS "${ARG_CFLAGS}")
78 set(c_flags "${ARG_CFLAGS} ${LLVM_DEFINITIONS}")
79 foreach( include_dir ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR} )
80 set(c_flags "${c_flags} -I${include_dir}")
81 endforeach()
82
83 foreach( ocaml_file ${ARG_OCAML} )
84 list(APPEND sources "${ocaml_file}.mli" "${ocaml_file}.ml")
85
86 list(APPEND ocaml_inputs "${bin}/${ocaml_file}.mli" "${bin}/${ocaml_file}.ml")
87
88 list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmi" "${bin}/${ocaml_file}.cmo")
89 if( HAVE_OCAMLOPT )
90 list(APPEND ocaml_outputs
91 "${bin}/${ocaml_file}.cmx"
92 "${bin}/${ocaml_file}${CMAKE_C_OUTPUT_EXTENSION}")
93 endif()
94 endforeach()
95
96 foreach( c_file ${ARG_C} )
97 list(APPEND sources "${c_file}.c")
98
99 list(APPEND c_inputs "${bin}/${c_file}.c")
100 list(APPEND c_outputs "${bin}/${c_file}${CMAKE_C_OUTPUT_EXTENSION}")
101 endforeach()
102
103 if( NOT ARG_NOCOPY )
104 foreach( source ${sources} )
105 add_custom_command(
106 OUTPUT "${bin}/${source}"
107 COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${src}/${source}" "${bin}"
108 DEPENDS "${src}/${source}"
109 COMMENT "Copying ${source} to build area")
110 endforeach()
111 endif()
112
113 foreach( c_input ${c_inputs} )
114 get_filename_component(basename "${c_input}" NAME_WE)
115 add_custom_command(
116 OUTPUT "${basename}${CMAKE_C_OUTPUT_EXTENSION}"
117 COMMAND "${OCAMLFIND}" "ocamlc" "-c" "${c_input}" -ccopt ${c_flags}
118 DEPENDS "${c_input}"
119 COMMENT "Building OCaml stub object file ${basename}${CMAKE_C_OUTPUT_EXTENSION}"
120 VERBATIM)
121 endforeach()
122
123 set(ocaml_params)
124 foreach( ocaml_input ${ocaml_inputs} ${c_outputs})
125 get_filename_component(filename "${ocaml_input}" NAME)
126 list(APPEND ocaml_params "${filename}")
127 endforeach()
128
129 if( APPLE )
130 set(ocaml_rpath "@executable_path/../../lib")
131 elseif( UNIX )
132 set(ocaml_rpath "\\$ORIGIN/../../lib")
133 endif()
134 list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}")
135
136 add_custom_command(
137 OUTPUT ${ocaml_outputs}
138 COMMAND "${OCAMLFIND}" "ocamlmklib" "-o" "${name}" ${ocaml_flags} ${ocaml_params}
139 DEPENDS ${ocaml_inputs} ${c_outputs}
140 COMMENT "Building OCaml library ${name}"
141 VERBATIM)
142
143 add_custom_command(
144 OUTPUT "${bin}/${name}.odoc"
145 COMMAND "${OCAMLFIND}" "ocamldoc"
146 "-I" "${bin}"
147 "-I" "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/"
148 "-dump" "${bin}/${name}.odoc"
149 ${ocaml_pkgs} ${ocaml_inputs}
150 DEPENDS ${ocaml_inputs}
151 COMMENT "Building OCaml documentation for ${name}"
152 VERBATIM)
153
154 add_custom_target("ocaml_${name}" ALL DEPENDS ${ocaml_outputs} "${bin}/${name}.odoc")
155
156 set_target_properties("ocaml_${name}" PROPERTIES
157 OCAML_FLAGS "-I;${bin}")
158 set_target_properties("ocaml_${name}" PROPERTIES
159 OCAML_ODOC "${bin}/${name}.odoc")
160
161 foreach( ocaml_dep ${ARG_OCAMLDEP} )
162 add_dependencies("ocaml_${name}" "ocaml_${ocaml_dep}")
163 endforeach()
164
165 foreach( llvm_lib ${llvm_libs} )
166 add_dependencies("ocaml_${name}" "${llvm_lib}")
167 endforeach()
168
169 set(install_files)
170 set(install_shlibs)
171 foreach( ocaml_output ${ocaml_outputs} )
172 get_filename_component(ext "${ocaml_output}" EXT)
173
174 if( NOT (ext STREQUAL ".cmo" OR
175 ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR
176 ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) )
177 list(APPEND install_files "${ocaml_output}")
178 elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX)
179 list(APPEND install_shlibs "${ocaml_output}")
180 endif()
181 endforeach()
182
183 install(FILES ${install_files}
184 DESTINATION lib/ocaml)
185 install(FILES ${install_shlibs}
186 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
187 GROUP_READ GROUP_EXECUTE
188 WORLD_READ WORLD_EXECUTE
189 DESTINATION lib/ocaml)
190
191 foreach( install_file ${install_files} ${install_shlibs} )
192 get_filename_component(filename "${install_file}" NAME)
193 add_custom_command(TARGET "ocaml_${name}" POST_BUILD
194 COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${install_file}"
195 "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/"
196 COMMENT "Copying OCaml library component ${filename} to intermediate area"
197 VERBATIM)
198 endforeach()
199endfunction()