From bcf0d5deafb5706a8652884c9e6e82d2b856a96e Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sun, 24 Nov 2019 14:08:47 +0800
Subject: [PATCH] more meson updates

---
 meson.build                          | 184 ++++++++++++++++++---------
 src/perl/scripts2/check_compilers.pl |  18 ++-
 src/setup/version.c                  |   2 +-
 3 files changed, 143 insertions(+), 61 deletions(-)

diff --git a/meson.build b/meson.build
index 1e86411df..786cc979c 100644
--- a/meson.build
+++ b/meson.build
@@ -34,21 +34,25 @@
 # define the binary_c project
 project(
     'binary_c','c',
-    version : meson.get_compiler('c').run('''
-#include <stdio.h>
-#include "''' + meson.source_root() + '''/src/binary_c_version.h"
-int main(int argc,char **argv)
-{
-        fprintf(stdout,"%s",BINARY_C_VERSION);
-        return 0;
-}
-''').stdout().strip(),
     default_options : [
         'c_std=gnu99',
-    ]
+    ],
+    version : '2.1.5',
 )
+
+############################################################
+# compiler object
+#
 compiler = meson.get_compiler('c')
 
+############################################################
+# bail if compiler is buggy/unsupported
+#
+if compiler.get_id() == 'gcc' and \
+   compiler.version() = '4.7.4'
+    error('gcc 4.7.4 is buggy and unsupported')
+endif
+
 ############################################################
 # System information
 cpufreq = run_command('meson/cpu_frequency.sh').stdout().strip()
@@ -62,9 +66,10 @@ homedir = run_command('sh','-c','echo $HOME').stdout().strip()
 binary_c = meson.source_root()
 binary_c_src = meson.source_root() + '/src'
 
-
 ############################################################
 # binary_c version
+# (should be the same as the project version)
+#
 binary_c_version = compiler.run('''
 #include <stdio.h>
 #include "''' + binary_c_src + '''/binary_c_version.h"
@@ -73,10 +78,16 @@ int main(int argc,char **argv)
         fprintf(stdout,"%s",BINARY_C_VERSION);
         return 0;
 }''').stdout().strip()  
-message('binary_c version is ' + binary_c_version)
+
+if binary_c_version != meson.project_version()
+    error('binary_c_version and meson.project_version are not the same : they should be!')
+else
+    message('binary_c version is ' + binary_c_version)
+endif
 
 ############################################################
 # SVN and git information
+#
 git_revision = run_command('sh','-c','meson/git_revision.sh').stdout().strip()
 svn_revision = run_command('sh','-c','meson/svn_revision.sh').stdout().strip()
 git_url = run_command('sh','-c','meson/git_url.sh').stdout().strip()
@@ -84,10 +95,20 @@ svn_url = run_command('sh','-c','meson/svn_url.sh').stdout().strip()
 
 ############################################################
 # default include and library search directories
-incdirs = [ '.', './src' ]
-libdirs = [ './src' ]
-absolute_incdirs = [ binary_c, binary_c_src ]
-absolute_libdirs = [ ]
+#
+incdirs = [
+    '.',
+    './src'
+]
+libdirs = [
+    './src'
+]
+absolute_incdirs = [
+    binary_c,
+    binary_c_src
+]
+absolute_libdirs = [
+]
 
 ############################################################
 # default C flags
@@ -95,6 +116,8 @@ absolute_libdirs = [ ]
 cflags = [
     '-DALIGNSIZE=16'
 ]
+executable_build_flags = [
+]
 
 ############################################################
 # cflags required for generic builds and other builds
@@ -165,7 +188,10 @@ optional_flags += [
 ############################################################
 # debug C flags
 if get_option('buildtype').startswith('debug')
-    optional_flags += [ '-rdynamic', '-O0' ]
+    optional_flags += [
+        '-rdynamic',
+        '-O0'
+    ]
 endif
 
 ############################################################
@@ -184,7 +210,17 @@ endforeach
 #
 if compiler.get_id() == 'clang'
     # clang-speific flags
-    cflags += [ '-fbracket-depth=512' ]
+    foreach _flag : [ '-fbracket-depth=512' ]
+        if compiler.has_argument(_flag)
+            cflags += _flag
+        else
+            error('clang requires ' + _flag + ' to build, but this flag is not available')
+        endif
+    endforeach
+
+    # for some reason we require an extra fPIC
+    # when building the executable *only*
+    executable_build_flags += '-fPIC'
 else
     # gcc-specific flags
     cflags += []
@@ -209,21 +245,25 @@ if os == 'linux'
         '-DPOSIX',
         '-DLARGEFILE_SOURCE'
     ]
+    unix = true
 elif os == 'windows'
     # windows
     cflags += [
         '-DWINDOWS',
     ]
+    unix = false
 elif os == 'darwin'
     # darwin (MacOSX)
     cflags += [
         '-DDARWIN',
         '-DPOSIX',
     ]
