From ec9261527fed261cc0a17533d3b0264f90ce97f7 Mon Sep 17 00:00:00 2001
From: Chichi Lalescu <chichilalescu@gmail.com>
Date: Fri, 14 Sep 2018 07:13:53 +0200
Subject: [PATCH] decide to compile based on actual dependency list

---
 cpp_build.py |  9 +++++++++
 setup.py     | 25 ++++++++++++++++++-------
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/cpp_build.py b/cpp_build.py
index a312191a..39371214 100644
--- a/cpp_build.py
+++ b/cpp_build.py
@@ -65,6 +65,15 @@ src_file_list = ['hdf5_tools',
                  'Lagrange_polys',
                  'scope_timer']
 
+def get_file_dependency_list(src_file):
+    p = subprocess.Popen(
+            ['g++', '-Ibfps/cpp', '-MM', 'bfps/cpp/' + src_file + '.cpp'],
+            stdout = subprocess.PIPE)
+    out, err = p.communicate()
+    p.terminate()
+    deps = str(out, 'ASCII').replace('\\\n', '')
+    return deps
+
 def get_dependency_list():
     ofile = open('dependencies.txt', 'w')
     for src_file in src_file_list:
diff --git a/setup.py b/setup.py
index b427ebe7..110f18d5 100644
--- a/setup.py
+++ b/setup.py
@@ -215,13 +215,10 @@ class CompileLibCommand(distutils.cmd.Command):
         if not os.path.isfile('bfps/libbfps.a'):
             need_to_compile = True
         else:
+            need_to_compile = False
             ofile = 'bfps/libbfps.a'
             libtime = datetime.datetime.fromtimestamp(os.path.getctime(ofile))
             latest = libtime
-            for fname in header_list:
-                latest = max(latest,
-                             datetime.datetime.fromtimestamp(os.path.getctime('bfps/' + fname)))
-            need_to_compile = (latest > libtime)
         eca = extra_compile_args
         eca += ['-fPIC']
         if self.timing_output:
@@ -238,9 +235,14 @@ class CompileLibCommand(distutils.cmd.Command):
             if not os.path.exists(ofile):
                 need_to_compile_file = True
             else:
-                need_to_compile_file = (need_to_compile or
-                                        (datetime.datetime.fromtimestamp(os.path.getctime(ofile)) <
-                                         datetime.datetime.fromtimestamp(os.path.getctime(ifile))))
+                need_to_compile_file = False
+                if not need_to_compile:
+                    latest = libtime
+                    dependency_list = get_file_dependency_list(fname)
+                    for depname in dependency_list.split()[1:]:
+                        latest = max(latest,
+                                     datetime.datetime.fromtimestamp(os.path.getctime(depname)))
+                need_to_compile_file = (latest > libtime)
             if need_to_compile_file:
                 command_strings = [compiler, '-c']
                 command_strings += ['bfps/cpp/' + fname + '.cpp']
@@ -269,6 +271,15 @@ class CompileLibCommand(distutils.cmd.Command):
                 protocol = 2)
         return None
 
+def get_file_dependency_list(src_file):
+    p = subprocess.Popen(
+            ['g++', '-Ibfps/cpp', '-MM', 'bfps/cpp/' + src_file + '.cpp'],
+            stdout = subprocess.PIPE)
+    out, err = p.communicate()
+    p.terminate()
+    deps = str(out, 'ASCII').replace('\\\n', '')
+    return deps
+
 from setuptools import setup
 
 setup(
-- 
GitLab