cmCPluginAPI.cxx 13.9 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
/*=========================================================================

3
  Program:   CMake - Cross-Platform Makefile Generator
Ken Martin's avatar
Ken Martin committed
4
5
6
7
8
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

9
10
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
Ken Martin's avatar
Ken Martin committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
/* 
   this file contains the implementation of the C API to CMake. Generally
   these routines just manipulate arguments and then call the associated
   methods on the CMake classes. */

#include "cmMakefile.h"
#include "cmCPluginAPI.h"

Ken Martin's avatar
Ken Martin committed
25
26
27
extern "C" 
{
  
Ken Martin's avatar
Ken Martin committed
28
29
30
31
32
33
34
35
36
37
void *cmGetClientData(void *info)
{
  return ((cmLoadedCommandInfo *)info)->ClientData;
}

void cmSetClientData(void *info, void *cd)
{
  ((cmLoadedCommandInfo *)info)->ClientData = cd;
}

Ken Martin's avatar
Ken Martin committed
38
39
40
41
42
43
44
45
46
void cmSetError(void *info, const char *err)
{
  if (((cmLoadedCommandInfo *)info)->Error)
    {
    free(((cmLoadedCommandInfo *)info)->Error);
    }
  ((cmLoadedCommandInfo *)info)->Error = strdup(err);
}

47
unsigned int cmGetCacheMajorVersion(void *arg)
Ken Martin's avatar
Ken Martin committed
48
{
49
50
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetCacheMajorVersion();
Ken Martin's avatar
Ken Martin committed
51
}
52
unsigned int cmGetCacheMinorVersion(void *arg)
Ken Martin's avatar
Ken Martin committed
53
{
54
55
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetCacheMinorVersion();
Ken Martin's avatar
Ken Martin committed
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
}

unsigned int cmGetMajorVersion(void *)
{
  return cmMakefile::GetMajorVersion();
}

unsigned int cmGetMinorVersion(void *)
{
  return cmMakefile::GetMinorVersion();
}

void cmAddDefinition(void *arg, const char* name, const char* value)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  mf->AddDefinition(name,value);
}

/* Add a definition to this makefile and the global cmake cache. */
void cmAddCacheDefinition(void *arg, const char* name, const char* value, 
                          const char* doc,
                          int type)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  
  switch (type)
    {
    case CM_CACHE_BOOL:
      mf->AddCacheDefinition(name,value,doc,
Ken Martin's avatar
Ken Martin committed
85
                             cmCacheManager::BOOL);
Ken Martin's avatar
Ken Martin committed
86
87
88
      break;
    case CM_CACHE_PATH:
      mf->AddCacheDefinition(name,value,doc,
Ken Martin's avatar
Ken Martin committed
89
                             cmCacheManager::PATH);
Ken Martin's avatar
Ken Martin committed
90
91
92
      break;
    case CM_CACHE_FILEPATH:
      mf->AddCacheDefinition(name,value,doc,
Ken Martin's avatar
Ken Martin committed
93
                             cmCacheManager::FILEPATH);
Ken Martin's avatar
Ken Martin committed
94
95
96
      break;
    case CM_CACHE_STRING:
      mf->AddCacheDefinition(name,value,doc,
Ken Martin's avatar
Ken Martin committed
97
                             cmCacheManager::STRING);
Ken Martin's avatar
Ken Martin committed
98
99
100
      break;
    case CM_CACHE_INTERNAL:
      mf->AddCacheDefinition(name,value,doc,
Ken Martin's avatar
Ken Martin committed
101
                             cmCacheManager::INTERNAL);
Ken Martin's avatar
Ken Martin committed
102
103
104
      break;
    case CM_CACHE_STATIC:
      mf->AddCacheDefinition(name,value,doc,
Ken Martin's avatar
Ken Martin committed
105
                             cmCacheManager::STATIC);
Ken Martin's avatar
Ken Martin committed
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
      break;
    }
}

const char* cmGetProjectName(void *arg)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetProjectName();
}

