corryvreckan.cmake 4.98 KB
Newer Older
1
# For every module, build a separate library to be loaded by corryvreckan core
2
MACRO(corryvreckan_enable_default val)
3
4
    # Get the name of the module
    GET_FILENAME_COMPONENT(_corryvreckan_module_dir ${CMAKE_CURRENT_SOURCE_DIR} NAME)
5

6
7
    # Build all modules by default if not specified otherwise
    OPTION(BUILD_${_corryvreckan_module_dir} "Build module in directory ${_corryvreckan_module_dir}?" ${val})
8
9
ENDMACRO()

10
# Common module definitions
11
MACRO(_corryvreckan_module_define_common name)
12
13
    # Get the name of the module
    GET_FILENAME_COMPONENT(_corryvreckan_module_dir ${CMAKE_CURRENT_SOURCE_DIR} NAME)
14

15
16
    # Build all modules by default if not specified otherwise
    OPTION(BUILD_${_corryvreckan_module_dir} "Build module in directory ${_corryvreckan_module_dir}?" ON)
17

18
19
    # Quit the file if not building this file or all modules
    IF(NOT (BUILD_${_corryvreckan_module_dir} OR BUILD_ALL_MODULES))
20
21
22
23
        RETURN()
    ENDIF()

    # Put message
24
    MESSAGE( STATUS "Building module: " ${_corryvreckan_module_dir} )
25

26
27
    # Prepend with the module prefix to create the name of the module
    SET(${name} "CorryvreckanModule${_corryvreckan_module_dir}")
28

29
30
    # Save the module library for prelinking in the executable (NOTE: see exec folder)
    SET(CORRYVRECKAN_MODULE_LIBRARIES ${CORRYVRECKAN_MODULE_LIBRARIES} ${${name}} CACHE INTERNAL "Module libraries")
31

32
33
    # Set default module class name
    SET(_corryvreckan_module_class "${_corryvreckan_module_dir}")
34

35
    # Find if alternative module class name is passed or we can use the default
36
37
38
    SET (extra_macro_args ${ARGN})
    LIST(LENGTH extra_macro_args num_extra_args)
    IF (${num_extra_args} GREATER 0)
39
40
        MESSAGE (AUTHOR_WARNING "Provided non-standard module class name! Naming it ${_corryvreckan_module_class} is recommended")
        LIST(GET extra_macro_args 0 _corryvreckan_module_class)
41
42
43
    ENDIF ()

    # check if main header file is defined
44
45
    IF(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_corryvreckan_module_class}.h")
        MESSAGE(FATAL_ERROR "Header file ${_corryvreckan_module_class}.h does not exist, cannot build module! \
46
47
48
49
50
51
52
53
54
55
Create the header or provide the alternative class name as first argument")
    ENDIF()

    # Define the library
    ADD_LIBRARY(${${name}} SHARED "")

    # Add the current directory as include directory
    TARGET_INCLUDE_DIRECTORIES(${${name}} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

    # Set the special header flags and add the special dynamic implementation file
56
57
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_NAME=${_corryvreckan_module_class})
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_HEADER="${_corryvreckan_module_class}.h")
58

59
60
    TARGET_SOURCES(${${name}} PRIVATE "${PROJECT_SOURCE_DIR}/src/core/module/dynamic_module_impl.cpp")
    SET_PROPERTY(SOURCE "${PROJECT_SOURCE_DIR}/src/core/module/dynamic_module_impl.cpp" APPEND PROPERTY OBJECT_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${_corryvreckan_module_class}.h")
61
62
ENDMACRO()

63
64
# Put this at the start of every global module
MACRO(corryvreckan_global_module name)
65
66
67
    _corryvreckan_module_define_common(${name} ${ARGN})

    # Set the unique flag to true
68
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_GLOBAL=1)
69
70
71
72
73
74
75
ENDMACRO()

# Put this at the start of every detector module
MACRO(corryvreckan_detector_module name)
    _corryvreckan_module_define_common(${name} ${ARGN})

    # Set the unique flag to false
76
77
78
79
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_GLOBAL=0)
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_DUT=0)
ENDMACRO()

80
81
82
83
84
85
86
87
88
89
90
# Append list of possible detector types as compile definition
MACRO(corryvreckan_detector_type name)
    SET(extra_macro_args ${ARGN})

    LIST(LENGTH extra_macro_args num_extra_args)
    IF(${num_extra_args} GREATER 0)
        STRING(REPLACE ";" "," TYPESLIST "${extra_macro_args}")
        TARGET_COMPILE_DEFINITIONS(${name} PRIVATE CORRYVRECKAN_DETECTOR_TYPE=${TYPESLIST})
    ENDIF()
ENDMACRO()

91
92
93
94
95
96
97
# Put this at the start of every detector module
MACRO(corryvreckan_dut_module name)
    _corryvreckan_module_define_common(${name} ${ARGN})

    # Set the unique flag to false
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_GLOBAL=0)
    TARGET_COMPILE_DEFINITIONS(${${name}} PRIVATE CORRYVRECKAN_MODULE_DUT=1)
98
99
ENDMACRO()

100
101
# Add sources to the module
MACRO(corryvreckan_module_sources name)
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    # Get the list of sources
    SET(_list_var "${ARGN}")
    LIST(REMOVE_ITEM _list_var ${name})

    # Include directories for dependencies
    INCLUDE_DIRECTORIES(SYSTEM ${CORRYVRECKAN_DEPS_INCLUDE_DIRS})

    # Add the library
    TARGET_SOURCES(${name} PRIVATE ${_list_var})

    # Link the standard corryvreckan libraries
    TARGET_LINK_LIBRARIES(${name} ${CORRYVRECKAN_LIBRARIES} ${CORRYVRECKAN_DEPS_LIBRARIES})
ENDMACRO()

116
117
# Provide default install target for the module
MACRO(corryvreckan_module_install name)
118
119
120
121
122
    INSTALL(TARGETS ${name}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)
ENDMACRO()