#!/usr/local/bin/perl

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Komodo code.
# 
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
# 
# Contributor(s):
#   ActiveState Software Inc
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# 
# ***** END LICENSE BLOCK *****

# Conscript for building the scintilla dll (with lexer)
#   Neil Hodgson provides a makefile to build this so we are no going to
#   duplicate that, however, to ensure rebuilding as necessary the
#   Command that call the makefile should have the proper dependencies
#   (i.e. inputs). In fact, to work with the build->src link they *must* all
#   be listed (this is a good thing). These will have to be kept in sync.

Import(
    'cons',
    'platform',
    'buildType',
    'buildFlavour',
    'mozPluginsDir',
    'unsiloedPythonExe',
    'scintillaBuildDir',
);

# this is what we want from the scintilla build process
my $libName = '../bin/headlessscintilla.a';
if ($platform eq "win") {
    $libName = '../bin/ScintillaHeadless.dll';
}

# build scintilla

# The SciMoz build on Linux explicitly links the Lex*.o files from the
# scintilla build AND with Cons multiple targets for a "Command" cannot be
# specified, so have to specify a makefile call for each platform.
# Use one entry per line for ease of maintenance.  Bits are cheap.
my @lexers = qw(
    A68k
    Abaqus
    Ada
    APDL
    Asm
    Asn1
    ASY
    AU3
    AVE
    AVS
    Baan
    Bash
    Basic
    BibTeX
    Bullant
    Caml
    CoffeeScript
    CLW
    Cmake
    COBOL
    CoffeeScript
    Conf
    CPP
    Crontab
    Csound
    CSS
    D
    DMAP
    DMIS
    ECL
    Eiffel
    Erlang
    EScript
    Flagship
    Forth
    Fortran
    GAP
    Gui4Cli
    Haskell
    HTML
    Inno
    Kix
    KVIrc
    LaTeX
    Lisp
    Lout
    Lua
    Magik
    Markdown
    Matlab
    Metapost
    MMIXAL
    Modula
    MPT
    MSSQL
    MySQL
    Nimrod
    Nsis
    Opal
    OScript
    Others
    Pascal
    PB
    Perl
    PLM
    PO
    POV
    PowerPro
    PowerShell
    Progress
    PS
    Python
    R
    Rebol
    Registry
    Ruby
    Rust
    Scriptol
    Smalltalk
    SML
    Sorcus
    Specman
    Spice
    SQL
    STTXT
    TACL
    TADS3
    TAL
    TCMD
    Tcl
    TeX
    Txt2tags
    UDL
    VB
    Verilog
    VisualProlog
    VHDL
    XSLT
    YAML
);
@lexers = map { "../lexers/Lex$_.cxx" } @lexers;

my @args = (
    # the generated files
    # More specifically: the generated files that other segments of the Cons
    # system care about.
    [$libName],
    # the files needed to generate these
    "makefile_converter.py",
    "../scripts/Face.py",
    "../scripts/HFacer.py",
    "../include/ILexer.h",
    "../include/Platform.h",
    "../include/SciLexer.h",
    "../include/Scintilla.h",
    "../include/Scintilla.iface",
    "../include/ScintillaWidget.h",

    "../lexlib/Accessor.cxx",
    "../lexlib/Accessor.h",
    "../lexlib/CharacterCategory.cxx",
    "../lexlib/CharacterCategory.h",
    "../lexlib/CharacterSet.cxx",
    "../lexlib/CharacterSet.h",
    "../lexlib/LexAccessor.h",
    "../lexlib/LexerBase.cxx",
    "../lexlib/LexerBase.h",
    "../lexlib/LexerModule.cxx",
    "../lexlib/LexerModule.h",
    "../lexlib/LexerNoExceptions.cxx",
    "../lexlib/LexerNoExceptions.h",
    "../lexlib/LexerSimple.cxx",
    "../lexlib/LexerSimple.h",
    "../lexlib/OptionSet.h",
    "../lexlib/PropSetSimple.cxx",
    "../lexlib/PropSetSimple.h",
    "../lexlib/SparseState.h",
    "../lexlib/StringCopy.h",
    "../lexlib/StyleContext.cxx",
    "../lexlib/StyleContext.h",
    "../lexlib/SubStyles.h",
    "../lexlib/WordList.cxx",
    "../lexlib/WordList.h",

    "../src/AutoComplete.cxx",
    "../src/AutoComplete.h",
    "../src/CaseConvert.cxx",
    "../src/CaseConvert.h",
    "../src/CaseFolder.cxx",
    "../src/CaseFolder.h",
    "../src/CallTip.cxx",
    "../src/CallTip.h",
    "../src/Catalogue.cxx",
    "../src/Catalogue.h",
    "../src/CellBuffer.cxx",
    "../src/CellBuffer.h",
    "../src/CharClassify.cxx",
    "../src/CharClassify.h",
    "../src/ContractionState.cxx",
    "../src/ContractionState.h",
    "../src/Decoration.cxx",
    "../src/Decoration.h",
    "../src/Document.cxx",
    "../src/Document.h",
    "../src/Editor.cxx",
    "../src/Editor.h",
    "../src/EditModel.cxx",
    "../src/EditModel.h",
    "../src/EditView.cxx",
    "../src/EditView.h",
    "../src/ExternalLexer.cxx",
    "../src/ExternalLexer.h",
    "../src/FontQuality.h",
    "../src/Indicator.cxx",
    "../src/Indicator.h",
    "../src/KeyMap.cxx",
    "../src/KeyMap.h",
    "../src/LineMarker.cxx",
    "../src/LineMarker.h",
    "../src/MarginView.cxx",
    "../src/MarginView.h",
    "../src/Partitioning.h",
    "../src/PerLine.cxx",
    "../src/PerLine.h",
    "../src/PositionCache.cxx",
    "../src/PositionCache.h",
    "../src/RESearch.cxx",
    "../src/RESearch.h",
    "../src/RunStyles.cxx",
    "../src/RunStyles.h",
    "../src/ScintillaBase.cxx",
    "../src/ScintillaBase.h",
    "../src/Selection.cxx",
    "../src/Selection.h",
    "../src/SplitVector.h",
    "../src/Style.cxx",
    "../src/Style.h",
    "../src/UniConversion.cxx",
    "../src/UniConversion.h",
    "../src/UnicodeFromUTF8.h",
    "../src/ViewStyle.cxx",
    "../src/ViewStyle.h",
    "../src/XPM.cxx",
    "../src/XPM.h",
    @lexers,
    "PlatHeadless.cxx",
    "ScintillaHeadless.cxx",
    "../include/ScintillaHeadless.h",
    "../bin/empty.txt",   # as for scintilla dist, only there to create bin dir

    #"libpcre.a", # built and installed from contrib/pcre
    #"../include/pcre.h", # installed from contrib/pcre
            );

