From 8459bafac09a59cc3ee3a4dec016c43cad1df19f Mon Sep 17 00:00:00 2001
From: Cristian C Lalescu <Cristian.Lalescu@ds.mpg.de>
Date: Wed, 9 Oct 2019 15:53:09 +0200
Subject: [PATCH] adds cmake options for NDEBUG and timing

cmake config can no toggle the use of "NDEBUG" or "USE_TIMINGOUTPUT"
compiler flags, hence source files and/or .bashrc no longer need to be changed
themselves.
I'm not sure the mechanism survives very nicely for executable
compilation, but it's better to have it and figure that out later.
---
 CMakeLists.txt                                | 10 ++++++++++
 TurTLE/_code.py                               |  6 ++++++
 cmake/TurTLEConfig.cmake.in                   |  2 ++
 cpp/fftw_tools.cpp                            |  2 --
 cpp/field.cpp                                 |  2 --
 cpp/field_layout.cpp                          |  2 --
 cpp/full_code/NSVE.cpp                        |  2 --
 cpp/full_code/NSVEcomplex_particles.cpp       |  2 --
 cpp/full_code/NSVEparticles.cpp               |  2 --
 cpp/full_code/code_base.cpp                   |  2 --
 cpp/full_code/direct_numerical_simulation.cpp |  2 --
 cpp/kspace.cpp                                |  2 --
 cpp/vorticity_equation.cpp                    |  2 --
 13 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ae33cd44..3220831c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -57,6 +57,16 @@ project(TurTLE
 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/morse ${CMAKE_MODULE_PATH})
 set(TURTLE_LIBS "")
 
+option(NDEBUG "Define NDEBUG macro" ON)
+if(NDEBUG)
+    add_definitions(-DNDEBUG)
+endif()
+
+option(TIMING_OUTPUT "Toggle timing output. WARNING: memory usage is proportional to `niter_todo`" OFF)
+if(TIMING_OUTPUT)
+    add_definitions(-DUSE_TIMINGOUTPUT)
+endif()
+
 #####################################################################################
 ## MPI
 
diff --git a/TurTLE/_code.py b/TurTLE/_code.py
index 2835ff91..359f9586 100644
--- a/TurTLE/_code.py
+++ b/TurTLE/_code.py
@@ -224,6 +224,12 @@ class _code(_base):
             outfile.write('set(CMAKE_CXX_COMPILE_FLAGS "${CMAKE_CXX_COMPILE_FLAGS} ${TURTLE_CXX_COMPILE_FLAGS}")\n')
             outfile.write('set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILE_FLAGS}")\n')
             outfile.write('set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TURTLE_EXE_LINKER_FLAGS}")\n')
+            outfile.write('if(NDEBUG)\n')
+            outfile.write('    add_definitions(-DNDEBUG)\n')
+            outfile.write('endif()\n')
+            outfile.write('if(TIMING_OUTPUT)\n')
+            outfile.write('    add_definitions(-DUSE_TIMINGOUTPUT)\n')
+            outfile.write('endif()\n')
             outfile.write('include_directories(${TURTLE_INCLUDE_DIRECTORIES} ${TURTLE_INCLUDE_DIR}/TurTLE)\n')
             outfile.write('link_directories(${TURTLE_LINK_DIRECTORIES} ${TURTLE_LIBRARIES_DIR})\n')
             outfile.write('find_library(TURTLE_STATIC_LIBRARY TurTLE)\n')
diff --git a/cmake/TurTLEConfig.cmake.in b/cmake/TurTLEConfig.cmake.in
index f91c66e5..1b4a7057 100644
--- a/cmake/TurTLEConfig.cmake.in
+++ b/cmake/TurTLEConfig.cmake.in
@@ -50,6 +50,8 @@ SET(TURTLE_C_COMPILER  "@CMAKE_C_COMPILER@")
 SET(TURTLE_EXE_LINKER_FLAGS  "@CMAKE_EXE_LINKER_FLAGS@")
 SET(TURTLE_LIBS  "@TURTLE_LIBS@")
 set(TURTLE_DEFINITIONS @COMPILE_DEFINITIONS@)
+set(NDEBUG "@NDEBUG@")
+set(TIMING_OUTPUT "@TIMING_OUTPUT@")
 
 #
 SET(TURTLE_SOURCE_DIR	   "@TURTLE_SOURCE_DIR@")
diff --git a/cpp/fftw_tools.cpp b/cpp/fftw_tools.cpp
index 55794b41..e28d86c0 100644
--- a/cpp/fftw_tools.cpp
+++ b/cpp/fftw_tools.cpp
@@ -29,8 +29,6 @@
 #include "fftw_tools.hpp"
 #include "fftw_interface.hpp"
 