const char* cmGetHomeDirectory(void *arg)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetHomeDirectory();
}
const char* cmGetHomeOutputDirectory(void *arg)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetHomeOutputDirectory();
}
const char* cmGetStartDirectory(void *arg)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetStartDirectory();
}
const char* cmGetStartOutputDirectory(void *arg)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetStartOutputDirectory();
}
const char* cmGetCurrentDirectory(void *arg) 
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetCurrentDirectory();
}
const char* cmGetCurrentOutputDirectory(void *arg)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetCurrentOutputDirectory();
}
const char* cmGetDefinition(void *arg,const char*def)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return mf->GetDefinition(def);
}

int cmIsOn(void *arg, const char* name)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return static_cast<int>(mf->IsOn(name));
}

/** Check if a command exists. */
int cmCommandExists(void *arg, const char* name)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return static_cast<int>(mf->CommandExists(name));
}

void cmAddDefineFlag(void *arg, const char* definition)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  mf->AddDefineFlag(definition);
}

void cmAddLinkDirectoryForTarget(void *arg, const char *tgt, const char* d)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  mf->AddLinkDirectoryForTarget(tgt,d);
}


void cmAddExecutable(void *arg, const char *exename, 
                     int numSrcs, const char **srcs, int win32)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  std::vector<std::string> srcs2;
  int i;
  for (i = 0; i < numSrcs; ++i)
    {
    srcs2.push_back(srcs[i]);
    }
  mf->AddExecutable(exename, srcs2, (win32 ? true : false));
}

void cmAddUtilityCommand(void *arg, const char* utilityName,
                         const char* command,
                         const char* arguments,
                         int all,
                         int numDepends,
                         const char **depends,
                         int numOutputs,
                         const char **outputs)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  std::vector<std::string> depends2;
  int i;
  for (i = 0; i < numDepends; ++i)
    {
    depends2.push_back(depends[i]);
    }
  std::vector<std::string> outputs2;
  for (i = 0; i < numOutputs; ++i)
    {
    outputs2.push_back(outputs[i]);
    }
  mf->AddUtilityCommand(utilityName,command,arguments, (all ? true : false),
                        depends2, outputs2);
}
void cmAddCustomCommand(void *arg, const char* source,
                        const char* command,
                        int numArgs, const char **args,
                        int numDepends, const char **depends,
                        int numOutputs, const char **outputs,
                        const char *target)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  int i;
  std::vector<std::string> args2;
  for (i = 0; i < numArgs; ++i)
    {
    args2.push_back(args[i]);
    }
  std::vector<std::string> depends2;
  for (i = 0; i < numDepends; ++i)
    {
    depends2.push_back(depends[i]);
    }
  std::vector<std::string> outputs2;
  for (i = 0; i < numOutputs; ++i)
    {
    outputs2.push_back(outputs[i]);
    }
  mf->AddCustomCommand(source, command, args2, depends2, outputs2, target);
}


void cmAddLinkLibraryForTarget(void *arg, const char *tgt, const char*value, 
                               int libtype)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  
  switch (libtype)
    {
    case CM_LIBRARY_GENERAL:
Ken Martin's avatar
Ken Martin committed
251
      mf->AddLinkLibraryForTarget(tgt,value, cmTarget::GENERAL);
Ken Martin's avatar
Ken Martin committed
252
253
      break;
    case CM_LIBRARY_DEBUG:
Ken Martin's avatar
Ken Martin committed
254
      mf->AddLinkLibraryForTarget(tgt,value, cmTarget::DEBUG);
Ken Martin's avatar
Ken Martin committed
255
256
      break;
    case CM_LIBRARY_OPTIMIZED:
Ken Martin's avatar
Ken Martin committed
257
      mf->AddLinkLibraryForTarget(tgt,value, cmTarget::OPTIMIZED);
Ken Martin's avatar
Ken Martin committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
      break;
    }
}

void cmAddLibrary(void *arg, const char *libname, int shared,
                  int numSrcs, const char **srcs)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  std::vector<std::string> srcs2;
  int i;
  for (i = 0; i < numSrcs; ++i)
    {
    srcs2.push_back(srcs[i]);
    }
  mf->AddLibrary(libname, (shared ? true : false), srcs2);
}

char *cmExpandVariablesInString(void *arg, const char *source, 
                                int escapeQuotes, int atOnly)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
