From fbe4ced435fff1804a1273b241a6126ffe8c1fdc Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Wed, 25 Aug 2021 16:23:00 +0200
Subject: [PATCH] Fix CMakeLists.txt to pick python executable from  rather
 than invoking directly python

---
 DDCAD/src/ASSIMPWriter.cpp | 116 ++++++++++++++++++-------------------
 cmake/DD4hepBuild.cmake    |  14 +++--
 2 files changed, 67 insertions(+), 63 deletions(-)

diff --git a/DDCAD/src/ASSIMPWriter.cpp b/DDCAD/src/ASSIMPWriter.cpp
index bdddac197..c4b8fd141 100644
--- a/DDCAD/src/ASSIMPWriter.cpp
+++ b/DDCAD/src/ASSIMPWriter.cpp
@@ -37,7 +37,7 @@ using namespace dd4hep::cad;
 namespace  {
 
   void _collect(std::vector<std::pair<PlacedVolume,TGeoHMatrix*> >& cont,
-		bool recursive, const TGeoHMatrix& to_global, PlacedVolume pv)
+                bool recursive, const TGeoHMatrix& to_global, PlacedVolume pv)
   {
     Volume v = pv.volume();
     for(Int_t i=0; i<v->GetNdaughters(); ++i)  {
@@ -49,11 +49,11 @@ namespace  {
       mother->Multiply(p->GetMatrix());
 
       if ( sol->IsA() != TGeoShapeAssembly::Class() )
-	cont.push_back(make_pair(p, mother.get()));
+        cont.push_back(make_pair(p, mother.get()));
       if ( recursive )
-	_collect(cont, recursive, *mother, p);	
+        _collect(cont, recursive, *mother, p);	
       if ( sol->IsA() != TGeoShapeAssembly::Class() )
-	mother.release();
+        mother.release();
     }
   }
   struct vertex{ double x,y,z;
@@ -95,32 +95,32 @@ namespace  {
       pol = (pol_t*)q;
       q += (2+pol->n);
       for( int j=0; j < pol->n; j += 2 )   {
-	/* ------------------------------------------------------------
-	//   Convert quadri-linear facet to 2 tri-linear facets
-	//
-	//    f1 +---------------+ v2/v3: f0
-	//      /                / 
-	//     /                /
-	//    /                /
-	//    +---------------+
-	//  v0             v1 v2/v3
-	// --------------------------------------------------------- */
-	const int    s1  = pol->segs[j], s2 = pol->segs[(j+1)%pol->n];
-	const int    s[] = { segs[s1]._1, segs[s1]._2, segs[s2]._1, segs[s2]._2 };
-	const vtx_t& v0  = vtcs[s[0]], &v1=vtcs[s[1]], &v2=vtcs[s[2]], &v3=vtcs[s[3]];
+        /* ------------------------------------------------------------
+        //   Convert quadri-linear facet to 2 tri-linear facets
+        //
+        //    f1 +---------------+ v2/v3: f0
+        //      /                / 
+        //     /                /
+        //    /                /
+        //    +---------------+
+        //  v0             v1 v2/v3
+        // --------------------------------------------------------- */
+        const int    s1  = pol->segs[j], s2 = pol->segs[(j+1)%pol->n];
+        const int    s[] = { segs[s1]._1, segs[s1]._2, segs[s2]._1, segs[s2]._2 };
+        const vtx_t& v0  = vtcs[s[0]], &v1=vtcs[s[1]], &v2=vtcs[s[2]], &v3=vtcs[s[3]];
 
-	if ( s[0] == s[2] )   {       // Points are ( s[1], s[0], s[3] )
-	  tes->AddFacet(v1, v0, v3);
-	}
-	else if ( s[0] == s[3] )   {  // Points are ( s[1], s[0], s[2] )
-	  tes->AddFacet(v1, v0, v2);
-	}
-	else if ( s[1] == s[2] )   {  // Points are ( s[0], s[1], s[3] )
-	  tes->AddFacet(v0, v1, v3);
-	}
-	else   {                      // Points are ( s[0], s[1], s[2] )
-	  tes->AddFacet(v0, v1, v2);
-	}
+        if ( s[0] == s[2] )   {       // Points are ( s[1], s[0], s[3] )
+          tes->AddFacet(v1, v0, v3);
+        }
+        else if ( s[0] == s[3] )   {  // Points are ( s[1], s[0], s[2] )
+          tes->AddFacet(v1, v0, v2);
+        }
+        else if ( s[1] == s[2] )   {  // Points are ( s[0], s[1], s[3] )
+          tes->AddFacet(v0, v1, v3);
+        }
+        else   {                      // Points are ( s[0], s[1], s[2] )
+          tes->AddFacet(v0, v1, v2);
+        }
       }
     }
     return tes;
@@ -135,9 +135,9 @@ namespace  {
     virtual ~TessellateComposite() = default;
     void collect_mesh(TGeoShape* s)  {
       if (TGeoCompositeShape *shape = dynamic_cast<TGeoCompositeShape *>(s))
-	collect_composite(shape);
+        collect_composite(shape);
       else
-	meshes.push_back(move(_tessellate_primitive(s->GetName(),s)));
+        meshes.push_back(move(_tessellate_primitive(s->GetName(),s)));
     }
     void collect_composite(TGeoCompositeShape* sh)   {
       TGeoBoolNode* node  = sh->GetBoolNode();
@@ -166,10 +166,10 @@ namespace  {
 
 /// Write output file
 int ASSIMPWriter::write(const std::string& file_name,
-			const std::string& file_type,
-			const VolumePlacements& places,
-			bool   recursive,
-			double unit_scale)  const
+                        const std::string& file_type,
+                        const VolumePlacements& places,
+                        bool   recursive,
+                        double unit_scale)  const
 {
   std::vector<std::pair<PlacedVolume,TGeoHMatrix*> > placements;
   vector<Material>        materials;
@@ -206,11 +206,11 @@ int ASSIMPWriter::write(const std::string& file_name,
     unique_ptr<TGeoTessellated> buf;
     if ( !tes.isValid() )   {
       if ( auto* shape=dynamic_cast<TGeoCompositeShape*>(s.ptr()) )   {
-	TessellateComposite helper;
-	buf = helper.build_composite(v.name(), shape);
+        TessellateComposite helper;
+        buf = helper.build_composite(v.name(), shape);
       }
       else   {
-	buf = _tessellate_primitive(v.name(), s);
+        buf = _tessellate_primitive(v.name(), s);
       }
       tes = buf.get();
     }
@@ -225,8 +225,8 @@ int ASSIMPWriter::write(const std::string& file_name,
     size_t index = std::numeric_limits<size_t>::max();
     for( size_t j=0; j<materials.size(); ++j )  {
       if( materials[j] == m )   {
-	index = j;
-	break;
+        index = j;
+        break;
       }
     }
     if ( index > materials.size() )   {
@@ -265,23 +265,23 @@ int ASSIMPWriter::write(const std::string& file_name,
       const auto& facet = tes->GetFacet(j);
       tmp = facet.ComputeNormal(degenerated);
       if ( !degenerated && facet.GetNvert() > 0 )   {
-	aiFace& face  = mesh->mFaces[mesh->mNumFaces];
-	double  u     = unit_scale;
+        aiFace& face  = mesh->mFaces[mesh->mNumFaces];
+        double  u     = unit_scale;
 
-	face.mIndices = new unsigned int[facet.GetNvert()];
-	trafo->LocalToMaster(&tmp.x, &norm.x);
-	face.mNumIndices = 0;
-	for( long k=0; k < facet.GetNvert(); ++k )  {
-	  tmp = facet.GetVertex(k);
-	  trafo->LocalToMaster(&tmp.x, &vtx.x);
-	  face.mIndices[face.mNumIndices] = nvx;
-	  mesh->mNormals[nvx]  = aiVector3D(norm.x, norm.y, norm.z);
-	  mesh->mVertices[nvx] = aiVector3D(vtx.x*u,vtx.y*u,vtx.z*u);
-	  ++mesh->mNumVertices;
-	  ++face.mNumIndices;
-	  ++nvx;
-	}
-	++mesh->mNumFaces;
+        face.mIndices = new unsigned int[facet.GetNvert()];
+        trafo->LocalToMaster(&tmp.x, &norm.x);
+        face.mNumIndices = 0;
+        for( long k=0; k < facet.GetNvert(); ++k )  {
+          tmp = facet.GetVertex(k);
+          trafo->LocalToMaster(&tmp.x, &vtx.x);
+          face.mIndices[face.mNumIndices] = nvx;
+          mesh->mNormals[nvx]  = aiVector3D(norm.x, norm.y, norm.z);
+          mesh->mVertices[nvx] = aiVector3D(vtx.x*u,vtx.y*u,vtx.z*u);
+          ++mesh->mNumVertices;
+          ++face.mNumIndices;
+          ++nvx;
+        }
+        ++mesh->mNumFaces;
       }
     }
     if ( imesh == 122585 )
@@ -314,12 +314,12 @@ int ASSIMPWriter::write(const std::string& file_name,
     ++scene.mNumMeshes;
   }
   printout(ALWAYS,"ASSIMPWriter","+++ Analysed %ld of %ld meshes.",
-	   scene.mNumMeshes, placements.size());
+           scene.mNumMeshes, placements.size());
   if ( scene.mNumMeshes > 0 )   {
     Assimp::Exporter exporter;
     Assimp::ExportProperties *props = new Assimp::ExportProperties;
     props->SetPropertyBool(AI_CONFIG_EXPORT_POINT_CLOUDS,
-			   flags&EXPORT_POINT_CLOUDS ? true : false);
+                           flags&EXPORT_POINT_CLOUDS ? true : false);
     exporter.Export(&scene, file_type.c_str(), file_name.c_str(), 0, props);
     return 1;
   }
diff --git a/cmake/DD4hepBuild.cmake b/cmake/DD4hepBuild.cmake
index b09d0b52f..5ba040ccd 100644
--- a/cmake/DD4hepBuild.cmake
+++ b/cmake/DD4hepBuild.cmake
@@ -25,11 +25,11 @@ macro(dd4hep_to_parent_scope val)
 endmacro(dd4hep_to_parent_scope)
 #---------------------------------------------------------------------------------------------------
 macro(dd4hep_use_python_executable)
-  find_package(Python)
-  if("${Python_EXECUTABLE}" STREQUAL "")
-     set (Python_EXECUTABLE python)
-  endif()
-  dd4hep_print("|++> Using python executable:  ${Python_EXECUTABLE}")
+  #find_package(Python)
+  #if("${Python_EXECUTABLE}" STREQUAL "")
+  #   set (Python_EXECUTABLE python)
+  #endif()
+  dd4hep_print("|++> Using python executable:  ${Python_EXECUTABLE} -- ${PYTHON_EXECUTABLE}")
 endmacro(dd4hep_use_python_executable)
 #---------------------------------------------------------------------------------------------------
 #  MACRO: dd4hep_set_compiler_flags
@@ -695,6 +695,10 @@ macro(DD4HEP_SETUP_ROOT_TARGETS)
   ELSE()
     FIND_PACKAGE(Python COMPONENTS Development)
   ENDIF()
+  IF("${Python_EXECUTABLE}" STREQUAL "")
+     set (Python_EXECUTABLE python${Python_VERSION_MAJOR})
+  ENDIF()
+  dd4hep_print("|++> Using python executable:  ${Python_EXECUTABLE} -- ${PYTHON_EXECUTABLE}")
 
   SET(DD4HEP_PYTHON_INSTALL_DIR lib/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages)
 
-- 
GitLab