

DB_VERSION = '4.5.20'
DB_URL = 'http://download.oracle.com/berkeley-db/db-%s.tar.gz' % DB_VERSION
DB_PATCHES = ['http://www.oracle.com/technology/products/berkeley-db/db/update/4.5.20/patch.4.5.20.1',
              'http://www.oracle.com/technology/products/berkeley-db/db/update/4.5.20/patch.4.5.20.2']
DB_FILES = ['db_static.vcproj', 'db_static_dbginfo.vsprops']
DB_CONFIGURE = '--disable-cryptography --disable-replication ' + \
               '--disable-statistics --disable-shared --enable-static'

LIBICONV_VERSION = '1.12'
LIBICONV_URL = 'ftp://ftp.gnu.org/pub/gnu/libiconv/libiconv-%s.tar.gz' % LIBICONV_VERSION
LIBICONV_CONFIGURE = '--disable-static'

LIBGOMP_URL = 'http://www.opensource.apple.com/darwinsource/tarballs/other/gcc_42-5531.tar.gz'
LIBGOMP_CONFIGURE = '--disable-multilib --disable-shared --with-pic'
LIBGOMP_PATCHES = ['libgomp-sysconf-failure-fix.patch',
                   'libgomp-osx-10.4-fix.patch']

GETTEXT_VERSION = '0.17'
GETTEXT_URL = 'ftp://ftp.gnu.org/pub/gnu/gettext/gettext-%s.tar.gz' % GETTEXT_VERSION
GETTEXT_CONFIGURE = '--disable-java --disable-rpath ' + \
                    '--disable-static '
GETTEXT_BINARIES = ['msgfmt', 'msgmerge', 'msgunfmt', 'xgettext', 'msgcat']

WX_VERSION = '2.8.7'
WX_URL = 'ftp://ftp.wxwidgets.org/pub/%s/wxWidgets-%s.tar.bz2' % (WX_VERSION, WX_VERSION)
WX_PATCHES = ['wx-2.8.7-mac-listctrl-refreshitem.patch',
              'wx-2.8.7-mac-listctrl-attributes.patch',
              'wx-2.8.7-msw-clipboard-events.patch',
              'wx-2.8.7-msw-richedit-border.patch']

# NB: don't use --enable-debug_info, it forces -O0!
WX_CONFIGURE = '--disable-shared --enable-unicode --disable-tga'
WX_WINFLAGS = 'SHARED=0 BUILD=release DEBUG_INFO=1 UNICODE=1 USE_MEDIA=0 USE_AUI=0 USE_RICHTEXT=0 USE_STC=0 USE_QA=1'

SPARKLE_VERSION = '1.5b6'
SPARKLE_URL = 'http://sparkle.downtowncode.com/sources/CarbonSparkle_%s.zip' % SPARKLE_VERSION

WINSPARKLE_VERSION = '0.1'
WINSPARKLE_URL = 'http://d10xg45o6p6dbl.cloudfront.net/projects/w/winsparkle/WinSparkle-%s.zip' % WINSPARKLE_VERSION

INNOSETUP_VERSION = '5.2.2'
INNOSETUP_URL = 'http://files.jrsoftware.org/ispack/ispack-%s.exe' % INNOSETUP_VERSION
INNOUNP_URL = 'http://innounp.sf.net/test020.rar'


from rlsbot import BuildEnvironment
env = BuildEnvironment()


env['OSX_SDKROOT'] = '/Developer/SDKs/MacOSX10.4u.sdk'
env['OSX_CFLAGS'] = '-isysroot $OSX_SDKROOT -O2 -ggdb3'
env['OSX_CXXFLAGS'] = '-isysroot $OSX_SDKROOT -O2 -ggdb3'
env['OSX_OBJCFLAGS'] = '-isysroot $OSX_SDKROOT -O2 -ggdb3'
env['OSX_LDFLAGS'] = '-Wl,-syslibroot,$OSX_SDKROOT -ggdb3' # -feliminate-dwarf2-dups'
env['OSX_CFLAGS_PPC'] = '$OSX_CFLAGS -arch ppc'
env['OSX_CXXFLAGS_PPC'] = '$OSX_CXXFLAGS -arch ppc'
env['OSX_OBJCFLAGS_PPC'] = '$OSX_OBJCFLAGS -arch ppc'
env['OSX_LDFLAGS_PPC'] = '$OSX_LDFLAGS -arch ppc'
env['OSX_CFLAGS_X86'] = '$OSX_CFLAGS -arch i386'
env['OSX_CXXFLAGS_X86'] = '$OSX_CXXFLAGS -arch i386'
env['OSX_OBJCFLAGS_X86'] = '$OSX_OBJCFLAGS -arch i386'
env['OSX_LDFLAGS_X86'] = '$OSX_LDFLAGS -arch i386'
env['OSX_HOST_PPC'] = 'powerpc-apple-darwin8'
env['OSX_HOST_X86'] = 'i686-apple-darwin8'