Ken Martin's avatar
Ken Martin committed
279
  std::string barf = source;
Ken Martin's avatar
Ken Martin committed
280
  std::string result = 
Ken Martin's avatar
Ken Martin committed
281
    mf->ExpandVariablesInString(barf, 
Ken Martin's avatar
Ken Martin committed
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
                                (escapeQuotes ? true : false),
                                (atOnly ? true : false));
  char *res = static_cast<char *>(malloc(result.size() + 1));
  if (result.size())
    {
    strcpy(res,result.c_str());
    }
  res[result.size()] = '\0';
  return res;
}


void cmExecuteCommand(void *arg, const char *name, 
                      int numArgs, const char **args)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  std::vector<std::string> args2;
  int i;
  for (i = 0; i < numArgs; ++i)
    {
    args2.push_back(args[i]);
    }
  mf->ExecuteCommand(std::string(name), args2);
}

void cmExpandSourceListArguments(void *arg, 
                                 int numArgs,
                                 const char **args,
                                 int *resArgc,
                                 char ***resArgv,
                                 unsigned int startArgumentIndex)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  std::vector<std::string> result;
  std::vector<std::string> args2;
  int i;
  for (i = 0; i < numArgs; ++i)
    {
    args2.push_back(args[i]);
    }
  mf->ExpandSourceListArguments(args2, result, startArgumentIndex);
Ken Martin's avatar
Ken Martin committed
323
  int resargc = static_cast<int>(result.size());
324
  char **resargv = 0;
Ken Martin's avatar
Ken Martin committed
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
  if (resargc)
    {
    resargv = (char **)malloc(resargc*sizeof(char *));
    }
  for (i = 0; i < resargc; ++i)
    {
    resargv[i] = strdup(result[i].c_str());
    }
  *resArgc = resargc;
  *resArgv = resargv;
}

void cmFreeArguments(int argc, char **argv)
{
  int i;
  for (i = 0; i < argc; ++i)
    {
    free(argv[i]);
    }
  if (argv)
    {
    free(argv);
    }
}

int cmGetTotalArgumentSize(int argc, char **argv)
{
  int i;
  int result = 0;
  for (i = 0; i < argc; ++i)
    {
    if (argv[i])
      {
Ken Martin's avatar
Ken Martin committed
358
      result = result + static_cast<int>(strlen(argv[i]));
Ken Martin's avatar
Ken Martin committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
      }
    }
  return result;
}

void *cmGetSource(void *arg, const char *name)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  return (void *)mf->GetSource(name);
}

void * cmAddSource(void *arg, void *arg2)
{
  cmMakefile *mf = static_cast<cmMakefile *>(arg);
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg2);
  return (void *)mf->AddSource(*sf);
}


void * cmCreateSourceFile()
{
  return (void *)(new cmSourceFile);
}

Ken Martin's avatar
Ken Martin committed
383
384
385
386
387
388
void cmDestroySourceFile(void *arg)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  delete sf;
}

Ken Martin's avatar
Ken Martin committed
389
390
391
392
393
394
const char *cmSourceFileGetSourceName(void *arg)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  return sf->GetSourceName().c_str();
}

Ken Martin's avatar
Ken Martin committed
395
396
397
398
399
400
const char *cmSourceFileGetFullPath(void *arg)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  return sf->GetFullPath().c_str();
}

Ken Martin's avatar
Ken Martin committed
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const char *cmSourceFileGetProperty(void *arg,const char *prop)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  return sf->GetProperty(prop);
}

int cmSourceFileGetPropertyAsBool(void *arg,const char *prop)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  return (sf->GetPropertyAsBool(prop) ? 1: 0);
}

void cmSourceFileSetProperty(void *arg,const char *prop, const char *val)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  sf->SetProperty(prop,val);
}

void cmSourceFileAddDepend(void *arg, const char *depend)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  sf->GetDepends().push_back(depend);
}

void cmSourceFileSetName(void *arg, const char* name, const char* dir,
                         int numSourceExtensions,
                         const char **sourceExtensions,
                         int numHeaderExtensions,
                         const char **headerExtensions)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  std::vector<std::string> srcs;
  std::vector<std::string> hdrs;
  int i;
  for (i = 0; i < numSourceExtensions; ++i)
    {
    srcs.push_back(sourceExtensions[i]);
    }
  for (i = 0; i < numHeaderExtensions; ++i)
    {
    hdrs.push_back(headerExtensions[i]);
    }
  sf->SetName(name,dir, srcs, hdrs);
}

