aboutsummaryrefslogtreecommitdiffstats
path: root/cmake/modules/FindOCaml.cmake
blob: 8eba2127df1b361d9b70728d9f09a85753eb3286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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()