# FIXME: this should be per-agent configuration
# FIXME: have some directives on specifying what is required from the agent
# native type of the agent we're executing on
env['OSX_NATIVE'] = 'ppc' # 'ppc' or 'x86'

OSX_ENVIRONMENT_COMMON = {
        'MACOSX_DEPLOYMENT_TARGET' : '10.4',
    }

OSX_ENVIRONMENT_PPC = {
        'CFLAGS'   : '$OSX_CFLAGS_PPC',
        'CXXFLAGS' : '$OSX_CXXFLAGS_PPC',
        'OBJCFLAGS' : '$OSX_OBJCFLAGS_PPC',
        'LDFLAGS'  : '$OSX_LDFLAGS_PPC',
    }
OSX_ENVIRONMENT_PPC.update(OSX_ENVIRONMENT_COMMON)

OSX_ENVIRONMENT_X86 = {
        'CFLAGS'   : '$OSX_CFLAGS_X86',
        'CXXFLAGS' : '$OSX_CXXFLAGS_X86',
        'OBJCFLAGS' : '$OSX_OBJCFLAGS_X86',
        'LDFLAGS'  : '$OSX_LDFLAGS_X86',
    }
OSX_ENVIRONMENT_X86.update(OSX_ENVIRONMENT_COMMON)


VS2005_ENVIRONMENT = {
        'MSVS_PATH' : 'C:\\Program Files\\Microsoft Visual Studio 8',
        'PSDK_PATH' : 'C:\\Program Files\\Microsoft Platform SDK',
    }


# -----------------------------------------------------------------
# Berkeley DB
# -----------------------------------------------------------------

src_db = env.ComponentSources(
        name='db',
        source=DB_URL,
        patches=DB_PATCHES,
        morefiles=DB_FILES,
        post_commands=['mv db_static* build_windows'])


db_osx_ppc = env.BuildComponent(
        name='db-osx-ppc',
        agent='osx',
        sources=src_db,
        deps=None,
        commands=[
            '(cd build_unix ; ../dist/configure --host=$OSX_HOST_PPC --prefix=/usr/local %s)' % DB_CONFIGURE,
            '(cd build_unix ; make)',
            '(cd build_unix; make install DESTDIR=`pwd`/../D)',
            'rm -rf D/usr/local/docs'
            ],
        environment=OSX_ENVIRONMENT_PPC,
        outputs=['D/usr/local/*'])

db_osx_x86 = env.BuildComponent(
        name='db-osx-x86',
        agent='osx',
        sources=src_db,
        deps=None,
        commands=[
            '(cd build_unix ; ../dist/configure --host=$OSX_HOST_X86 --prefix=/usr/local %s)' % DB_CONFIGURE,
            '(cd build_unix ; make)',
            '(cd build_unix; make install DESTDIR=`pwd`/../D)',
            'rm -rf D/usr/local/docs'
            ],
        environment=OSX_ENVIRONMENT_X86,
        outputs=['D/usr/local/*'])

db_osx = env.Alias('build-db-osx', [db_osx_ppc, db_osx_x86])


db_win32 = env.BuildComponent(
        name='db-win32',
        agent=['win32','vs2005'],
        sources=src_db,
        deps=None,
        toolchain=env.VisualStudioToolchain,
        commands=[
            'cd build_windows',
            'vcbuild /useenv /override:db_static_dbginfo.vsprops /platform:Win32 db_static.vcproj Release',
            'del /f /q Release\\db_static\\*.*',
            ],
        environment=VS2005_ENVIRONMENT,
        outputs=['build_windows'])


# -----------------------------------------------------------------
# libiconv (needed by gettext)
# -----------------------------------------------------------------

src_libiconv = env.ComponentSources(
        name='libiconv',
        source=LIBICONV_URL)


libiconv_win32 = env.BuildComponent(
        name='libiconv-win32',
        agent='mingw',
        sources=src_libiconv,
        deps=None,
        commands=[
            './configure --host=$$MINGW_HOST %s' % LIBICONV_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/D',
            '$$MINGW_HOST-strip -x D/usr/local/bin/*.dll', # FIXME
            ],
        outputs=['D/usr/local/*'])

# -----------------------------------------------------------------
# libgomp (needed by gettext's msgmerge on OS X)
# -----------------------------------------------------------------

