5 kx # -*- python -*-
5 kx #
5 kx # ====================================================================
5 kx # Licensed to the Apache Software Foundation (ASF) under one
5 kx # or more contributor license agreements. See the NOTICE file
5 kx # distributed with this work for additional information
5 kx # regarding copyright ownership. The ASF licenses this file
5 kx # to you under the Apache License, Version 2.0 (the
5 kx # "License"); you may not use this file except in compliance
5 kx # with the License. You may obtain a copy of the License at
5 kx #
5 kx # http://www.apache.org/licenses/LICENSE-2.0
5 kx #
5 kx # Unless required by applicable law or agreed to in writing,
5 kx # software distributed under the License is distributed on an
5 kx # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
5 kx # KIND, either express or implied. See the License for the
5 kx # specific language governing permissions and limitations
5 kx # under the License.
5 kx # ====================================================================
5 kx #
5 kx
5 kx import sys
5 kx import os
5 kx import re
5 kx
5 kx EnsureSConsVersion(2,3,0)
5 kx
5 kx HEADER_FILES = ['serf.h',
5 kx 'serf_bucket_types.h',
5 kx 'serf_bucket_util.h',
5 kx ]
5 kx
5 kx # where we save the configuration variables
5 kx SAVED_CONFIG = '.saved_config'
5 kx
5 kx # Variable class that does no validation on the input
5 kx def _converter(val):
5 kx """
5 kx """
5 kx if val == 'none':
5 kx val = []
5 kx else:
5 kx val = val.split(' ')
5 kx return val
5 kx
5 kx def RawListVariable(key, help, default):
5 kx """
5 kx The input parameters describe a 'raw string list' option. This class
5 kx accepts a space-separated string and converts it to a list.
5 kx """
5 kx return (key, '%s' % (help), default, None, lambda val: _converter(val))
5 kx
5 kx # Custom path validator, creates directory when a specified option is set.
5 kx # To be used to ensure a PREFIX directory is only created when installing.
5 kx def createPathIsDirCreateWithTarget(target):
5 kx def my_validator(key, val, env):
5 kx build_targets = (map(str, BUILD_TARGETS))
5 kx if target in build_targets:
5 kx return PathVariable.PathIsDirCreate(key, val, env)
5 kx else:
5 kx return PathVariable.PathAccept(key, val, env)
5 kx return my_validator
5 kx
5 kx # default directories
5 kx if sys.platform == 'win32':
5 kx default_incdir='..'
5 kx default_libdir='..'
5 kx default_prefix='Debug'
5 kx else:
5 kx default_incdir='/usr'
5 kx default_libdir='$PREFIX/lib'
5 kx default_prefix='/usr/local'
5 kx
5 kx opts = Variables(files=[SAVED_CONFIG])
5 kx opts.AddVariables(
5 kx PathVariable('PREFIX',
5 kx 'Directory to install under',
5 kx default_prefix,
5 kx createPathIsDirCreateWithTarget('install')),
5 kx PathVariable('LIBDIR',
5 kx 'Directory to install architecture dependent libraries under',
5 kx default_libdir,
5 kx createPathIsDirCreateWithTarget('install')),
5 kx PathVariable('APR',
5 kx "Path to apr-1-config, or to APR's install area",
5 kx default_incdir,
5 kx PathVariable.PathAccept),
5 kx PathVariable('APU',
5 kx "Path to apu-1-config, or to APR's install area",
5 kx default_incdir,
5 kx PathVariable.PathAccept),
5 kx PathVariable('OPENSSL',
5 kx "Path to OpenSSL's install area",
5 kx default_incdir,
5 kx PathVariable.PathIsDir),
5 kx PathVariable('ZLIB',
5 kx "Path to zlib's install area",
5 kx default_incdir,
5 kx PathVariable.PathIsDir),
5 kx PathVariable('GSSAPI',
5 kx "Path to GSSAPI's install area",
5 kx None,
5 kx None),
5 kx BoolVariable('DEBUG',
5 kx "Enable debugging info and strict compile warnings",
5 kx False),
5 kx BoolVariable('APR_STATIC',
5 kx "Enable using a static compiled APR",
5 kx False),
5 kx RawListVariable('CC', "Command name or path of the C compiler", None),
5 kx RawListVariable('CFLAGS', "Extra flags for the C compiler (space-separated)",
5 kx None),
5 kx RawListVariable('LIBS', "Extra libraries passed to the linker, "
5 kx "e.g. \"-l<library1> -l<library2>\" (space separated)", None),
5 kx RawListVariable('LINKFLAGS', "Extra flags for the linker (space-separated)",
5 kx None),
5 kx RawListVariable('CPPFLAGS', "Extra flags for the C preprocessor "
5 kx "(space separated)", None),
5 kx )
5 kx
5 kx if sys.platform == 'win32':
5 kx opts.AddVariables(
5 kx # By default SCons builds for the host platform on Windows, when using
5 kx # a supported compiler (E.g. VS2010/VS2012). Allow overriding
5 kx
5 kx # Note that Scons 1.3 only supports this on Windows and only when
5 kx # constructing Environment(). Later changes to TARGET_ARCH are ignored
5 kx EnumVariable('TARGET_ARCH',
5 kx "Platform to build for (x86|x64|win32|x86_64)",
5 kx 'x86',
5 kx allowed_values=('x86', 'x86_64', 'ia64'),
5 kx map={'X86' : 'x86',
5 kx 'win32': 'x86',
5 kx 'Win32': 'x86',
5 kx 'x64' : 'x86_64',
5 kx 'X64' : 'x86_64'
5 kx }),
5 kx
5 kx EnumVariable('MSVC_VERSION',
5 kx "Visual C++ to use for building (E.g. 11.0, 9.0)",
5 kx None,
5 kx allowed_values=('14.0', '12.0',
5 kx '11.0', '10.0', '9.0', '8.0', '6.0')
5 kx ),
5 kx
5 kx # We always documented that we handle an install layout, but in fact we
5 kx # hardcoded source layouts. Allow disabling this behavior.
5 kx # ### Fix default?
5 kx BoolVariable('SOURCE_LAYOUT',
5 kx "Assume a source layout instead of install layout",
5 kx True),
5 kx )
5 kx
5 kx env = Environment(variables=opts,
5 kx tools=('default', 'textfile',),
5 kx CPPPATH=['.', ],
5 kx )
5 kx
5 kx env.Append(BUILDERS = {
5 kx 'GenDef' :
5 kx Builder(action = sys.executable + ' build/gen_def.py $SOURCES > $TARGET',
5 kx suffix='.def', src_suffix='.h')
5 kx })
5 kx
5 kx match = re.search(b'SERF_MAJOR_VERSION ([0-9]+).*'
5 kx b'SERF_MINOR_VERSION ([0-9]+).*'
5 kx b'SERF_PATCH_VERSION ([0-9]+)',
5 kx env.File('serf.h').get_contents(),
5 kx re.DOTALL)
5 kx MAJOR, MINOR, PATCH = [int(x) for x in match.groups()]
5 kx env.Append(MAJOR=str(MAJOR))
5 kx env.Append(MINOR=str(MINOR))
5 kx env.Append(PATCH=str(PATCH))
5 kx
5 kx # Calling external programs is okay if we're not cleaning or printing help.
5 kx # (cleaning: no sense in fetching information; help: we may not know where
5 kx # they are)
5 kx CALLOUT_OKAY = not (env.GetOption('clean') or env.GetOption('help'))
5 kx
5 kx
5 kx # HANDLING OF OPTION VARIABLES
5 kx
5 kx unknown = opts.UnknownVariables()
5 kx if unknown:
5 kx print ('Warning: Used unknown variables:', ', '.join(unknown.keys()))
5 kx
5 kx apr = str(env['APR'])
5 kx apu = str(env['APU'])
5 kx zlib = str(env['ZLIB'])
5 kx gssapi = env.get('GSSAPI', None)
5 kx
5 kx if gssapi and os.path.isdir(gssapi):
5 kx krb5_config = os.path.join(gssapi, 'bin', 'krb5-config')
5 kx if os.path.isfile(krb5_config):
5 kx gssapi = krb5_config
5 kx env['GSSAPI'] = krb5_config
5 kx
5 kx debug = env.get('DEBUG', None)
5 kx aprstatic = env.get('APR_STATIC', None)
5 kx
5 kx Help(opts.GenerateHelpText(env))
5 kx opts.Save(SAVED_CONFIG, env)
5 kx
5 kx
5 kx # PLATFORM-SPECIFIC BUILD TWEAKS
5 kx
5 kx thisdir = os.getcwd()
5 kx libdir = '$LIBDIR'
5 kx incdir = '$PREFIX/include/serf-$MAJOR'
5 kx
5 kx # This version string is used in the dynamic library name, and for Mac OS X also
5 kx # for the current_version and compatibility_version options in the .dylib
5 kx #
5 kx # Unfortunately we can't set the .dylib compatibility_version option separately
5 kx # from current_version, so don't use the PATCH level to avoid that build and
5 kx # runtime patch levels have to be identical.
5 kx if sys.platform != 'sunos5':
5 kx env['SHLIBVERSION'] = '%d.%d.%d' % (MAJOR, MINOR, 0)
5 kx
5 kx LIBNAME = 'libserf-%d' % (MAJOR,)
5 kx if sys.platform != 'win32':
5 kx LIBNAMESTATIC = LIBNAME
5 kx else:
5 kx LIBNAMESTATIC = 'serf-${MAJOR}'
5 kx
5 kx env.Append(RPATH=libdir,
5 kx PDB='${TARGET.filebase}.pdb')
5 kx
5 kx if sys.platform == 'darwin':
5 kx # linkflags.append('-Wl,-install_name,@executable_path/%s.dylib' % (LIBNAME,))
5 kx env.Append(LINKFLAGS=['-Wl,-install_name,%s/%s.dylib' % (thisdir, LIBNAME,)])
5 kx
5 kx if sys.platform != 'win32':
5 kx def CheckGnuCC(context):
5 kx src = '''
5 kx #ifndef __GNUC__
5 kx oh noes!
5 kx #endif
5 kx '''
5 kx context.Message('Checking for GNU-compatible C compiler...')
5 kx result = context.TryCompile(src, '.c')
5 kx context.Result(result)
5 kx return result
5 kx
5 kx conf = Configure(env, custom_tests = dict(CheckGnuCC=CheckGnuCC))
5 kx have_gcc = conf.CheckGnuCC()
5 kx env = conf.Finish()
5 kx
5 kx if have_gcc:
5 kx env.Append(CFLAGS=['-std=c89'])
5 kx env.Append(CCFLAGS=['-Wdeclaration-after-statement',
5 kx '-Wmissing-prototypes',
5 kx '-Wall'])
5 kx
5 kx if debug:
5 kx env.Append(CCFLAGS=['-g'])
5 kx env.Append(CPPDEFINES=['DEBUG', '_DEBUG'])
5 kx else:
5 kx env.Append(CCFLAGS=['-O2'])
5 kx env.Append(CPPDEFINES=['NDEBUG'])
5 kx
5 kx ### works for Mac OS. probably needs to change
5 kx env.Append(LIBS=['ssl', 'crypto', 'z', ])
5 kx
5 kx if sys.platform == 'sunos5':
5 kx env.Append(LIBS=['m'])
5 kx env.Append(PLATFORM='posix')
5 kx else:
5 kx # Warning level 4, no unused argument warnings
5 kx env.Append(CCFLAGS=['/W4', '/wd4100'])
5 kx
5 kx # Choose runtime and optimization
5 kx if debug:
5 kx # Disable optimizations for debugging, use debug DLL runtime
5 kx env.Append(CCFLAGS=['/Od', '/MDd'])
5 kx env.Append(CPPDEFINES=['DEBUG', '_DEBUG'])
5 kx else:
5 kx # Optimize for speed, use DLL runtime
5 kx env.Append(CCFLAGS=['/O2', '/MD'])
5 kx env.Append(CPPDEFINES=['NDEBUG'])
5 kx env.Append(LINKFLAGS=['/RELEASE'])
5 kx
5 kx # PLAN THE BUILD
5 kx SHARED_SOURCES = []
5 kx if sys.platform == 'win32':
5 kx env.GenDef(['serf.h','serf_bucket_types.h', 'serf_bucket_util.h'])
5 kx SHARED_SOURCES.append(['serf.def'])
5 kx
5 kx SOURCES = Glob('*.c') + Glob('buckets/*.c') + Glob('auth/*.c')
5 kx
5 kx lib_static = env.StaticLibrary(LIBNAMESTATIC, SOURCES)
5 kx lib_shared = env.SharedLibrary(LIBNAME, SOURCES + SHARED_SOURCES)
5 kx
5 kx if aprstatic:
5 kx env.Append(CPPDEFINES=['APR_DECLARE_STATIC', 'APU_DECLARE_STATIC'])
5 kx
5 kx if sys.platform == 'win32':
5 kx env.Append(LIBS=['user32.lib', 'advapi32.lib', 'gdi32.lib', 'ws2_32.lib',
5 kx 'crypt32.lib', 'mswsock.lib', 'rpcrt4.lib', 'secur32.lib'])
5 kx
5 kx # Get apr/apu information into our build
5 kx env.Append(CPPDEFINES=['WIN32','WIN32_LEAN_AND_MEAN','NOUSER',
5 kx 'NOGDI', 'NONLS','NOCRYPT'])
5 kx
5 kx if env.get('TARGET_ARCH', None) == 'x86_64':
5 kx env.Append(CPPDEFINES=['WIN64'])
5 kx
5 kx if aprstatic:
5 kx apr_libs='apr-1.lib'
5 kx apu_libs='aprutil-1.lib'
5 kx env.Append(LIBS=['shell32.lib', 'xml.lib'])
5 kx else:
5 kx apr_libs='libapr-1.lib'
5 kx apu_libs='libaprutil-1.lib'
5 kx
5 kx env.Append(LIBS=[apr_libs, apu_libs])
5 kx if not env.get('SOURCE_LAYOUT', None):
5 kx env.Append(LIBPATH=['$APR/lib', '$APU/lib'],
5 kx CPPPATH=['$APR/include/apr-1', '$APU/include/apr-1'])
5 kx elif aprstatic:
5 kx env.Append(LIBPATH=['$APR/LibR','$APU/LibR'],
5 kx CPPPATH=['$APR/include', '$APU/include'])
5 kx else:
5 kx env.Append(LIBPATH=['$APR/Release','$APU/Release'],
5 kx CPPPATH=['$APR/include', '$APU/include'])
5 kx
5 kx # zlib
5 kx env.Append(LIBS=['zlib.lib'])
5 kx if not env.get('SOURCE_LAYOUT', None):
5 kx env.Append(CPPPATH=['$ZLIB/include'],
5 kx LIBPATH=['$ZLIB/lib'])
5 kx else:
5 kx env.Append(CPPPATH=['$ZLIB'],
5 kx LIBPATH=['$ZLIB'])
5 kx
5 kx # openssl
5 kx env.Append(LIBS=['libeay32.lib', 'ssleay32.lib'])
5 kx if not env.get('SOURCE_LAYOUT', None):
5 kx env.Append(CPPPATH=['$OPENSSL/include/openssl'],
5 kx LIBPATH=['$OPENSSL/lib'])
5 kx elif 0: # opensslstatic:
5 kx env.Append(CPPPATH=['$OPENSSL/inc32'],
5 kx LIBPATH=['$OPENSSL/out32'])
5 kx else:
5 kx env.Append(CPPPATH=['$OPENSSL/inc32'],
5 kx LIBPATH=['$OPENSSL/out32dll'])
5 kx else:
5 kx if os.path.isdir(apr):
5 kx apr = os.path.join(apr, 'bin', 'apr-1-config')
5 kx env['APR'] = apr
5 kx if os.path.isdir(apu):
5 kx apu = os.path.join(apu, 'bin', 'apu-1-config')
5 kx env['APU'] = apu
5 kx
5 kx ### we should use --cc, but that is giving some scons error about an implict
5 kx ### dependency upon gcc. probably ParseConfig doesn't know what to do with
5 kx ### the apr-1-config output
5 kx if CALLOUT_OKAY:
5 kx env.ParseConfig('$APR --cflags --cppflags --ldflags --includes'
5 kx ' --link-ld --libs')
5 kx env.ParseConfig('$APU --ldflags --includes --link-ld --libs')
5 kx
5 kx ### there is probably a better way to run/capture output.
5 kx ### env.ParseConfig() may be handy for getting this stuff into the build
5 kx if CALLOUT_OKAY:
5 kx apr_libs = os.popen(env.subst('$APR --link-libtool --libs')).read().strip()
5 kx apu_libs = os.popen(env.subst('$APU --link-libtool --libs')).read().strip()
5 kx else:
5 kx apr_libs = ''
5 kx apu_libs = ''
5 kx
5 kx env.Append(CPPPATH=['$OPENSSL/include'])
5 kx env.Append(LIBPATH=['$OPENSSL/lib'])
5 kx
5 kx
5 kx # If build with gssapi, get its information and define SERF_HAVE_GSSAPI
5 kx if gssapi and CALLOUT_OKAY:
5 kx env.ParseConfig('$GSSAPI --cflags gssapi')
5 kx def parse_libs(env, cmd, unique=1):
5 kx env['GSSAPI_LIBS'] = cmd.strip()
5 kx return env.MergeFlags(cmd, unique)
5 kx env.ParseConfig('$GSSAPI --libs gssapi', parse_libs)
5 kx env.Append(CPPDEFINES=['SERF_HAVE_GSSAPI'])
5 kx if sys.platform == 'win32':
5 kx env.Append(CPPDEFINES=['SERF_HAVE_SSPI'])
5 kx
5 kx # On some systems, the -R values that APR describes never make it into actual
5 kx # RPATH flags. We'll manually map all directories in LIBPATH into new
5 kx # flags to set RPATH values.
5 kx for d in env['LIBPATH']:
5 kx env.Append(RPATH=':'+d)
5 kx
5 kx # Set up the construction of serf-*.pc
5 kx pkgconfig = env.Textfile('serf-%d.pc' % (MAJOR,),
5 kx env.File('build/serf.pc.in'),
5 kx SUBST_DICT = {
5 kx '@MAJOR@': str(MAJOR),
5 kx '@PREFIX@': '$PREFIX',
5 kx '@LIBDIR@': '$LIBDIR',
5 kx '@INCLUDE_SUBDIR@': 'serf-%d' % (MAJOR,),
5 kx '@VERSION@': '%d.%d.%d' % (MAJOR, MINOR, PATCH),
5 kx '@LIBS@': '%s %s %s -lz' % (apu_libs, apr_libs,
5 kx env.get('GSSAPI_LIBS', '')),
5 kx })
5 kx
5 kx env.Default(lib_static, lib_shared, pkgconfig)
5 kx
5 kx if CALLOUT_OKAY:
5 kx conf = Configure(env)
5 kx
5 kx ### some configuration stuffs
5 kx
5 kx env = conf.Finish()
5 kx
5 kx
5 kx # INSTALLATION STUFF
5 kx
5 kx install_static = env.Install(libdir, lib_static)
5 kx install_shared = env.InstallVersionedLib(libdir, lib_shared)
5 kx
5 kx if sys.platform == 'darwin':
5 kx # Change the shared library install name (id) to its final name and location.
5 kx # Notes:
5 kx # If --install-sandbox=<path> is specified, install_shared_path will point
5 kx # to a path in the sandbox. We can't use that path because the sandbox is
5 kx # only a temporary location. The id should be the final target path.
5 kx # Also, we shouldn't use the complete version number for id, as that'll
5 kx # make applications depend on the exact major.minor.patch version of serf.
5 kx
5 kx install_shared_path = install_shared[0].abspath
5 kx target_install_shared_path = os.path.join(libdir, '%s.dylib' % LIBNAME)
5 kx env.AddPostAction(install_shared, ('install_name_tool -id %s %s'
5 kx % (target_install_shared_path,
5 kx install_shared_path)))
5 kx
5 kx env.Alias('install-lib', [install_static, install_shared,
5 kx ])
5 kx env.Alias('install-inc', env.Install(incdir, HEADER_FILES))
5 kx env.Alias('install-pc', env.Install(os.path.join(libdir, 'pkgconfig'),
5 kx pkgconfig))
5 kx env.Alias('install', ['install-lib', 'install-inc', 'install-pc', ])
5 kx
5 kx
5 kx # TESTS
5 kx ### make move to a separate scons file in the test/ subdir?
5 kx
5 kx tenv = env.Clone()
5 kx
5 kx # MockHTTP requires C99 standard, so use it for the test suite.
5 kx cflags = tenv['CFLAGS']
5 kx tenv.Replace(CFLAGS = [f.replace('-std=c89', '-std=c99') for f in cflags])
5 kx
5 kx tenv.Append(CPPDEFINES=['MOCKHTTP_OPENSSL'])
5 kx
5 kx TEST_PROGRAMS = [ 'serf_get', 'serf_response', 'serf_request', 'serf_spider',
5 kx 'test_all', 'serf_bwtp' ]
5 kx if sys.platform == 'win32':
5 kx TEST_EXES = [ os.path.join('test', '%s.exe' % (prog)) for prog in TEST_PROGRAMS ]
5 kx else:
5 kx TEST_EXES = [ os.path.join('test', '%s' % (prog)) for prog in TEST_PROGRAMS ]
5 kx
5 kx # Find the (dynamic) library in this directory
5 kx tenv.Replace(RPATH=thisdir)
5 kx tenv.Prepend(LIBS=[LIBNAMESTATIC, ],
5 kx LIBPATH=[thisdir, ])
5 kx
5 kx check_script = env.File('build/check.py').rstr()
5 kx test_dir = env.File('test/test_all.c').rfile().get_dir()
5 kx src_dir = env.File('serf.h').rfile().get_dir()
5 kx test_app = ("%s %s %s %s") % (sys.executable, check_script, test_dir, 'test')
5 kx
5 kx # Set the library search path for the test programs
5 kx test_env = {'PATH' : os.environ['PATH'],
5 kx 'srcdir' : src_dir}
5 kx if sys.platform != 'win32':
5 kx test_env['LD_LIBRARY_PATH'] = ':'.join(tenv.get('LIBPATH', []))
5 kx env.AlwaysBuild(env.Alias('check', TEST_EXES, test_app, ENV=test_env))
5 kx
5 kx testall_files = [
5 kx 'test/test_all.c',
5 kx 'test/CuTest.c',
5 kx 'test/test_util.c',
5 kx 'test/test_context.c',
5 kx 'test/test_buckets.c',
5 kx 'test/test_auth.c',
5 kx 'test/mock_buckets.c',
5 kx 'test/test_ssl.c',
5 kx 'test/server/test_server.c',
5 kx 'test/server/test_sslserver.c',
5 kx ]
5 kx
5 kx for proggie in TEST_EXES:
5 kx if 'test_all' in proggie:
5 kx tenv.Program(proggie, testall_files )
5 kx else:
5 kx tenv.Program(target = proggie, source = [proggie.replace('.exe','') + '.c'])
5 kx
5 kx
5 kx # HANDLE CLEANING
5 kx
5 kx if env.GetOption('clean'):
5 kx # When we're cleaning, we want the dependency tree to include "everything"
5 kx # that could be built. Thus, include all of the tests.
5 kx env.Default('check')