Projekt

Allgemein

Profil

Feature #837 » config_header.txt

Maximilian Seesslen, 22.10.2026 15:02

 
1
#------------------------------------------------------------------------------
2
#
3
# @brief	CMake package for handling configuration headers in campo projects
4
#
5
#    An configuration header can be specified via cmake variable 
6
#    COMMON_CONFIG_HEADER. 
7
#    This header will not be included directly but another configuration header
8
#    Will be generated with project specific defines only.
9
#    This generated header could then be installed into a sysroot.
10
#
11
#    There is also a checksum included in the header as define which can be 
12
#    used verified that used headers match used library binary.
13
#
14
#    In the CMakeLists.txt, "add_generated_config( <target> )" has to be added 
15
#    after declaring the target.
16
#
17
#    In source code, the define <PROJECT>_GENERATED_CONFIG has to be used to 
18
#    check weather the generated header has to be included or the one from the 
19
#    sysroot.
20
#
21
# @author     Maximilian Seesslen <src@seesslen.net>
22
# @copyright  SPDX-License-Identifier: Apache-2.0
23
#
24
#------------------------------------------------------------------------------
25

    
26

    
27
function( add_generated_config project )
28
   d
29
   # It's not guaranteed that the target is equal to ${PROJECT_NAME}
30
   # set( project ${PROJECT_NAME} )
31
   
32
   string(TOUPPER ${project} PROJECT )
33
   
34
   get_property(
35
      TARGET_DEFS
36
      TARGET ${project}
37
      PROPERTY COMPILE_DEFINITIONS
38
   )
39

    
40
   get_target_property(
41
      INTERFACE_DEFS
42
      ${project}
43
      INTERFACE_COMPILE_DEFINITIONS
44
   )
45

    
46
   get_directory_property(
47
      DIRECTORY_DEFS
48
      COMPILE_DEFINITIONS
49
   )
50

    
51
   # get_property(XDEFS DIRECTORY PROPERTY COMPILE_DEFINITIONS)
52

    
53
   # message( FATAL_ERROR "### CD ${XDEFS}")
54
   list( APPEND DEFS  ${DIRECTORY_DEFS} ${TARGET_DEFS} ${INTERFACE_DEFS} )
55
   list( TRANSFORM DEFS PREPEND "-D" )
56
   
57
   if ( COMMON_CONFIG_HEADER )
58
      set ( ${PROJECT}_CONFIG_HEADER "${COMMON_CONFIG_HEADER${VARIANT_POSTFIX_UPPER_CASE}}" PARENT_SCOPE)
59
      set ( ${PROJECT}_CONFIG_HEADER "${COMMON_CONFIG_HEADER${VARIANT_POSTFIX_UPPER_CASE}}")
60
      if( NOT COMMON_CONFIG_HEADER${VARIANT_POSTFIX_UPPER_CASE} )
61
         message( WARNING "A variant '${VARIANT_POSTFIX}' is used but the common header is not defined for it.")
62
         message( WARNING " Please set COMMON_CONFIG_HEADER${VARIANT_POSTFIX_UPPER_CASE} in top level cmake file." ) 
63
         message( WARNING " I will use COMMON_CONFIG_HEADER instead." ) 
64
         set ( ${PROJECT}_CONFIG_HEADER "${COMMON_CONFIG_HEADER}" PARENT_SCOPE)
65
         set ( ${PROJECT}_CONFIG_HEADER "${COMMON_CONFIG_HEADER}")
66
      endif()
67
   elseif( NOT ${PROJECT}_CONFIG_HEADER )
68
      set ( ${PROJECT}_CONFIG_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/include/${project}/presets/config_full.h" PARENT_SCOPE)
69
      set ( ${PROJECT}_CONFIG_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/include/${project}/presets/config_full.h")
70
   endif()
71
   
72
   set( input_header ${${PROJECT}_CONFIG_HEADER} )
73
   set( generated_header ${CMAKE_CURRENT_BINARY_DIR}/${project}/config_generated_local.h )
74

    
75
   if( NOT ${PROJECT}_CONFIG_HEADER )
76
      message( FATAL_ERROR "Config header could not be evaluated." )
77
   endif()
78

    
79
   add_custom_command(
80
      OUTPUT ${generated_header}
81
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
82
      COMMAND
83
         mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${project}
84
      COMMAND
85
         echo "// Generated configuration header. Do not change!" > ${generated_header}
86
      COMMAND
87
         ${CMAKE_CXX_COMPILER} -include ${input_header} ${DEFS}
88
            -dM -E - < /dev/null >> ${CMAKE_CURRENT_BINARY_DIR}/config_generated_${project}_all.h
89
      COMMAND
90
         ${CMAKE_CXX_COMPILER} -include ${input_header} ${DEFS}
91
            -dM -E - < /dev/null | grep CONFIG_${PROJECT} >> ${generated_header}
92
      COMMAND
93
         echo -n "\\#define ${PROJECT}_CONFIG_CHECKSUM 0x" >> ${generated_header}
94
      COMMAND
95
         bash -c 'sha256sum ${generated_header} | cut -c1-8' >> ${generated_header}
96
      COMMAND
97
         echo  "\\#define ${project}_CONFIG_CHECKSUM ${PROJECT}_CONFIG_CHECKSUM" >> ${generated_header}
98
      COMMAND
99
         echo -n "\\#define ${PROJECT}_CODE_SHA 0x" >> ${generated_header}
100
      COMMAND
101
         git --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git rev-parse HEAD | head -c8  >> ${generated_header}
102
      COMMAND
103
         echo >> ${generated_header}
104
      COMMAND
105
         echo  "\\#define ${project}_CODE_SHA ${PROJECT}_CODE_SHA" >> ${generated_header}
106
      COMMAND
107
         echo  "\\#define ${PROJECT}_CONFIGURED 1" >> ${generated_header}
108
      COMMAND
109
         echo >> ${generated_header}
110
      COMMAND
111
         echo "//--- Fin ---------------------------" >> ${generated_header}
112
      DEPENDS ${input_header}
113
   )
114

    
115
   #add_custom_target(
116
   #   generate_config_${project}
117
   #   DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/config_generated_${project}.h
118
   #)
119

    
120
# endfunction()
121

    
122
# function( use_generated_config project )
123

    
124
   # string(TOUPPER ${project} PROJECT )
125

    
126
   target_compile_definitions(
127
      ${project}
128
      PUBLIC
129
         -D${PROJECT}_LOCAL_CONFIG
130
   )
131

    
132
   target_include_directories(
133
      ${project}
134
      PUBLIC
135
         ${CMAKE_CURRENT_BINARY_DIR}
136
   )
137

    
138
   # message( FATAL_ERROR "### Header: ${CMAKE_CURRENT_BINARY_DIR}/${project}/config_generated_local.h" )
139

    
140
   #add_dependencies( ${project} generate_config_${project} )
141
   target_sources(
142
      ${project}
143
      PUBLIC
144
         ${generated_header}
145
   )
146

    
147
   #add_dependencies( ${project} generate_config_${project} )
148

    
149
endfunction()
150

    
151

    
152
#------------------------------------------------------------------------------
    (1-1/1)