Changed names of static libraries, targets and updated README_CMake
[u/mrichter/AliRoot.git] / cmake / CMakeDA.cmake
1 cmake_minimum_required(VERSION 2.8.2 FATAL_ERROR)
2
3 # -----------Utilities--------------------
4
5 #list of detectors 
6 set(ONLINEDETECTORS SPD SDD SSD TPC TRD TOF HMP PHS CPV PMD MCH MTR FMD T00 V00 ZDC ACO TRI EMC HLT TST GRP)
7
8 function(expand output input)
9     string(REGEX MATCH "\\\${[^}]*}" m "${input}")
10     while(m)
11         string(REGEX REPLACE "\\\${(.*)}" "\\1" v "${m}")
12         string(REPLACE "\${${v}}" "${${v}}" input "${input}")
13         string(REGEX MATCH "\\\${[^}]*}" m "${input}")
14     endwhile()
15     set("${output}" "${input}" PARENT_SCOPE)
16 endfunction()
17
18 #function to get module for detector
19 function (detector_module _module detector)
20   #Map of online detectors to DA in pairs of ONLINEDETECTORNAME DAMODULE
21   set (DETECTORMODULEMAP SPD ITS SDD ITS SSD ITS HMP HMPID PHS PHOS CPV PHOS MCH MUON MTR MUON T00 T0 V00 VZERO ACO ACORDE EMC EMCAL)
22   list(FIND DETECTORMODULEMAP ${detector} _index)
23   if(_index STREQUAL "-1")
24     set(${_module} "${detector}" PARENT_SCOPE)
25   else()
26     math(EXPR _index "${_index}+1")
27     list(GET DETECTORMODULEMAP ${_index} _index)
28     set(${_module} ${_index} PARENT_SCOPE)
29   endif(_index STREQUAL "-1")
30 endfunction()
31
32 #function to get subDA for detector
33 function (detector_subda _subda detector)
34   #Map of online detectors to SUBDAMODULE in pairs of ONLINEDETECTORNAME SUBDAMODULE
35   set (DETECTORSUBDAMAP SPD SPD SDD SDD SSD SSD CPV CPV MCH TRK MTR TRG)
36   list(FIND DETECTORSUBDAMAP ${detector} _index)
37   if(_index STREQUAL "-1")
38     set(${_subda} "" PARENT_SCOPE)
39   else()
40     math(EXPR _index "${_index}+1")
41     list(GET DETECTORSUBDAMAP ${_index} _index)
42     set(${_subda} ${_index} PARENT_SCOPE)
43   endif(_index STREQUAL "-1")
44 endfunction()
45
46 #function to extract info
47 function (getinfo _info pattern file)
48   string(REGEX MATCH "${pattern}:[^\n]*" _match ${file})
49   if(_match)
50     string(REGEX REPLACE "${pattern}:[ ]*" "" _data ${_match})
51   endif(_match)
52   set(${_info} ${_data} PARENT_SCOPE)
53 endfunction()
54
55 # ----------Common stuff-------------------
56
57 file(GLOB_RECURSE _dafiles $ENV{ALICE_ROOT}/*da.cxx)
58 set(DAINSTALL "$ENV{ALICE_INSTALL}/DA")
59
60 file(MAKE_DIRECTORY ${DAINSTALL})
61
62 string (REPLACE "-pedantic-errors" "" CXXFLAGS ${CXXFLAGS})
63
64 find_program(XML2 xml2-config)
65
66 if(AMORE_FOUND)
67   #Set compiler flags
68   set(CXXFLAGS "${CXXFLAGS} ${AMOREFLAGS}")
69   set(CFLAGS "${CFLAGS} ${AMOREFLAGS}")
70   set(CINTFLAGS "${CINTFLAGS} ${AMOREFLAGS}")
71 else()
72   set(AMORELIBS)
73 endif(AMORE_FOUND)
74
75 execute_process(COMMAND ${XML2} --libs OUTPUT_VARIABLE _xml2libs OUTPUT_STRIP_TRAILING_WHITESPACE)
76 execute_process(COMMAND ${DATE_CONFIG} --monitorlibs=noshift OUTPUT_VARIABLE MONITORLIBS OUTPUT_STRIP_TRAILING_WHITESPACE)
77
78 separate_arguments(MONITORLIBS)
79
80 set(SYSLIBS -ldl -lpthread ${_xml2libs})
81
82
83 set(EXTRAROOTLIB "libRootExtra.a")
84
85 file(GLOB _extraroot "$ENV{ROOTSYS}/montercarlo/vmc/src/*.o" "$ENV{ROOTSYS}/tree/treeplayer/src/*.o" "$ENV{ROOTSYS}/io/xmlparser/src/*.o" "$ENV{ROOTSYS}/math/minuit2/src/*.o")
86
87 add_library(${EXTRAROOTLIB} STATIC ${_extraroot})       
88 set_target_properties(${EXTRAROOTLIB} PROPERTIES LINKER_LANGUAGE CXX)
89
90 set(DAQDALIB_PATH $ENV{DAQDALIB_PATH})  
91 if(DAQDALIB_PATH)
92   set(DAQDADIR "${DAQDALIB_PATH}")
93 else()
94   set(DAQDADIR "$ENV{ALICE}/daqDAlib")
95 endif(DAQDALIB_PATH)
96 set(DAQDALIB "${DAQDADIR}/libdaqDA.a")
97
98 include_directories(${DAQDADIR} RAW include STEER)
99 include_directories(SYSTEM ${ROOTINCDIR})
100
101 # ----------Create All Valid targets---------
102           
103 foreach(detector ${ONLINEDETECTORS} )
104
105   set(ONLINEDETECTORNAME ${detector})
106
107   detector_module(DAMODULE ${ONLINEDETECTORNAME})
108
109   detector_subda(SUBDAMODULE ${ONLINEDETECTORNAME})
110   
111   #ALIROOTALIBS
112
113   set(ALIROOTALIBS)
114
115   list(APPEND ALIROOTALIBS RAWDatabase-static RAWDatarec-static RAWDatasim-static STEERBase-static STEER-static CDB-static ESD-static STAT-static AOD-static )
116
117   expand(ALIROOTALIBS2 "\${${DAMODULE}ALIBS}")
118   expand(DAINCDIRS "\${${DAMODULE}INC}")
119   list(APPEND ALIROOTALIBS ${ALIROOTALIBS2})
120   
121   include_directories(${DAMODULE} ${SUBDIR} ${DAINCDIRS})
122 #Get detector algorithms for this detector
123
124   foreach(dafile ${_dafiles})
125
126         string(REGEX MATCH "$ENV{ALICE_ROOT}/${DAMODULE}/${DAMODULE}${SUBDAMODULE}" match ${dafile})
127 #Found a valid target name
128         if(match)
129           string(REGEX REPLACE "$ENV{ALICE_ROOT}/${DAMODULE}/${DAMODULE}${SUBDAMODULE}(.*)da\\.cxx" "\\1" DANAME ${dafile})
130 #Check for default DA 
131           if(DANAME)
132             set(DATARGETNAME "daqDA-${ONLINEDETECTORNAME}-${DANAME}")
133           else()
134             set(DATARGETNAME "daqDA-${ONLINEDETECTORNAME}")
135           endif(DANAME)
136
137           set(DATARGETDIR "${DAINSTALL}/${DAMODULE}/tgt_$ENV{ALICE_TARGET}")
138           file(MAKE_DIRECTORY ${DATARGETDIR})
139
140           set(DASRC "${DAMODULE}/${DAMODULE}${SUBDAMODULE}${DANAME}da.cxx")
141           set(DALIB "${DAMODULE}${SUBDAMODULE}${DANAME}DA")
142           set(DAEXE "${DAMODULE}${SUBDAMODULE}${DANAME}da.exe")
143           
144         # DAVERSION
145           execute_process(COMMAND svn info $ENV{ALICE_ROOT}/${DASRC} OUTPUT_VARIABLE _daversion OUTPUT_STRIP_TRAILING_WHITESPACE)
146           string(REGEX REPLACE ".*Last Changed Rev: ([^\n]+)\n.*" "\\1" DAVERSION ${_daversion})
147
148         # DAROOTRELEASE 
149           execute_process(COMMAND root-config --version OUTPUT_VARIABLE _darootrelease OUTPUT_STRIP_TRAILING_WHITESPACE)
150           string(REGEX REPLACE "/" "." DAROOTRELEASE ${_darootrelease})
151         
152         # DAALIROOTRELEASE
153           string(REGEX REPLACE ".*URL: .*/(.+)/${DASRC}.*$" "\\1" DAALIROOTRELEASE ${_daversion})
154         
155           set(DAARCNAME "${DATARGETNAME}")
156           set(DAARC "${DAARCNAME}-${DAVERSION}")
157           set(DATAR "${DATARGETDIR}/${DAARC}.src.tar.gz")
158           set(DASPECFILE "${DATARGETDIR}/${DAMODULE}${SUBDAMODULE}${DANAME}da.spec")
159           set(DAMAKEFILE "${DATARGETDIR}/${DAMODULE}${SUBDAMODULE}${DANAME}da.make")
160
161
162           
163           if(EXTRADAMODULE)
164             ##**set
165           endif(EXTRADAMODULE)
166
167           file(READ "$ENV{ALICE_ROOT}/${DASRC}" _dasrc)
168           getinfo(DACONTACT "Contact" ${_dasrc})
169           getinfo(DALINKPAGE "Link" ${_dasrc})
170           getinfo(DAREFRUN "Reference Run" ${_dasrc})
171           getinfo(DARUNTYPE "Run Type" ${_dasrc})
172           getinfo(DATYPE "DA Type" ${_dasrc})
173           getinfo(DANUMBEROFEVENTS "Number of events needed" ${_dasrc})
174           getinfo(DAINPUTFILES "Input Files" ${_dasrc})
175           getinfo(DAOUTPUTFILES "Output Files" ${_dasrc})
176           getinfo(DATRIGGERTYPE "Trigger types used" ${_dasrc})
177
178           if(NOT _dasrc)
179            message(FATAL_ERROR "SRC FILE MISSING")
180           endif(NOT _dasrc)
181
182 #----------- Targets ----------------------
183
184           set(CMAKE_CXX_FLAGS "${CXXFLAGS}")
185           set(CMAKE_C_FLAGS "${CFLAGS}")
186           set(CMAKE_Fortran_FLAGS ${FFLAGS})
187           set(CMAKE_SHARED_LINKER_FLAGS ${SOFLAGS}) 
188           set(CMAKE_MODULE_LINKER_FLAGS ${LDFLAGS})
189
190           set(ZIP)
191           foreach(_lib ${ALIROOTALIBS})
192            string(REGEX REPLACE "-static" "" _lib ${_lib})
193            list(APPEND ZIP && ar x "../lib${_lib}.a")
194           endforeach(_lib)
195           list (APPEND ZIP && ar r "../lib${DALIB}.a" "*.o")
196
197           add_custom_target( ${DALIB} COMMAND rm -rf junk && mkdir -p junk 
198                                 COMMAND cd junk${DAEXE} ${ZIP}
199                                 COMMAND cd ../ && rm -rf junk${DAEXE}
200                                 DEPENDS ${ALIROOTALIBS}
201                                 WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
202           add_custom_command( TARGET clean
203                              COMMAND rm -rf junk${DAEXE}
204                              WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
205             
206
207           add_custom_target(${DATARGETNAME})
208           add_executable(${DAEXE} ${DASRC})
209           set_property(TARGET ${DAEXE} PROPERTY EXCLUDE_FROM_ALL TRUE)
210           add_dependencies(${DAEXE} ${DALIB})
211           add_dependencies(DA-all ${DATARGETNAME})
212           target_link_libraries(${DAEXE} "-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${DALIB}.a" ${EXTRAROOTLIB} "${ROOTLIBDIR}/libRoot.a" "${ROOTLIBDIR}/libfreetype.a" "${ROOTLIBDIR}/libpcre.a" ${SYSLIBS} ${DAQDALIB} ${MONITORLIBS} ${AMOREDALIBS})
213           add_dependencies(${DATARGETNAME} ${DAEXE})
214           
215
216         endif(match)
217   endforeach(dafile)
218 endforeach(detector)