summaryrefslogtreecommitdiff
path: root/soldep/bootstrp/XmlBuildList.cxx
blob: 670a3eb7f771aa4f8025b9af9ec99a18cf6b114b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: XmlBuildList.cxx,v $
 * $Revision: 1.5 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include <EXTERN.h>               /* from the Perl distribution     */

#ifdef WNT
#include <tools/prewin.h>
#include "perl.h"
#include <tools/postwin.h>
#else
#include "perl.h"
#endif
#undef Copy                     /* from Perl handy.h */

#include <soldep/XmlBuildList.hxx>
#include "XmlBuildListDef.hxx"

#ifdef __cplusplus
#  define EXTERN_C extern "C"
#else
#  define EXTERN_C extern
#endif

static PerlInterpreter *my_perl;  /***    The Perl interpreter    ***/
static const char * DEP_MD_SIMPLE_STR = "md-simple";

EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
static void xs_init (pTHX);
static void dl_init (pTHX);

static void xs_init(pTHX)
{
        char *file = __FILE__;
        dXSUB_SYS;
        {
        /* DynaLoader is a special case */
        newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
        }
}

static void dl_init(pTHX)
{
    char *file = __FILE__;
    dTARG;
    dSP;
/* Dynamicboot strapping code*/
        SAVETMPS;
        targ=sv_newmortal();
        FREETMPS;
/* end Dynamic bootstrapping code */
#ifdef MACOSX
    *file=0; // how does this works???
    sp=0;
#endif
}

FullByteStringList::FullByteStringList()
                    : FullByteStingListImpl(),
                    nRef (0)
{
}

FullByteStringList::~FullByteStringList()
{
    ByteString* pStr = First();
    while (pStr)
    {
        delete pStr;
        pStr = Next();
    }
}

ULONG FullByteStringList::GetPos (ByteString& rStr)
{
    ULONG nCurPos = GetCurPos();
    ULONG nPos = 0;
    ByteString* pStr = First();
    while (pStr)
    {
        if (*pStr == rStr)
        {
            GetObject (nCurPos);
            return nPos;
        }
        pStr = Next();
        nPos ++;
    }
    GetObject (nCurPos);
    return LIST_ENTRY_NOTFOUND;
}

FullByteStringListWrapper& FullByteStringListWrapper::operator=( const FullByteStringListWrapper& rFullByteStringListWrapper )
{
    if (pStringList)
        if (pStringList->GetRef() == 0)
            delete pStringList;
        else
            pStringList->DecRef();

    pStringList = rFullByteStringListWrapper.GetStringList();
    if (pStringList)
        pStringList->IncRef();
    return *this;
}

FullByteStringListWrapper::~FullByteStringListWrapper ()
{
    if (pStringList)
    {
        if (pStringList->GetRef() == 0)
            delete pStringList;
        else
            pStringList->DecRef();
    }
}

//
// Function initializes Perl
// ATTENTION: No check built in - YET TO DO
//
void XmlBuildList::initPerl(const char* ModulePath) {
    if (PerlInitialized)
        return;
    my_perl = perl_alloc();
    if (!my_perl)
        throw XmlBuildListException("Cannot initialize perl");
    perl_construct(my_perl);
    char* embedding[] = {"", "-e", "0"};
    int exitstatus = perl_parse(my_perl, xs_init, 3, embedding, (char **)NULL);
    if (!exitstatus) {
        dl_init(aTHX);
        exitstatus = perl_run(my_perl);
    }
    //perl_run(my_perl);
    // Create a variable (s. perlguts)
    SV* sv = get_sv("main::module_path", TRUE);
    sv_setpv(sv, ModulePath);
    eval_pv("use lib $main::module_path; use XMLBuildListParser;", FALSE);
    checkOperationSuccess();
    eval_pv("$main::build_lst_obj = XMLBuildListParser->new();", FALSE);
    checkOperationSuccess();
    eval_pv("@array = ();", FALSE);
    checkOperationSuccess();
    PerlInitialized = TRUE;
};

