103 changes: 103 additions & 0 deletions llvm/cmake/modules/FindOCaml.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# CMake find_package() module for the OCaml language.
# Assumes ocamlfind will be used for compilation.
# http://ocaml.org/
#
# Example usage:
#
# find_package(OCaml)
#
# If successful, the following variables will be defined:
# OCAMLFIND
# OCAML_VERSION
# OCAML_STDLIB_PATH
# HAVE_OCAMLOPT
#
# Also provides find_ocamlfind_package() macro.
#
# Example usage:
#
# find_ocamlfind_package(ctypes)
#
# In any case, the following variables are defined:
#
# HAVE_OCAML_${pkg}
#
# If successful, the following variables will be defined:
#
# OCAML_${pkg}_VERSION

include( FindPackageHandleStandardArgs )

find_program(OCAMLFIND
NAMES ocamlfind)

if( OCAMLFIND )
execute_process(
COMMAND ${OCAMLFIND} ocamlc -version
OUTPUT_VARIABLE OCAML_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process(
COMMAND ${OCAMLFIND} ocamlc -where
OUTPUT_VARIABLE OCAML_STDLIB_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process(
COMMAND ${OCAMLFIND} ocamlc -version
OUTPUT_QUIET
RESULT_VARIABLE find_ocaml_result)
if( find_ocaml_result EQUAL 0 )
set(HAVE_OCAMLOPT TRUE)
else()
set(HAVE_OCAMLOPT FALSE)
endif()
endif()

find_package_handle_standard_args( OCaml DEFAULT_MSG
OCAMLFIND
OCAML_VERSION
OCAML_STDLIB_PATH)

mark_as_advanced(
OCAMLFIND)

function(find_ocamlfind_package pkg)
CMAKE_PARSE_ARGUMENTS(ARG "OPTIONAL" "VERSION" "" ${ARGN})

execute_process(
COMMAND "${OCAMLFIND}" "query" "${pkg}" "-format" "%v"
RESULT_VARIABLE result
OUTPUT_VARIABLE version
ERROR_VARIABLE error
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)

if( NOT result EQUAL 0 AND NOT ARG_OPTIONAL )
message(FATAL_ERROR ${error})
endif()

if( result EQUAL 0 )
set(found TRUE)
else()
set(found FALSE)
endif()

if( found AND ARG_VERSION )
if( version VERSION_LESS ARG_VERSION AND ARG_OPTIONAL )
# If it's optional and the constraint is not satisfied, pretend
# it wasn't found.
set(found FALSE)
elseif( version VERSION_LESS ARG_VERSION )
message(FATAL_ERROR
"ocamlfind package ${pkg} should have version ${ARG_VERSION} or newer")
endif()
endif()

string(TOUPPER ${pkg} pkg)

set(HAVE_OCAML_${pkg} ${found}
PARENT_SCOPE)

set(OCAML_${pkg}_VERSION ${version}
PARENT_SCOPE)
endfunction()
42 changes: 42 additions & 0 deletions llvm/docs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,45 @@ if (LLVM_ENABLE_SPHINX)

endif()
endif()

list(FIND LLVM_BINDINGS_LIST ocaml uses_ocaml)
if( NOT uses_ocaml LESS 0 )
set(doc_targets
ocaml_llvm
ocaml_llvm_all_backends
ocaml_llvm_analysis
ocaml_llvm_bitreader
ocaml_llvm_bitwriter
ocaml_llvm_executionengine
ocaml_llvm_irreader
ocaml_llvm_linker
ocaml_llvm_target
ocaml_llvm_ipo
ocaml_llvm_passmgr_builder
ocaml_llvm_scalar_opts
ocaml_llvm_vectorize
)

foreach(llvm_target ${LLVM_TARGETS_TO_BUILD})
list(APPEND doc_targets ocaml_llvm_${llvm_target})
endforeach()

set(odoc_files)
foreach( doc_target ${doc_targets} )
get_target_property(odoc_file ${doc_target} OCAML_ODOC)
list(APPEND odoc_files -load ${odoc_file})
endforeach()

add_custom_target(ocaml_doc
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
COMMAND ${OCAMLFIND} ocamldoc -d ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
-sort -colorize-code -html ${odoc_files})

add_dependencies(ocaml_doc ${doc_targets})

if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ocamldoc/html
DESTINATION docs/ocaml/html)
endif()
endif()
2 changes: 1 addition & 1 deletion llvm/test/Bindings/OCaml/lit.local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ config.suffixes = ['.ml']
if not 'ocaml' in config.root.llvm_bindings:
config.unsupported = True

if config.root.have_ocaml_ounit != '1':
if config.root.have_ocaml_ounit not in ('1', 'TRUE'):
config.unsupported = True
18 changes: 18 additions & 0 deletions llvm/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ if(TARGET llvm-go)
set(LLVM_TEST_DEPENDS ${LLVM_TEST_DEPENDS} llvm-go)
endif()

if(TARGET ocaml_llvm)
set(LLVM_TEST_DEPENDS ${LLVM_TEST_DEPENDS}
ocaml_llvm
ocaml_llvm_all_backends
ocaml_llvm_analysis
ocaml_llvm_bitreader
ocaml_llvm_bitwriter
ocaml_llvm_executionengine
ocaml_llvm_irreader
ocaml_llvm_linker
ocaml_llvm_target
ocaml_llvm_ipo
ocaml_llvm_passmgr_builder
ocaml_llvm_scalar_opts
ocaml_llvm_vectorize
)
endif()

add_lit_testsuite(check-llvm "Running the LLVM regression tests"
${CMAKE_CURRENT_BINARY_DIR}
PARAMS llvm_site_config=${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ config.substitutions.append( ('%python', config.python_executable) )
# OCaml substitutions.
# Support tests for both native and bytecode builds.
config.substitutions.append( ('%ocamlc',
"%s ocamlc %s" % (config.ocamlfind_executable, config.ocaml_flags)) )
"%s ocamlc -cclib -L%s %s" %
(config.ocamlfind_executable, llvm_lib_dir, config.ocaml_flags)) )
if config.have_ocamlopt in ('1', 'TRUE'):
config.substitutions.append( ('%ocamlopt',
"%s ocamlopt -cclib -L%s -cclib -Wl,-rpath,%s %s" %
Expand Down