llvm.org GIT mirror llvm / a0a26f2
[OCaml] [cmake] Add CMake buildsystem for OCaml. Closes PR15325. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223071 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Zotov 4 years ago
24 changed file(s) with 507 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
560560 add_subdirectory(utils/unittest)
561561 endif()
562562
563 foreach( binding ${LLVM_BINDINGS_LIST} )
564 if( EXISTS "${LLVM_MAIN_SRC_DIR}/bindings/${binding}/CMakeLists.txt" )
565 add_subdirectory(bindings/${binding})
566 endif()
567 endforeach()
568
563569 add_subdirectory(projects)
564570
565571 if(WITH_POLLY)
0 add_subdirectory(llvm)
1 add_subdirectory(all_backends)
2 add_subdirectory(analysis)
3 add_subdirectory(backends)
4 add_subdirectory(bitreader)
5 add_subdirectory(bitwriter)
6 add_subdirectory(irreader)
7 add_subdirectory(linker)
8 add_subdirectory(target)
9 add_subdirectory(transforms)
10 add_subdirectory(executionengine)
0 add_ocaml_library(llvm_all_backends
1 OCAML llvm_all_backends
2 OCAMLDEP llvm
3 C all_backends_ocaml
4 LLVM ${LLVM_TARGETS_TO_BUILD})
0 add_ocaml_library(llvm_analysis
1 OCAML llvm_analysis
2 OCAMLDEP llvm
3 C analysis_ocaml
4 LLVM analysis)
0 foreach(TARGET ${LLVM_TARGETS_TO_BUILD})
1 set(OCAML_LLVM_TARGET ${TARGET})
2
3 foreach( ext ml mli )
4 configure_file(
5 "${CMAKE_CURRENT_SOURCE_DIR}/llvm_backend.${ext}.in"
6 "${CMAKE_CURRENT_BINARY_DIR}/llvm_${TARGET}.${ext}")
7 endforeach()
8
9 configure_file(
10 "${CMAKE_CURRENT_SOURCE_DIR}/backend_ocaml.c"
11 "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_ocaml.c")
12
13 add_ocaml_library(llvm_${TARGET}
14 OCAML llvm_${TARGET}
15 C ${TARGET}_ocaml
16 CFLAGS -DTARGET=${TARGET}
17 LLVM ${TARGET}
18 NOCOPY)
19
20 configure_file(
21 "${CMAKE_CURRENT_SOURCE_DIR}/META.llvm_backend.in"
22 "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/META.llvm_${TARGET}")
23
24 install(FILES "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/META.llvm_${TARGET}"
25 DESTINATION lib/ocaml)
26 endforeach()
0 add_ocaml_library(llvm_bitreader
1 OCAML llvm_bitreader
2 OCAMLDEP llvm
3 C bitreader_ocaml
4 LLVM bitreader)
0 add_ocaml_library(llvm_bitwriter
1 OCAML llvm_bitwriter
2 OCAMLDEP llvm
3 C bitwriter_ocaml
4 LLVM bitwriter)
0 add_ocaml_library(llvm_executionengine
1 OCAML llvm_executionengine
2 OCAMLDEP llvm llvm_target
3 C executionengine_ocaml
4 LLVM executionengine mcjit native
5 PKG ctypes)
0 add_ocaml_library(llvm_irreader
1 OCAML llvm_irreader
2 OCAMLDEP llvm
3 C irreader_ocaml
4 LLVM irreader)
0 add_ocaml_library(llvm_linker
1 OCAML llvm_linker
2 OCAMLDEP llvm
3 C linker_ocaml
4 LLVM linker)
0 add_ocaml_library(llvm
1 OCAML llvm
2 C llvm_ocaml
3 LLVM core transformutils support)
4
5 configure_file(
6 "${CMAKE_CURRENT_SOURCE_DIR}/META.llvm.in"
7 "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/META.llvm")
8
9 install(FILES "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/META.llvm"
10 DESTINATION lib/ocaml)
0 add_ocaml_library(llvm_target
1 OCAML llvm_target
2 OCAMLDEP llvm
3 C target_ocaml
4 LLVM target)
0 add_subdirectory(ipo)
1 add_subdirectory(passmgr_builder)
2 add_subdirectory(scalar_opts)
3 add_subdirectory(vectorize)
0 add_ocaml_library(llvm_ipo
1 OCAML llvm_ipo
2 OCAMLDEP llvm
3 C ipo_ocaml
4 LLVM ipo)
0 add_ocaml_library(llvm_passmgr_builder
1 OCAML llvm_passmgr_builder
2 OCAMLDEP llvm
3 C passmgr_builder_ocaml
4 LLVM ipo)
0 add_ocaml_library(llvm_scalar_opts
1 OCAML llvm_scalar_opts
2 OCAMLDEP llvm
3 C scalar_opts_ocaml
4 LLVM scalaropts)
0 add_ocaml_library(llvm_vectorize
1 OCAML llvm_vectorize
2 OCAMLDEP llvm
3 C vectorize_ocaml
4 LLVM vectorize)
515515 endif()
516516 endif()
517517 endif()
518
519 include(FindOCaml)
520 include(AddOCaml)
521 if(WIN32)
522 message(STATUS "OCaml bindings disabled.")
523 else()
524 find_package(OCaml)
525 if( NOT OCAML_FOUND )
526 message(STATUS "OCaml bindings disabled.")
527 else()
528 if( OCAML_VERSION VERSION_LESS "4.00.0" )
529 message(STATUS "OCaml bindings disabled, need OCaml >=4.00.0.")
530 else()
531 message(STATUS "OCaml bindings enabled.")
532 find_ocamlfind_package(ctypes VERSION 0.3)
533 find_ocamlfind_package(oUnit VERSION 2 OPTIONAL)
534 set(LLVM_BINDINGS "${LLVM_BINDINGS} ocaml")
535 endif()
536 endif()
537 endif()
538
539 string(REPLACE " " ";" LLVM_BINDINGS_LIST "${LLVM_BINDINGS}")
0 # CMake build rules for the OCaml language.
1 # Assumes FindOCaml is used.
2 # http://ocaml.org/
3 #
4 # Example usage:
5 #
6 # add_ocaml_library(pkg_a OCAML mod_a OCAMLDEP pkg_b C mod_a_stubs PKG ctypes LLVM core)
7 #
8 # Unnamed parameters:
9 #
10 # * Library name.
11 #
12 # Named parameters:
13 #
14 # OCAML OCaml module names. Imply presence of a corresponding .ml and .mli files.
15 # OCAMLDEP Names of libraries this library depends on.
16 # C C stub sources. Imply presence of a corresponding .c file.
17 # CFLAGS Additional arguments passed when compiling C stubs.
18 # PKG Names of ocamlfind packages this library depends on.
19 # LLVM Names of LLVM libraries this library depends on.
20 # NOCOPY Do not automatically copy sources (.c, .ml, .mli) from the source directory,
21 # e.g. if they are generated.
22 #
23
24 function(add_ocaml_library name)
25 CMAKE_PARSE_ARGUMENTS(ARG "NOCOPY" "" "OCAML;OCAMLDEP;C;CFLAGS;PKG;LLVM" ${ARGN})
26
27 set(src ${CMAKE_CURRENT_SOURCE_DIR})
28 set(bin ${CMAKE_CURRENT_BINARY_DIR})
29
30 set(ocaml_pkgs)
31 foreach( ocaml_pkg ${ARG_PKG} )
32 list(APPEND ocaml_pkgs "-package" "${ocaml_pkg}")
33 endforeach()
34
35 set(sources)
36
37 set(ocaml_inputs)
38
39 set(ocaml_outputs "${bin}/${name}.cma")
40 if( ARG_C )
41 list(APPEND ocaml_outputs
42 "${bin}/lib${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
43 if ( BUILD_SHARED_LIBS )
44 list(APPEND ocaml_outputs
45 "${bin}/dll${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
46 endif()
47 endif()
48 if( HAVE_OCAMLOPT )
49 list(APPEND ocaml_outputs
50 "${bin}/${name}.cmxa"
51 "${bin}/${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
52 endif()
53
54 set(ocaml_flags "-lstdc++" "-ldopt" "-L${LLVM_LIBRARY_OUTPUT_INTDIR}"
55 ${ocaml_pkgs})
56
57 foreach( ocaml_dep ${ARG_OCAMLDEP} )
58 get_target_property(dep_ocaml_flags "ocaml_${ocaml_dep}" OCAML_FLAGS)
59 list(APPEND ocaml_flags ${dep_ocaml_flags})
60 endforeach()
61
62 if( NOT BUILD_SHARED_LIBS )
63 list(APPEND ocaml_flags "-custom")
64 endif()
65
66 explicit_map_components_to_libraries(llvm_libs ${ARG_LLVM})
67 foreach( llvm_lib ${llvm_libs} )
68 list(APPEND ocaml_flags "-l${llvm_lib}" )
69 endforeach()
70
71 get_property(system_libs TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS)
72 foreach(system_lib ${system_libs})
73 list(APPEND ocaml_flags "-l${system_lib}" )
74 endforeach()
75
76 string(REPLACE ";" " " ARG_CFLAGS "${ARG_CFLAGS}")
77 set(c_flags "${ARG_CFLAGS} ${LLVM_DEFINITIONS}")
78 foreach( include_dir ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR} )
79 set(c_flags "${c_flags} -I${include_dir}")
80 endforeach()
81
82 foreach( ocaml_file ${ARG_OCAML} )
83 list(APPEND sources "${ocaml_file}.mli" "${ocaml_file}.ml")
84
85 list(APPEND ocaml_inputs "${bin}/${ocaml_file}.mli" "${bin}/${ocaml_file}.ml")
86
87 list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmi" "${bin}/${ocaml_file}.cmo")
88 if( HAVE_OCAMLOPT )
89 list(APPEND ocaml_outputs
90 "${bin}/${ocaml_file}.cmx"
91 "${bin}/${ocaml_file}${CMAKE_C_OUTPUT_EXTENSION}")
92 endif()
93 endforeach()
94
95 foreach( c_file ${ARG_C} )
96 list(APPEND sources "${c_file}.c")
97
98 list(APPEND c_inputs "${bin}/${c_file}.c")
99 list(APPEND c_outputs "${bin}/${c_file}${CMAKE_C_OUTPUT_EXTENSION}")
100 endforeach()
101
102 if( NOT ARG_NOCOPY )
103 foreach( source ${sources} )
104 add_custom_command(
105 OUTPUT "${bin}/${source}"
106 COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${src}/${source}" "${bin}"
107 DEPENDS "${src}/${source}"
108 COMMENT "Copying ${source} to build area")
109 endforeach()
110 endif()
111
112 foreach( c_input ${c_inputs} )
113 get_filename_component(basename "${c_input}" NAME_WE)
114 add_custom_command(
115 OUTPUT "${basename}${CMAKE_C_OUTPUT_EXTENSION}"
116 COMMAND "${OCAMLFIND}" "ocamlc" "-c" "${c_input}" -ccopt ${c_flags}
117 DEPENDS "${c_input}"
118 COMMENT "Building OCaml stub object file ${basename}${CMAKE_C_OUTPUT_EXTENSION}"
119 VERBATIM)
120 endforeach()
121
122 set(ocaml_params)
123 foreach( ocaml_input ${ocaml_inputs} ${c_outputs})
124 get_filename_component(filename "${ocaml_input}" NAME)
125 list(APPEND ocaml_params "${filename}")
126 endforeach()
127
128 if( APPLE )
129 set(ocaml_rpath "@executable_path/../../lib")
130 elseif( UNIX )
131 set(ocaml_rpath "\\$ORIGIN/../../lib")
132 endif()
133 list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}")
134
135 add_custom_command(
136 OUTPUT ${ocaml_outputs}
137 COMMAND "${OCAMLFIND}" "ocamlmklib" "-o" "${name}" ${ocaml_flags} ${ocaml_params}
138 DEPENDS ${ocaml_inputs} ${c_outputs}
139 COMMENT "Building OCaml library ${name}"
140 VERBATIM)
141
142 add_custom_command(
143 OUTPUT "${bin}/${name}.odoc"
144 COMMAND "${OCAMLFIND}" "ocamldoc"
145 "-I" "${bin}"
146 "-I" "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/"
147 "-dump" "${bin}/${name}.odoc"
148 ${ocaml_pkgs} ${ocaml_inputs}
149 DEPENDS ${ocaml_inputs}
150 COMMENT "Building OCaml documentation for ${name}"
151 VERBATIM)
152
153 add_custom_target("ocaml_${name}" ALL DEPENDS ${ocaml_outputs} "${bin}/${name}.odoc")
154
155 set_target_properties("ocaml_${name}" PROPERTIES
156 OCAML_FLAGS "-I;${bin}")
157 set_target_properties("ocaml_${name}" PROPERTIES
158 OCAML_ODOC "${bin}/${name}.odoc")
159
160 foreach( ocaml_dep ${ARG_OCAMLDEP} )
161 add_dependencies("ocaml_${name}" "ocaml_${ocaml_dep}")
162 endforeach()
163
164 foreach( llvm_lib ${llvm_libs} )
165 add_dependencies("ocaml_${name}" "${llvm_lib}")
166 endforeach()
167
168 set(install_files)
169 set(install_shlibs)
170 foreach( ocaml_output ${ocaml_outputs} )
171 get_filename_component(ext "${ocaml_output}" EXT)
172
173 if( NOT (ext STREQUAL ".cmo" OR
174 ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR
175 ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) )
176 list(APPEND install_files "${ocaml_output}")
177 elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX)
178 list(APPEND install_shlibs "${ocaml_output}")
179 endif()
180 endforeach()
181
182 install(FILES ${install_files}
183 DESTINATION lib/ocaml)
184 install(FILES ${install_shlibs}
185 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
186 GROUP_READ GROUP_EXECUTE
187 WORLD_READ WORLD_EXECUTE
188 DESTINATION lib/ocaml)
189
190 foreach( install_file ${install_files} ${install_shlibs} )
191 get_filename_component(filename "${install_file}" NAME)
192 add_custom_command(TARGET "ocaml_${name}" POST_BUILD
193 COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${install_file}"
194 "${LLVM_LIBRARY_OUTPUT_INTDIR}/ocaml/"
195 COMMENT "Copying OCaml library component ${filename} to intermediate area"
196 VERBATIM)
197 endforeach()
198 endfunction()
0 # CMake find_package() module for the OCaml language.
1 # Assumes ocamlfind will be used for compilation.
2 # http://ocaml.org/
3 #
4 # Example usage:
5 #
6 # find_package(OCaml)
7 #
8 # If successful, the following variables will be defined:
9 # OCAMLFIND
10 # OCAML_VERSION
11 # OCAML_STDLIB_PATH
12 # HAVE_OCAMLOPT
13 #
14 # Also provides find_ocamlfind_package() macro.
15 #
16 # Example usage:
17 #
18 # find_ocamlfind_package(ctypes)
19 #
20 # In any case, the following variables are defined:
21 #
22 # HAVE_OCAML_${pkg}
23 #
24 # If successful, the following variables will be defined:
25 #
26 # OCAML_${pkg}_VERSION
27
28 include( FindPackageHandleStandardArgs )
29
30 find_program(OCAMLFIND
31 NAMES ocamlfind)
32
33 if( OCAMLFIND )
34 execute_process(
35 COMMAND ${OCAMLFIND} ocamlc -version
36 OUTPUT_VARIABLE OCAML_VERSION
37 OUTPUT_STRIP_TRAILING_WHITESPACE)
38
39 execute_process(
40 COMMAND ${OCAMLFIND} ocamlc -where
41 OUTPUT_VARIABLE OCAML_STDLIB_PATH
42 OUTPUT_STRIP_TRAILING_WHITESPACE)
43
44 execute_process(
45 COMMAND ${OCAMLFIND} ocamlc -version
46 OUTPUT_QUIET
47 RESULT_VARIABLE find_ocaml_result)
48 if( find_ocaml_result EQUAL 0 )
49 set(HAVE_OCAMLOPT TRUE)
50 else()
51 set(HAVE_OCAMLOPT FALSE)
52 endif()
53 endif()
54
55 find_package_handle_standard_args( OCaml DEFAULT_MSG
56 OCAMLFIND
57 OCAML_VERSION
58 OCAML_STDLIB_PATH)
59
60 mark_as_advanced(
61 OCAMLFIND)
62
63 function(find_ocamlfind_package pkg)
64 CMAKE_PARSE_ARGUMENTS(ARG "OPTIONAL" "VERSION" "" ${ARGN})
65
66 execute_process(
67 COMMAND "${OCAMLFIND}" "query" "${pkg}" "-format" "%v"
68 RESULT_VARIABLE result
69 OUTPUT_VARIABLE version
70 ERROR_VARIABLE error
71 OUTPUT_STRIP_TRAILING_WHITESPACE
72 ERROR_STRIP_TRAILING_WHITESPACE)
73
74 if( NOT result EQUAL 0 AND NOT ARG_OPTIONAL )
75 message(FATAL_ERROR ${error})
76 endif()
77
78 if( result EQUAL 0 )
79 set(found TRUE)
80 else()
81 set(found FALSE)
82 endif()
83
84 if( found AND ARG_VERSION )
85 if( version VERSION_LESS ARG_VERSION AND ARG_OPTIONAL )
86 # If it's optional and the constraint is not satisfied, pretend
87 # it wasn't found.
88 set(found FALSE)
89 elseif( version VERSION_LESS ARG_VERSION )
90 message(FATAL_ERROR
91 "ocamlfind package ${pkg} should have version ${ARG_VERSION} or newer")
92 endif()
93 endif()
94
95 string(TOUPPER ${pkg} pkg)
96
97 set(HAVE_OCAML_${pkg} ${found}
98 PARENT_SCOPE)
99
100 set(OCAML_${pkg}_VERSION ${version}
101 PARENT_SCOPE)
102 endfunction()
103103
104104 endif()
105105 endif()
106
107 list(FIND LLVM_BINDINGS_LIST ocaml uses_ocaml)
108 if( NOT uses_ocaml LESS 0 )
109 set(doc_targets
110 ocaml_llvm
111 ocaml_llvm_all_backends
112 ocaml_llvm_analysis
113 ocaml_llvm_bitreader
114 ocaml_llvm_bitwriter
115 ocaml_llvm_executionengine
116 ocaml_llvm_irreader
117 ocaml_llvm_linker
118 ocaml_llvm_target
119 ocaml_llvm_ipo
120 ocaml_llvm_passmgr_builder
121 ocaml_llvm_scalar_opts
122 ocaml_llvm_vectorize
123 )
124
125 foreach(llvm_target ${LLVM_TARGETS_TO_BUILD})
126 list(APPEND doc_targets ocaml_llvm_${llvm_target})
127 endforeach()
128
129 set(odoc_files)
130 foreach( doc_target ${doc_targets} )
131 get_target_property(odoc_file ${doc_target} OCAML_ODOC)
132 list(APPEND odoc_files -load ${odoc_file})
133 endforeach()
134
135 add_custom_target(ocaml_doc
136 COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
137 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
138 COMMAND ${OCAMLFIND} ocamldoc -d ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
139 -sort -colorize-code -html ${odoc_files})
140
141 add_dependencies(ocaml_doc ${doc_targets})
142
143 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
144 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
145 DESTINATION docs/ocaml/html)
146 endif()
147 endif()
22 if not 'ocaml' in config.root.llvm_bindings:
33 config.unsupported = True
44
5 if config.root.have_ocaml_ounit != '1':
5 if config.root.have_ocaml_ounit not in ('1', 'TRUE'):
66 config.unsupported = True
6767 set(LLVM_TEST_DEPENDS ${LLVM_TEST_DEPENDS} llvm-go)
6868 endif()
6969
70 if(TARGET ocaml_llvm)
71 set(LLVM_TEST_DEPENDS ${LLVM_TEST_DEPENDS}
72 ocaml_llvm
73 ocaml_llvm_all_backends
74 ocaml_llvm_analysis
75 ocaml_llvm_bitreader
76 ocaml_llvm_bitwriter
77 ocaml_llvm_executionengine
78 ocaml_llvm_irreader
79 ocaml_llvm_linker
80 ocaml_llvm_target
81 ocaml_llvm_ipo
82 ocaml_llvm_passmgr_builder
83 ocaml_llvm_scalar_opts
84 ocaml_llvm_vectorize
85 )
86 endif()
87
7088 add_lit_testsuite(check-llvm "Running the LLVM regression tests"
7189 ${CMAKE_CURRENT_BINARY_DIR}
7290 PARAMS llvm_site_config=${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
195195 # OCaml substitutions.
196196 # Support tests for both native and bytecode builds.
197197 config.substitutions.append( ('%ocamlc',
198 "%s ocamlc %s" % (config.ocamlfind_executable, config.ocaml_flags)) )
198 "%s ocamlc -cclib -L%s %s" %
199 (config.ocamlfind_executable, llvm_lib_dir, config.ocaml_flags)) )
199200 if config.have_ocamlopt in ('1', 'TRUE'):
200201 config.substitutions.append( ('%ocamlopt',
201202 "%s ocamlopt -cclib -L%s -cclib -Wl,-rpath,%s %s" %