-#define NDEBUG
-
 std::map<std::string, unsigned> fftw_planner_string_to_flag = {
     {"FFTW_ESTIMATE", FFTW_ESTIMATE},
     {"FFTW_MEASURE", FFTW_MEASURE},
diff --git a/cpp/field.cpp b/cpp/field.cpp
index c26b1c9e..657cc0cb 100644
--- a/cpp/field.cpp
+++ b/cpp/field.cpp
@@ -33,8 +33,6 @@
 #include "scope_timer.hpp"
 #include "shared_array.hpp"
 
-#define NDEBUG
-
 template <typename rnumber,
           field_backend be,
           field_components fc>
diff --git a/cpp/field_layout.cpp b/cpp/field_layout.cpp
index 61dd3f2a..e2872a64 100644
--- a/cpp/field_layout.cpp
+++ b/cpp/field_layout.cpp
@@ -24,8 +24,6 @@
 
 
 
-#define NDEBUG
-
 #include <cassert>
 #include "field_layout.hpp"
 #include "scope_timer.hpp"
diff --git a/cpp/full_code/NSVE.cpp b/cpp/full_code/NSVE.cpp
index 7b1b2d95..21788cc9 100644
--- a/cpp/full_code/NSVE.cpp
+++ b/cpp/full_code/NSVE.cpp
@@ -23,8 +23,6 @@
 
 
 
-#define NDEBUG
-
 #include <string>
 #include <cmath>
 #include "NSVE.hpp"
diff --git a/cpp/full_code/NSVEcomplex_particles.cpp b/cpp/full_code/NSVEcomplex_particles.cpp
index 701892f9..81408d2b 100644
--- a/cpp/full_code/NSVEcomplex_particles.cpp
+++ b/cpp/full_code/NSVEcomplex_particles.cpp
@@ -32,8 +32,6 @@
 #include "particles/p2p_computer.hpp"
 #include "particles/particles_inner_computer.hpp"
 
-#define NDEBUG
-
 template <typename rnumber>
 int NSVEcomplex_particles<rnumber>::initialize(void)
 {
diff --git a/cpp/full_code/NSVEparticles.cpp b/cpp/full_code/NSVEparticles.cpp
index 1952bcfc..81e94677 100644
--- a/cpp/full_code/NSVEparticles.cpp
+++ b/cpp/full_code/NSVEparticles.cpp
@@ -29,8 +29,6 @@
 #include "NSVEparticles.hpp"
 #include "scope_timer.hpp"
 
-#define NDEBUG
-
 template <typename rnumber>
 int NSVEparticles<rnumber>::initialize(void)
 {
diff --git a/cpp/full_code/code_base.cpp b/cpp/full_code/code_base.cpp
index f412a792..e9d062f9 100644
--- a/cpp/full_code/code_base.cpp
+++ b/cpp/full_code/code_base.cpp
@@ -27,8 +27,6 @@
 #include "code_base.hpp"
 #include "scope_timer.hpp"
 
-#define NDEBUG
-
 code_base::code_base(
         const MPI_Comm COMMUNICATOR,
         const std::string &simulation_name):
diff --git a/cpp/full_code/direct_numerical_simulation.cpp b/cpp/full_code/direct_numerical_simulation.cpp
index 955c1f2e..15312236 100644
--- a/cpp/full_code/direct_numerical_simulation.cpp
+++ b/cpp/full_code/direct_numerical_simulation.cpp
@@ -30,8 +30,6 @@
 #include "scope_timer.hpp"
 #include "hdf5_tools.hpp"
 
-#define NDEBUG
-
 int direct_numerical_simulation::grow_file_datasets()
 {
     TIMEZONE("direct_numerical_simulation::grow_file_datasets");
diff --git a/cpp/kspace.cpp b/cpp/kspace.cpp
index 452ca305..86d692a2 100644
--- a/cpp/kspace.cpp
+++ b/cpp/kspace.cpp
@@ -32,8 +32,6 @@
 #include "scope_timer.hpp"
 #include "shared_array.hpp"
 
-#define NDEBUG
-
 template <field_backend be,
           kspace_dealias_type dt>
 template <field_components fc>
diff --git a/cpp/vorticity_equation.cpp b/cpp/vorticity_equation.cpp
index 38ca8f6d..58de8a5e 100644
--- a/cpp/vorticity_equation.cpp
+++ b/cpp/vorticity_equation.cpp
@@ -33,8 +33,6 @@
 #include "scope_timer.hpp"
 #include "shared_array.hpp"
 
-#define NDEBUG
-
 
 
 template <class rnumber,
-- 
GitLab