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}) target_link_libraries(extentsampling -lgsl -lgslcblas) 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)