PAR: gen dicts using compiler's preprocessor
[u/mrichter/AliRoot.git] / cmake / ALICE_CMake.cmake
index 1d5e786..1a5c2aa 100644 (file)
@@ -1,9 +1,11 @@
+# -*- mode: cmake -*-
+
 # AliRoot Build System Utility Macro and Function definitions
 #
 # Author: Johny Jose (johny.jose@cern.ch)
 #         Port of previous Makefile build to cmake
 
-cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
+cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)
 
 macro(ALICE_DevFlagsOutput)
 
@@ -16,6 +18,12 @@ macro(ALICE_DevFlagsOutput)
     message(STATUS "SHLIB     : ${SHLIB}")
     message(STATUS "SYSLIBS   : ${SYSLIBS}")
     message(STATUS "CINTFLAGS : ${CINTFLAGS}")
+    message(STATUS "CMAKE_Fortran_FLAGS              : ${CMAKE_Fortran_FLAGS}")
+    message(STATUS "CMAKE_CXX_FLAGS          : ${CMAKE_CXX_FLAGS}")
+    message(STATUS "CMAKE_C_FLAGS            : ${CMAKE_C_FLAGS}")
+    message(STATUS "CMAKE_SHARED_LINKER_FLAGS : ${CMAKE_SHARED_LINKER_FLAGS}")
+    message(STATUS "CMAKE_MODULE_LINKER_FLAGS : ${CMAKE_MODULE_LINKER_FLAGS}")
+    message(STATUS "CMAKE_EXE_LINKER_FLAGS    : ${CMAKE_EXE_LINKER_FLAGS}")
   endif(ALICEDEV STREQUAL "YES")
 
 endmacro(ALICE_DevFlagsOutput)
@@ -34,7 +42,7 @@ function(ALICE_CorrectPaths _output value )
     set(external)
     string(REGEX MATCH "^/" external "${path}")
     if(NOT external)
-      list(APPEND corrected "${CMAKE_SOURCE_DIR}/${path}" )
+      list(APPEND corrected "${AliRoot_SOURCE_DIR}/${path}" )
     else()
       list(APPEND corrected "${path}")
     endif(NOT external)
@@ -100,12 +108,12 @@ endfunction(ALICE_RootConfig)
 macro(ALICE_CheckOutOfSourceBuild)
   
   #Check if previous in-source build failed