my $cxxFlags = " -DCONTAINER_HANDLES_EVENTS=1 -DINCLUDE_DEPRECATED_FEATURES";

# Still have to define a platform setting...
$cxxFlags = " -DFOX";

if ($buildType eq 'debug'
    || $buildFlavour eq 'dev') {
    $cxxFlags .= " -DDEBUG=1 -g";
    $cxxFlags .= " -DUDL_DEBUG_TIME=1" if ($ENV{UDL_DEBUG_TIME});
    $cxxFlags .= " -DUDL_DEBUG=1" if ($ENV{UDL_DEBUG});
    $cxxFlags .= " -DUDL_DEBUG_OTHER=1" if ($ENV{UDL_DEBUG_OTHER});
}

%consLocal = $cons->copy();
my $LDFLAGS = $consLocal{'LDFLAGS'};
$consLocal{ENV}{'CXXFLAGS'} .= $cxxFlags;
# Scintilla uses exceptions - remove any disabling of it from CXXFLAGS.
$consLocal{ENV}{'CXXFLAGS'} =~ s/-fno-exceptions //g;
$consLocal = new cons(%consLocal);

my $makeFlags = "";
if ($platform eq "darwin") {
    $makeFlags = "shared NATIVE=1";
}

if ($platform eq "win") {
    push(@args, "../win32/libpcre.lib"); # built and installed from contrib/pcre
    $LDFLAGS .= " /libpath:../win32";
    push(@args, (qq(
                 $unsiloedPythonExe bin/run-in-dir.py %1:d/../scripts/ python HFacer.py
                 $unsiloedPythonExe bin/run-in-dir.py %1:d python makefile_converter.py
                 set LINK=$LDFLAGS && $unsiloedPythonExe bin/run-in-dir.py %1:d nmake /nologo QUIET=0 $libName -f scintilla.mak
                 )));
} else {
    push(@args, (qq(
                 $unsiloedPythonExe bin/run-in-dir.py %1:d/../include/ chmod a+w Scintilla.h
                 $unsiloedPythonExe bin/run-in-dir.py %1:d/../include/ chmod a+w SciLexer.h
                 $unsiloedPythonExe bin/run-in-dir.py %1:d/../scripts/ python HFacer.py
                 $unsiloedPythonExe bin/run-in-dir.py %1:d python makefile_converter.py
                 $unsiloedPythonExe bin/run-in-dir.py %1:d make $makeFlags -f makefile $libName
                 )));
}

$consLocal->Command(@args);
$consLocal->Depends($libName, "../include/pcre.h"); # depends upon contrib/pcre

# Headless depends upon the regular scintilla build being completed.
if ($platform eq "win") {
    $consLocal->Depends($libName, "../bin/SciLexer.dll");
} else {
    $consLocal->Depends($libName, "../bin/scintilla.a");
}