# Apple's GCC 4.2 Preview 1 has broken OpenMP installation:
#   1) omp.h header is not installed in SDKs
#   2) libgomp.a included in 10.4u SDK is built for 10.5 and depends on
#      newly added symbols
# Because of this, we have to build our own copy if GOMP if we want to
# take advantage of OpenMP on OSX 10.4.

src_libgomp = env.ComponentSources(
        name='libgomp',
        source=LIBGOMP_URL,
        patches=LIBGOMP_PATCHES,
        post_commands=[
            'mkdir .junk',
            'mv * .junk',
            'mkdir -p gcc/doc',
            'mv .junk/gcc/doc/include gcc/doc',
            'mv .junk/libgomp .',
            'mv `find .junk -maxdepth 1 -type f` .',
            'rm -rf .junk',
        ])

libgomp_osx_ppc = env.BuildComponent(
        name='libgomp-osx-ppc',
        agent='osx10.5',
        sources=src_libgomp,
        deps=None,
        commands=[
            'mkdir libgomp/build',
            'cd libgomp/build',
            '../configure --host=$OSX_HOST_PPC CC=$$OSX_CC_42 --prefix=/usr/local %s' % LIBGOMP_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/../../D',
            'mkdir ../../D/usr/local/include',
            'cp omp.h ../../D/usr/local/include',
            ],
        environment=OSX_ENVIRONMENT_PPC,
        outputs=['D/usr/local/*'])

libgomp_osx_x86 = env.BuildComponent(
        name='libgomp-osx-x86',
        agent='osx10.5',
        sources=src_libgomp,
        deps=None,
        commands=[
            'mkdir libgomp/build',
            'cd libgomp/build',
            '../configure --host=$OSX_HOST_X86 CC=$$OSX_CC_42 --prefix=/usr/local %s' % LIBGOMP_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/../../D',
            'mkdir ../../D/usr/local/include',
            'cp omp.h ../../D/usr/local/include',
            ],
        environment=OSX_ENVIRONMENT_X86,
        outputs=['D/usr/local/*'])

libgomp_osx = env.Alias('build-libgomp-osx', [libgomp_osx_ppc, libgomp_osx_x86])


# -----------------------------------------------------------------
# GNU gettext
# -----------------------------------------------------------------

src_gettext = env.ComponentSources(
        name='gettext',
        source=GETTEXT_URL)

gettext_osx_ppc = env.BuildComponent(
        name='gettext-osx-ppc',
        agent='osx10.5',
        sources=src_gettext,
        deps=[(libgomp_osx_ppc, 'libgomp')],
        commands=[
            './configure --host=$OSX_HOST_PPC'
            '       --with-libiconv-prefix=$OSX_SDKROOT/usr %s'
            '       CC=$$OSX_CC_42 CXX=$$OSX_CXX_42'
            '       CPPFLAGS="-I`pwd`/libgomp/include $$CPPFLAGS"'
            '       LDFLAGS="-L`pwd`/libgomp/lib $$LDFLAGS"' % GETTEXT_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/D -j1',
            ],
        environment=OSX_ENVIRONMENT_PPC,
        outputs=['D/usr/local/*'])

gettext_osx_x86 = env.BuildComponent(
        name='gettext-osx-x86',
        agent='osx10.5',
        sources=src_gettext,
        deps=[(libgomp_osx_x86, 'libgomp')],
        commands=[
            './configure --host=$OSX_HOST_X86'
            '       --with-libiconv-prefix=$OSX_SDKROOT/usr %s'
            '       CC=$$OSX_CC_42 CXX=$$OSX_CXX_42'
            '       CPPFLAGS="-I`pwd`/libgomp/include $$CPPFLAGS"'
            '       LDFLAGS="-L`pwd`/libgomp/lib $$LDFLAGS"' % GETTEXT_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/D -j1',
            ],
        environment=OSX_ENVIRONMENT_X86,
        outputs=['D/usr/local/*'])

gettext_osx = env.BuildComponent(
        name='gettext-osx',
        agent='osx',
        sources=[],
        deps=[(gettext_osx_ppc, 'ppc'), (gettext_osx_x86, 'x86')],
        commands=[
            'mkdir -p univ/bin univ/lib',
            'for i in %s ; do lipo -arch ppc ppc/bin/$$i -arch i386 x86/bin/$$i -create -output univ/bin/$$i; done' % ' '.join(GETTEXT_BINARIES),
            'for i in ppc/lib/*.dylib ; do lipo -arch ppc ppc/lib/`basename $$i` -arch i386 x86/lib/`basename $$i` -create -output univ/lib/`basename $$i`; done',
            # FIXME: save dSYM info?
            'strip -u -r univ/bin/*',
            'strip -x univ/lib/*.dylib',
            ],
        environment=OSX_ENVIRONMENT_COMMON,
        outputs=['univ/*'])

