From ba6f1d2a4d7559a2a4b3ca147bd2dcbea73542be Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Tue, 15 Oct 2019 13:26:04 +0200
Subject: [PATCH] CMake: add possibility to compile static libraries instead of
 shared libraries

---
 CMakeLists.txt                    |  1 +
 DDAlign/CMakeLists.txt            |  6 ++++--
 DDCond/CMakeLists.txt             |  6 ++++--
 DDCore/CMakeLists.txt             | 11 +++++++----
 DDDetectors/CMakeLists.txt        |  5 ++++-
 DDDigi/CMakeLists.txt             |  7 +++++--
 DDEve/CMakeLists.txt              |  8 ++++++--
 DDG4/CMakeLists.txt               |  5 +++--
 DDParsers/CMakeLists.txt          |  6 ++++--
 DDRec/CMakeLists.txt              |  7 +++++--
 GaudiPluginService/CMakeLists.txt |  3 ++-
 cmake/DD4hepBuild.cmake           | 11 ++++++++---
 12 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 49db2de8d..6dbc0154c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -68,6 +68,7 @@ option(DD4HEP_IGNORE_GEANT4_TLS "Ignore the tls flag Geant4 was compiled with" O
 option(DD4HEP_USE_GEAR    "Build gear wrapper for backward compatibility" OFF)
 option(DD4HEP_USE_LCIO    "Build lcio extensions"     OFF)
 option(BUILD_TESTING      "Enable and build tests"    ON)
+option(BUILD_SHARED_LIBS  "If OFF build STATIC Libraries" ON)
 option(CMAKE_MACOSX_RPATH "Build with rpath on macos" ON)
 SET(DD4HEP_BUILD_PACKAGES "DDRec DDDetectors DDCond DDAlign DDDigi DDG4 DDEve UtilityApps"
   CACHE STRING "List of DD4hep packages to build")
diff --git a/DDAlign/CMakeLists.txt b/DDAlign/CMakeLists.txt
index 4a3f56d9c..fea6fe8f6 100644
--- a/DDAlign/CMakeLists.txt
+++ b/DDAlign/CMakeLists.txt
@@ -12,7 +12,7 @@ SET_PROPERTY(DIRECTORY . PROPERTY PACKAGE_NAME DDAlign)
 
 file(GLOB DDAlign_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
 
-add_library(DDAlign SHARED ${DDAlign_SOURCES})
+add_library(DDAlign ${DDAlign_SOURCES})
 add_library(DD4hep::DDAlign ALIAS DDAlign)
 
 target_include_directories(DDAlign
@@ -28,4 +28,6 @@ dd4hep_add_plugin(DDAlignPlugins SOURCES src/plugins/*.cpp USES DDAlign)
 set_target_properties(DDAlign DDAlignPlugins PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
 
 install(DIRECTORY include/DDAlign DESTINATION include)
-install(TARGETS DDAlign DDAlignPlugins EXPORT DD4hep DESTINATION lib)
+install(TARGETS DDAlign DDAlignPlugins EXPORT DD4hep
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib)
diff --git a/DDCond/CMakeLists.txt b/DDCond/CMakeLists.txt
index f611348fe..10c8fade1 100644
--- a/DDCond/CMakeLists.txt
+++ b/DDCond/CMakeLists.txt
@@ -18,7 +18,7 @@ dd4hep_add_dictionary(G__DDCond
 
 file(GLOB DDCond_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
 
-add_library(DDCond SHARED ${DDCond_SOURCES} G__DDCond.cxx)
+add_library(DDCond ${DDCond_SOURCES} G__DDCond.cxx)
 add_library(DD4hep::DDCond ALIAS DDCond)
 
 target_include_directories(DDCond
@@ -37,4 +37,6 @@ dd4hep_add_plugin(DDCondPlugins
 set_target_properties(DDCond DDCondPlugins PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
 
 install(DIRECTORY include/DDCond DESTINATION include)
-install(TARGETS DDCond DDCondPlugins EXPORT DD4hep DESTINATION lib)
+install(TARGETS DDCond DDCondPlugins EXPORT DD4hep
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib)
diff --git a/DDCore/CMakeLists.txt b/DDCore/CMakeLists.txt
index 44d188773..2a3ac3366 100644
--- a/DDCore/CMakeLists.txt
+++ b/DDCore/CMakeLists.txt
@@ -58,7 +58,7 @@ if(TARGET Boost::boost)
   file(GLOB DDCore_BOOST_SOURCES src/JSON/*.cpp)
 endif()
 
-add_library(DDCore SHARED ${DDCore_SOURCES} ${DDCore_BOOST_SOURCES}
+add_library(DDCore ${DDCore_SOURCES} ${DDCore_BOOST_SOURCES}
   G__DD4hep.cxx
   G__DD4hepSegmentations.cxx
   G__DD4hepProperties.cxx
@@ -97,7 +97,7 @@ IF(TARGET ROOT::Gdml)
     USES DD4hep::DDCore ROOT::Core ROOT::Gdml
     )
   set_target_properties(DDGDMLPlugins PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
-  install(TARGETS DDGDMLPlugins EXPORT DD4hep LIBRARY DESTINATION lib)
+  LIST(APPEND OPTIONAL_TARGETS DDGDMLPlugins)
 ELSE()
   dd4hep_print("ROOT does not include GDML, not building DDCore GDML Plugins")
 ENDIF()
@@ -109,7 +109,7 @@ IF(TARGET ROOT::PyROOT)
     USES DDCore ROOT::Core ROOT::PyROOT
     )
   set_target_properties(DDPythonPlugins PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
-  install(TARGETS DDPythonPlugins EXPORT DD4hep LIBRARY DESTINATION lib)
+  LIST(APPEND OPTIONAL_TARGETS DDPythonPlugins)
 ELSE()
   dd4hep_print("ROOT does not include PyRoot, not building DDCore DDPython Plugins")
 ENDIF()
@@ -130,4 +130,7 @@ install(DIRECTORY
   )
 
 set_target_properties(DDCore DDCorePlugins PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
-install(TARGETS DDCore DDCorePlugins EXPORT DD4hep LIBRARY DESTINATION lib)
+install(TARGETS DDCore DDCorePlugins ${OPTIONAL_TARGETS} EXPORT DD4hep
+  ARCHIVE DESTINATION lib
+  LIBRARY DESTINATION lib
+  )
diff --git a/DDDetectors/CMakeLists.txt b/DDDetectors/CMakeLists.txt
index 8e248693e..1851b2a97 100644
--- a/DDDetectors/CMakeLists.txt
+++ b/DDDetectors/CMakeLists.txt
@@ -25,5 +25,8 @@ dd4hep_add_plugin(DDDetectors
 )
 
 set_target_properties(DDDetectors PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
-install(TARGETS DDDetectors EXPORT DD4hep LIBRARY DESTINATION lib)
+install(TARGETS DDDetectors EXPORT DD4hep
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib
+  )
 install(DIRECTORY compact DESTINATION DDDetectors)
diff --git a/DDDigi/CMakeLists.txt b/DDDigi/CMakeLists.txt
index c0ac553a0..d6f26201f 100644
--- a/DDDigi/CMakeLists.txt
+++ b/DDDigi/CMakeLists.txt
@@ -10,7 +10,7 @@
 #==========================================================================
 SET_PROPERTY(DIRECTORY . PROPERTY PACKAGE_NAME DDDigi)
 file(GLOB DDDigi_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
-add_library(DDDigi SHARED ${DDDigi_SOURCES})
+add_library(DDDigi ${DDDigi_SOURCES})
 add_library(DD4hep::DDDigi ALIAS DDDigi)
 
 target_link_libraries(DDDigi PUBLIC
@@ -53,6 +53,9 @@ set_target_properties(DDDigi DDDigiPlugins PROPERTIES VERSION ${DD4hep_VERSION}
 
 file(GLOB DDigi_python python/*.py python/*.C)
 install(FILES ${DDigi_python} DESTINATION ${DD4HEP_PYTHON_INSTALL_DIR})
-install(TARGETS DDDigi DDDigiPlugins EXPORT DD4hep LIBRARY DESTINATION lib)
+install(TARGETS DDDigi DDDigiPlugins EXPORT DD4hep
+  ARCHIVE DESTINATION lib
+  LIBRARY DESTINATION lib
+  )
 install(DIRECTORY include/DDDigi DESTINATION include)
 
diff --git a/DDEve/CMakeLists.txt b/DDEve/CMakeLists.txt
index 3c8fbdf6e..e0cbca66f 100644
--- a/DDEve/CMakeLists.txt
+++ b/DDEve/CMakeLists.txt
@@ -45,10 +45,13 @@ if(DD4HEP_USE_GEANT4)
     LINKDEF ../DDCore/include/ROOT/LinkDef.h
     USES    DD4hep::DDG4 
     )
-  add_library(DDG4IO SHARED DDEve/IO.cpp G__DDG4IO.cxx)
+  add_library(DDG4IO DDEve/IO.cpp G__DDG4IO.cxx)
   target_link_libraries(DDG4IO DD4hep::DDG4 DD4hep::DDEve_Interface)
   set_target_properties(DDG4IO PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
-  install(TARGETS DDG4IO EXPORT DD4hep LIBRARY DESTINATION lib)
+  install(TARGETS DDG4IO EXPORT DD4hep
+    LIBRARY DESTINATION lib
+    ARCHIVE DESTINATION lib
+    )
 endif()
 
 add_executable(ddeve DDEve/DDEve.cpp)
@@ -61,6 +64,7 @@ set_target_properties(DDEvePlugins PROPERTIES VERSION ${DD4hep_VERSION} SOVERSIO
 install(DIRECTORY DDEve DESTINATION examples )
 install(DIRECTORY include/DDEve DESTINATION include)
 install(TARGETS ddeve DDEvePlugins DDEve_Interface EXPORT DD4hep
+  ARCHIVE DESTINATION lib
   LIBRARY DESTINATION lib
   RUNTIME DESTINATION bin
   )
\ No newline at end of file
diff --git a/DDG4/CMakeLists.txt b/DDG4/CMakeLists.txt
index 0623be586..98fa70bf2 100644
--- a/DDG4/CMakeLists.txt
+++ b/DDG4/CMakeLists.txt
@@ -18,7 +18,7 @@ ENDIF()
 
 #---Add Library---------------------------------------------------------------------
 file(GLOB DDG4_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
-add_library(DDG4 SHARED ${DDG4_SOURCES})
+add_library(DDG4 ${DDG4_SOURCES})
 add_library(DD4hep::DDG4 ALIAS DDG4)
 
 target_link_libraries(DDG4
@@ -63,7 +63,7 @@ if(TARGET Python::Python)
   if(TARGET ROOT::PyROOT)
     #---------------------------  Specialized python plugins  --------------------------
     dd4hep_print("|++> ROOT Has Python, creating DDPython library and plugins")
-    add_library(DDPython SHARED G__DDPython.cxx tpython/DDPython.cpp)
+    add_library(DDPython G__DDPython.cxx tpython/DDPython.cpp)
     add_library(DD4hep::DDPython ALIAS DDPython)
     target_link_libraries(DDPython DD4hep::DDG4 ROOT::Core Python::Python ROOT::PyROOT)
     IF(${CMAKE_CXX_STANDARD} GREATER 16)
@@ -137,6 +137,7 @@ set_target_properties(DDG4 DDG4Plugins DDG4Legacy PROPERTIES VERSION ${DD4hep_VE
 
 install(TARGETS DDG4 DDG4Plugins DDG4Legacy g4gdmlDisplay g4FromXML
   EXPORT DD4hep
+  ARCHIVE DESTINATION lib
   LIBRARY DESTINATION lib
   RUNTIME DESTINATION bin)
 install(DIRECTORY include/DDG4 DESTINATION include)
diff --git a/DDParsers/CMakeLists.txt b/DDParsers/CMakeLists.txt
index ac1be0f3a..4f857c33b 100644
--- a/DDParsers/CMakeLists.txt
+++ b/DDParsers/CMakeLists.txt
@@ -15,7 +15,7 @@ dd4hep_print("|++> Will build DDParsers")
 file(GLOB DDParsers_SOURCES src/Parsers/*.cpp src/Evaluator/*.cpp)
 file(GLOB DDParsers_SPIRIT_SOURCES src/Spirit/*.cpp)
 
-add_library(DDParsers SHARED ${DDParsers_SOURCES} ${DDParsers_SPIRIT_SOURCES})
+add_library(DDParsers ${DDParsers_SOURCES} ${DDParsers_SPIRIT_SOURCES})
 add_library(DD4hep::DDParsers ALIAS DDParsers)
 
 target_compile_definitions(DDParsers INTERFACE BOOST_SPIRIT_USE_PHOENIX_V3)
@@ -29,5 +29,7 @@ target_include_directories(DDParsers
 target_link_libraries(DDParsers PUBLIC ROOT::Core Boost::boost ${XML_LIBRARIES})
 
 set_target_properties(DDParsers PROPERTIES VERSION ${DD4hep_VERSION} SOVERSION ${DD4hep_SOVERSION})
-install(TARGETS DDParsers EXPORT DD4hep LIBRARY DESTINATION lib)
 install(DIRECTORY include/Parsers include/Evaluator DESTINATION include)
+install(TARGETS DDParsers ${OPTIONAL_TARGETS} EXPORT DD4hep
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib)
diff --git a/DDRec/CMakeLists.txt b/DDRec/CMakeLists.txt
index 4633ebb14..543cb0aff 100644
--- a/DDRec/CMakeLists.txt
+++ b/DDRec/CMakeLists.txt
@@ -27,7 +27,7 @@ if(DD4HEP_USE_GEAR)
   SET(GEAR_SOURCE_FILE src/gear/DDGear.cpp)
 endif()
 
-ADD_LIBRARY(DDRec SHARED
+ADD_LIBRARY(DDRec
   ${DDRec_SOURCES}
   ${GEAR_SOURCE_FILE}
   G__DDRec.cxx
@@ -75,4 +75,7 @@ set_target_properties(DDRec DDRecPlugins PROPERTIES VERSION ${DD4hep_VERSION} SO
 file(GLOB DDRec_python python/*.py)
 install(FILES ${DDRec_python} DESTINATION ${DD4HEP_PYTHON_INSTALL_DIR})
 install(DIRECTORY include/DDRec DESTINATION include)
-install(TARGETS DDRec DDRecPlugins EXPORT DD4hep LIBRARY DESTINATION lib)
+install(TARGETS DDRec DDRecPlugins EXPORT DD4hep
+  ARCHIVE DESTINATION lib
+  LIBRARY DESTINATION lib
+  )
diff --git a/GaudiPluginService/CMakeLists.txt b/GaudiPluginService/CMakeLists.txt
index f8aaa6aee..b3196eaa6 100644
--- a/GaudiPluginService/CMakeLists.txt
+++ b/GaudiPluginService/CMakeLists.txt
@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
 
 project(GaudiPluginService)
-add_library(DD4hepGaudiPluginMgr SHARED src/PluginServiceV1.cpp src/PluginServiceV2.cpp interface/DD4hepV1.cpp interface/DD4hepV2.cpp)
+add_library(DD4hepGaudiPluginMgr src/PluginServiceV1.cpp src/PluginServiceV2.cpp interface/DD4hepV1.cpp interface/DD4hepV2.cpp)
 add_library(DD4hep::DD4hepGaudiPluginMgr ALIAS DD4hepGaudiPluginMgr)
 
 target_compile_definitions(DD4hepGaudiPluginMgr PUBLIC Gaudi=DD4hep_Flavor)
@@ -31,4 +31,5 @@ target_compile_options(listcomponents PRIVATE -Wno-deprecated)
 
 INSTALL(TARGETS listcomponents DD4hepGaudiPluginMgr EXPORT DD4hep
   RUNTIME DESTINATION bin
+  ARCHIVE DESTINATION lib
   LIBRARY DESTINATION lib)
\ No newline at end of file
diff --git a/cmake/DD4hepBuild.cmake b/cmake/DD4hepBuild.cmake
index 4b114b7a6..f5c95aa32 100644
--- a/cmake/DD4hepBuild.cmake
+++ b/cmake/DD4hepBuild.cmake
@@ -613,17 +613,22 @@ function(dd4hep_add_plugin binary)
   dd4hep_debug("|++++> USES ${ARG_USES}")
   dd4hep_debug("|++++> INCLUDES ${ARG_INCLUDES}")
   dd4hep_debug("|++++> DEFINITIONS ${ARG_DEFINITIONS}")
-  add_library(${binary} SHARED ${SOURCES} ${ARG_GENERATED})
+  add_library(${binary} ${SOURCES} ${ARG_GENERATED})
   target_link_libraries(${binary} PUBLIC ${ARG_USES})
   target_include_directories(${binary} PUBLIC ${ARG_INCLUDES})
   target_compile_definitions(${binary} PUBLIC ${ARG_DEFINITIONS})
-  dd4hep_generate_rootmap(${binary})
+  IF(BUILD_SHARED_LIBS)
+    dd4hep_generate_rootmap(${binary})
+  ENDIF()
   if(NOT ${ARG_NOINSTALL})
     set(install_destination "lib")
     if(CMAKE_INSTALL_LIBDIR)
       set(install_destination ${CMAKE_INSTALL_LIBDIR})
     endif()
-    install(TARGETS ${binary} LIBRARY DESTINATION ${install_destination})
+    install(TARGETS ${binary}
+      ARCHIVE DESTINATION ${install_destination}
+      LIBRARY DESTINATION ${install_destination}
+      )
   endif()
 endfunction(dd4hep_add_plugin)
 
-- 
GitLab