// Function proves if the $@ perl variable is set, if yes -
// last operation was unsuccessfull -> throws exception
void XmlBuildList::checkOperationSuccess() {
    char* op_result = SvPV_nolen(get_sv("main::@", FALSE));
    if (strcmp(op_result, ""))
        throw XmlBuildListException(op_result);
}

//
// Function generates a regular array with NULL as last element
// from the Perl-object @array
//
FullByteStringList* XmlBuildList::extractArray() {
    FullByteStringList* pStringList = new FullByteStringList();
    AV* theArrayObj = get_av("main::array", FALSE);
    I32 arrayLength = av_len(theArrayObj); // $#array value
    if (arrayLength == -1)
        return pStringList;

    SV** string_ptr;
    char* pStoredString;
    // populate vector with strings (char*)
    for (int i = 0; i <= arrayLength; i++) {
        string_ptr = av_fetch(theArrayObj, i, NULL);
        //pStoredString = savepv(SvPV_nolen(*string_ptr));
        pStoredString = SvPV_nolen(*string_ptr);
        ByteString* pStr = new ByteString(pStoredString);
        pStringList->Insert(pStr, LIST_APPEND);
    };
    return pStringList;
};


char* XmlBuildList::getError() {
    eval_pv("$main::string1 = $main::build_lst_obj->getError();", FALSE);
    checkOperationSuccess();
    return SvPV_nolen(get_sv("main::string1", FALSE));
};

/*****************************************************************************/
XmlBuildList::XmlBuildList(const ByteString& rModulePath)
/*****************************************************************************/
                : PerlInitialized (FALSE)
{
    initPerl(rModulePath.GetBuffer());
    string_obj1 = get_sv("main::string1", TRUE);
    string_obj2 = get_sv("main::string2", TRUE);
    string_obj3 = get_sv("main::string3", TRUE);
    if (!(string_obj1 && string_obj2 && string_obj3))
        throw XmlBuildListException("Cannot initialize Perl string objects");
};

//
// Function uninitializes Perl
//
/*****************************************************************************/
XmlBuildList::~XmlBuildList()
/*****************************************************************************/
{
    if (!PerlInitialized)
        return;
    perl_destruct(my_perl);
    perl_free(my_perl);
    PerlInitialized = FALSE;
};

/*****************************************************************************/
void XmlBuildList::loadXMLFile(const ByteString& rBuildList)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rBuildList.GetBuffer());
    eval_pv("$main::string2 = $main::build_lst_obj->loadXMLFile($main::string1);", FALSE);
    checkOperationSuccess();
    if(!SvTRUE(string_obj2)) {
        const char* Message = getError();
        throw XmlBuildListException(Message);
    };
};

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getProducts()
/*****************************************************************************/
{
    eval_pv("@array = $main::build_lst_obj->getProducts();", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
}

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getJobDirectories(const ByteString& rJobType, const ByteString& rJobPlatform)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rJobType.GetBuffer());
    sv_setpv(string_obj2, rJobPlatform.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getJobDirectories($main::string1, $main::string2);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
}

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getModuleDependencies(const ByteString& rProduct, const ByteString& rDependencyType)
/*****************************************************************************/
{
    FullByteStringList* pProducts = new FullByteStringList();
    FullByteStringListWrapper aProducts (pProducts);
    if (rProduct != "")
        aProducts.Insert (new ByteString(rProduct), LIST_APPEND);
    return getModuleDependencies(aProducts, rDependencyType);
};

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getModuleDependencies(FullByteStringListWrapper& rProducts, const ByteString& rDependencyType)
/*****************************************************************************/
{
    eval_pv("@products = ();", FALSE);
    checkOperationSuccess();
    AV* theArrayObj = get_av("main::products", FALSE);
    FullByteStringList* pProducts = rProducts.GetStringList();
    ByteString* pStr = pProducts->First();
    while (pStr)
    {
        sv_setpv(string_obj2, pStr->GetBuffer());
        av_push(theArrayObj, string_obj2);
        pStr = pProducts->Next();
    }

    sv_setpv(string_obj1, rDependencyType.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getModuleDependencies(\\@products, $main::string1);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
};

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getJobBuildReqs(const ByteString& rJobDir, const ByteString& rBuildReqPlatform)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rJobDir.GetBuffer());
    sv_setpv(string_obj2, rBuildReqPlatform.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getJobBuildReqs($main::string1, $main::string2);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
}