gettext_win32 = env.BuildComponent(
        name='gettext-win32',
        agent='mingw',
        sources=src_gettext,
        deps=[(libiconv_win32, 'libiconv')],
        commands=[
            './configure --host=$$MINGW_HOST --with-libiconv-prefix=`pwd`/libiconv --enable-threads=win32 %s CC=$$MINGW_CC_42 CXX=$$MINGW_CXX_42' % GETTEXT_CONFIGURE,
            'make',
            'make install-strip DESTDIR=`pwd`/D',
            'cp libiconv/bin/*.dll D/usr/local/bin',
            '$$MINGW_HOST-strip -x D/usr/local/bin/*.dll', # FIXME
            'cp COPYING D/usr/local/bin',
            'cp $$MINGW_DIR/bin/mingwm10.dll D/usr/local/bin',
            'cp $$MINGW_DIR/bin/pthreadGC2.dll D/usr/local/bin',
            ],
        outputs=['D/usr/local/bin/*'])


# -----------------------------------------------------------------
# Sparkle
# -----------------------------------------------------------------

src_sparkle = env.ComponentSources(
        name='sparkle',
        source=SPARKLE_URL,
        post_commands=[
            'rm -rf __MACOSX',
            'mv Sparkle*/* .',
        ])


sparkle = env.BuildComponent(
        name='sparkle',
        agent='osx',
        sources=src_sparkle,
        deps=None,
        commands=[
            'cd "Extras/Source Code Carbon"',
            'xcodebuild -project Sparkle.xcodeproj -target Sparkle -configuration Release GCC_TREAT_WARNINGS_AS_ERRORS=NO',
            ],
        environment=OSX_ENVIRONMENT_COMMON,
        outputs=['Extras/Source*Carbon/build/Release/*'])

# -----------------------------------------------------------------
# wxWidgets
# -----------------------------------------------------------------

src_wx = env.ComponentSources(
        name='wx',
        source=WX_URL,
        patches=WX_PATCHES,
        post_commands=['(cd locale ; make allmo)'])

wx_osx_ppc = env.BuildComponent(
        name='wx-osx-ppc',
        agent='osx',
        sources=src_wx,
        deps=None,
        commands=[
            './configure --host=$OSX_HOST_PPC %s' % WX_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/D',
            '(cd D/usr/local/bin; ln -sf ../lib/wx/config/* wx-config)'],
        environment=OSX_ENVIRONMENT_PPC,
        outputs=['D/usr/local/*'])

wx_osx_x86 = env.BuildComponent(
        name='wx-osx-x86',
        agent='osx',
        sources=src_wx,
        deps=None,
        commands=[
            './configure --host=$OSX_HOST_X86 %s' % WX_CONFIGURE,
            'make',
            'make install DESTDIR=`pwd`/D',
            '(cd D/usr/local/bin; ln -sf ../lib/wx/config/* wx-config)'],
        environment=OSX_ENVIRONMENT_X86,
        outputs=['D/usr/local/*'])

wx_osx = env.Alias('build-wx-osx', [wx_osx_ppc, wx_osx_x86])

wx_win32 = env.BuildComponent(
        name='wx-win32',
        agent=['win32', 'vs2005'],
        sources=src_wx,
        deps=None,
        toolchain=env.VisualStudioToolchain,
        commands=[
            'cd build/msw',
            'nmake -f makefile.vc %s' % WX_WINFLAGS,
            'cd ../../utils/wxrc',
            'nmake -f makefile.vc %s' % WX_WINFLAGS],
        environment=VS2005_ENVIRONMENT,
        outputs=['include', 'lib', 'locale', 'utils/wxrc/vc_*/wxrc.exe*'])


# -----------------------------------------------------------------
# WinSparkle
# -----------------------------------------------------------------

src_winsparkle = env.ComponentSources(
        name='winsparkle',
        source=WINSPARKLE_URL)

winsparkle = env.BuildComponent(
        name='winsparkle',
        agent=['win32'],
        sources=src_winsparkle,
        deps=None,
        commands=[],
        environment=VS2005_ENVIRONMENT,
        outputs=['*'])


# -----------------------------------------------------------------
# innosetup
# -----------------------------------------------------------------

src_innounp = env.ComponentSources(
        name='innounp',
        source=INNOUNP_URL)

src_innosetup = env.ComponentSources(
        name='innosetup',
        source=INNOSETUP_URL)

