From: Neil Williams <codehelp@debian.org>
Date: Fri, 21 Dec 2012 16:10:19 +0200
Subject: Working around git-archive

Upstream build system relies on running from the original git
clone.
---
 SConstruct                          |  3 ++-
 site_scons/libobjcrystbuildutils.py | 30 ++++++++++++++++++++++++++++++
 src/SConscript                      | 17 +++++++++++------
 src/SConscript.cctbx                | 32 ++++++++++++++++----------------
 src/SConscript.sdist                |  2 +-
 5 files changed, 60 insertions(+), 24 deletions(-)

diff --git a/SConstruct b/SConstruct
index fa6447a..66a0399 100644
--- a/SConstruct
+++ b/SConstruct
@@ -70,6 +70,7 @@ else:
         'prefix',
         'installation prefix directory',
         '/usr/local'))
+    vars.Update(env)
 
 if platform.system().lower() == "windows":
     # Installation paths
@@ -146,7 +147,7 @@ env.PrependUnique(LIBPATH=getsyspaths('LIBRARY_PATH'))
 # so we can use conda-installed libraries
 env.AppendUnique(CPPDEFINES='BOOST_ALL_NO_LIB')
 
-builddir = env.Dir('build/%s-%s' % (env['build'], platform.machine()))
+builddir = env.Dir('debian/objcryst/usr')
 
 Export('env')
 
diff --git a/site_scons/libobjcrystbuildutils.py b/site_scons/libobjcrystbuildutils.py
index 5d6b363..b32fe46 100644
--- a/site_scons/libobjcrystbuildutils.py
+++ b/site_scons/libobjcrystbuildutils.py
@@ -51,6 +51,35 @@ def gitinfo():
 _cached_gitinfo = None
 
 
+def debinfo():
+    'Extract dictionary of version data from debian changelog.'
+    from subprocess import check_output
+    from os.path import exists, join
+    global _cached_debinfo
+    if _cached_debinfo is None:
+        path = os.getcwd()
+        while not exists(join(path, 'debian')):
+            path = join(path, '..')
+        deb_version = check_output(('dpkg-parsechangelog', '-Sversion'),
+                                   cwd=path, encoding='ascii')
+        deb_version = deb_version.split('+', 1)[0]
+        parts = [int(part) for part in deb_version.split('.')]
+        _cached_debinfo = {
+            "major": parts[0],
+            "minor": parts[1],
+            "micro": parts[2],
+            "patch": 0,
+            "date": "",
+            "commit": "",
+            "version": deb_version,
+            "patchnumber": 0,
+            "prerelease": 0,
+        }
+    return _cached_debinfo
+
+_cached_debinfo = None
+
+
 def getversion():
     """Extract version from gitinfo and/or gitarchive.cfg file.
 
@@ -58,6 +87,7 @@ def getversion():
     in gitarchive.cfg.  Use expanded data from gitarchive.cfg when
     these sources are from git archive bundle.
     """
+    return debinfo()
     from fallback_version import FALLBACK_VERSION
     gitarchivecfgfile = os.path.join(MYDIR, 'gitarchive.cfg')
     assert os.path.isfile(gitarchivecfgfile)
diff --git a/src/SConscript b/src/SConscript
index 60c7a83..3835879 100644
--- a/src/SConscript
+++ b/src/SConscript
@@ -45,7 +45,8 @@ else:
 
     # Configure build variants
     if env['build'] == 'debug':
-        env.Append(CCFLAGS='-g')
+        env.Append(CCFLAGS=f"-Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -ffile-prefix-map={os.getcwd()}=. -fstack-protector-strong -Wformat -Werror=format-security -I/usr/include/cctbx/")
+        env.Append(LINKFLAGS="-Wl,-z,relro -lnewmat")
     elif env['build'] == 'fast':
         env.AppendUnique(CCFLAGS=['-O3'] + fast_optimflags)
         env.AppendUnique(CPPDEFINES={'NDEBUG' : None})
@@ -56,7 +57,7 @@ else:
         env.AppendUnique(LINKFLAGS='-pg')
 
 # Lists for storing built objects and header files
-env['newmatobjs'] = []
+# env['newmatobjs'] = []
 env['cctbxobjs'] = []
 env['objcrystobjs'] = []
 env['lib_includes'] = []
@@ -64,7 +65,7 @@ env['lib_includes'] = []
 # Subsidiary SConscripts -----------------------------------------------------
 
 # These will create the built objects and header file lists.