void cmSourceFileSetName2(void *arg, const char* name, const char* dir, 
                          const char *ext, int headerFileOnly)
{
  cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  sf->SetName(name,dir,ext,(headerFileOnly ? true : false));
}


char *cmGetFilenameWithoutExtension(const char *name)
{
  std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  char *result = (char *)malloc(sres.size()+1);  
  strcpy(result,sres.c_str());
  return result;
}

Ken Martin's avatar
Ken Martin committed
462
463
464
465
466
467
468
469
char *cmGetFilenamePath(const char *name)
{
  std::string sres = cmSystemTools::GetFilenamePath(name);
  char *result = (char *)malloc(sres.size()+1);  
  strcpy(result,sres.c_str());
  return result;
}

Ken Martin's avatar
Ken Martin committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
char *cmCapitalized(const char *name)
{
  std::string sres = cmSystemTools::Capitalized(name);
  char *result = (char *)malloc(sres.size()+1);  
  strcpy(result,sres.c_str());
  return result;
}

void cmCopyFileIfDifferent(const char *name1, const char *name2)
{
  cmSystemTools::CopyFileIfDifferent(name1,name2);
}

void cmRemoveFile(const char *name)
{
  cmSystemTools::RemoveFile(name);
}

Ken Martin's avatar
Ken Martin committed
488
489
490
491
492
void cmFree(void *data)
{
  free(data);
}

Ken Martin's avatar
Ken Martin committed
493
494
} // close the extern "C" scope

Ken Martin's avatar
Ken Martin committed
495
496
497
cmCAPI cmStaticCAPI =
{
  cmGetClientData,
Ken Martin's avatar
Ken Martin committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  cmGetTotalArgumentSize,
  cmFreeArguments,
  cmSetClientData,
  cmSetError,
  cmAddCacheDefinition,
  cmAddCustomCommand,
  cmAddDefineFlag,
  cmAddDefinition,
  cmAddExecutable,
  cmAddLibrary,
  cmAddLinkDirectoryForTarget,
  cmAddLinkLibraryForTarget,
  cmAddUtilityCommand,
  cmCommandExists,
  cmExecuteCommand,
  cmExpandSourceListArguments,
  cmExpandVariablesInString,
  cmGetCacheMajorVersion,
  cmGetCacheMinorVersion,
  cmGetCurrentDirectory,
  cmGetCurrentOutputDirectory,
  cmGetDefinition,
  cmGetHomeDirectory,
  cmGetHomeOutputDirectory,
  cmGetMajorVersion,
  cmGetMinorVersion,
  cmGetProjectName,
  cmGetStartDirectory,
  cmGetStartOutputDirectory,
  cmIsOn,
  
  cmAddSource,
  cmCreateSourceFile,
Ken Martin's avatar
Ken Martin committed
531
  cmDestroySourceFile,
Ken Martin's avatar
Ken Martin committed
532
533
534
535
536
  cmGetSource,
  cmSourceFileAddDepend,
  cmSourceFileGetProperty,
  cmSourceFileGetPropertyAsBool,
  cmSourceFileGetSourceName,
Ken Martin's avatar
Ken Martin committed
537
  cmSourceFileGetFullPath,
Ken Martin's avatar
Ken Martin committed
538
539
540
  cmSourceFileSetName,
  cmSourceFileSetName2,
  cmSourceFileSetProperty,
Ken Martin's avatar
Ken Martin committed
541
  
Ken Martin's avatar
Ken Martin committed
542
543
544
  cmCapitalized,
  cmCopyFileIfDifferent,
  cmGetFilenameWithoutExtension,
Ken Martin's avatar
Ken Martin committed
545
  cmGetFilenamePath,
Ken Martin's avatar
Ken Martin committed
546
  cmRemoveFile,
Ken Martin's avatar
Ken Martin committed
547
  cmFree,
Ken Martin's avatar
Ken Martin committed
548
549
};