/*****************************************************************************/
ByteString XmlBuildList::getModuleDepType(const ByteString& rDepModuleName)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rDepModuleName.GetBuffer());
    eval_pv("$main::string1 = $main::build_lst_obj->getModuleDepType($main::string1);", FALSE);
    checkOperationSuccess();
    char* pString = SvPV_nolen(get_sv("main::string1", FALSE));
    ByteString sDependencyType(pString);
    return sDependencyType;
}

/*****************************************************************************/
sal_Bool XmlBuildList::hasModuleDepType(FullByteStringListWrapper& rProducts, const ByteString& rDependencyType)
/*****************************************************************************/
{
    FullByteStringListWrapper aDepencendModules = getModuleDependencies(rProducts, rDependencyType);
    if (aDepencendModules.Count()>0)
    {
        return sal_True;
    }
    return sal_False;
}

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getModuleDepTypes(FullByteStringListWrapper& rProducts)
/*****************************************************************************/
{
    FullByteStringList * pList = new FullByteStringList();

    ByteString aDepType = ByteString( DEP_MD_SIMPLE_STR );
    bool bHasType = hasModuleDepType(rProducts, aDepType);
    if (bHasType)
        pList->Insert(new ByteString (aDepType));

    aDepType = ByteString(DEP_MD_ALWAYS_STR);
    bHasType = hasModuleDepType(rProducts, aDepType);
    if (bHasType)
        pList->Insert(new ByteString (aDepType));

    aDepType = ByteString(DEP_MD_FORCE_STR);
    bHasType = hasModuleDepType(rProducts, aDepType);
    if (bHasType)
        pList->Insert(new ByteString (aDepType));

    return FullByteStringListWrapper (pList);
}

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getModuleProducts(const ByteString& rDepModuleName)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rDepModuleName.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getModuleProducts($main::string1);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
};

/*****************************************************************************/
ByteString XmlBuildList::getModuleName()
/*****************************************************************************/
{
    eval_pv("$main::string1 = $main::build_lst_obj->getModuleName();", FALSE);
    checkOperationSuccess();
    char* pString = SvPV_nolen(get_sv("main::string1", FALSE));
    ByteString sModuleName(pString);
    return sModuleName;
}

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getDirDependencies(const ByteString& rJobDir, const ByteString& rJobType, const ByteString& rJobPlatform)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rJobDir.GetBuffer());
    sv_setpv(string_obj2, rJobType.GetBuffer());
    sv_setpv(string_obj3, rJobPlatform.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getDirDependencies($main::string1, $main::string2, $main::string3);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
};

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getJobTypes(const ByteString& rJobDir)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rJobDir.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getJobTypes($main::string1);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
};

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getJobPlatforms(const ByteString& rJobDir)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rJobDir.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getJobPlatforms($main::string1);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
};

/*****************************************************************************/
FullByteStringListWrapper XmlBuildList::getJobBuildReqPlatforms(const ByteString& rJobDir, const ByteString& rBuildReqName)
/*****************************************************************************/
{
    sv_setpv(string_obj1, rJobDir.GetBuffer());
    sv_setpv(string_obj2, rBuildReqName.GetBuffer());
    eval_pv("@array = $main::build_lst_obj->getJobBuildReqPlatforms($main::string1, $main::string2);", FALSE);
    checkOperationSuccess();
    FullByteStringList* pList = extractArray();
    return FullByteStringListWrapper(pList);
};