innosetup = env.BuildComponent(
        name='innosetup',
        agent='win32',
        sources=src_innosetup,
        deps=[src_innounp],
        commands=[
            'innounp -x ispack-5.2.2.exe',
            'move {app}\\ISCC?2.exe {app}\\ISCC.exe',
            'del {app}\\ISCC?1.exe',
            'move {app}\\ISCmplr?2.dll {app}\\ISCmplr.dll',
            'move {app}\\ISCmplr?1.dll {app}\\ISCmplr.dls',
        ],
        outputs=['{app}/*'])


# -----------------------------------------------------------------
# poedit
# -----------------------------------------------------------------

src_poedit = env.ComponentSources(
        name='poedit',
        source='..',
        post_commands=['./bootstrap'])

poedit_tarball = env.BuildComponent(
        name='poedit-tarball',
        agent='unix',
        sources=src_poedit,
        deps=None,
        commands=['./make-distrib.sh'],
        outputs=['poedit-*.tar.gz'])

poedit_osx = env.BuildComponent(
        name='poedit-osx',
        agent='osx',
        sources=src_poedit,
        deps=[
              (wx_osx_ppc, 'ppc/deps'),
              (wx_osx_x86, 'x86/deps'),
              (db_osx_ppc, 'ppc/deps'),
              (db_osx_x86, 'x86/deps'),
              (gettext_osx, 'deps/gettext'),
              (sparkle, 'deps/sparkle'),
              ],
        commands=[
            # build PPC binary:
            'cd ppc',
            '../configure'
            '  --host=$OSX_HOST_PPC'
            '  --with-wx-config=`pwd`/deps/bin/wx-config --with-wx-prefix=`pwd`/deps'
            '  --with-sparkle=`pwd`/../deps/sparkle'
            '  CFLAGS="$OSX_CFLAGS_PPC"'
            '  CXXFLAGS="$OSX_CXXFLAGS_PPC"'
            '  OBJCFLAGS="$OSX_OBJCFLAGS_PPC"'
            '  CPPFLAGS="-I`pwd`/deps/include"'
            '  LDFLAGS="$OSX_LDFLAGS_PPC -L`pwd`/deps/lib"'
            '  WXRC="`pwd`/../$OSX_NATIVE/deps/bin/wxrc"',
            'make',
            'cd ..',
            # build Intel binary:
            'cd x86',
            '../configure'
            '  --host=$OSX_HOST_X86'
            '  --with-wx-config=`pwd`/deps/bin/wx-config --with-wx-prefix=`pwd`/deps'
            '  --with-sparkle=`pwd`/../deps/sparkle'
            '  CFLAGS="$OSX_CFLAGS_X86"'
            '  CXXFLAGS="$OSX_CXXFLAGS_X86"'
            '  OBJCFLAGS="$OSX_OBJCFLAGS_X86"'
            '  CPPFLAGS="-I`pwd`/deps/include"'
            '  LDFLAGS="$OSX_LDFLAGS_X86 -L`pwd`/deps/lib"'
            '  WXRC="`pwd`/../$OSX_NATIVE/deps/bin/wxrc"',
            'make',
            # merge them:
            'mv src/poedit src/poedit-x86',
            'lipo -arch ppc ../ppc/src/poedit -arch i386 src/poedit-x86 -create -output src/poedit',
            # finally, create the bundle:
            'GETTEXT_PREFIX="`pwd`/../deps/gettext" WX_ROOT="`pwd`/deps" make dist-osx',
            ],
        environment=OSX_ENVIRONMENT_COMMON,
        outputs=['x86/poedit-*.dmg', 'x86/src/*dSYM*'])

poedit_win32 = env.BuildComponent(
        name='poedit-win32',
        agent=['win32', 'vs2005'],
        sources=src_poedit,
        deps=[
              (wx_win32, 'deps/wx'),
              (gettext_win32, 'deps/gettext'),
              (db_win32, 'deps/db'),
              (innosetup, 'deps/inno'),
              (winsparkle, 'deps/winsparkle'),
              ],
        toolchain=env.VisualStudioToolchain,
        commands=[
            'cd win32',
            'set WXWIN=..\\deps\\wx',
            'set WXRC="%WXWIN%\\wxrc.exe"',
            'nmake -f makefile.vc WX_DEBUG=0 WXRC=%WXRC% DB_PATH=..\\deps\\db WINSPARKLE_PATH=..\\deps\\winsparkle',
            '..\\deps\\inno\\iscc poedit.iss',
            ],
        environment=VS2005_ENVIRONMENT,
        outputs=['poedit-*-setup.exe', 'win32/poedit.pdb'])