-  if(EXISTS ${CMAKE_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${CMAKE_SOURCE_DIR}/CMakeFiles)
+  if(EXISTS ${AliRoot_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${AliRoot_SOURCE_DIR}/CMakeFiles)
     message(FATAL_ERROR "CMakeCache.txt or CMakeFiles exists in source directory! Please remove them before running cmake $ALICE_ROOT")
-  endif(EXISTS ${CMAKE_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${CMAKE_SOURCE_DIR}/CMakeFiles)
+  endif(EXISTS ${AliRoot_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${AliRoot_SOURCE_DIR}/CMakeFiles)
   
   #Get Real Paths of the source and binary directories
-  get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
+  get_filename_component(srcdir "${AliRoot_SOURCE_DIR}" REALPATH)
   get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
   
   #Check for in-source builds
@@ -117,22 +125,6 @@ endmacro(ALICE_CheckOutOfSourceBuild)
 
 macro(ALICE_ConfigureCompiler)
 
-  if(ALIDEBUG STREQUAL "YES")
-    set(ALICE_TARGET ${ALICE_TARGET}DEBUG)
-    message("Befor ${CXXFLAGS}")
-    string(REGEX MATCHALL "-O[^ ]*" FFLAGS "${FFLAGS}")
-    string(REGEX MATCHALL "-O[^ ]*" CXXFLAGS "${CXXFLAGS}")
-    message("After ${CXXFLAGS}")
-    string(REGEX MATCHALL "-O[^ ]*" CFLAGS "${CFLAGS}")
-    string(REGEX MATCHALL "-O[^ ]*" SOFLAGS "${SOFLAGS}")
-    string(REGEX MATCHALL "-O[^ ]*" LDFLAGS "${LDFLAGS}")
-    set(FFLAGS "-g ${FFLAGS}")
-    set(CXXFLAGS "-g ${CXXFLAGS}")
-    set(CFLAGS "-g ${CFLAGS}")
-    set(SOFLAGS "-g ${SOFLAGS}")
-    set(LDFLAGS "-g ${LDFLAGS}")
-  endif(ALIDEBUG STREQUAL "YES")
-
   if(ALIPROFILE STREQUAL "YES")
     set(ALICE_TARGET ${ALICE_TARGET}PROF)
     set(FFLAGS "-pg ${FFLAGS}")
@@ -143,9 +135,14 @@ macro(ALICE_ConfigureCompiler)
   endif(ALIPROFILE STREQUAL "YES")
 
   ALICE_RootConfig(RCFLAGS --auxcflags)
+  if ( CMAKE_CXX_COMPILER MATCHES "clang") 
+  string(REPLACE "-pthread" " " RCFLAGS "${RCFLAGS}")
+  endif ( CMAKE_CXX_COMPILER MATCHES "clang") 
+
   ALICE_RootConfig(RLFLAGS --ldflags)
 
   set(CXXFLAGS "${CXXFLAGS} ${RCFLAGS}")
+  string(REPLACE "-std=c++11" " " RCFLAGS "${RCFLAGS}")
   set(CFLAGS  "${CFLAGS} ${RCFLAGS}")
   set(SOFLAGS "${SOFLAGS} ${RLFLAGS}")
   set(LDFLAGS "${LDFLAGS} ${RLFLAGS}")
@@ -246,6 +243,8 @@ macro(ALICE_ResetPackage)
   set(PSHLIBS)
   set(PDS)
 
+  set(saveEINCLUDE)
+
 endmacro(ALICE_ResetPackage)
 
 function(ALICE_SetPackageVariable _var ext setvalue unsetvalue )
@@ -262,8 +261,15 @@ function(ALICE_SetPackageVariable _var ext setvalue unsetvalue )
 endfunction(ALICE_SetPackageVariable)
 
 macro(ALICE_BuildPackage)
+  # message(STATUS " ${PACKAGE}")
  
   list(APPEND EINCLUDE STEER) 
+  # Needed for PAR files - we need to do this before path 
+  # expansion so that the PAR files do not contain references to 
+  # specific directories but depend solely on ALICE_ROOT
+  string(REPLACE ";" " " saveEINCLUDE "${EINCLUDE}")
+  # set(saveEINCLUDE "${EINCLUDE}")
+  # message(STATUS "saveEINCLUDE=${saveEINCLUDE} EINCLUDE=${EINCLUDE}")
 
   ALICE_SetPackageVariable(PFFLAGS "FFLAGS" "${PACKFFLAGS}" "${FFLAGS}")
   ALICE_SetPackageVariable(PCFLAGS "CFLAGS" "${PACKCFLAGS}" "${CFLAGS}")
@@ -280,6 +286,7 @@ macro(ALICE_BuildPackage)
 
   ALICE_SetPackageVariable(PEDEFINE "EDEFINE" "${EDEFINE}" "${EDEFINE}")
   ALICE_SetPackageVariable(PEXPORT "EXPORT" "${EXPORT}" "${EXPORT}")
+#  message(${CMAKE_INCLUDE_EXPORT_DIRECTORY})
   ALICE_SetPackageVariable(PEXPORTDEST "EXPORTDEST" "${CMAKE_INCLUDE_EXPORT_DIRECTORY}" "${CMAKE_INCLUDE_EXPORT_DIRECTORY}")
   ALICE_SetPackageVariable(PINC "INC" "${EINCLUDE};${MODULE}" "${EINCLUDE};${MODULE}")
   ALICE_SetPackageVariable(PELIBS "ELIBS" "${ELIBS}" "${ELIBS}")
@@ -311,6 +318,8 @@ macro(ALICE_BuildPackage)
     list(APPEND PMALIBS ${PACKAGE}-static)
     list(APPEND ALLALIBS ${PACKAGE}-static)
     list(APPEND BINLIBS ${PACKAGE})
+    # Do not link against other libraries when creating a library 
+    # set(PELIBS)
   else()
     list(APPEND ALLEXECS ${PACKAGE})
   endif(lib)
@@ -318,9 +327,11 @@ macro(ALICE_BuildPackage)
   set(${MODULE}LIBS "${PMLIBS}" PARENT_SCOPE)
   set(${MODULE}INC "${EINCLUDE}" PARENT_SCOPE)
   list(APPEND INCLUDEFILES ${PEXPORTDEST})
-  ALICE_CopyHeaders()
   if(WITHDICT)  
-    ALICE_SetPackageVariable(PDS "DS" "G__${PACKAGE}.cxx" "G__${PACKAGE}.cxx")
+    # Replace dots in file name with underscores, or rootcint will 
+    # write invalid code (namespace names with dots in them)
+    string(REPLACE "." "_" ESC_PACKAGE ${PACKAGE})
+    ALICE_SetPackageVariable(PDS "DS" "G__${ESC_PACKAGE}.cxx" "G__${ESC_PACKAGE}.cxx")
     ALICE_GenerateDictionary()
   else()
     if(lib)
@@ -334,15 +345,16 @@ macro(ALICE_BuildPackage)
       ALICE_BuildExecutable()
     endif(lib)
   endif(PS OR PCS OR PFS)
+  ALICE_CopyHeaders()
   ALICE_BuildPAR()
-  ALICE_CheckModule()
 
 endmacro(ALICE_BuildPackage)
 
 
 macro(ALICE_BuildModule)
-
-  add_definitions(-D_MODULE="${MODULE}")
+  # message(STATUS "${MODULE}")
+  execute_process(COMMAND ${CMAKE_COMMAND} -E echo_append "-- ${MODULE}:")
+  add_definitions(-D_MODULE_="${MODULE}")
   foreach(PACKAGEFILE ${PACKAGES})
       set(lib)
       set(bin)
@@ -350,6 +362,7 @@ macro(ALICE_BuildModule)
       string(REGEX MATCH "CMakebin" bin "${PACKAGEFILE}")
       get_filename_component(PACKAGE ${PACKAGEFILE} NAME)
       string(REGEX REPLACE "^CMake(lib|bin)(.*)\\.pkg" "\\2" PACKAGE "${PACKAGE}")
+      execute_process(COMMAND ${CMAKE_COMMAND} -E echo_append " ${PACKAGE}")
       if(ALICEDEV)
         message("Adding package ${PACKAGE} in ${MODULE}")
       endif(ALICEDEV)
@@ -365,7 +378,8 @@ macro(ALICE_BuildModule)
                 ARCHIVE DESTINATION bin/tgt_${ALICE_TARGET})
       endif(NOT EXCLUDEPACKAGE)
   endforeach(PACKAGEFILE)
-
+  ALICE_CheckModule()
+  execute_process(COMMAND ${CMAKE_COMMAND} -E echo " done")
 endmacro(ALICE_BuildModule)
 
 macro(ALICE_GenerateDictionary)
@@ -388,7 +402,20 @@ macro(ALICE_GenerateDictionary)
   separate_arguments(DCINTHDRS)
   separate_arguments(DDH)
   # Format neccesary arguments
+  set(ROOT6_ALPHA "5.99.1")
+  string(REGEX REPLACE "/0*" "." ROOT_DOTVERSION ${ROOT_VERSION})
+  if(ROOT_DOTVERSION VERSION_GREATER ROOT6_ALPHA)
+    # For current ROOT6-alpha - without proper PCM support - we need to 
+    # give the _full_ path for all include paths, so that libCling can
+    # later attach those to the internal include path of the interpreter 
+    # and so that the interpreter can parse the header files. 
+    #
+    # THIS SHOULD BE A TEMPORARY HACK!
+    ALICE_Format(DINC "-I${PROJECT_SOURCE_DIR}/" "" 
+      "${DINC};${CMAKE_INCLUDE_EXPORT_DIRECTORY}")
+  else()
   ALICE_Format(DINC "-I" "" "${DINC};${CMAKE_INCLUDE_EXPORT_DIRECTORY}")
+  endif(ROOT_DOTVERSION VERSION_GREATER ROOT6_ALPHA)
   set_source_files_properties(${PDS} PROPERTIES GENERATED TRUE)
   set_source_files_properties(${PDS} PROPERTIES COMPILE_FLAGS "-w")
   add_custom_command(OUTPUT  ${PDS}
@@ -403,12 +430,12 @@ endmacro(ALICE_GenerateDictionary)
 
 macro(ALICE_BuildLibrary)
 
-  ALICE_DevFlagsOutput()
   set(CMAKE_CXX_FLAGS "${PEDEFINE} ${PCXXFLAGS}")
   set(CMAKE_C_FLAGS "${PEDEFINE} ${PCFLAGS}")
-  set(CMAKE_Fortran_FLAGS ${PEDEFINE} ${PFFLAGS})
+  set(CMAKE_Fortran_FLAGS "${PEDEFINE} ${PFFLAGS}")
   set(CMAKE_SHARED_LINKER_FLAGS ${PSOFLAGS}) 
   set(CMAKE_MODULE_LINKER_FLAGS ${PLDFLAGS})
+  ALICE_DevFlagsOutput()
   
   
   separate_arguments(PINC)
@@ -420,7 +447,6 @@ macro(ALICE_BuildLibrary)
 
   ALICE_Format(PELIBSDIR "-L" "" "${PELIBSDIR}")
   ALICE_CheckLibraries(PBLIBS "${PBLIBS}")
-  ALICE_CheckLibraries(PELIBS "${PELIBS}")
 
   ALICE_CorrectPaths(EINCLUDE "${EINCLUDE}")
   ALICE_CorrectPaths(PINC "${PINC}")
@@ -428,7 +454,8 @@ macro(ALICE_BuildLibrary)
   include_directories(SYSTEM ${ROOTINCDIR})
   include_directories(${PINC})  
   include_directories(${EINCLUDE})
-  include_directories(${CMAKE_INCLUDE_EXPORT_DIRECTORY})
+  include_directories(${CMAKE_INCLUDE_EXPORT_DIRECTORY}/FromTemplate)
+  include_directories(${AliRoot_SOURCE_DIR})
   
   add_library(${PACKAGE} SHARED ${PCS} ${PFS} ${PS} ${PDS})
   set_target_properties(${PACKAGE} PROPERTIES SUFFIX .so)  
@@ -457,16 +484,19 @@ macro(ALICE_BuildLibrary)
     set_property(TARGET ${PACKAGE} PROPERTY EXCLUDE_FROM_ALL TRUE)
   endif(NOT RESULT STREQUAL "-1")
 
+  set_target_properties(${PACKAGE} PROPERTIES INSTALL_NAME_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
+
 endmacro(ALICE_BuildLibrary)
 
 macro(ALICE_BuildExecutable)
 
-  ALICE_DevFlagsOutput()
   set(CMAKE_CXX_FLAGS "${PEDEFINE} ${PCXXFLAGS} ${EXEFLAGS}")
   set(CMAKE_C_FLAGS "${PEDEFINE} ${PCFLAGS} ${EXEFLAGS}")
   set(CMAKE_Fortran_FLAGS "${PEDEFINE} ${PFFLAGS} ${EXEFLAGS}")
   set(CMAKE_SHARED_LINKER_FLAGS ${PSOFLAGS}) 
   set(CMAKE_MODULE_LINKER_FLAGS ${PLDFLAGS})
+  set(CMAKE_EXE_LINKER_FLAGS ${PLDFLAGS})
+  ALICE_DevFlagsOutput()
   
   separate_arguments(PINC)
   separate_arguments(EINCLUDE)
@@ -474,10 +504,9 @@ macro(ALICE_BuildExecutable)
   separate_arguments(PBLIBS)
   separate_arguments(PELIBS)
   separate_arguments(SHLIB)
-
+  
   ALICE_Format(PELIBSDIR "-L" "" "${PELIBSDIR}")
   ALICE_CheckLibraries(PBLIBS "${PBLIBS}")
-  #ALICE_CheckLibraries(PELIBS "${PELIBS}")
 
   ALICE_CorrectPaths(EINCLUDE "${EINCLUDE}")
   ALICE_CorrectPaths(PINC "${PINC}")
@@ -550,51 +579,53 @@ endmacro(ALICE_BuildExecutable)
 
 macro(ALICE_CopyHeaders)
   
-  if(NOT EXPORT )
-    set(HEADERS)
-  else()
+  if(EXPORT )
     set(HEADERS ${EXPORT})
-  endif(NOT EXPORT)
-  foreach(header ${HEADERS})
-      configure_file(${header} ${PEXPORTDEST} COPYONLY)
+    set(_headersdep)
+    foreach(header ${HEADERS})
+      get_filename_component( header_name ${header} NAME )
+      add_custom_command(OUTPUT ${PEXPORTDEST}/${header_name}
+                         COMMAND ${CMAKE_COMMAND} -E copy ${AliRoot_SOURCE_DIR}/${MODULE}/${header} ${PEXPORTDEST}/${header_name}
+                        DEPENDS ${AliRoot_SOURCE_DIR}/${MODULE}/${header})
+      list(APPEND _headersdep ${PEXPORTDEST}/${header_name})
       install(FILES ${header} DESTINATION include)
-  endforeach(header)
+    endforeach(header)
+    add_custom_target(${PACKAGE}-headers DEPENDS ${_headersdep})
+    add_dependencies(${PACKAGE} ${PACKAGE}-headers)
+  endif(EXPORT)
 
 endmacro(ALICE_CopyHeaders)
 
 macro(ALICE_GenerateLinkDef)
-
-  file (WRITE ${PDAL} "//--------------------------------------------------------------------------------\n")
-  file (APPEND ${PDAL} "// This is an automatically generated Root Dictionary Linkdef file for ${PACKAGE}\n")
-  file (APPEND ${PDAL} "// !!! DO NOT EDIT THIS FILE !!!\n")
-  file (APPEND ${PDAL} "#ifdef __CINT__\n")
-  file (APPEND ${PDAL} "#pragma link off all globals;\n")
-  file (APPEND ${PDAL} "#pragma link off all classes;\n")
-  file (APPEND ${PDAL} "#pragma link off all functions;\n")
+  set(PCLASSES)
   foreach (class ${PCINTCLASSES})
     get_filename_component(classname ${class} NAME)
-    file(APPEND ${PDAL} "#pragma link C++ class ${classname}+;\n")
+    # set(PCLASSES ${PCLASSES} "\\n#pragma link C++ class ${classname}+;")
+    set(PCLASSES ${PCLASSES} "${classname}")
   endforeach(class)
-  file (APPEND ${PDAL} "#endif\n")
+
+  add_custom_command(OUTPUT ${PDAL}
+    COMMAND sh ${AliRoot_SOURCE_DIR}/cmake/GenerateLinkDef.sh ${PCLASSES} > ${PDAL} 
+    DEPENDS ${PCINTHDRS} ${AliRoot_SOURCE_DIR}/cmake/GenerateLinkDef.sh)
 endmacro(ALICE_GenerateLinkDef)
 
 macro(ALICE_BuildPAR)
   
-  # if(EXISTS ${ALICE_ROOT}/${MODULE}/PROOF-INF.${PACKAGE})
-  if(EXISTS ${CMAKE_SOURCE_DIR}/${MODULE}/PROOF-INF.${PACKAGE})
+  if(EXISTS ${AliRoot_SOURCE_DIR}/${MODULE}/PROOF-INF.${PACKAGE})
     set(PARSRCS)
-    foreach(file ${SRCS} ${HDRS} ${FSRCS} ${DHDR})
+    foreach(file ${SRCS} ${HDRS} ${FSRCS} ${DHDR} ${OTHERS} )
       get_filename_component(srcdir ${file} PATH)
       add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${file}-par                         
                          COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${srcdir}
                          COMMAND cp -pR ${file} ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${file}
                          WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
       list(APPEND PARSRCS ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/${file}-par)
-    endforeach(file ${SRCS} ${HDRS} ${FSRCS} ${DHDR})
+    endforeach(file ${SRCS} ${HDRS} ${FSRCS} ${DHDR} ${OTHERS} )
     
+    # message(STATUS "saveEINCLUDE=${saveEINCLUDE}")
     add_custom_target(${PACKAGE}.par
-                      COMMAND sed -e 's/include .\(ROOTSYS\)\\/test\\/Makefile.arch/include Makefile.arch/\; s/PACKAGE = .*/PACKAGE = ${PACKAGE}/' < Makefile > ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/Makefile
-                      COMMAND cp -pR ${ROOTSYS}/test/Makefile.arch ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/Makefile.arch
+                      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
+                      COMMAND cp -pR `root-config --etcdir`/Makefile.arch ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/Makefile.arch
                       COMMAND cp -pR PROOF-INF.${PACKAGE} ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}/PROOF-INF
 #                      COMMAND cp -pR lib${PACKAGE}.pkg ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE}
                       COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR} tar --exclude=.svn -czhf ${CMAKE_BINARY_DIR}/${PACKAGE}.par ${PACKAGE}
@@ -604,6 +635,7 @@ macro(ALICE_BuildPAR)
                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
 
 
+    add_dependencies(${PACKAGE}.par ${SRCS} ${FSRCS} ${HDRS} ${DHDR})
     add_dependencies(par-all ${PACKAGE}.par)
     add_dependencies(${MODULE}-par-all ${PACKAGE}.par)
 
@@ -615,10 +647,11 @@ macro(ALICE_BuildPAR)
                       COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --green --bold "${PACKAGE}.par testing succeeded"
                       WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
 
+    add_dependencies(test-${PACKAGE}.par ${PACKAGE}.par)
     add_dependencies(test-par-all test-${PACKAGE}.par)
     add_dependencies(test-${MODULE}-par-all test-${PACKAGE}.par)
 
-  endif(EXISTS ${CMAKE_SOURCE_DIR}/${MODULE}/PROOF-INF.${PACKAGE})
+  endif(EXISTS ${AliRoot_SOURCE_DIR}/${MODULE}/PROOF-INF.${PACKAGE})
   # endif(EXISTS ${ALICE_ROOT}/${MODULE}/PROOF-INF.${PACKAGE})
 
 endmacro(ALICE_BuildPAR)