~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to aqsl/aqslcomp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Implements a Renderman Shading Language compiler that generates Aqsis bytecode 
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
                \author Timothy M. Shead (tshead@k-3d.com)
 
25
*/
 
26
 
 
27
#include        "aqsis.h"
 
28
#include        "logging_streambufs.h"
 
29
 
 
30
#include        <iostream>
 
31
#include        <fstream>
 
32
#include        <sstream>
 
33
#include        <stdio.h>
 
34
 
 
35
#ifdef  AQSIS_SYSTEM_WIN32
 
36
#include        "io.h"
 
37
#else
 
38
#include        "unistd.h"
 
39
#endif //AQSIS_SYSTEM_WIN32
 
40
 
 
41
#include        "libslparse.h"
 
42
#include        "icodegen.h"
 
43
#include        "codegenvm.h"
 
44
#include        "vmoutput.h"
 
45
#include        "argparse.h"
 
46
 
 
47
#if defined(AQSIS_SYSTEM_WIN32) || defined(AQSIS_SYSTEM_MACOSX)
 
48
#include        "version.h"
 
49
#endif
 
50
 
 
51
using namespace Aqsis;
 
52
 
 
53
extern "C" void PreProcess(int argc, char** argv);
 
54
 
 
55
ArgParse::apstring      g_stroutname = "";
 
56
bool    g_help = 0;
 
57
bool    g_version = 0;
 
58
ArgParse::apstringvec g_defines; // Filled in with strings to pass to the preprocessor
 
59
ArgParse::apstringvec g_includes; // Filled in with strings to pass to the preprocessor
 
60
ArgParse::apstringvec g_undefines; // Filled in with strings to pass to the preprocessor
 
61
 
 
62
bool g_cl_no_color = false;
 
63
bool g_cl_syslog = false;
 
64
 
 
65
void version( std::ostream& Stream )
 
66
{
 
67
#if defined(AQSIS_SYSTEM_WIN32) || defined(AQSIS_SYSTEM_MACOSX)
 
68
    Stream << "aqsl version " << VERSION_STR << std::endl;
 
69
#else
 
70
    Stream << "aqsl version " << VERSION << std::endl;
 
71
#endif
 
72
}
 
73
 
 
74
 
 
75
char* g_slppDefArgs[] =
 
76
    {
 
77
        "slpp",
 
78
        "-d",
 
79
        "PI=3.141592654",
 
80
        "-d",
 
81
        "AQSIS",
 
82
        "-c6",
 
83
    };
 
84
int g_cslppDefArgs = sizeof( g_slppDefArgs ) / sizeof( g_slppDefArgs[0] );
 
85
 
 
86
 
 
87
/** Process the sl file from stdin and produce an slx bytestream.
 
88
 */
 
89
int main( int argc, const char** argv )
 
