[Scons-users] Pass multiple build flags on the command line

Bertrand Marc beberking at gmail.com
Sat Aug 9 10:00:47 EDT 2014


Dear Scons users,

I am trying to build pingus with custom build flags. I'd like to pass
two flags to g++ (-g -O2), so I launch the following:
scons CXXFLAGS='-g -O2'

Unfortunately it fails because the Sconstruct file appends
["-std=c++0x"] after CXXFLAGS and env.Append doesn't mix well lists and
strings. I get the following error:
g++ -o .sconf_temp/conftest_1.o -c "-g O2" -std=c++0x
-isystem/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT
.sconf_temp/conftest_1.cpp
cc1plus: error: unrecognised debug output level " O2"
scons: Configure: failed

You can see the issue: my build flags are between "".

Of course I can change the Sconstruct file to make CXXFLAGS a string
instead of a list, but is it the right solution ? Are there any downside
to using strings instead of lists for buildflags ? Is there a way to
pass a list of flags to the command line ?

You'll find a minimal Sconstruct file based on the pingus' one. You can
test it with the -c target easily.

Thanks for your answer,
Bertrand
-------------- next part --------------
##  -*- python -*-
##  Pingus - A free Lemmings clone
##  Copyright (C) 1999 Ingo Ruhnke <grumbel at gmx.de>,
##                     Francois Beerten
##
##  This program is free software; you can redistribute it and/or
##  modify it under the terms of the GNU General Public License
##  as published by the Free Software Foundation; either version 2
##  of the License, or (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

import sys, os
import SCons.Util

def CheckSDLLib(context, sdllib):
    """
    On some platforms, SDL does this ugly redefine-main thing, that can
    interact badly with CheckLibWithHeader.
    """
    lib = "SDL_%s" % sdllib
    context.Message('Checking for %s...' % lib)
    text = """
#include "SDL.h"
#include "%s.h"
int main(int argc, char* argv[]) { return 0; }
""" % lib
    context.AppendLIBS(lib)
    if context.BuildProg(text, ".cpp"):
        context.Result("failed")
        return False
    else:
        context.Result("ok")
        return True

def CheckMyProgram(context, prgn):
    context.Message('Checking for %s...' % prgn)
    for i in context.env['ENV']['PATH'].split(":"):
        filename = os.path.join(i, prgn)
        if os.path.exists(filename):
            context.Result(filename)
            return True
    context.Result("failed")
    return False

class Project:
    def configure(self):
        self.configure_begin()
        self.configure_sdl()
        self.configure_end()

    def configure_begin(self):
        self.opts = Variables("custom.py", ARGUMENTS)
        self.opts.Add('BUILD', 'Set the build type', "default")
        self.opts.Add('CC', 'C Compiler', 'gcc')
        self.opts.Add('CXX', 'C++ Compiler', 'g++')
        self.opts.Add('CXXFLAGS',   'C++ Compiler flags', ["-O2", "-s"])

        self.env = Environment(options = self.opts, ENV=os.environ)
        self.env.Append(CXXFLAGS = ["-std=c++0x"])
        
        Help(self.opts.GenerateHelpText(self.env))
        
        
                

        self.conf = self.env.Configure(custom_tests = {
            'CheckMyProgram' : CheckMyProgram,
            'CheckSDLLib': CheckSDLLib,
            })
        self.fatal_error = ""
        self.reports = ""

    def configure_end(self):
        self.env = self.conf.Finish()

        print "Reports:"
        print self.reports

        if not self.fatal_error == "":
            print "Fatal Errors:"
            print self.fatal_error
            Exit(1)

    def configure_sdl(self):
        if self.conf.CheckMyProgram('sdl-config'):
            self.conf.env.ParseConfig("sdl-config  --cflags --libs | sed 's/-I/-isystem/g'")
            for sdllib in ['image', 'mixer']:
                if not self.conf.CheckSDLLib(sdllib):
                    self.fatal_error += "  * SDL library '%s' not found\n" % sdllib
        else:
                self.fatal_error += "  * couldn't find sdl-config, SDL missing\n"

    def build(self):
        libpingus = self.env.StaticLibrary('pingus')

        self.env.Default(self.env.Program('pingus', 'main.cpp'))

project = Project()
project.configure()
project.build()

## EOF ##


More information about the Scons-users mailing list