Search code examples
sconscompiler-flags

Scons checking for compiler option


I want to check in Scons that my compiler support some option (for example -fcilkplus). The only way I manage to do it is the following sequence of operations:

env.Prepend(CXXFLAGS = ['-fcilkplus'], LIBS = ['cilkrts'])

Then I launch my custom checker:

def CheckCilkPlusCompiler(context):
    test = """
    #include <cilk/cilk.h>
    #include <assert.h>
    int fib(int n) {
      if (n < 2) return n;
      int a = cilk_spawn fib(n-1);
      int b = fib(n-2);
      cilk_sync;
      return a + b;
    }
    int main() {
      int result = fib(30);
      assert(result == 832040);
      return 0;
    }
    """
    context.Message('Checking Cilk++ compiler ... ')
    result = context.TryRun(test, '.cpp')
    context.Result(result[0])
    return result[0]

Now if it fails, I have to remove the two options extra flags -fcilkplus cilkrts from the environment variables. Is there a better way to do that ?

The problem is that I can't manage to access to the env from the context and therefore I can't make a clone of it.


Solution

  • You can use check the availability of a library with SCons as follows:

    env = Environment()
    conf = Configure(env)
    if not conf.CheckLib('cilkrts'):
        print 'Did not find libcilkrts.a, exiting!'
        Exit(1)
    else:
        env.Prepend(CXXFLAGS = ['-fcilkplus'], LIBS = ['cilkrts'])
    
    env = conf.Finish()
    

    You could also check the availability of a header as follows:

    env = Environment()
    conf = Configure(env)
    if conf.CheckCHeader('cilk/cilk.h'):
        env.Prepend(CXXFLAGS = ['-fcilkplus'], LIBS = ['cilkrts'])
    env = conf.Finish()
    

    Update:

    I just realized, you can access the environment on the Configure object as follows:

    conf.env.Append(...)