+    unix = true
 else
     cflags += [
         '-DUNKNOWN_OPERATING_SYSTEM'
     ]
+    unix = false
 endif
 
 ############################################################
@@ -259,7 +299,6 @@ if svn_url != ''
     cflags += _flag
 endif  
 
-
 ############################################################
 # dependencies are put in the list called dependencies
 #
@@ -271,12 +310,12 @@ dependencies = [ ]
 #
 # system libraries : note that libm may not be required
 #                    on some systems (GSL may also specify)
+#
 dependencies += [
     compiler.find_library('c', required: true),
     compiler.find_library('m', required: false),
 ]
 
-
 ############################################################
 # libraries : required and optional.
 #
@@ -293,7 +332,7 @@ _optional_libraries = [
     'bfd',
     'bsd',
     'iberty',
-    'm', # optional on some platforms (gsl probably required is)
+    'm', # optional on some platforms (but gsl requires it)
     'memoize',
     'rinterpolate',
 ]
@@ -302,9 +341,9 @@ foreach libname : _required_libraries
     _dep = compiler.find_library(libname,
                                  required:true) 
     if _dep.found()
-        cflags += '-D__HAVE_LIB' + libname.to_upper() +'__'
         libs += '-l' + libname
-
+        cflags += '-D__HAVE_LIB' + libname.to_upper() +'__'
+    
         # extras
         if libname == 'gsl'
             # use gsl-config to find cflags, libraries, etc.
@@ -341,7 +380,8 @@ endforeach
 #
 if get_option('generic') == false
     foreach libname : _optional_libraries
-        _dep = compiler.find_library(libname,required:false) 
+        _dep = compiler.find_library(libname,
+                                     required:false) 
         if _dep.found()
             cflags += '-D__HAVE_LIB' + libname.to_upper() +'__'
             libs += '-l' + libname
