Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 1 | # 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 | |
| 25 | function(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 Zotov | 0bd44501 | 2014-12-30 03:24:07 +0000 | [diff] [blame] | 55 | set(ocaml_flags "-lstdc++" "-ldopt" "-L${LLVM_LIBRARY_DIR}" |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 56 | "-ccopt" "-L\\$CAMLORIGIN/../.." |
| 57 | "-ccopt" "-Wl,-rpath,\\$CAMLORIGIN/../.." |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 58 | ${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}) |
Artem Belevich | 62ef3bf | 2016-06-21 23:10:37 +0000 | [diff] [blame] | 76 | if (system_lib MATCHES "^-") |
| 77 | # If it's an option, pass it without changes. |
| 78 | list(APPEND ocaml_flags "${system_lib}" ) |
| 79 | else() |
| 80 | # Otherwise assume it's a library name we need to link with. |
| 81 | list(APPEND ocaml_flags "-l${system_lib}" ) |
| 82 | endif() |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 83 | endforeach() |
| 84 | |
| 85 | string(REPLACE ";" " " ARG_CFLAGS "${ARG_CFLAGS}") |
| 86 | set(c_flags "${ARG_CFLAGS} ${LLVM_DEFINITIONS}") |
| 87 | foreach( include_dir ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR} ) |
| 88 | set(c_flags "${c_flags} -I${include_dir}") |
| 89 | endforeach() |
| 90 | |
| 91 | foreach( ocaml_file ${ARG_OCAML} ) |
| 92 | list(APPEND sources "${ocaml_file}.mli" "${ocaml_file}.ml") |
| 93 | |
| 94 | list(APPEND ocaml_inputs "${bin}/${ocaml_file}.mli" "${bin}/${ocaml_file}.ml") |
| 95 | |
| 96 | list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmi" "${bin}/${ocaml_file}.cmo") |
| 97 | if( HAVE_OCAMLOPT ) |
| 98 | list(APPEND ocaml_outputs |
| 99 | "${bin}/${ocaml_file}.cmx" |
| 100 | "${bin}/${ocaml_file}${CMAKE_C_OUTPUT_EXTENSION}") |
| 101 | endif() |
| 102 | endforeach() |
| 103 | |
| 104 | foreach( c_file ${ARG_C} ) |
| 105 | list(APPEND sources "${c_file}.c") |
| 106 | |
| 107 | list(APPEND c_inputs "${bin}/${c_file}.c") |
| 108 | list(APPEND c_outputs "${bin}/${c_file}${CMAKE_C_OUTPUT_EXTENSION}") |
| 109 | endforeach() |
| 110 | |
| 111 | if( NOT ARG_NOCOPY ) |
| 112 | foreach( source ${sources} ) |
| 113 | add_custom_command( |
| 114 | OUTPUT "${bin}/${source}" |
| 115 | COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${src}/${source}" "${bin}" |
| 116 | DEPENDS "${src}/${source}" |
| 117 | COMMENT "Copying ${source} to build area") |
| 118 | endforeach() |
| 119 | endif() |
| 120 | |
| 121 | foreach( c_input ${c_inputs} ) |
| 122 | get_filename_component(basename "${c_input}" NAME_WE) |
| 123 | add_custom_command( |
| 124 | OUTPUT "${basename}${CMAKE_C_OUTPUT_EXTENSION}" |
| 125 | COMMAND "${OCAMLFIND}" "ocamlc" "-c" "${c_input}" -ccopt ${c_flags} |
| 126 | DEPENDS "${c_input}" |
| 127 | COMMENT "Building OCaml stub object file ${basename}${CMAKE_C_OUTPUT_EXTENSION}" |
| 128 | VERBATIM) |
| 129 | endforeach() |
| 130 | |
| 131 | set(ocaml_params) |
| 132 | foreach( ocaml_input ${ocaml_inputs} ${c_outputs}) |
| 133 | get_filename_component(filename "${ocaml_input}" NAME) |
| 134 | list(APPEND ocaml_params "${filename}") |
| 135 | endforeach() |
| 136 | |
| 137 | if( APPLE ) |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 138 | set(ocaml_rpath "@executable_path/../../../lib${LLVM_LIBDIR_SUFFIX}") |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 139 | elseif( UNIX ) |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 140 | set(ocaml_rpath "\\$ORIGIN/../../../lib${LLVM_LIBDIR_SUFFIX}") |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 141 | endif() |
| 142 | list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}") |
| 143 | |
| 144 | add_custom_command( |
| 145 | OUTPUT ${ocaml_outputs} |
| 146 | COMMAND "${OCAMLFIND}" "ocamlmklib" "-o" "${name}" ${ocaml_flags} ${ocaml_params} |
| 147 | DEPENDS ${ocaml_inputs} ${c_outputs} |
| 148 | COMMENT "Building OCaml library ${name}" |
| 149 | VERBATIM) |
| 150 | |
| 151 | add_custom_command( |
| 152 | OUTPUT "${bin}/${name}.odoc" |
| 153 | COMMAND "${OCAMLFIND}" "ocamldoc" |
| 154 | "-I" "${bin}" |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 155 | "-I" "${LLVM_LIBRARY_DIR}/ocaml/llvm/" |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 156 | "-dump" "${bin}/${name}.odoc" |
| 157 | ${ocaml_pkgs} ${ocaml_inputs} |
Peter Zotov | 3c40cbb | 2015-06-07 19:22:22 +0000 | [diff] [blame] | 158 | DEPENDS ${ocaml_inputs} ${ocaml_outputs} |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 159 | COMMENT "Building OCaml documentation for ${name}" |
| 160 | VERBATIM) |
| 161 | |
| 162 | add_custom_target("ocaml_${name}" ALL DEPENDS ${ocaml_outputs} "${bin}/${name}.odoc") |
| 163 | |
| 164 | set_target_properties("ocaml_${name}" PROPERTIES |
| 165 | OCAML_FLAGS "-I;${bin}") |
| 166 | set_target_properties("ocaml_${name}" PROPERTIES |
| 167 | OCAML_ODOC "${bin}/${name}.odoc") |
| 168 | |
| 169 | foreach( ocaml_dep ${ARG_OCAMLDEP} ) |
| 170 | add_dependencies("ocaml_${name}" "ocaml_${ocaml_dep}") |
| 171 | endforeach() |
| 172 | |
Peter Zotov | e9dd46f | 2016-09-05 01:42:22 +0000 | [diff] [blame] | 173 | if( NOT LLVM_OCAML_OUT_OF_TREE ) |
| 174 | foreach( llvm_lib ${llvm_libs} ) |
| 175 | add_dependencies("ocaml_${name}" "${llvm_lib}") |
| 176 | endforeach() |
| 177 | endif() |
| 178 | |
| 179 | add_dependencies("ocaml_all" "ocaml_${name}") |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 180 | |
| 181 | set(install_files) |
| 182 | set(install_shlibs) |
Michal Gorny | 0b515d4 | 2016-10-01 09:26:23 +0000 | [diff] [blame] | 183 | foreach( ocaml_output ${ocaml_inputs} ${ocaml_outputs} ) |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 184 | get_filename_component(ext "${ocaml_output}" EXT) |
| 185 | |
| 186 | if( NOT (ext STREQUAL ".cmo" OR |
Michal Gorny | 0b515d4 | 2016-10-01 09:26:23 +0000 | [diff] [blame] | 187 | ext STREQUAL ".ml" OR |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 188 | ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR |
| 189 | ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) ) |
| 190 | list(APPEND install_files "${ocaml_output}") |
| 191 | elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) |
| 192 | list(APPEND install_shlibs "${ocaml_output}") |
| 193 | endif() |
| 194 | endforeach() |
| 195 | |
| 196 | install(FILES ${install_files} |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 197 | DESTINATION "${LLVM_OCAML_INSTALL_PATH}/llvm") |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 198 | install(FILES ${install_shlibs} |
| 199 | PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE |
| 200 | GROUP_READ GROUP_EXECUTE |
| 201 | WORLD_READ WORLD_EXECUTE |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 202 | DESTINATION "${LLVM_OCAML_INSTALL_PATH}/llvm") |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 203 | |
| 204 | foreach( install_file ${install_files} ${install_shlibs} ) |
| 205 | get_filename_component(filename "${install_file}" NAME) |
| 206 | add_custom_command(TARGET "ocaml_${name}" POST_BUILD |
| 207 | COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${install_file}" |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 208 | "${LLVM_LIBRARY_DIR}/ocaml/llvm/" |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 209 | COMMENT "Copying OCaml library component ${filename} to intermediate area" |
| 210 | VERBATIM) |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 211 | add_dependencies("ocaml_${name}" ocaml_make_directory) |
Peter Zotov | b20073c | 2014-12-01 19:50:23 +0000 | [diff] [blame] | 212 | endforeach() |
| 213 | endfunction() |
Peter Zotov | e9dd46f | 2016-09-05 01:42:22 +0000 | [diff] [blame] | 214 | |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 215 | add_custom_target(ocaml_make_directory |
| 216 | COMMAND "${CMAKE_COMMAND}" "-E" "make_directory" "${LLVM_LIBRARY_DIR}/ocaml/llvm") |
Peter Zotov | e9dd46f | 2016-09-05 01:42:22 +0000 | [diff] [blame] | 217 | add_custom_target("ocaml_all") |
Michal Gorny | b002f63 | 2016-09-30 18:34:23 +0000 | [diff] [blame] | 218 | set_target_properties(ocaml_all PROPERTIES FOLDER "Misc") |