]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STARLIGHT/starlight/cmake_modules/FindROOT.cmake
STARLIGHT code and interface
[u/mrichter/AliRoot.git] / STARLIGHT / starlight / cmake_modules / FindROOT.cmake
CommitLineData
da32329d
AM
1###########################################################################
2#
3# Copyright 2010
4#
5# This file is part of Starlight.
6#
7# Starlight is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# Starlight is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Starlight. If not, see <http://www.gnu.org/licenses/>.
19#
20###########################################################################
21#
22# File and Version Information:
23# $Rev:: 28 $: revision of last commit
24# $Author:: bgrube $: author of last commit
25# $Date:: 2010-12-10 19:30:01 +0100 #$: date of last commit
26#
27# Description:
28# cmake module for finding ROOT installation
29# requires root-config to be in PATH
30# based on AliRoots's FindROOT.cmake (r41015)
31# in https://alisoft.cern.ch/AliRoot/trunk/cmake/modules
32#
33# following variables are defined:
34# ROOT_CONFIG_EXECUTABLE - path to root-config program
35# ROOTSYS - path to root installation directory
36# ROOT_TARGET - target architecture
37# ROOT_F77 - Fortran complier used building ROOT
38# ROOT_CC - C complier used building ROOT
39# ROOT_CPP - C++ complier used building ROOT
40# ROOT_VERSION - ROOT version
41# ROOT_SVN_REVISION - ROOT subversion revision
42# ROOT_BIN_DIR - ROOT executable directory
43# ROOT_INCLUDE_DIR - ROOT header directory
44# ROOT_LIBRARY_DIR - ROOT library directory
45# ROOT_LIBRARIES - linker flags for ROOT libraries
46# ROOT_AUX_LIBRARIES - linker flags for auxiliary libraries
47# ROOTCINT_EXECUTABLE - path to rootcint program
48# ROOT_MAJOR_VERSION - ROOT major version
49# ROOT_MINOR_VERSION - ROOT minor version
50# ROOT_PATCH_VERSION - ROOT patch level
51# ROOT_LIBS - list of ROOT library files
52#
53# Example usage:
54# find_package(ROOT 5.26 REQUIRED Minuit2)
55#
56#
57# The module also provides a function to generate ROOT dictionaries.
58# Example usage:
59# set(ROOTPWA_DICTIONARY ${CMAKE_CURRENT_BINARY_DIR}/someDict.cc) # set dictionary path
60# root_generate_dictionary(
61# "${ROOTPWA_DICTIONARY}" # path to dictionary to generate
62# "${INCLUDE_DIR1};${INCLUDE_DIR2}" # list of includes
63# "class1.h;class2.h;class3.h" # list of classes to process
64# "someLinkDef.h" # ROOT linkDef file
65# )
66# set(SOURCES ${SOURCES} ${ROOTPWA_DICTIONARY}) # append dictionary to sources
67#
68#
69###########################################################################
70
71
72set(ROOT_FOUND FALSE)
73set(ROOT_ERROR_REASON "")
74set(ROOT_DEFINITIONS "")
75set(ROOT_LIBS)
76
77
78find_program(ROOT_CONFIG_EXECUTABLE root-config)
79if(NOT ROOT_CONFIG_EXECUTABLE)
80 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} Cannot find root-config.")
81else()
82
83 set(ROOT_FOUND TRUE)
84
85 execute_process(
86 COMMAND ${ROOT_CONFIG_EXECUTABLE} --prefix
87 OUTPUT_VARIABLE ROOTSYS
88 OUTPUT_STRIP_TRAILING_WHITESPACE)
89
90 execute_process(
91 COMMAND ${ROOT_CONFIG_EXECUTABLE} --arch
92 OUTPUT_VARIABLE ROOT_TARGET
93 OUTPUT_STRIP_TRAILING_WHITESPACE)
94
95 execute_process(
96 COMMAND ${ROOT_CONFIG_EXECUTABLE} --f77
97 OUTPUT_VARIABLE ROOT_F77
98 OUTPUT_STRIP_TRAILING_WHITESPACE)
99
100 execute_process(
101 COMMAND ${ROOT_CONFIG_EXECUTABLE} --cc
102 OUTPUT_VARIABLE ROOT_CC
103 OUTPUT_STRIP_TRAILING_WHITESPACE)
104
105 execute_process(
106 COMMAND ${ROOT_CONFIG_EXECUTABLE} --cxx
107 OUTPUT_VARIABLE ROOT_CPP
108 OUTPUT_STRIP_TRAILING_WHITESPACE)
109
110 execute_process(
111 COMMAND ${ROOT_CONFIG_EXECUTABLE} --version
112 OUTPUT_VARIABLE ROOT_VERSION
113 OUTPUT_STRIP_TRAILING_WHITESPACE)
114
115 execute_process(
116 COMMAND ${ROOT_CONFIG_EXECUTABLE} --svn-revision
117 OUTPUT_VARIABLE ROOT_SVN_REVISION
118 OUTPUT_STRIP_TRAILING_WHITESPACE)
119
120 execute_process(
121 COMMAND ${ROOT_CONFIG_EXECUTABLE} --bindir
122 OUTPUT_VARIABLE ROOT_BIN_DIR
123 OUTPUT_STRIP_TRAILING_WHITESPACE)
124 if(NOT EXISTS "${ROOT_BIN_DIR}")
125 set(ROOT_FOUND FALSE)
126 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} ROOT executable directory ${ROOT_BIN_DIR} does not exist.")
127 endif()
128
129 execute_process(
130 COMMAND ${ROOT_CONFIG_EXECUTABLE} --incdir
131 OUTPUT_VARIABLE ROOT_INCLUDE_DIR
132 OUTPUT_STRIP_TRAILING_WHITESPACE)
133 if(NOT EXISTS "${ROOT_INCLUDE_DIR}")
134 set(ROOT_FOUND FALSE)
135 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} ROOT include directory ${ROOT_INCLUDE_DIR} does not exist.")
136 endif()
137
138 execute_process(
139 COMMAND ${ROOT_CONFIG_EXECUTABLE} --libdir
140 OUTPUT_VARIABLE ROOT_LIBRARY_DIR
141 OUTPUT_STRIP_TRAILING_WHITESPACE)
142 if(NOT EXISTS "${ROOT_LIBRARY_DIR}")
143 set(ROOT_FOUND FALSE)
144 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} ROOT library directory ${ROOT_LIBRARY_DIR} does not exist.")
145 endif()
146
147 execute_process(
148 COMMAND ${ROOT_CONFIG_EXECUTABLE} --noauxlibs --glibs
149 OUTPUT_VARIABLE ROOT_LIBRARIES
150 OUTPUT_STRIP_TRAILING_WHITESPACE)
151
152 execute_process(
153 COMMAND ${ROOT_CONFIG_EXECUTABLE} --auxlibs
154 OUTPUT_VARIABLE ROOT_AUX_LIBRARIES
155 OUTPUT_STRIP_TRAILING_WHITESPACE)
156
157 find_program(ROOTCINT_EXECUTABLE rootcint)
158 if(NOT ROOTCINT_EXECUTABLE)
159 set(ROOT_FOUND FALSE)
160 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} Cannot find rootcint.")
161 endif()
162
163 # parse version string
164 string(REGEX REPLACE "^([0-9]+)\\.[0-9][0-9]+\\/[0-9][0-9]+.*" "\\1"
165 ROOT_MAJOR_VERSION "${ROOT_VERSION}")
166 string(REGEX REPLACE "^[0-9]+\\.([0-9][0-9])+\\/[0-9][0-9]+.*" "\\1"
167 ROOT_MINOR_VERSION "${ROOT_VERSION}")
168 string(REGEX REPLACE "^[0-9]+\\.[0-9][0-9]+\\/([0-9][0-9]+).*" "\\1"
169 ROOT_PATCH_VERSION "${ROOT_VERSION}")
170
171 set(ROOT_FIND_VERSION_MAJOR ${ROOT_MAJOR_VERSION})
172 set(ROOT_FIND_VERSION_MINOR ${ROOT_MINOR_VERSION})
173 set(ROOT_FIND_VERSION_PATCH ${ROOT_PATCH_VERSION})
174
175 # make sure minor version is specified
176 if(ROOT_FIND_VERSION AND NOT ROOT_FIND_VERSION_MINOR)
177 message(FATAL_ERROR "When requesting a specific version of ROOT, you must provide at least the major and minor version numbers, e.g., 5.22")
178 endif()
179 # set patchlevel to 0, if not specified
180 if(NOT ROOT_FIND_VERSION_PATCH)
181 set(ROOT_FIND_VERSION_PATCH 0)
182 endif()
183 # compute an overall version number which can be compared at once
184 math(EXPR _ROOT_FIND_VERSION "${ROOT_FIND_VERSION_MAJOR} * 10000 + ${ROOT_FIND_VERSION_MINOR} * 100 + ${ROOT_FIND_VERSION_PATCH}")
185 math(EXPR _ROOT_VERSION "${ROOT_MAJOR_VERSION} * 10000 + ${ROOT_MINOR_VERSION} * 100 + ${ROOT_PATCH_VERSION}")
186 # compare version
187 if(ROOT_FIND_VERSION_EXACT)
188 if(NOT _ROOT_VERSION EQUAL "${_ROOT_FIND_VERSION}")
189 set(ROOT_FOUND FALSE)
190 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} ROOT version ${ROOT_VERSION} does not match requested version ${ROOT_FIND_VERSION_MAJOR}.${ROOT_FIND_VERSION_MINOR}/${ROOT_FIND_VERSION_PATCH}.")
191 endif()
192 else()
193 if(_ROOT_VERSION LESS "${_ROOT_FIND_VERSION}")
194 set(ROOT_FOUND FALSE)
195 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} ROOT version ${ROOT_VERSION} is lower than requested version ${ROOT_FIND_VERSION_MAJOR}.${ROOT_FIND_VERSION_MINOR}/${ROOT_FIND_VERSION_PATCH}.")
196 endif()
197 endif()
198
199endif()
200
201
202# generate list of ROOT libraries
203if(ROOT_FOUND)
204
205 # create list of internal libraries from root-config output
206 set(_LIBRARY_NAMES)
207 set(_EXTERNAL_ZLIB)
208 separate_arguments(ROOT_LIBRARIES)
209 # remove first -L entry
210 list(REMOVE_AT ROOT_LIBRARIES 0)
211 # loop over -l entries
212 foreach(_LIBRARY ${ROOT_LIBRARIES})
213 # extract library name from compiler flag and append to list
214 string(REGEX REPLACE "^-.(.*)$" "\\1" _LIBNAME "${_LIBRARY}")
215 # workaround for root-config inconsistency: if ROOT is built with --disable-builtin-zlib
216 # root-config returns the flag for the external zlib together with the internal libraries
217 if(_LIBNAME STREQUAL "z")
218 set(_EXTERNAL_ZLIB "-lz")
219 else()
220 list(APPEND _LIBRARY_NAMES ${_LIBNAME})
221 endif()
222 endforeach()
223
224 # append components
225 list(REMOVE_DUPLICATES ROOT_FIND_COMPONENTS)
226 if(ROOT_FIND_COMPONENTS)
227 set(_LIBRARY_NAMES "${_LIBRARY_NAMES};${ROOT_FIND_COMPONENTS}")
228 endif()
229
230 # check whether libraries exist
231 foreach(_LIBNAME ${_LIBRARY_NAMES})
232 find_library(_ROOT_LIB_${_LIBNAME}
233 NAMES ${_LIBNAME}
234 PATHS ${ROOT_LIBRARY_DIR}
235 NO_DEFAULT_PATH)
236 if(NOT _ROOT_LIB_${_LIBNAME})
237 set(ROOT_FOUND FALSE)
238 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} Cannot find ROOT library ${_LIBNAME} in ${ROOT_LIBRARY_DIR}.")
239 else()
240 list(APPEND ROOT_LIBS ${_ROOT_LIB_${_LIBNAME}})
241 endif()
242 endforeach()
243
244 # create list of external libraries from root-config output
245 separate_arguments(ROOT_AUX_LIBRARIES)
246 # append external zlib to auxiliary libraries
247 if(_EXTERNAL_ZLIB)
248 list(APPEND ROOT_AUX_LIBRARIES ${_EXTERNAL_ZLIB})
249 endif()
250 # loop over -l entries
251 foreach(_LIBRARY ${ROOT_AUX_LIBRARIES})
252 # extract library name from compiler flag
253 string(REGEX MATCH "^-l(.*)$" _LIBNAME "${_LIBRARY}")
254 if(_LIBNAME)
255 string(REGEX REPLACE "^-.(.*)$" "\\1" _LIBNAME "${_LIBNAME}")
256 # check whether libraries exist
257 find_library(_AUX_LIB_${_LIBNAME}
258 NAMES ${_LIBNAME})
259 if(NOT _AUX_LIB_${_LIBNAME})
260 set(ROOT_FOUND FALSE)
261 set(ROOT_ERROR_REASON "${ROOT_ERROR_REASON} Cannot find ROOT library ${_LIBNAME}.")
262 else()
263 list(APPEND ROOT_LIBS ${_AUX_LIB_${_LIBNAME}})
264 endif()
265 endif()
266 endforeach()
267
268endif()
269
270
271# make variables changeable
272mark_as_advanced(
273 ROOT_INCLUDE_DIR
274 ROOT_LIBRARY_DIR
275 ROOT_LIBRARIES
276 ROOT_LIBS
277 ROOT_DEFINITIONS
278)
279
280
281# report result
282if(ROOT_FOUND)
283 message(STATUS "Found ROOT version ${ROOT_VERSION} r${ROOT_SVN_REVISION} in ${ROOTSYS}")
284 message(STATUS "Using ROOT include dir ${ROOT_INCLUDE_DIR}")
285 message(STATUS "Using ROOT library dir ${ROOT_LIBRARY_DIR}")
286 message(STATUS "Using ROOT libraries: ${ROOT_LIBRARIES}")
287 message(STATUS "Using ROOT additional components: ${ROOT_FIND_COMPONENTS}")
288else()
289 if(ROOT_FIND_REQUIRED)
290 message(FATAL_ERROR "Unable to find requested ROOT installation:${ROOT_ERROR_REASON}")
291 else()
292 if(NOT ROOT_FIND_QUIETLY)
293 message(STATUS "ROOT was not found.")
294 endif()
295 endif()
296endif()
297
298
299# macro that generates ROOT dictionary
300function(root_generate_dictionary DICT_FILE INCLUDE_DIRS HEADER_FILES LINKDEF_FILE)
301
302 if(NOT ROOT_FOUND)
303 message(FATAL_ERROR "Impossible to generate dictionary ${DICT_FILE}, because no ROOT installation was found.")
304 endif()
305
306 # prepare command line argument for compiler definitions (put -D in front)
307 set(_DEFINITIONS)
308 get_property(_DEFS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY COMPILE_DEFINITIONS)
309 foreach(_DEF ${_DEFS})
310 set(_DEFINITIONS "${_DEFINITIONS} -D${_DEF}")
311 endforeach()
312 separate_arguments(_DEFINITIONS)
313
314 # prepare command line argument for include directories (put -I in front)
315 set(_INCLUDES)
316 foreach(_FILE ${INCLUDE_DIRS})
317 set(_INCLUDES ${_INCLUDES} -I${_FILE})
318 endforeach()
319
320 # strip paths from header file names
321 set(_HEADERS)
322 foreach(_FILE ${HEADER_FILES})
323 get_filename_component(_NAME ${_FILE} NAME)
324 set(_HEADERS ${_HEADERS} ${_NAME})
325 endforeach()
326
327 # add dictionary header file to output files
328 string(REGEX REPLACE "^(.*)\\.(.*)$" "\\1.h" _DICT_HEADER "${DICT_FILE}")
329 set(OUTPUT_FILES ${DICT_FILE} ${_DICT_HEADER})
330
331 add_custom_command(OUTPUT ${OUTPUT_FILES}
332 COMMAND ${ROOTCINT_EXECUTABLE}
333 ARGS -f ${DICT_FILE} -c -DHAVE_CONFIG_H ${_DEFINITIONS} ${_INCLUDES} ${_HEADERS} ${LINKDEF_FILE}
334 DEPENDS ${HEADER_FILES} ${LINKDEF_FILE}
335 )
336
337endfunction(root_generate_dictionary)