90
{
 
91
    ArgParse ap;
 
92
    CqCodeGenVM codegen; // Should be a pointer determined by what we want to generate
 
93
    bool error = false; ///! Couldn't compile shader
 
94
 
 
95
    ap.usageHeader( ArgParse::apstring( "Usage: " ) + argv[ 0 ] + " [options] <filename>" );
 
96
    ap.argString( "o", " %s \aspecify output filename", &g_stroutname );
 
97
    ap.argStrings( "i", "%s \aSet path for #include files.", &g_includes );
 
98
    ap.argStrings( "I", "%s \aSet path for #include files.", &g_includes );
 
99
    ap.argStrings( "D", "Sym[=value] \adefine symbol <string> to have value <value> (default 1).", &g_defines );
 
100
    ap.argStrings( "U", "Sym \aUndefine an initial symbol.", &g_undefines );
 
101
    ap.argFlag( "help", "\aprint this help and exit", &g_help );
 
102
    ap.argFlag( "version", "\aprint version information and exit", &g_version );
 
103
 
 
104
    if ( argc > 1 && !ap.parse( argc - 1, argv + 1 ) )
 
105
    {
 
106
        std::cerr << ap.errmsg() << std::endl << ap.usagemsg();
 
107
        exit( 1 );
 
108
    }
 
109
 
 
110
    if ( g_version )
 
111
    {
 
112
        version( std::cout );
 
113
        exit( 0 );
 
114
    }
 
115
 
 
116
    if ( g_help )
 
117
    {
 
118
        std::cout << ap.usagemsg();
 
119
        exit( 0 );
 
120
    }
 
121
 
 
122
#ifdef  AQSIS_SYSTEM_WIN32
 
123
        std::auto_ptr<std::streambuf> ansi( new Aqsis::ansi_buf(std::cerr) );
 
124
#endif
 
125
        std::auto_ptr<std::streambuf> reset_level( new Aqsis::reset_level_buf(std::cerr) );
 
126
        std::auto_ptr<std::streambuf> show_timestamps( new Aqsis::timestamp_buf(std::cerr) );
 
127
        std::auto_ptr<std::streambuf> fold_duplicates( new Aqsis::fold_duplicates_buf(std::cerr) );
 
128
        std::auto_ptr<std::streambuf> color_level;
 
129
        if(!g_cl_no_color)
 
130
        {
 
131
            std::auto_ptr<std::streambuf> temp_color_level( new Aqsis::color_level_buf(std::cerr) );
 
132
            color_level = temp_color_level;
 
133
        }
 
134
        std::auto_ptr<std::streambuf> show_level( new Aqsis::show_level_buf(std::cerr) );
 
135
        std::auto_ptr<std::streambuf> filter_level( new Aqsis::filter_by_level_buf(Aqsis::DEBUG, std::cerr) );
 
136
#ifdef  AQSIS_SYSTEM_POSIX
 
137
        if( g_cl_syslog )
 
138
            std::auto_ptr<std::streambuf> use_syslog( new Aqsis::syslog_buf(std::cerr) );
 
139
#endif  // AQSIS_SYSTEM_POSIX
 
140
 
 
141
    // Pass the shader file through the slpp preprocessor first to generate a temporary file.
 
142
    if ( ap.leftovers().size() == 0 )     // If no files specified, take input from stdin.
 
143
    {
 
144
        //if ( Parse( std::cin, "stdin", std::cerr ) )
 
145
        //      codegen.OutputTree( GetParseTree(), g_stroutname );
 
146
        std::cout << ap.usagemsg();
 
147
        exit( 0 );
 
148
    }
 
149
    else
 
150
    {
 
151
        const char* _template = "slppXXXXXX";
 
152
        char ifile[11];
 
153
        for ( ArgParse::apstringvec::const_iterator e = ap.leftovers().begin(); e != ap.leftovers().end(); e++ )
 
154
        {
 
155
            FILE *file = fopen( e->c_str(), "rb" );
 
156
            if ( file != NULL )
 
157
            {
 
158
                fclose(file);
 
159
                strcpy( ifile, _template );
 
160
                char* tempname;
 
161
                                #ifdef  AQSIS_SYSTEM_WIN32
 
162
                tempname = _mktemp( ifile );
 
163
                                #else
 
164
                tempname = mktemp( ifile );
 
165
                                #endif //AQSIS_SYSTEM_WIN32
 
166
                if( NULL != tempname )
 
167
                {
 
168
                    // Build the arguments array for slpp.
 
169
                    std::vector<char*>  slppArgs;
 
170
                    for ( int defArg = 0; defArg != g_cslppDefArgs; defArg++ )
 
171
                    {
 
172
                        char* Arg = new char[strlen(g_slppDefArgs[ defArg ]) + 1];
 
173
                        sprintf( Arg, g_slppDefArgs[ defArg ] );
 
174
                        slppArgs.push_back(Arg);
 
175
                    }
 
176
 
 
177
                    // Append the -d arguments passed in to forward them to the preprocessor.
 
178
                    for ( ArgParse::apstringvec::const_iterator define = g_defines.begin(); define != g_defines.end(); define++ )
 
179
                    {
 
180
                        char* Arg = new char[strlen("-d") + 1];
 
181
                        strcpy( Arg, "-d" );
 
182
                        slppArgs.push_back(Arg);
 
183
                        Arg = new char[define->size() + 1];
 
184
                        strcpy( Arg, define->c_str() );
 
185
                        slppArgs.push_back(Arg);
 
186
                    }
 
187
 
 
188
                    // Append the -i arguments passed in to forward them to the preprocessor.
 
189
                    for ( ArgParse::apstringvec::const_iterator include = g_includes.begin(); include != g_includes.end(); include++ )
 
190
                    {
 
191
                        char* Arg = new char[strlen("-i") + 1];
 
192
                        strcpy( Arg, "-i" );
 
193
                        slppArgs.push_back(Arg);
 
194
                        Arg = new char[include->size() + 1];
 
195
                        strcpy( Arg, include->c_str() );
 
196
                        slppArgs.push_back(Arg);
 
197
                    }
 
198
 
 
199
                    // Append the -u arguments passed in to forward them to the preprocessor.
 
200
                    for ( ArgParse::apstringvec::const_iterator undefine = g_undefines.begin(); undefine != g_undefines.end(); undefine++ )
 
201
                    {
 
202
                        char* Arg = new char[strlen("-u") + 1];
 
203
                        strcpy( Arg, "-u" );
 
204
                        slppArgs.push_back(Arg);
 
205
                        Arg = new char[undefine->size() + 1];
 
206
                        strcpy( Arg, undefine->c_str() );
 
207
                        slppArgs.push_back(Arg);
 
208
                    }
 
209
 
 
210
                    // Set the output filename.
 
211
                    char* Arg = new char[strlen("-o") + 1];
 
212
                    strcpy( Arg, "-o" );
 
213
                    slppArgs.push_back(Arg);
 
214
                    Arg = new char[strlen(tempname) + 1];
 
215
                    strcpy( Arg, tempname );
 
216
                    slppArgs.push_back(Arg);
 
217
 
 
218
                    // Set the input filename.
 
219
                    char* fileArg = new char[e->size() + 1];
 
220
                    sprintf( fileArg, e->c_str() );
 
221
                    slppArgs.push_back(fileArg);
 
222
 
 
223
                    PreProcess(slppArgs.size(),&slppArgs[0]);
 
224
 
 
225
                    std::ifstream ppfile( tempname );
 
226
                    if ( Parse( ppfile, e->c_str(), std::cerr ) )
 
227
                        codegen.OutputTree( GetParseTree(), g_stroutname );
 
228
                    else
 
229
                        error = true;
 
230
 
 
231
                    // Delete the temporary file.
 
232
                    ppfile.close();
 
233
                    remove(tempname);
 
234
                }
 
235
                else
 
236
                {
 
237
                    std::cout << "Could not create temporary file for preprocessing." << std::endl;
 
238
                    exit( -1 );
 
239
                }
 
240
            }
 
241
            else
 
242
            {
 
243
                std::cout << "Warning: Cannot open file \"" << *e << "\"" << std::endl;
 
244
            }
 
245
        }
 
246
    }
 
247
 
 
248
    return error ? -1 : 0;
 
249
}
 
250