summaryrefslogtreecommitdiffstats
path: root/cmake/modules/CuraMacros.cmake
blob: 0b750f705df2d9fe0ac2d64f4f696c6de9ab74bb (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

# This macro takes name of the MOF for one provider and header files using
# konkretcmpi. It also generates provider skeleton if it doesn't exist.
#
# @param[in] MOF name of the MOF file (should be in mof/ directory of the project root)
# @param[out] CIM_PROVIDERS list of sources of the provider generated from the MOF
# @param[out] CIM_HEADERS list of header file generated from the MOF
#
macro(konkretcmpi_generate MOF CIM_PROVIDERS CIM_HEADERS)
    # Check if MOF exists
    set(MOF_FILE ${CMAKE_SOURCE_DIR}/mof/${MOF})
    message(STATUS "Using mof ${MOF} ${MOF_FILE}")
    if (NOT EXISTS ${MOF_FILE})
        message(FATAL_ERROR "MOF file ${MOF} not found")
    endif (NOT EXISTS ${MOF_FILE})

    # Read CIM classes out of MOF file
    execute_process(COMMAND sed -e "/class/ !D" -e "s/class \\(.*\\):.*/\\1/g"
                    INPUT_FILE ${MOF_FILE}
                    OUTPUT_VARIABLE CIM_CLASSES
    )

    if (${CIM_CLASSES} STREQUAL "")
        message(FATAL_ERROR "No class found in the MOF file ${MOF_FILE}")
    endif (${CIM_CLASSES} STREQUAL "")

    # And fill list with them
    string(REGEX MATCHALL "[a-zA-Z_-]+" CIM_CLASSES ${CIM_CLASSES})

    # Get headers and sources names from the list of CIM classes
    set(HEADERS "")
    set(PROVIDERS "")
    set(GENERATE_PROVIDERS "")
    set(NEW_PROVIDERS "")
    foreach(CLASS ${CIM_CLASSES})
        # Add generated header to the list
        set(HEADERS ${HEADERS} ${CLASS}.h)
        # Get name of the source file
        set(PROVIDER ${CLASS}Provider.c)
        # If the provider doesn't exist, generate it
        if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${PROVIDER})
            # Part of generating command - passed to konkret
            set(GENERATE_PROVIDERS ${GENERATE_PROVIDERS} -s ${CLASS})
            # List of freshly generated providers
            set(NEW_PROVIDERS ${NEW_PROVIDERS} ${PROVIDER})
        endif (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${PROVIDER})
        # Add provider source to the list
        set(PROVIDERS ${PROVIDERS} ${PROVIDER})
    endforeach(CLASS ${CIM_CLASSES})

    # Generate headers for CIM classes
    set(ENV{KONKRET_SCHEMA_DIR} "/usr/share/mof/cim-current")
    execute_process(COMMAND ${KONKRETCMPI_KONKRET}
                            #-m /usr/share/sblim-cmpi-base/Linux_Base.mof
                            -m ${MOF_FILE}
                            ${GENERATE_PROVIDERS}
                            ${CIM_CLASSES}
                    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                    RESULT_VARIABLE RES
                    OUTPUT_VARIABLE OUT
                    ERROR_VARIABLE ERR
                )

    # Show error message when konkret fails
    if (NOT ${RES} EQUAL 0)
        message(FATAL_ERROR "KonkretCMPI failed: ${RES} ${ERR}")
    endif (NOT ${RES} EQUAL 0)

    # Move pregenerated sources for providers to source directory
    foreach(PROVIDER ${NEW_PROVIDERS})
        file(RENAME ${CMAKE_CURRENT_BINARY_DIR}/${PROVIDER} ${CMAKE_CURRENT_SOURCE_DIR}/${PROVIDER})
    endforeach(PROVIDER ${NEW_PROVIDERS})

    # Return to caller
    set(${CIM_HEADERS} ${HEADERS})
    set(${CIM_PROVIDERS} ${PROVIDERS})
endmacro(konkretcmpi_generate MOF PROVIDERS HEADERS)

# This macro creates registration file from shared library
#
# @param[in] PROVIDER_NAME human-readable name of the provider
# @param[in] LIBRARY_NAME name of the library without lib prefix and .so suffix (same as for add_library)
# @param[in] MOF name of the MOF file
#
macro(cim_registration PROVIDER_NAME LIBRARY_NAME MOF)
    # Create registration out of shared library
    add_custom_command(TARGET ${LIBRARY_NAME}
                       POST_BUILD
                       COMMAND ${KONKRETCMPI_KONKRETREG} -r lib${LIBRARY_NAME}.so > ${PROVIDER_NAME}.registration
                       COMMENT "Generating .reg file from library for ${PROVIDER_NAME}"
                       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    )
    # Install it
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROVIDER_NAME}.registration DESTINATION share/cura-providers/)

    # Add custom target for registration
    find_file(MOF_FILE
              ${MOF}
              PATHS ${CMAKE_SOURCE_DIR}/mof/
    )
    add_custom_target(register-${PROVIDER_NAME}
                      ${CMAKE_SOURCE_DIR}/provider-register.sh -m ${MOF_FILE} -r ${CMAKE_CURRENT_BINARY_DIR}/${PROVIDER_NAME}.registration)
endmacro(cim_registration)