aboutsummaryrefslogtreecommitdiff
path: root/CMakeLists.txt
blob: 5c0adf227b08ccabcbf51aca39828f834321a7c6 (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
cmake_minimum_required(VERSION 3.10)
project(extent-sampling VERSION 0.1.0)

find_package(GSL REQUIRED)
find_program(SC NAMES sc REQUIRED)
find_program(INDENT NAMES indent)
# TODO: Maybe, support other versions of guile such as 2.2.
pkg_check_modules(GUILE guile-3.0)
pkg_get_variable(GUILD guile-3.0 guild)

function(change_extension var filename extension)
  get_filename_component(basename ${filename} NAME_WE)
  get_filename_component(dirname ${filename} DIRECTORY)
  set(${var} ${dirname}/${basename}.${extension} PARENT_SCOPE)
endfunction()

# Generate C source files from SC source files, and optionally indent
# them.
file(GLOB SC_SOURCES "src/*.sc")
foreach(sc_source ${SC_SOURCES})
  get_filename_component(source_basename ${sc_source} NAME_WE)
  set(c_file ${source_basename}.c)
  if(${INDENT} STREQUAL INDENT-NOTFOUND)
    add_custom_command(
      OUTPUT ${c_file}
      COMMAND ${SC} ${sc_source} ${c_file}
      DEPENDS ${sc_source} src/macros/macros.sc
      VERBATIM)
  else()
    add_custom_command(
      OUTPUT ${c_file}
      COMMAND ${SC} ${sc_source} ${c_file}
      COMMAND ${INDENT} ${c_file}
      DEPENDS ${sc_source} src/macros/macros.sc
      VERBATIM)
  endif()
  list(APPEND C_SOURCES ${c_file})
endforeach()

include_directories("include")

include(GNUInstallDirs)

add_library(extentsampling SHARED ${C_SOURCES})
set_target_properties(extentsampling PROPERTIES
  PUBLIC_HEADER "include/extent-sampling.h;include/gaussian-nd-random.h;include/nd-random.h;include/oracles.h")
install(TARGETS extentsampling LIBRARY
  PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/extent-sampling)

# Build and install scheme wrapper.
if(${GUILE_FOUND})
  configure_file(scm/extent-sampling/load-libs.scm.in scm/extent-sampling/load-libs.scm)
  set(SCM_WRAPPER_PATH scm/extent-sampling/wrap.scm)
  change_extension(SCM_WRAPPER_GO_PATH ${SCM_WRAPPER_PATH} go)
  add_custom_command(
    OUTPUT ${SCM_WRAPPER_GO_PATH}
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/${SCM_WRAPPER_PATH} ${CMAKE_BINARY_DIR}/${SCM_WRAPPER_PATH}
    COMMAND ${GUILD} compile ${CMAKE_BINARY_DIR}/${SCM_WRAPPER_PATH} -o ${CMAKE_BINARY_DIR}/${SCM_WRAPPER_GO_PATH}
    DEPENDS ${SCM_WRAPPER_PATH}
    VERBATIM)
  add_custom_target(scheme ALL DEPENDS ${SCM_WRAPPER_GO_PATH})
  string(REPLACE "." ";" GUILE_VERSION_LIST ${GUILE_VERSION})
  list(GET GUILE_VERSION_LIST 0 GUILE_MAJOR_VERSION)
  list(GET GUILE_VERSION_LIST 1 GUILE_MINOR_VERSION)
  install(FILES ${CMAKE_BINARY_DIR}/scm/extent-sampling/load-libs.scm
    DESTINATION ${CMAKE_INSTALL_DATADIR}/guile/site/${GUILE_MAJOR_VERSION}.${GUILE_MINOR_VERSION}/extent-sampling)
  install(FILES ${CMAKE_BINARY_DIR}/${SCM_WRAPPER_PATH}
    DESTINATION ${CMAKE_INSTALL_DATADIR}/guile/site/${GUILE_MAJOR_VERSION}.${GUILE_MINOR_VERSION}/extent-sampling)
  install(FILES ${CMAKE_BINARY_DIR}/${SCM_WRAPPER_GO_PATH}
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/guile/site/${GUILE_MAJOR_VERSION}.${GUILE_MINOR_VERSION}/site-ccache/extent-sampling)
endif()

# Configure environment wrapper script.
configure_file(pre-inst-env.in pre-inst-env @ONLY)