@@ -538,7 +578,8 @@ if get_option('usepch') == true
         _pch = 'binary_c.h.pch'
         use_pch_cflags = [
             '-include-pch',
-            _pch ]
+            _pch,
+        ]
         pch_cflags = [
             _opt,
             _pic,
@@ -576,9 +617,6 @@ if get_option('usepch') == true
     # make cflags for pch build
     pch_cflags_array = cflags + pch_cflags
 
-    # append usage flags
-    cflags += use_pch_cflags
-
     # make PCH compiler: this is just the normal compiler with PCH flags
     pch_compiler = compiler.cmd_array() + pch_cflags_array
 
@@ -586,7 +624,9 @@ if get_option('usepch') == true
     precompiled_headers = custom_target(
         'binary_c.h.pch',
         build_by_default : true,
-        input : ['src/binary_c.h'],
+        input : [
+            'src/binary_c.h'
+        ],
         output :  pch_sourcefiles,
         command : [
             pch_compiler,
@@ -597,6 +637,9 @@ if get_option('usepch') == true
         depend_files : [ h_sourcefiles ],
     )
 
+    # append usage flags for normal build
+    cflags += use_pch_cflags
+
 else
     # disable PCH by removing the files it would have generated
     
@@ -615,15 +658,25 @@ endif
 ############################################################
 # command to install binary_c in its 'legacy' locations
 # (required for binary_grid2)
-binary_c_legacy_install_cmd = [
-    'cp','--remove-destination','binary_c','../',
-    '&&',
-    'cp','--remove-destination','libbinary_c.so','../src/',
-    '&&',
-    'echo','libbinary_c.so built with symbols:',
-    '&&',
-    '../meson/list_shared_symbols.sh','libbinary_c.so'
-]
+#
+# NB will only work on Unix-compatibles
+#
+# TODO use meson's installation instead of my own
+if unix 
+    binary_c_legacy_install_cmd = [
+        'cp','--remove-destination','binary_c','../',
+        '&&',
+        'cp','--remove-destination','libbinary_c.so','../src/',
+        '&&',
+        'echo','libbinary_c.so built with symbols:',
+        '&&',
+        '../meson/list_shared_symbols.sh','libbinary_c.so'
+    ]
+else
+    binary_c_legacy_install_cmd = [
+        'echo','"Legacy install not supported on this operating system"'
+        ]
+endif
 
 ############################################################
 #
@@ -631,7 +684,9 @@ binary_c_legacy_install_cmd = [
 # hence touch (and rebuild) it when the shared library is
 # rebuild
 #
-if run_command('sh','-c','meson/check_binary_grid2.sh').returncode() == 0
+
+if os == 'linux' and \
+   run_command('sh','-c','meson/check_binary_grid2.sh').returncode() == 0
     binary_grid2_file = run_command(
         'sh',
         '-c',
@@ -651,26 +706,31 @@ endif
 # symlink shared_library to binary_c/src directory
 # run: ninja libbinary_c_symlink
 #
-libbinary_c_symlink = custom_target(
-    'libbinary_c_symlink',
-    build_by_default: false,
-    output: [ 'libbinary_c_symlink' ],
-    command: [
-        'sh',
-        '-c',
-        '../meson/symlink_libbinary_c.sh'
-    ],
-)
+# NB only works if we can symlink, i.e. unix systems
+#
+if unix
+    libbinary_c_symlink = custom_target(
+        'libbinary_c_symlink',
+        build_by_default: false,
+        output: [ 'libbinary_c_symlink' ],
+        command: [
+            'sh',
+            '-c',
+            '../meson/symlink_libbinary_c.sh'
+        ],
+    )
+endif
 
 
 ############################################################
 #
 # make binary_c objects
 #
-# We put the objects in here so they can be shared by
-# the executable and library builds. We're not really
-# interested in the static library, but we have to call
-# this build something.
+# We put the objects in this build target so they can be 
+# shared by the executable and library builds.
+#
+# We're not really interested in the static library that
+# results, but it gets the job done.
 #
 binary_c_objects = build_target(
     'binary_c_objects',
@@ -678,12 +738,12 @@ binary_c_objects = build_target(
     pic: true,
     target_type : 'static_library', # pretend we're a static library!
     sources : [
+        precompiled_headers,
         c_sourcefiles,
         h_sourcefiles,
-        precompiled_headers
     ],
     include_directories: include_directories(incdirs),
-    dependencies : dependencies,
+    dependencies : [ dependencies ],
     c_args : [
         cflags,
         quoted_cflags_list,
@@ -704,7 +764,9 @@ binary_c_shared_library = shared_library(
     build_by_default : false,    
     install: true,
     include_directories: include_directories(incdirs),
-    dependencies : [ dependencies ],
+    dependencies : [
+        dependencies
+    ],
     c_args : [
         cflags,
         quoted_cflags_list,
@@ -729,19 +791,25 @@ binary_c_executable = executable(
     build_by_default : true,    
     install: true,
     sources : [
+        precompiled_headers,
         c_main_sourcefiles,
     ],
     include_directories: include_directories(incdirs),
-    dependencies : [ dependencies ],
+    dependencies : [
+        dependencies
+    ],
     c_args : [
         cflags,
         quoted_cflags_list,
+        executable_build_flags,
     ],
     objects : data_objects,
     link_args : [
         libs,
     ],
-    link_with: [ binary_c_objects ],    
+    link_with: [
+        binary_c_objects
+    ],    
 )
 
 ############################################################
diff --git a/src/perl/scripts2/check_compilers.pl b/src/perl/scripts2/check_compilers.pl
index e7b02c338..76b52aaf1 100755
--- a/src/perl/scripts2/check_compilers.pl
+++ b/src/perl/scripts2/check_compilers.pl
@@ -18,6 +18,15 @@ my $compiler_regexp = join('|',@compilers);
 my $meson_version = (`meson --version`=~/^(\d+\.\d+\.\d+)/)[0]; # set to 1 to use meson
 my $meson = defined $meson_version ? 1 : 0;
 my $meson_builddir = "builddir_check_compilers";
+# exclude these buggy compilers
+my $exclude_compilers =
+{
+    'gcc' => {
+        '4.7.4' => 'Crashes with internal errors on build'
+        },
+        'clang' => {
+        },
+};
 
 printx "Check binary_c compilers (vb = $vb), logging to $logfile, building with ",($meson ? "meson $meson_version" : 'make')."\n";
 
@@ -59,7 +68,12 @@ sub find_executables
     foreach my $executable (@executables)
     {
         my $v = version_of($executable);
-        if(!defined $v)
+        
+        if($exclude_compilers->{$compiler}->{$v})
+        {
+            printx "Skipping $compiler $v because it is excluded ($exclude_compilers->{$compiler}->{$v})\n";
+        }
+        elsif(!defined $v)
         {
             printx "Warning: $executable gave no version\n";
         }
@@ -68,7 +82,7 @@ sub find_executables
             $versions{$v} = $executable;
         }
     }
-
+    
     # order by version
     @executables = sort {versioncmp($a,$b)} values %versions;
     
diff --git a/src/setup/version.c b/src/setup/version.c
index f749b32ac..d291c6e36 100644
--- a/src/setup/version.c
+++ b/src/setup/version.c
@@ -41,7 +41,7 @@ void version(struct stardata_t * RESTRICT const stardata)
 #endif//BINARY_C_PRE_VERSION
     
     /* SVN : deprecated from early 2018 but will do no harm if left in */
-#if (SVN_REVISION+0)
+#if defined SVN_REVISION && (SVN_REVISION+0)
     Printf("SVN revision %d\n",SVN_REVISION+0);
 #else
     Printf("SVN revision unknown\n");
-- 
GitLab