-SConscript(["SConscript.cctbx", "SConscript.newmat", "SConscript.objcryst"])
+SConscript(["SConscript.cctbx", "SConscript.objcryst"])
 
 # Define sdist target for creating source distribution bundle
 # Do so only if required to avoid extra git executions.
@@ -75,18 +76,22 @@ if 'sdist' in COMMAND_LINE_TARGETS:
 # Top Level Targets ----------------------------------------------------------
 
 # This retrieves the intermediate objects
-newmatobjs = env["newmatobjs"]
+# newmatobjs = env["newmatobjs"]
 cctbxobjs = env["cctbxobjs"]
 objcrystobjs = env["objcrystobjs"]
 
 # This builds the library
+
+# Add SONAME_VERSION support
+version = "0.0.1"
+
 if platform.system().lower() == "windows":
     # Use a static library (does not require __declspec(dllexport) all over the code)
     libobjcryst = env.StaticLibrary("libObjCryst",
-                                    objcrystobjs + cctbxobjs + newmatobjs)
+                                    objcrystobjs + cctbxobjs, SHLIBVERSION=version)
 else:
     libobjcryst = env.SharedLibrary("libObjCryst",
-                                    objcrystobjs + cctbxobjs + newmatobjs)
+                                    objcrystobjs + cctbxobjs, SHLIBVERSION=version)
 
 lib = Alias('lib', [libobjcryst, env['lib_includes']])
 Default(lib)
diff --git a/src/SConscript.cctbx b/src/SConscript.cctbx
index 76f1c90..a7cb6f3 100644
--- a/src/SConscript.cctbx
+++ b/src/SConscript.cctbx
@@ -3,25 +3,25 @@ Import('env')
 # Determine if we should use shared installation of cctbx
 if 'with_shared_cctbx' in env:
     has_shared_cctbx = env['with_shared_cctbx']
-else:
-    conf = Configure(env)
-    has_shared_cctbx = conf.CheckLibWithHeader('cctbx', '''
-        cctbx/sgtbx/space_group.h
-        cctbx/miller/sym_equiv.h
-        cctbx/miller/index_generator.h
-        cctbx/eltbx/wavelengths.h
-        cctbx/eltbx/xray_scattering.h
-        cctbx/eltbx/tiny_pse.h
-        cctbx/eltbx/icsd_radii.h
-        cctbx/eltbx/henke.h
-        cctbx/eltbx/neutron.h
-        cctbx/sgtbx/brick.h
-        '''.split(), language='C++', autoadd=False)
-    env = conf.Finish()
+#else:
+#    conf = Configure(env)
+#    has_shared_cctbx = conf.CheckLibWithHeader('cctbx', '''
+#        cctbx/sgtbx/space_group.h
+#        cctbx/miller/sym_equiv.h
+#        cctbx/miller/index_generator.h
+#        cctbx/eltbx/wavelengths.h
+#        cctbx/eltbx/xray_scattering.h
+#        cctbx/eltbx/tiny_pse.h
+#        cctbx/eltbx/icsd_radii.h
+#        cctbx/eltbx/henke.h
+#        cctbx/eltbx/neutron.h
+#        cctbx/sgtbx/brick.h
+#        '''.split(), language='C++', autoadd=False)
+#    env = conf.Finish()
 
 # reuse the installed cctbx library if available
 if has_shared_cctbx:
-    env.AppendUnique(LIBS='cctbx')
+    env.AppendUnique(LIBS=['cctbx', 'newmat'])
 # otherwise link in the included cctbx sources
 else:
     # Define the what we need for the cctbx
diff --git a/src/SConscript.sdist b/src/SConscript.sdist
index 82d74c6..57f8de4 100644
--- a/src/SConscript.sdist
+++ b/src/SConscript.sdist
@@ -44,7 +44,7 @@ def generate_sdist_actions(target, source, env, for_signature):
     if not ginfo:
         return [die_without_git]
     actions = []
-    actions.append('echo "gitarchive.cfg -export-subst" > $SDIST_ATTRIBUTES')
+    # actions.append('echo "gitarchive.cfg -export-subst" > $SDIST_ATTRIBUTES')
     prefix = 'libobjcryst-%(version)s/' % ginfo
     gitcmd = ('git -c tar.umask=0022 archive '
               '--worktree-attributes '
