c333308e7f9c6378c4710bf9dd6a4606ef82fd0f
[platform/upstream/dbus.git] / cmake / modules / MacrosAutotools.cmake
1 #
2 # cmake package for autotools support
3 #
4 # @Author Ralf Habacker
5
6
7 #
8 # load autotools configure file into an internal list named _configure_ac
9 #
10 macro(autoinit config)
11     set(_configure_ac_name ${config})
12     file(READ ${config} _configure_ac_raw)
13     # Convert file contents into a CMake list (where each element in the list
14     # is one line of the file)
15     STRING(REGEX REPLACE ";" "\\\\;" _configure_ac "${_configure_ac_raw}")
16     STRING(REGEX REPLACE "\n" ";" _configure_ac "${_configure_ac}")
17 endmacro()
18
19 # extracts version information from autoconf config file
20 # and set related cmake variables
21
22 # returns  
23 #   ${prefix}_VERSION
24 #   ${prefix}_VERSION_STRING
25 #   ${prefix}_MAJOR_VERSION
26 #   ${prefix}_MINOR_VERSION
27 #   ${prefix}_MICRO_VERSION
28 #   ${prefix}_LIBRARY_AGE
29 #   ${prefix}_LIBRARY_REVISION
30 #   ${prefix}_LIBRARY_CURRENT
31
32 macro(autoversion prefix)
33     string(TOUPPER ${prefix} prefix_upper)
34     string (REGEX REPLACE ".*${prefix}_major_version], .([0-9]+).*" "\\1" ${prefix_upper}_MAJOR_VERSION ${_configure_ac_raw})
35     string (REGEX REPLACE ".*${prefix}_minor_version], .([0-9]+).*" "\\1" ${prefix_upper}_MINOR_VERSION ${_configure_ac_raw})
36     string (REGEX REPLACE ".*${prefix}_micro_version], .([0-9]+).*" "\\1" ${prefix_upper}_MICRO_VERSION ${_configure_ac_raw})
37     set (${prefix_upper}_VERSION ${${prefix_upper}_MAJOR_VERSION}.${${prefix_upper}_MINOR_VERSION}.${${prefix_upper}_MICRO_VERSION})
38     set (${prefix_upper}_VERSION_STRING "${${prefix_upper}_VERSION}")
39     string (REGEX REPLACE ".*LT_AGE=([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_AGE ${_configure_ac_raw})
40     string (REGEX REPLACE ".*LT_CURRENT=([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_CURRENT ${_configure_ac_raw})
41     string (REGEX REPLACE ".*LT_REVISION=([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_REVISION ${_configure_ac_raw})
42 endmacro()
43
44 #
45 # Defines package related variables (PACKAGE_..., PACKAGE and VERSION)
46 # as done by autotools.
47 #
48 # Additional it defines a cmake variable named PACKAGE_CONFIG_H_TEMPLATE
49 # which could be placed in config.h templates to have those variables
50 # defined at code level like shown below:
51 #
52 # config.h.template
53 #   ...
54 #   @AUTOPACKAGE_CONFIG_H_TEMPLATE@
55 #   ...
56 #
57 macro(autopackage name version url support_url)
58     # Define to the full name of this package.
59     set(PACKAGE_NAME ${name})
60
61     # Define to the version of this package.
62     set(PACKAGE_VERSION ${version})
63
64     # Define to the home page for this package.
65     set(PACKAGE_URL ${url})
66
67     # Define to the address where bug reports for this package should be sent.
68     set(PACKAGE_BUGREPORT ${support_url})
69
70     # Define to the full name and version of this package.
71     set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
72
73     # Define to the one symbol short name of this package.
74     set(PACKAGE_TARNAME ${PACKAGE_NAME})
75
76     set(PACKAGE ${name})
77     set(VERSION ${DBUS_VERSION_STRING})
78
79     string(CONFIGURE "/* generated by cmake macro autopackage */\n
80 /* Define to the address where bug reports for this package should be sent. */
81 #define PACKAGE_BUGREPORT \"@PACKAGE_BUGREPORT@\"
82
83 /* Define to the full name of this package. */
84 #define PACKAGE_NAME \"@PACKAGE_NAME@\"
85
86 /* Define to the full name and version of this package. */
87 #define PACKAGE_STRING \"@PACKAGE_STRING@\"
88
89 /* Define to the one symbol short name of this package. */
90 #define PACKAGE_TARNAME \"@PACKAGE_TARNAME@\"
91
92 /* Define to the home page for this package. */
93 #define PACKAGE_URL \"@PACKAGE_URL@\"
94
95 /* Define to the version of this package. */
96 #define PACKAGE_VERSION \"@PACKAGE_VERSION@\"
97
98 /* defined by autotools package */
99 #define PACKAGE \"@PACKAGE@\"
100 #define VERSION \"@VERSION@\"
101 " AUTOPACKAGE_CONFIG_H_TEMPLATE)
102 endmacro(autopackage)
103
104 #
105 # define a cmake variable from autotools AC_DEFINE statement
106 #
107 macro(autodefine name)
108     foreach(line ${_configure_ac})
109         if(line MATCHES ".*AC_DEFINE(.*${name}.*).*")
110             string (REGEX REPLACE ".*AC_DEFINE(.*).*" "\\1" value ${line})
111             string (REGEX REPLACE ".*,(.*),.*" "\\1" value2 ${value})
112             string (REPLACE "[" "" value3 ${value2})
113             string (REPLACE "]" "" value4 ${value3})
114             set(${name} ${value4})
115         endif()
116     endforeach()
117 endmacro()
118
119 #
120 # fetch compiler warnings settings from autotools
121 #
122 # @param target_warnings the variable name which will contain the warnings keys
123 # @param target_disabled_warnings the variable name which will contain the disabled warnings keys
124 #
125 macro(autocompilerwarnings target_warnings target_disabled_warnings)
126     set(in_warnings 0)
127     set(content)
128     # parse configure_ac
129     foreach(line ${_configure_ac})
130         if(line MATCHES ".*TP_COMPILER_WARNINGS.*")
131             set(in_warnings 1)
132             string(REGEX REPLACE "^.*TP_COMPILER_WARNINGS\\((.*)$" "\\1" temp ${line})
133             set(content "${content}${temp}")
134         elseif(in_warnings EQUAL 1)
135             if(line MATCHES ".*dnl.*")
136                 # skip
137             elseif(line MATCHES "\\)$")
138                 string(REGEX REPLACE "(.*)\\)$" "\\1" temp ${line})
139                 set(content "${content}${temp}")
140                 set(in_warnings 2)
141             else()
142                 string(STRIP ${line} _line)
143                 if(NOT _line STREQUAL "")
144                     set(content "${content}${line}")
145                 endif()
146             endif()
147         endif()
148     endforeach()
149
150     # extract macro parts
151     string(REPLACE ";" "#" temp ${content})
152     string(REPLACE "[" "" temp ${temp})
153     string(REPLACE "]" "" temp ${temp})
154     string(REPLACE "," ";" alist ${temp})
155     list(GET alist 0 flag_name)
156     list(GET alist 2 warnings_flag)
157     list(GET alist 3 no_warnings_flag)
158     string(REPLACE "#" ";" warnings_list ${warnings_flag})
159     string(REPLACE "#" ";" no_warnings_list ${no_warnings_flag})
160     list(REMOVE_AT no_warnings_list 0)
161
162     set(warnings)
163     foreach(warning ${warnings_list})
164         string(STRIP ${warning} _warning)
165         if(NOT _warning STREQUAL "")
166             set(warnings "${warnings} ${_warning}")
167         endif()
168     endforeach()
169     set(${target_warnings} "${warnings}")
170
171     set(warnings)
172     foreach(warning ${no_warnings_list})
173         string(STRIP ${warning} _warning)
174         if(NOT _warning STREQUAL "")
175             set(warnings "${warnings} ${_warning}")
176         endif()
177     endforeach()
178     set(${target_disabled_warnings} "${warnings}")
179     if(DEBUG_MACROS)
180         message("autocompilerwarnings returns: warnings: ${${target_warnings}} disabled_warnings: ${${target_disabled_warnings}}")
181     endif()
182 endmacro()
183
184 macro(autoheaderchecks config_h_in configure_checks_file config_h_cmake)
185     file(READ ${configure_checks_file} configure_checks_file_raw)
186     file(READ ${config_h_in} _config_h_in_raw)
187     file(READ ${config_h_cmake} _config_h_cmake_raw)
188     STRING(REGEX REPLACE ";" "\\\\;" _config_h_in "${_config_h_in_raw}")
189     STRING(REGEX REPLACE "\n" ";" _config_h_in "${_config_h_in}")
190     foreach(line ${_config_h_in})
191         #message(STATUS ${line})
192         if(line MATCHES ".*HAVE_.*_H.*")
193             string (REGEX REPLACE ".*HAVE_(.*)_H.*" "\\1" key ${line})
194             set(full_key "HAVE_${key}_H")
195             if(key MATCHES ".*_.*")
196                 string(REGEX MATCH "^[A-Z0-9]+" dir ${key})
197                 string(REGEX MATCH "[A-Z0-9]+$" file ${key})
198                 string(TOLOWER ${dir} dirname)
199                 string(TOLOWER ${file} filename)
200                 set(check "check_include_file(${dirname}/${filename}.h     ${full_key})")
201                 set(config_define "#cmakedefine ${full_key}")
202             else()
203                 set(file ${key})
204                 string(TOLOWER ${file} filename)
205                 set(check "check_include_file(${filename}.h     ${full_key})")
206                 set(config_define "#cmakedefine ${full_key}")
207             endif()
208             if(NOT configure_checks_file_raw MATCHES ".*${full_key}.*")
209                 message("${check}")
210             endif()
211             if(NOT _config_h_cmake_raw MATCHES "${full_key}")
212                 message("${config_define}")
213             endif()
214         endif()
215     endforeach()
216 endmacro(autoheaderchecks)
217
218 #
219 # parses config.h template and create cmake equivalent 
220 # not implemented yet
221
222 macro(autoconfig template output)
223         file(READ ${template} contents)
224         # Convert file contents into a CMake list (where each element in the list
225         # is one line of the file)
226         STRING(REGEX REPLACE ";" "\\\\;" contents "${contents}")
227         STRING(REGEX REPLACE "\n" ";" contents "${contents}")
228         foreach(line contents)
229                 message(STATUS ${line})
230                 # find #undef lines
231                 # append to config.h #define <variable-name> <variable-content>
232         endforeach()
233 endmacro()