1a5c2aa3c67b97b4bb7019712b0a81d4db2f7a70
[u/mrichter/AliRoot.git] / cmake / ALICE_CMake.cmake
1 # -*- mode: cmake -*-
2
3 # AliRoot Build System Utility Macro and Function definitions
4 #
5 # Author: Johny Jose (johny.jose@cern.ch)
6 #         Port of previous Makefile build to cmake
7
8 cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)
9
10 macro(ALICE_DevFlagsOutput)
11
12   if(ALICEDEV STREQUAL "YES")
13     message(STATUS "FFLAGS    : ${FFLAGS}")
14     message(STATUS "CXXFLAGS  : ${CXXFLAGS}")
15     message(STATUS "CFLAGS    : ${CFLAGS}")
16     message(STATUS "SOFLAGS   : ${SOFLAGS}")
17     message(STATUS "LDFLAGS   : ${LDFLAGS}")
18     message(STATUS "SHLIB     : ${SHLIB}")
19     message(STATUS "SYSLIBS   : ${SYSLIBS}")
20     message(STATUS "CINTFLAGS : ${CINTFLAGS}")
21     message(STATUS "CMAKE_Fortran_FLAGS       : ${CMAKE_Fortran_FLAGS}")
22     message(STATUS "CMAKE_CXX_FLAGS           : ${CMAKE_CXX_FLAGS}")
23     message(STATUS "CMAKE_C_FLAGS             : ${CMAKE_C_FLAGS}")
24     message(STATUS "CMAKE_SHARED_LINKER_FLAGS : ${CMAKE_SHARED_LINKER_FLAGS}")
25     message(STATUS "CMAKE_MODULE_LINKER_FLAGS : ${CMAKE_MODULE_LINKER_FLAGS}")
26     message(STATUS "CMAKE_EXE_LINKER_FLAGS    : ${CMAKE_EXE_LINKER_FLAGS}")
27   endif(ALICEDEV STREQUAL "YES")
28
29 endmacro(ALICE_DevFlagsOutput)
30
31 function(ALICE_CleanOutput _output input)
32
33   string(REGEX REPLACE "\n" " " clean "${input}")
34   set(${_output} ${clean} PARENT_SCOPE)
35
36 endfunction(ALICE_CleanOutput)
37
38 function(ALICE_CorrectPaths _output value )
39
40   set(corrected)
41   foreach(path ${value})
42     set(external)
43     string(REGEX MATCH "^/" external "${path}")
44     if(NOT external)
45       list(APPEND corrected "${AliRoot_SOURCE_DIR}/${path}" )
46     else()
47       list(APPEND corrected "${path}")
48     endif(NOT external)
49   endforeach(path)
50   set(${_output} ${corrected} PARENT_SCOPE)
51
52 endfunction(ALICE_CorrectPaths)
53
54 function(ALICE_Format _output prefix suffix input)
55
56 # DevNotes - input should be put in quotes or the complete list does not get passed to the function
57   set(format)
58   foreach(arg ${input})
59     set(item ${arg})
60     if(prefix)
61       string(REGEX MATCH "^${prefix}" pre "${arg}")
62     endif(prefix)
63     if(suffix)
64       string(REGEX MATCH "${suffix}$" suf "${arg}")
65     endif(suffix)
66     if(NOT pre)
67       set(item "${prefix}${item}")
68     endif(NOT pre)
69     if(NOT suf)
70       set(item "${item}${suffix}")
71     endif(NOT suf)
72     list(APPEND format ${item})
73   endforeach(arg)
74   set(${_output} ${format} PARENT_SCOPE)
75
76 endfunction(ALICE_Format)
77
78 function(ALICE_CheckLibraries _output input)
79
80   set(format)
81   foreach(arg ${input})
82     set(item ${arg})
83     string(REGEX MATCH "^(/|-)" preformatted "${item}")
84     if(NOT preformatted)
85       set(item "-l${item}")
86     endif(NOT preformatted)
87     list(APPEND format ${item})
88   endforeach(arg)
89   set(${_output} ${format} PARENT_SCOPE)
90   
91 endfunction(ALICE_CheckLibraries)
92
93 function(ALICE_RootConfig _output parameters)
94
95   if(ROOT_CONFIG)
96     execute_process(COMMAND ${ROOT_CONFIG} ${parameters} OUTPUT_VARIABLE result ERROR_VARIABLE error OUTPUT_STRIP_TRAILING_WHITESPACE )
97     if(error)
98       message(FATAL_ERROR "root-config ERROR : ${error}")
99     endif(error)
100     ALICE_CleanOutput(result "${result}")
101     set(${_output} "${result}" PARENT_SCOPE)
102   else()
103     message(FATAL_ERROR "root-config not found")
104   endif(ROOT_CONFIG)
105
106 endfunction(ALICE_RootConfig)
107
108 macro(ALICE_CheckOutOfSourceBuild)
109   
110   #Check if previous in-source build failed
111   if(EXISTS ${AliRoot_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${AliRoot_SOURCE_DIR}/CMakeFiles)
112     message(FATAL_ERROR "CMakeCache.txt or CMakeFiles exists in source directory! Please remove them before running cmake $ALICE_ROOT")
113   endif(EXISTS ${AliRoot_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${AliRoot_SOURCE_DIR}/CMakeFiles)
114   
115   #Get Real Paths of the source and binary directories
116   get_filename_component(srcdir "${AliRoot_SOURCE_DIR}" REALPATH)
117   get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
118   
119   #Check for in-source builds
120   if(${srcdir} STREQUAL ${bindir})
121     message(FATAL_ERROR "AliRoot cannot be built in-source! Please run cmake $ALICE_ROOT outside the source directory")
122   endif(${srcdir} STREQUAL ${bindir})
123
124 endmacro(ALICE_CheckOutOfSourceBuild)
125
126 macro(ALICE_ConfigureCompiler)
127
128   if(ALIPROFILE STREQUAL "YES")
129     set(ALICE_TARGET ${ALICE_TARGET}PROF)
130     set(FFLAGS "-pg ${FFLAGS}")
131     set(CXXFLAGS "-pg ${CXXFLAGS}")
132     set(CFLAGS "-pg ${CFLAGS}")
133     set(SOFLAGS "-pg ${SOFLAGS}")
134     set(LDFLAGS "-pg ${LDFLAGS}")
135   endif(ALIPROFILE STREQUAL "YES")
136
137   ALICE_RootConfig(RCFLAGS --auxcflags)
138   if ( CMAKE_CXX_COMPILER MATCHES "clang") 
139   string(REPLACE "-pthread" " " RCFLAGS "${RCFLAGS}")
140   endif ( CMAKE_CXX_COMPILER MATCHES "clang") 
141
142   ALICE_RootConfig(RLFLAGS --ldflags)
143
144   set(CXXFLAGS "${CXXFLAGS} ${RCFLAGS}")
145   string(REPLACE "-std=c++11" " " RCFLAGS "${RCFLAGS}")
146   set(CFLAGS  "${CFLAGS} ${RCFLAGS}")
147   set(SOFLAGS "${SOFLAGS} ${RLFLAGS}")
148   set(LDFLAGS "${LDFLAGS} ${RLFLAGS}")
149
150   if(NOT ALICXXWARN STREQUAL "NO")
151     set(CXXFLAGS "${CXXFLAGS} ${CXXWARN}")
152   endif(NOT ALICXXWARN STREQUAL "NO")
153
154   set(CXXFLAGS "${DATEFLAGS} ${CXXFLAGS}")
155   set(CFLAGS "${DATEFLAGS} ${CFLAGS}")
156   set(CINTFLAGS "${DATEFLAGS}")
157
158 endmacro(ALICE_ConfigureCompiler)
159
160 macro(ALICE_ConfigurePlatform)
161   
162   ALICE_GetTarget()
163   include("CMake${ALICE_TARGET}")
164   
165 endmacro(ALICE_ConfigurePlatform)
166
167 macro(ALICE_GetTarget)
168   
169   #Set ALICE_TARGET if it has not been set yet
170   if(NOT ALICE_TARGET)  
171     set(ALICE_TARGET $ENV{ALICE_TARGET})
172     #If ALICE_TARGET is not defined assign default values
173     if(NOT ALICE_TARGET)
174       message(WARNING "Environment variable ALICE_TARGET is not set ! Setting to default value.")
175       ALICE_RootConfig(ALICE_TARGET --arch)
176       message("ALICE_TARGET set to ${ALICE_TARGET}")
177     endif(NOT ALICE_TARGET)
178   endif(NOT ALICE_TARGET)
179
180 endmacro(ALICE_GetTarget) 
181
182 macro(ALICE_ResetPackage)
183
184   set(MODDIR ${MODULE})
185   set(MODDIRI ${PROJECT_SOURCE_DIR}/${MODULE})
186
187   set(SRCS)
188   set(HDRS)
189   set(FSRCS)
190   set(DHDR)
191   set(CSRCS)
192   set(CHDRS)
193   set(EINCLUDE)
194   set(EDEFINE)
195   set(ELIBS)
196   set(ELIBSDIR)
197   set(PACKFFLAGS)
198   set(PACKCXXFLAGS)
199   set(PACKCFLAGS)
200   set(PACKDYFLAGS)
201   set(PACKSOFLAGS)
202   set(PACKLDFLAGS)
203   set(PACKBLIBS)
204   set(EXPORT)
205   set(EHDRS)
206   set(CINTHDRS)
207   set(CINTAUTOLINK)
208   set(ARLIBS)
209   set(SHLIBS)
210
211   #HLT Variables
212   set(MODULE_HDRS)
213   set(MODULE_SRCS)
214   set(MODULE_DHDR)
215   set(CLASS_HDRS)
216   set(LIBRARY_DEP)
217   set(HLTDEFS)
218   set(HLTSOFLAGS) 
219
220
221   set(PSRCS)
222   set(PHDRS)
223   set(PFSRCS)
224   set(PDHDR)
225   set(PCSRCS)
226   set(PCHDRS)
227   set(PEINCLUDE)
228   set(PEDEFINE)
229   set(PELIBS)
230   set(PELIBSDIR)
231   set(PPACKFFLAGS)
232   set(PPACKCXXFLAGS)
233   set(PPACKCFLAGS)
234   set(PPACKDYFLAGS)
235   set(PPACKSOFLAGS)
236   set(PPACKLDFLAGS)
237   set(PPACKBLIBS)
238   set(PEXPORT)
239   set(PEHDRS)
240   set(PCINTHDRS)
241   set(PCINTAUTOLINK)
242   set(PARLIBS)
243   set(PSHLIBS)
244   set(PDS)
245
246   set(saveEINCLUDE)
247
248 endmacro(ALICE_ResetPackage)
249
250 function(ALICE_SetPackageVariable _var ext setvalue unsetvalue )
251
252 #DevNotes - setvalue and unsetcalue should both be put in quotes or cmake throws an error if they have undefined values
253   if(setvalue)
254     set(${_var} ${setvalue} PARENT_SCOPE)
255     set(${PACKAGE}${ext} ${setvalue} PARENT_SCOPE)
256   else()
257     set(${_var} ${unsetvalue} PARENT_SCOPE)
258     set(${PACKAGE}${ext} ${unsetvalue} PARENT_SCOPE)
259   endif(setvalue)
260
261 endfunction(ALICE_SetPackageVariable)
262
263 macro(ALICE_BuildPackage)
264   # message(STATUS " ${PACKAGE}")
265  
266   list(APPEND EINCLUDE STEER) 
267   # Needed for PAR files - we need to do this before path 
268   # expansion so that the PAR files do not contain references to 
269   # specific directories but depend solely on ALICE_ROOT
270   string(REPLACE ";" " " saveEINCLUDE "${EINCLUDE}")
271   # set(saveEINCLUDE "${EINCLUDE}")
272   # message(STATUS "saveEINCLUDE=${saveEINCLUDE} EINCLUDE=${EINCLUDE}")
273
274   ALICE_SetPackageVariable(PFFLAGS "FFLAGS" "${PACKFFLAGS}" "${FFLAGS}")
275   ALICE_SetPackageVariable(PCFLAGS "CFLAGS" "${PACKCFLAGS}" "${CFLAGS}")
276   ALICE_SetPackageVariable(PCXXFLAGS "CXXFLAGS" "${PACKCXXFLAGS}" "${CXXFLAGS}")
277   ALICE_SetPackageVariable(PSOFLAGS "SOFLAGS" "${PACKSOFLAGS}" "${SOFLAGS}")
278   ALICE_SetPackageVariable(PLDFLAGS "LDFLAGS" "${PACKLDFLAGS}" "${LDFLAGS}")
279   ALICE_SetPackageVariable(PDCXXFLAGS "DCXXFLAGS" "${PACKDCXXFLAGS}" "${CXXFLAGSNO}")
280   ALICE_SetPackageVariable(PBLIBS "BLIBS" "${PACKBLIBS}" "${LIBS}")
281
282   set(WITHDICT)
283   if(DHDR OR CINTAUTOLINK)
284     set(WITHDICT TRUE)
285   endif(DHDR OR CINTAUTOLINK)
286
287   ALICE_SetPackageVariable(PEDEFINE "EDEFINE" "${EDEFINE}" "${EDEFINE}")
288   ALICE_SetPackageVariable(PEXPORT "EXPORT" "${EXPORT}" "${EXPORT}")
289 #  message(${CMAKE_INCLUDE_EXPORT_DIRECTORY})
290   ALICE_SetPackageVariable(PEXPORTDEST "EXPORTDEST" "${CMAKE_INCLUDE_EXPORT_DIRECTORY}" "${CMAKE_INCLUDE_EXPORT_DIRECTORY}")
291   ALICE_SetPackageVariable(PINC "INC" "${EINCLUDE};${MODULE}" "${EINCLUDE};${MODULE}")
292   ALICE_SetPackageVariable(PELIBS "ELIBS" "${ELIBS}" "${ELIBS}")
293   ALICE_SetPackageVariable(PELIBSDIR "ELIBSDIR" "${ELIBSDIR}" "${ELIBSDIR}")
294   
295   ALICE_SetPackageVariable(PCS "CS" "${CSRCS}" "${CSRCS}")
296   ALICE_SetPackageVariable(PCH "CH" "${CHDRS}" "${CHDRS}")
297   
298   ALICE_SetPackageVariable(PFS "FS" "${FSRCS}" "${FSRCS}")
299
300   ALICE_SetPackageVariable(PS "S" "${SRCS}" "${SRCS}")
301   ALICE_SetPackageVariable(PH "H" "${HDRS} ${EHDRS}" "${HDRS} ${EHDRS}")
302   ALICE_SetPackageVariable(PCINTHDRS "CINTHDRS" "${CINTHDRS}" "${PH}")
303   
304   string( REPLACE ".h" "" PCINTCLASSES "${PCINTHDRS}")
305   set ( ${PACKAGE}CINTCLASSES ${PCINTCLASSES})
306
307   ALICE_SetPackageVariable(PDH "DH" "${DHDR}" "")
308
309   if(CINTAUTOLINK)
310     ALICE_SetPackageVariable(PDAL "DAL" "${CMAKE_CURRENT_BINARY_DIR}/G__${PACKAGE}AutoLinkDef.h" "${CMAKE_CURRENT_BINARY_DIR}/G__${PACKAGE}AutoLinkDef.h")
311     ALICE_SetPackageVariable(PDH "DH" "${PDAL}" "${PDAL}")
312     ALICE_GenerateLinkDef()
313   endif(CINTAUTOLINK)
314
315   if(lib)
316     list(APPEND PMLIBS ${PACKAGE})
317     list(APPEND ALLLIBS ${PACKAGE})
318     list(APPEND PMALIBS ${PACKAGE}-static)
319     list(APPEND ALLALIBS ${PACKAGE}-static)
320     list(APPEND BINLIBS ${PACKAGE})
321     # Do not link against other libraries when creating a library 
322     # set(PELIBS)
323   else()
324     list(APPEND ALLEXECS ${PACKAGE})
325   endif(lib)
326   set(${MODULE}ALIBS "${PMALIBS}" PARENT_SCOPE)
327   set(${MODULE}LIBS "${PMLIBS}" PARENT_SCOPE)
328   set(${MODULE}INC "${EINCLUDE}" PARENT_SCOPE)
329   list(APPEND INCLUDEFILES ${PEXPORTDEST})
330   if(WITHDICT)  
331     # Replace dots in file name with underscores, or rootcint will 
332     # write invalid code (namespace names with dots in them)
333     string(REPLACE "." "_" ESC_PACKAGE ${PACKAGE})
334     ALICE_SetPackageVariable(PDS "DS" "G__${ESC_PACKAGE}.cxx" "G__${ESC_PACKAGE}.cxx")
335     ALICE_GenerateDictionary()
336   else()
337     if(lib)
338       message(STATUS "No dictionary generated for ${PACKAGE}")  
339     endif(lib)
340   endif(WITHDICT)
341   if(PS OR PCS OR PFS)
342     if(lib)
343       ALICE_BuildLibrary()
344     elseif(bin)
345       ALICE_BuildExecutable()
346     endif(lib)
347   endif(PS OR PCS OR PFS)
348   ALICE_CopyHeaders()
349   ALICE_BuildPAR()
350
351 endmacro(ALICE_BuildPackage)
352
353
354 macro(ALICE_BuildModule)
355   # message(STATUS "${MODULE}")
356   execute_process(COMMAND ${CMAKE_COMMAND} -E echo_append "-- ${MODULE}:")
357   add_definitions(-D_MODULE_="${MODULE}")
358   foreach(PACKAGEFILE ${PACKAGES})
359       set(lib)
360       set(bin)
361       string(REGEX MATCH "CMakelib" lib "${PACKAGEFILE}")
362       string(REGEX MATCH "CMakebin" bin "${PACKAGEFILE}")
363       get_filename_component(PACKAGE ${PACKAGEFILE} NAME)
364       string(REGEX REPLACE "^CMake(lib|bin)(.*)\\.pkg" "\\2" PACKAGE "${PACKAGE}")
365       execute_process(COMMAND ${CMAKE_COMMAND} -E echo_append " ${PACKAGE}")
366       if(ALICEDEV)
367         message("Adding package ${PACKAGE} in ${MODULE}")
368       endif(ALICEDEV)
369       ALICE_ResetPackage()
370       include(${PACKAGEFILE})
371       ALICE_BuildPackage()
372      
373       get_property(EXCLUDEPACKAGE TARGET ${PACKAGE} PROPERTY EXCLUDE_FROM_ALL) 
374       if(NOT EXCLUDEPACKAGE)
375         install(TARGETS ${PACKAGE} 
376                 RUNTIME DESTINATION bin/tgt_${ALICE_TARGET}
377                 LIBRARY DESTINATION lib/tgt_${ALICE_TARGET}
378                 ARCHIVE DESTINATION bin/tgt_${ALICE_TARGET})
379       endif(NOT EXCLUDEPACKAGE)
380   endforeach(PACKAGEFILE)
381   ALICE_CheckModule()
382   execute_process(COMMAND ${CMAKE_COMMAND} -E echo " done")
383 endmacro(ALICE_BuildModule)
384
385 macro(ALICE_GenerateDictionary)
386  
387   if(ALICEDEV STREQUAL "YES")
388     message("Generating Dictionary rule for ${PACKAGE}")
389     message("${ROOTCINT} -f ${PDS} -c ${PEDEFINE} ${CINTFLAGS} ${PINC} ${PCINTHDRS} ${PDH}")
390   endif(ALICEDEV STREQUAL "YES")
391  
392   # Split up all arguments
393
394   set(DEDEFINE ${PEDEFINE})
395   set(DCINTFLAGS ${CINTFLAGS})
396   set(DINC ${PINC})
397   set(DCINTHDRS ${PCINTHDRS})
398   set(DDH ${PDH})
399   separate_arguments(DEDEFINE)
400   separate_arguments(DCINTFLAGS)
401   separate_arguments(DINC)
402   separate_arguments(DCINTHDRS)
403   separate_arguments(DDH)
404   # Format neccesary arguments
405   set(ROOT6_ALPHA "5.99.1")
406   string(REGEX REPLACE "/0*" "." ROOT_DOTVERSION ${ROOT_VERSION})
407   if(ROOT_DOTVERSION VERSION_GREATER ROOT6_ALPHA)
408     # For current ROOT6-alpha - without proper PCM support - we need to 
409     # give the _full_ path for all include paths, so that libCling can
410     # later attach those to the internal include path of the interpreter 
411     # and so that the interpreter can parse the header files. 
412     #
413     # THIS SHOULD BE A TEMPORARY HACK!
414     ALICE_Format(DINC "-I${PROJECT_SOURCE_DIR}/" "" 
415       "${DINC};${CMAKE_INCLUDE_EXPORT_DIRECTORY}")
416   else()
417   ALICE_Format(DINC "-I" "" "${DINC};${CMAKE_INCLUDE_EXPORT_DIRECTORY}")
418   endif(ROOT_DOTVERSION VERSION_GREATER ROOT6_ALPHA)
419   set_source_files_properties(${PDS} PROPERTIES GENERATED TRUE)
420   set_source_files_properties(${PDS} PROPERTIES COMPILE_FLAGS "-w")
421   add_custom_command(OUTPUT  ${PDS}
422                      COMMAND cd ${PROJECT_SOURCE_DIR} && ${ROOTCINT} -f ${CMAKE_CURRENT_BINARY_DIR}/${PDS} -c -D_MODULE=\\\"${MODULE}\\\" ${DEDEFINE} ${DCINTFLAGS} ${DINC} ${DCINTHDRS} ${DDH}
423                      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
424                      DEPENDS ${DCINTHDRS} ${DDH}
425                      )
426   add_custom_target(G${PACKAGE})
427   add_dependencies(G${PACKAGE} ${PDS})
428
429 endmacro(ALICE_GenerateDictionary)
430
431 macro(ALICE_BuildLibrary)
432
433   set(CMAKE_CXX_FLAGS "${PEDEFINE} ${PCXXFLAGS}")
434   set(CMAKE_C_FLAGS "${PEDEFINE} ${PCFLAGS}")
435   set(CMAKE_Fortran_FLAGS "${PEDEFINE} ${PFFLAGS}")
436   set(CMAKE_SHARED_LINKER_FLAGS ${PSOFLAGS}) 
437   set(CMAKE_MODULE_LINKER_FLAGS ${PLDFLAGS})
438   ALICE_DevFlagsOutput()
439   
440   
441   separate_arguments(PINC)
442   separate_arguments(EINCLUDE)
443   separate_arguments(PELIBSDIR)
444   separate_arguments(PBLIBS)
445   separate_arguments(PELIBS)
446   separate_arguments(SHLIB)
447
448   ALICE_Format(PELIBSDIR "-L" "" "${PELIBSDIR}")
449   ALICE_CheckLibraries(PBLIBS "${PBLIBS}")
450
451   ALICE_CorrectPaths(EINCLUDE "${EINCLUDE}")
452   ALICE_CorrectPaths(PINC "${PINC}")
453
454   include_directories(SYSTEM ${ROOTINCDIR})
455   include_directories(${PINC})  
456   include_directories(${EINCLUDE})
457   include_directories(${CMAKE_INCLUDE_EXPORT_DIRECTORY}/FromTemplate)
458   include_directories(${AliRoot_SOURCE_DIR})
459   
460   add_library(${PACKAGE} SHARED ${PCS} ${PFS} ${PS} ${PDS})
461   set_target_properties(${PACKAGE} PROPERTIES SUFFIX .so)  
462   
463
464   if(PELIBS OR SHLIB)
465     target_link_libraries(${PACKAGE} ${PELIBSDIR} ${PELIBS} ${SHLIB})
466   endif(PELIBS OR SHLIB)
467   
468   add_dependencies(${MODULE}-all ${PACKAGE})
469      
470   add_library(${PACKAGE}-static STATIC EXCLUDE_FROM_ALL ${PCS} ${PFS} ${PS} ${PDS})
471   set_target_properties(${PACKAGE}-static PROPERTIES OUTPUT_NAME ${PACKAGE})
472   if(PELIBS OR ALLIB)
473     target_link_libraries(${PACKAGE}-static ${PELIBSDIR} ${PELIBS} ${ALLIB})
474   endif(PELIBS OR ALLIB)
475
476   add_dependencies(${MODULE}-all-static ${PACKAGE}-static)
477   if(ALIPROFILE STREQUAL "YES")
478     add_dependencies(${MODULE}-all ${MODULE}-static)
479   endif(ALIPROFILE STREQUAL "YES")
480
481   list(FIND EXCLUDEMODULES ${MODULE} RESULT)
482   if(NOT RESULT STREQUAL "-1")
483     message(STATUS "${MODULE} will not be built by default. Type make ${MODULE}-all to build.")
484     set_property(TARGET ${PACKAGE} PROPERTY EXCLUDE_FROM_ALL TRUE)
485   endif(NOT RESULT STREQUAL "-1")
486
487   set_target_properties(${PACKAGE} PROPERTIES INSTALL_NAME_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
488
489 endmacro(ALICE_BuildLibrary)
490
491 macro(ALICE_BuildExecutable)
492
493   set(CMAKE_CXX_FLAGS "${PEDEFINE} ${PCXXFLAGS} ${EXEFLAGS}")
494   set(CMAKE_C_FLAGS "${PEDEFINE} ${PCFLAGS} ${EXEFLAGS}")
495   set(CMAKE_Fortran_FLAGS "${PEDEFINE} ${PFFLAGS} ${EXEFLAGS}")
496   set(CMAKE_SHARED_LINKER_FLAGS ${PSOFLAGS}) 
497   set(CMAKE_MODULE_LINKER_FLAGS ${PLDFLAGS})
498   set(CMAKE_EXE_LINKER_FLAGS ${PLDFLAGS})
499   ALICE_DevFlagsOutput()
500   
501   separate_arguments(PINC)
502   separate_arguments(EINCLUDE)
503   separate_arguments(PELIBSDIR)
504   separate_arguments(PBLIBS)
505   separate_arguments(PELIBS)
506   separate_arguments(SHLIB)
507   
508   ALICE_Format(PELIBSDIR "-L" "" "${PELIBSDIR}")
509   ALICE_CheckLibraries(PBLIBS "${PBLIBS}")
510
511   ALICE_CorrectPaths(EINCLUDE "${EINCLUDE}")
512   ALICE_CorrectPaths(PINC "${PINC}")
513
514   include_directories(SYSTEM ${ROOTINCDIR})
515   include_directories(${PINC})  
516   include_directories(${EINCLUDE})
517   include_directories(${CMAKE_INCLUDE_EXPORT_DIRECTORY})
518
519   if(ALIPROFILE STREQUAL "YES")
520     add_executable(${PACKAGE} ${PFS} ${PCS} ${PS} ${PDS})
521     if(ARLIBS OR SHLIBS OR PBLIBS OR EXEFLAGS)
522       target_link_libraries(${PACKAGE} ${ARLIBS} ${SHLIBS} ${PBLIBS} ${EXEFLAGS})
523     endif(ARLIBS OR SHLIBS OR PBLIBS OR EXEFLAGS)
524     add_dependencies(${MODULE}-all ${PACKAGE})
525   else()
526     add_executable(${PACKAGE} ${PFS} ${PCS} ${PS} ${PDS})
527     if(PELIBS OR PBLIBS OR EXEFLAGS)
528       target_link_libraries(${PACKAGE} ${BINLIBDIRS} ${PELIBSDIR} ${PELIBS} ${PBLIBS} ${EXEFLAGS})
529     endif(PELIBS OR PBLIBS OR EXEFLAGS)
530     add_dependencies(${MODULE}-all ${PACKAGE})
531   endif(ALIPROFILE STREQUAL "YES")
532
533 # AliMDC
534 # ------------------------------
535   if(PACKAGE STREQUAL "alimdc" AND GENERATEPACKAGES)
536
537     add_executable(${PACKAGE}-static ${PFS} ${PCS} ${PS} ${PDS})
538     set_target_properties(${PACKAGE}-static PROPERTIES OUTPUT_NAME alimdca)
539     add_custom_target( libAliMDC
540                         COMMAND rm -rf ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libAliMDC.a
541                         COMMAND rm -rf junkmdc
542                         COMMAND mkdir junkmdc && cd junkmdc && ar x ../libRAWDatabase.a && ar x ../libMDC.a && ar x ../libESD.a && ar x ../libSTEERBase.a && ar r ../libAliMDC.a *.o && cd .. && rm -rf junkmdc
543                         WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
544     add_custom_target( root-static-libs 
545                        COMMAND make static
546                        WORKING_DIRECTORY ${ROOTSYS} )
547                        
548     add_dependencies(libAliMDC RAWDatabase-static STEERBase-static MDC-static ESD-static)
549     add_dependencies(${PACKAGE}-static libAliMDC root-static-libs)
550     target_link_libraries(${PACKAGE}-static ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libAliMDC.a ${ROOTLIBDIR}/libRoot.a ${ROOTLIBDIR}/libfreetype.a ${ROOTLIBDIR}/libpcre.a -pthread -ldl -lcurses)
551
552     install(TARGETS ${PACKAGE}-static RUNTIME DESTINATION bin/tgt_${ALICE_TARGET} COMPONENT MDC)
553     install(FILES ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libAliMDC.a ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libAliMDC.a ${ROOTLIBDIR}/libRoot.a ${ROOTLIBDIR}/libfreetype.a ${ROOTLIBDIR}/libpcre.a DESTINATION lib/tgt_${ALICE_TARGET} COMPONENT MDC)
554     install(FILES ${PROJECT_SOURCE_DIR}/RAW/mdc.h DESTINATION include COMPONENT MDC)
555     set(CPACK_RPM_PACKAGE_SUMMARY "AliMDC static libraries")
556     set(CPACK_RPM_PACKAGE_NAME "alimdc")
557     set(CPACK_RPM_PACKAGE_VERSION "${ALIMDCVERSION}")
558     set(CPACK_RPM_PACKAGE_RELEASE "${ALIMDCRELEASE}")
559     set(CPACK_RPM_PACKAGE_LICENSE "License: CERN ALICE Off-line Group")
560     set(CPACK_RPM_PACKAGE_VENDOR "ALICE Core Off-line Group")
561     set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "AliMDC static libraries")
562     set(CPACK_RPM_PACKAGE_GROUP "Applications/Alice")
563     set(CPACK_RPM_PACKAGE_REQUIRES "glibc")
564     set(CPACK_PACKAGE_VERSION "1.0.0")
565     set(CPACK_PACKAGE_VERSION_MAJOR "1")
566     set(CPACK_PACKAGE_VERSION_MINOR "0")
567     set(CPACK_PACKAGE_VERSION_PATCH "0")
568     set(CPACK_PACKAGE_INSTALL_DIRECTORY "/opt/")
569     add_component_package(MDC alimdc-rpm)
570
571   endif(PACKAGE STREQUAL "alimdc" AND GENERATEPACKAGES)
572
573   list(FIND EXCLUDEMODULES ${MODULE} RESULT)
574   if(NOT RESULT STREQUAL "-1")
575     set_property(TARGET ${PACKAGE} PROPERTY EXCLUDE_FROM_ALL TRUE)
576   endif(NOT RESULT STREQUAL "-1")
577
578 endmacro(ALICE_BuildExecutable)
579
580 macro(ALICE_CopyHeaders)
581   
582   if(EXPORT )
583     set(HEADERS ${EXPORT})
584     set(_headersdep)
585     foreach(header ${HEADERS})
586       get_filename_component( header_name ${header} NAME )
587       add_custom_command(OUTPUT ${PEXPORTDEST}/${header_name}
588                          COMMAND ${CMAKE_COMMAND} -E copy ${AliRoot_SOURCE_DIR}/${MODULE}/${header} ${PEXPORTDEST}/${header_name}
589                          DEPENDS ${AliRoot_SOURCE_DIR}/${MODULE}/${header})
590       list(APPEND _headersdep ${PEXPORTDEST}/${header_name})
591       install(FILES ${header} DESTINATION include)
592     endforeach(header)
593     add_custom_target(${PACKAGE}-headers DEPENDS ${_headersdep})
594     add_dependencies(${PACKAGE} ${PACKAGE}-headers)
595   endif(EXPORT)
596
597 endmacro(ALICE_CopyHeaders)
598
599 macro(ALICE_GenerateLinkDef)
600   set(PCLASSES)
601   foreach (class ${PCINTCLASSES})
602     get_filename_component(classname ${class} NAME)
603     # set(PCLASSES ${PCLASSES} "\\n#pragma link C++ class ${classname}+;")
604     set(PCLASSES ${PCLASSES} "${classname}")
605   endforeach(class)
606
607   add_custom_command(OUTPUT ${PDAL}
608     COMMAND sh ${AliRoot_SOURCE_DIR}/cmake/GenerateLinkDef.sh ${PCLASSES} > ${PDAL} 
609     DEPENDS ${PCINTHDRS} ${AliRoot_SOURCE_DIR}/cmake/GenerateLinkDef.sh)
610 endmacro(ALICE_GenerateLinkDef)
611
612 macro(ALICE_BuildPAR)
613   
614   if(EXISTS ${AliRoot_SOURCE_DIR}/${MODULE}/PROOF-INF.${PACKAGE})
615     set(PARSRCS)
616     foreach(file ${SRCS} ${HDRS} ${FSRCS} ${DHDR} ${OTHERS} )
617       get_filename_component(srcdir ${file} PATH)
618       add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${file}-par                         
619                          COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${srcdir}
620                          COMMAND cp -pR ${file} ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${file}
621                          WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
622       list(APPEND PARSRCS ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${file}-par)
623     endforeach(file ${SRCS} ${HDRS} ${FSRCS} ${DHDR} ${OTHERS} )
624     
625     # message(STATUS "saveEINCLUDE=${saveEINCLUDE}")
626     add_custom_target(${PACKAGE}.par
627                       COMMAND sed -e 's,include ..ROOTSYS./\\\(etc\\|test\\\)/Makefile.arch,include Makefile.arch,\; s/PACKAGE = .*/PACKAGE = ${PACKAGE}/\; s,SRCS *=.*,SRCS = ${SRCS},\;' < Makefile | sed -e 's,HDRS *=.*,HDRS = ${HDRS},\; s,FSRCS *=.*,FSRCS = ${FSRCS},\; s,DHDR *=.*,DHDR = ${DHDR},\; s,EINCLUDE *:=.*,EINCLUDE := ${saveEINCLUDE},' > ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/Makefile
628                       COMMAND cp -pR `root-config --etcdir`/Makefile.arch ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/Makefile.arch
629                       COMMAND cp -pR PROOF-INF.${PACKAGE} ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/PROOF-INF
630 #                      COMMAND cp -pR lib${PACKAGE}.pkg ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}
631                       COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR} tar --exclude=.svn -czhf ${CMAKE_BINARY_DIR}/${PACKAGE}.par ${PACKAGE}
632                       COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}
633                       COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold "${PACKAGE}.par has been created in ${CMAKE_BINARY_DIR}"
634                       DEPENDS ${PARSRCS} 
635                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
636
637
638     add_dependencies(${PACKAGE}.par ${SRCS} ${FSRCS} ${HDRS} ${DHDR})
639     add_dependencies(par-all ${PACKAGE}.par)
640     add_dependencies(${MODULE}-par-all ${PACKAGE}.par)
641
642     add_custom_target(test-${PACKAGE}.par
643                       COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold "The file ${PACKAGE}.par is now being tested, in case of an error check in par-tmp/${PACKAGE}."
644                       COMMAND ${CMAKE_COMMAND} -E make_directory par-tmp
645                       COMMAND ${CMAKE_COMMAND} -E chdir par-tmp tar xfz ../${PACKAGE}.par && ${CMAKE_COMMAND} -E chdir par-tmp/${PACKAGE} PROOF-INF/BUILD.sh
646                       COMMAND ${CMAKE_COMMAND} -E remove_directory par-tmp
647                       COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --green --bold "${PACKAGE}.par testing succeeded"
648                       WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
649
650     add_dependencies(test-${PACKAGE}.par ${PACKAGE}.par)
651     add_dependencies(test-par-all test-${PACKAGE}.par)
652     add_dependencies(test-${MODULE}-par-all test-${PACKAGE}.par)
653
654   endif(EXISTS ${AliRoot_SOURCE_DIR}/${MODULE}/PROOF-INF.${PACKAGE})
655   # endif(EXISTS ${ALICE_ROOT}/${MODULE}/PROOF-INF.${PACKAGE})
656
657 endmacro(ALICE_BuildPAR)
658