cmCPluginAPI.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. /*
  11. this file contains the implementation of the C API to CMake. Generally
  12. these routines just manipulate arguments and then call the associated
  13. methods on the CMake classes. */
  14. #include "cmMakefile.h"
  15. #include "cmCPluginAPI.h"
  16. #include "cmVersion.h"
  17. #include "cmSourceFile.h"
  18. #include <stdlib.h>
  19. #ifdef __QNX__
  20. # include <malloc.h> /* for malloc/free on QNX */
  21. #endif
  22. extern "C"
  23. {
  24. void CCONV *cmGetClientData(void *info)
  25. {
  26. return ((cmLoadedCommandInfo *)info)->ClientData;
  27. }
  28. void CCONV cmSetClientData(void *info, void *cd)
  29. {
  30. ((cmLoadedCommandInfo *)info)->ClientData = cd;
  31. }
  32. void CCONV cmSetError(void *info, const char *err)
  33. {
  34. if (((cmLoadedCommandInfo *)info)->Error)
  35. {
  36. free(((cmLoadedCommandInfo *)info)->Error);
  37. }
  38. ((cmLoadedCommandInfo *)info)->Error = strdup(err);
  39. }
  40. unsigned int CCONV cmGetCacheMajorVersion(void *arg)
  41. {
  42. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  43. cmCacheManager *manager = mf->GetCMakeInstance()->GetCacheManager();
  44. return manager->GetCacheMajorVersion();
  45. }
  46. unsigned int CCONV cmGetCacheMinorVersion(void *arg)
  47. {
  48. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  49. cmCacheManager *manager = mf->GetCMakeInstance()->GetCacheManager();
  50. return manager->GetCacheMinorVersion();
  51. }
  52. unsigned int CCONV cmGetMajorVersion(void *)
  53. {
  54. return cmVersion::GetMajorVersion();
  55. }
  56. unsigned int CCONV cmGetMinorVersion(void *)
  57. {
  58. return cmVersion::GetMinorVersion();
  59. }
  60. void CCONV cmAddDefinition(void *arg, const char* name, const char* value)
  61. {
  62. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  63. mf->AddDefinition(name,value);
  64. }
  65. /* Add a definition to this makefile and the global cmake cache. */
  66. void CCONV cmAddCacheDefinition(void *arg, const char* name,
  67. const char* value, const char* doc, int type)
  68. {
  69. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  70. switch (type)
  71. {
  72. case CM_CACHE_BOOL:
  73. mf->AddCacheDefinition(name,value,doc,
  74. cmState::BOOL);
  75. break;
  76. case CM_CACHE_PATH:
  77. mf->AddCacheDefinition(name,value,doc,
  78. cmState::PATH);
  79. break;
  80. case CM_CACHE_FILEPATH:
  81. mf->AddCacheDefinition(name,value,doc,
  82. cmState::FILEPATH);
  83. break;
  84. case CM_CACHE_STRING:
  85. mf->AddCacheDefinition(name,value,doc,
  86. cmState::STRING);
  87. break;
  88. case CM_CACHE_INTERNAL:
  89. mf->AddCacheDefinition(name,value,doc,
  90. cmState::INTERNAL);
  91. break;
  92. case CM_CACHE_STATIC:
  93. mf->AddCacheDefinition(name,value,doc,
  94. cmState::STATIC);
  95. break;
  96. }
  97. }
  98. const char* CCONV cmGetProjectName(void *arg)
  99. {
  100. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  101. static std::string name;
  102. name = mf->GetStateSnapshot().GetProjectName();
  103. return name.c_str();
  104. }
  105. const char* CCONV cmGetHomeDirectory(void *arg)
  106. {
  107. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  108. return mf->GetHomeDirectory();
  109. }
  110. const char* CCONV cmGetHomeOutputDirectory(void *arg)
  111. {
  112. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  113. return mf->GetHomeOutputDirectory();
  114. }
  115. const char* CCONV cmGetStartDirectory(void *arg)
  116. {
  117. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  118. return mf->GetCurrentSourceDirectory();
  119. }
  120. const char* CCONV cmGetStartOutputDirectory(void *arg)
  121. {
  122. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  123. return mf->GetCurrentBinaryDirectory();
  124. }
  125. const char* CCONV cmGetCurrentDirectory(void *arg)
  126. {
  127. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  128. return mf->GetCurrentSourceDirectory();
  129. }
  130. const char* CCONV cmGetCurrentOutputDirectory(void *arg)
  131. {
  132. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  133. return mf->GetCurrentBinaryDirectory();
  134. }
  135. const char* CCONV cmGetDefinition(void *arg,const char*def)
  136. {
  137. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  138. return mf->GetDefinition(def);
  139. }
  140. int CCONV cmIsOn(void *arg, const char* name)
  141. {
  142. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  143. return static_cast<int>(mf->IsOn(name));
  144. }
  145. /** Check if a command exists. */
  146. int CCONV cmCommandExists(void *arg, const char* name)
  147. {
  148. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  149. return static_cast<int>(mf->GetState()->GetCommand(name) ? 1 : 0);
  150. }
  151. void CCONV cmAddDefineFlag(void *arg, const char* definition)
  152. {
  153. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  154. mf->AddDefineFlag(definition);
  155. }
  156. void CCONV cmAddLinkDirectoryForTarget(void *arg, const char *tgt,
  157. const char* d)
  158. {
  159. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  160. mf->AddLinkDirectoryForTarget(tgt,d);
  161. }
  162. void CCONV cmAddExecutable(void *arg, const char *exename,
  163. int numSrcs, const char **srcs, int win32)
  164. {
  165. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  166. std::vector<std::string> srcs2;
  167. int i;
  168. for (i = 0; i < numSrcs; ++i)
  169. {
  170. srcs2.push_back(srcs[i]);
  171. }
  172. cmTarget* tg = mf->AddExecutable(exename, srcs2);
  173. if ( win32 )
  174. {
  175. tg->SetProperty("WIN32_EXECUTABLE", "ON");
  176. }
  177. }
  178. void CCONV cmAddUtilityCommand(void *arg, const char* utilityName,
  179. const char* command,
  180. const char* arguments,
  181. int all,
  182. int numDepends,
  183. const char **depends,
  184. int,
  185. const char **)
  186. {
  187. // Get the makefile instance. Perform an extra variable expansion
  188. // now because the API caller expects it.
  189. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  190. // Construct the command line for the command.
  191. cmCustomCommandLine commandLine;
  192. std::string expand = command;
  193. commandLine.push_back(mf->ExpandVariablesInString(expand));
  194. if(arguments && arguments[0])
  195. {
  196. // TODO: Parse arguments!
  197. expand = arguments;
  198. commandLine.push_back(mf->ExpandVariablesInString(expand));
  199. }
  200. cmCustomCommandLines commandLines;
  201. commandLines.push_back(commandLine);
  202. // Accumulate the list of dependencies.
  203. std::vector<std::string> depends2;
  204. for(int i = 0; i < numDepends; ++i)
  205. {
  206. expand = depends[i];
  207. depends2.push_back(mf->ExpandVariablesInString(expand));
  208. }
  209. // Pass the call to the makefile instance.
  210. mf->AddUtilityCommand(utilityName, (all ? false : true),
  211. 0, depends2, commandLines);
  212. }
  213. void CCONV cmAddCustomCommand(void *arg, const char* source,
  214. const char* command,
  215. int numArgs, const char **args,
  216. int numDepends, const char **depends,
  217. int numOutputs, const char **outputs,
  218. const char *target)
  219. {
  220. // Get the makefile instance. Perform an extra variable expansion
  221. // now because the API caller expects it.
  222. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  223. // Construct the command line for the command.
  224. cmCustomCommandLine commandLine;
  225. std::string expand = command;
  226. commandLine.push_back(mf->ExpandVariablesInString(expand));
  227. for(int i=0; i < numArgs; ++i)
  228. {
  229. expand = args[i];
  230. commandLine.push_back(mf->ExpandVariablesInString(expand));
  231. }
  232. cmCustomCommandLines commandLines;
  233. commandLines.push_back(commandLine);
  234. // Accumulate the list of dependencies.
  235. std::vector<std::string> depends2;
  236. for(int i = 0; i < numDepends; ++i)
  237. {
  238. expand = depends[i];
  239. depends2.push_back(mf->ExpandVariablesInString(expand));
  240. }
  241. // Accumulate the list of outputs.
  242. std::vector<std::string> outputs2;
  243. for(int i = 0; i < numOutputs; ++i)
  244. {
  245. expand = outputs[i];
  246. outputs2.push_back(mf->ExpandVariablesInString(expand));
  247. }
  248. // Pass the call to the makefile instance.
  249. const char* no_comment = 0;
  250. mf->AddCustomCommandOldStyle(target, outputs2, depends2, source,
  251. commandLines, no_comment);
  252. }
  253. void CCONV cmAddCustomCommandToOutput(void *arg, const char* output,
  254. const char* command,
  255. int numArgs, const char **args,
  256. const char* main_dependency,
  257. int numDepends, const char **depends)
  258. {
  259. // Get the makefile instance. Perform an extra variable expansion
  260. // now because the API caller expects it.
  261. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  262. // Construct the command line for the command.
  263. cmCustomCommandLine commandLine;
  264. std::string expand = command;
  265. commandLine.push_back(mf->ExpandVariablesInString(expand));
  266. for(int i=0; i < numArgs; ++i)
  267. {
  268. expand = args[i];
  269. commandLine.push_back(mf->ExpandVariablesInString(expand));
  270. }
  271. cmCustomCommandLines commandLines;
  272. commandLines.push_back(commandLine);
  273. // Accumulate the list of dependencies.
  274. std::vector<std::string> depends2;
  275. for(int i = 0; i < numDepends; ++i)
  276. {
  277. expand = depends[i];
  278. depends2.push_back(mf->ExpandVariablesInString(expand));
  279. }
  280. // Pass the call to the makefile instance.
  281. const char* no_comment = 0;
  282. const char* no_working_dir = 0;
  283. mf->AddCustomCommandToOutput(output, depends2, main_dependency,
  284. commandLines, no_comment, no_working_dir);
  285. }
  286. void CCONV cmAddCustomCommandToTarget(void *arg, const char* target,
  287. const char* command,
  288. int numArgs, const char **args,
  289. int commandType)
  290. {
  291. // Get the makefile instance.
  292. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  293. // Construct the command line for the command. Perform an extra
  294. // variable expansion now because the API caller expects it.
  295. cmCustomCommandLine commandLine;
  296. std::string expand = command;
  297. commandLine.push_back(mf->ExpandVariablesInString(expand));
  298. for(int i=0; i < numArgs; ++i)
  299. {
  300. expand = args[i];
  301. commandLine.push_back(mf->ExpandVariablesInString(expand));
  302. }
  303. cmCustomCommandLines commandLines;
  304. commandLines.push_back(commandLine);
  305. // Select the command type.
  306. cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
  307. switch (commandType)
  308. {
  309. case CM_PRE_BUILD:
  310. cctype = cmTarget::PRE_BUILD;
  311. break;
  312. case CM_PRE_LINK:
  313. cctype = cmTarget::PRE_LINK;
  314. break;
  315. case CM_POST_BUILD:
  316. cctype = cmTarget::POST_BUILD;
  317. break;
  318. }
  319. // Pass the call to the makefile instance.
  320. std::vector<std::string> no_byproducts;
  321. std::vector<std::string> no_depends;
  322. const char* no_comment = 0;
  323. const char* no_working_dir = 0;
  324. mf->AddCustomCommandToTarget(target, no_byproducts, no_depends, commandLines,
  325. cctype, no_comment, no_working_dir);
  326. }
  327. void CCONV cmAddLinkLibraryForTarget(void *arg, const char *tgt,
  328. const char*value, int libtype)
  329. {
  330. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  331. switch (libtype)
  332. {
  333. case CM_LIBRARY_GENERAL:
  334. mf->AddLinkLibraryForTarget(tgt,value, cmTarget::GENERAL);
  335. break;
  336. case CM_LIBRARY_DEBUG:
  337. mf->AddLinkLibraryForTarget(tgt,value, cmTarget::DEBUG);
  338. break;
  339. case CM_LIBRARY_OPTIMIZED:
  340. mf->AddLinkLibraryForTarget(tgt,value, cmTarget::OPTIMIZED);
  341. break;
  342. }
  343. }
  344. void CCONV cmAddLibrary(void *arg, const char *libname, int shared,
  345. int numSrcs, const char **srcs)
  346. {
  347. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  348. std::vector<std::string> srcs2;
  349. int i;
  350. for (i = 0; i < numSrcs; ++i)
  351. {
  352. srcs2.push_back(srcs[i]);
  353. }
  354. mf->AddLibrary(libname,
  355. (shared? cmTarget::SHARED_LIBRARY : cmTarget::STATIC_LIBRARY),
  356. srcs2);
  357. }
  358. char CCONV *cmExpandVariablesInString(void *arg, const char *source,
  359. int escapeQuotes, int atOnly)
  360. {
  361. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  362. std::string barf = source;
  363. std::string result =
  364. mf->ExpandVariablesInString(barf,
  365. (escapeQuotes ? true : false),
  366. (atOnly ? true : false));
  367. char *res = static_cast<char *>(malloc(result.size() + 1));
  368. if (result.size())
  369. {
  370. strcpy(res,result.c_str());
  371. }
  372. res[result.size()] = '\0';
  373. return res;
  374. }
  375. int CCONV cmExecuteCommand(void *arg, const char *name,
  376. int numArgs, const char **args)
  377. {
  378. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  379. cmListFileFunction lff;
  380. lff.Name = name;
  381. for(int i = 0; i < numArgs; ++i)
  382. {
  383. // Assume all arguments are quoted.
  384. lff.Arguments.push_back(
  385. cmListFileArgument(args[i], cmListFileArgument::Quoted, 0));
  386. }
  387. cmExecutionStatus status;
  388. return mf->ExecuteCommand(lff,status);
  389. }
  390. void CCONV cmExpandSourceListArguments(void *arg,
  391. int numArgs,
  392. const char **args,
  393. int *resArgc,
  394. char ***resArgv,
  395. unsigned int startArgumentIndex)
  396. {
  397. (void)arg;
  398. (void)startArgumentIndex;
  399. std::vector<std::string> result;
  400. int i;
  401. for (i = 0; i < numArgs; ++i)
  402. {
  403. result.push_back(args[i]);
  404. }
  405. int resargc = static_cast<int>(result.size());
  406. char **resargv = 0;
  407. if (resargc)
  408. {
  409. resargv = (char **)malloc(resargc*sizeof(char *));
  410. }
  411. for (i = 0; i < resargc; ++i)
  412. {
  413. resargv[i] = strdup(result[i].c_str());
  414. }
  415. *resArgc = resargc;
  416. *resArgv = resargv;
  417. }
  418. void CCONV cmFreeArguments(int argc, char **argv)
  419. {
  420. int i;
  421. for (i = 0; i < argc; ++i)
  422. {
  423. free(argv[i]);
  424. }
  425. if (argv)
  426. {
  427. free(argv);
  428. }
  429. }
  430. int CCONV cmGetTotalArgumentSize(int argc, char **argv)
  431. {
  432. int i;
  433. int result = 0;
  434. for (i = 0; i < argc; ++i)
  435. {
  436. if (argv[i])
  437. {
  438. result = result + static_cast<int>(strlen(argv[i]));
  439. }
  440. }
  441. return result;
  442. }
  443. // Source file proxy object to support the old cmSourceFile/cmMakefile
  444. // API for source files.
  445. struct cmCPluginAPISourceFile
  446. {
  447. cmCPluginAPISourceFile(): RealSourceFile(0) {}
  448. cmSourceFile* RealSourceFile;
  449. std::string SourceName;
  450. std::string SourceExtension;
  451. std::string FullPath;
  452. std::vector<std::string> Depends;
  453. cmPropertyMap Properties;
  454. };
  455. // Keep a map from real cmSourceFile instances stored in a makefile to
  456. // the CPluginAPI proxy source file.
  457. class cmCPluginAPISourceFileMap:
  458. public std::map<cmSourceFile*, cmCPluginAPISourceFile*>
  459. {
  460. public:
  461. typedef std::map<cmSourceFile*, cmCPluginAPISourceFile*> derived;
  462. typedef derived::iterator iterator;
  463. typedef derived::value_type value_type;
  464. ~cmCPluginAPISourceFileMap()
  465. {
  466. for(iterator i=this->begin(); i != this->end(); ++i)
  467. {
  468. delete i->second;
  469. }
  470. }
  471. };
  472. cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
  473. void * CCONV cmCreateSourceFile(void)
  474. {
  475. return (void*)new cmCPluginAPISourceFile;
  476. }
  477. void * CCONV cmCreateNewSourceFile(void *)
  478. {
  479. cmCPluginAPISourceFile *sf = new cmCPluginAPISourceFile;
  480. return (void*)sf;
  481. }
  482. void CCONV cmDestroySourceFile(void *arg)
  483. {
  484. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  485. // Only delete if it was created by cmCreateSourceFile or
  486. // cmCreateNewSourceFile and is therefore not in the map.
  487. if(!sf->RealSourceFile)
  488. {
  489. delete sf;
  490. }
  491. }
  492. void CCONV *cmGetSource(void *arg, const char *name)
  493. {
  494. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  495. if(cmSourceFile* rsf = mf->GetSource(name))
  496. {
  497. // Lookup the proxy source file object for this source.
  498. cmCPluginAPISourceFileMap::iterator i = cmCPluginAPISourceFiles.find(rsf);
  499. if(i == cmCPluginAPISourceFiles.end())
  500. {
  501. // Create a proxy source file object for this source.
  502. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  503. sf->RealSourceFile = rsf;
  504. sf->FullPath = rsf->GetFullPath();
  505. sf->SourceName =
  506. cmSystemTools::GetFilenameWithoutLastExtension(sf->FullPath);
  507. sf->SourceExtension =
  508. cmSystemTools::GetFilenameLastExtension(sf->FullPath);
  509. // Store the proxy in the map so it can be re-used and deleted later.
  510. cmCPluginAPISourceFileMap::value_type entry(rsf, sf);
  511. i = cmCPluginAPISourceFiles.insert(entry).first;
  512. }
  513. return (void *)i->second;
  514. }
  515. else
  516. {
  517. return 0;
  518. }
  519. }
  520. void * CCONV cmAddSource(void *arg, void *arg2)
  521. {
  522. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  523. cmCPluginAPISourceFile* osf = static_cast<cmCPluginAPISourceFile*>(arg2);
  524. if(osf->FullPath.empty())
  525. {
  526. return 0;
  527. }
  528. // Create the real cmSourceFile instance and copy over saved information.
  529. cmSourceFile* rsf = mf->GetOrCreateSource(osf->FullPath);
  530. rsf->GetProperties() = osf->Properties;
  531. for(std::vector<std::string>::iterator i = osf->Depends.begin();
  532. i != osf->Depends.end(); ++i)
  533. {
  534. rsf->AddDepend(i->c_str());
  535. }
  536. // Create the proxy for the real source file.
  537. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  538. sf->RealSourceFile = rsf;
  539. sf->FullPath = osf->FullPath;
  540. sf->SourceName = osf->SourceName;
  541. sf->SourceExtension = osf->SourceExtension;
  542. // Store the proxy in the map so it can be re-used and deleted later.
  543. cmCPluginAPISourceFiles[rsf] = sf;
  544. return (void *)sf;
  545. }
  546. const char * CCONV cmSourceFileGetSourceName(void *arg)
  547. {
  548. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  549. return sf->SourceName.c_str();
  550. }
  551. const char * CCONV cmSourceFileGetFullPath(void *arg)
  552. {
  553. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  554. return sf->FullPath.c_str();
  555. }
  556. const char * CCONV cmSourceFileGetProperty(void *arg,const char *prop)
  557. {
  558. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  559. if(cmSourceFile* rsf = sf->RealSourceFile)
  560. {
  561. return rsf->GetProperty(prop);
  562. }
  563. else
  564. {
  565. if(!strcmp(prop,"LOCATION"))
  566. {
  567. return sf->FullPath.c_str();
  568. }
  569. return sf->Properties.GetPropertyValue(prop);
  570. }
  571. }
  572. int CCONV cmSourceFileGetPropertyAsBool(void *arg,const char *prop)
  573. {
  574. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  575. if(cmSourceFile* rsf = sf->RealSourceFile)
  576. {
  577. return rsf->GetPropertyAsBool(prop) ? 1:0;
  578. }
  579. else
  580. {
  581. return cmSystemTools::IsOn(cmSourceFileGetProperty(arg, prop))? 1:0;
  582. }
  583. }
  584. void CCONV cmSourceFileSetProperty(void *arg,const char *prop,
  585. const char *value)
  586. {
  587. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  588. if(cmSourceFile* rsf = sf->RealSourceFile)
  589. {
  590. rsf->SetProperty(prop, value);
  591. }
  592. else if(prop)
  593. {
  594. if(!value) { value = "NOTFOUND"; }
  595. sf->Properties.SetProperty(prop, value);
  596. }
  597. }
  598. void CCONV cmSourceFileAddDepend(void *arg, const char *depend)
  599. {
  600. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  601. if(cmSourceFile* rsf = sf->RealSourceFile)
  602. {
  603. rsf->AddDepend(depend);
  604. }
  605. else
  606. {
  607. sf->Depends.push_back(depend);
  608. }
  609. }
  610. void CCONV cmSourceFileSetName(void *arg, const char* name, const char* dir,
  611. int numSourceExtensions,
  612. const char **sourceExtensions,
  613. int numHeaderExtensions,
  614. const char **headerExtensions)
  615. {
  616. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  617. if(sf->RealSourceFile)
  618. {
  619. // SetName is allowed only on temporary source files created by
  620. // the command for building and passing to AddSource.
  621. return;
  622. }
  623. std::vector<std::string> sourceExts;
  624. std::vector<std::string> headerExts;
  625. int i;
  626. for (i = 0; i < numSourceExtensions; ++i)
  627. {
  628. sourceExts.push_back(sourceExtensions[i]);
  629. }
  630. for (i = 0; i < numHeaderExtensions; ++i)
  631. {
  632. headerExts.push_back(headerExtensions[i]);
  633. }
  634. // Save the original name given.
  635. sf->SourceName = name;
  636. // Convert the name to a full path in case the given name is a
  637. // relative path.
  638. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  639. // First try and see whether the listed file can be found
  640. // as is without extensions added on.
  641. std::string hname = pathname;
  642. if(cmSystemTools::FileExists(hname.c_str()))
  643. {
  644. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  645. if ( sf->SourceName.size() > 0 )
  646. {
  647. sf->SourceName += "/";
  648. }
  649. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  650. std::string::size_type pos = hname.rfind('.');
  651. if(pos != std::string::npos)
  652. {
  653. sf->SourceExtension = hname.substr(pos+1, hname.size()-pos);
  654. if ( cmSystemTools::FileIsFullPath(name) )
  655. {
  656. std::string::size_type pos2 = hname.rfind('/');
  657. if(pos2 != std::string::npos)
  658. {
  659. sf->SourceName = hname.substr(pos2+1, pos - pos2-1);
  660. }
  661. }
  662. }
  663. sf->FullPath = hname;
  664. return;
  665. }
  666. // Next, try the various source extensions
  667. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  668. ext != sourceExts.end(); ++ext )
  669. {
  670. hname = pathname;
  671. hname += ".";
  672. hname += *ext;
  673. if(cmSystemTools::FileExists(hname.c_str()))
  674. {
  675. sf->SourceExtension = *ext;
  676. sf->FullPath = hname;
  677. return;
  678. }
  679. }
  680. // Finally, try the various header extensions
  681. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  682. ext != headerExts.end(); ++ext )
  683. {
  684. hname = pathname;
  685. hname += ".";
  686. hname += *ext;
  687. if(cmSystemTools::FileExists(hname.c_str()))
  688. {
  689. sf->SourceExtension = *ext;
  690. sf->FullPath = hname;
  691. return;
  692. }
  693. }
  694. std::ostringstream e;
  695. e << "Cannot find source file \"" << pathname << "\"";
  696. e << "\n\nTried extensions";
  697. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  698. ext != sourceExts.end(); ++ext )
  699. {
  700. e << " ." << *ext;
  701. }
  702. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  703. ext != headerExts.end(); ++ext )
  704. {
  705. e << " ." << *ext;
  706. }
  707. cmSystemTools::Error(e.str().c_str());
  708. return;
  709. }
  710. void CCONV cmSourceFileSetName2(void *arg, const char* name, const char* dir,
  711. const char *ext, int headerFileOnly)
  712. {
  713. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  714. if(sf->RealSourceFile)
  715. {
  716. // SetName is allowed only on temporary source files created by
  717. // the command for building and passing to AddSource.
  718. return;
  719. }
  720. // Implement the old SetName method code here.
  721. if(headerFileOnly)
  722. {
  723. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1");
  724. }
  725. sf->SourceName = name;
  726. std::string fname = sf->SourceName;
  727. if(ext && strlen(ext))
  728. {
  729. fname += ".";
  730. fname += ext;
  731. }
  732. sf->FullPath = cmSystemTools::CollapseFullPath(fname.c_str(), dir);
  733. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  734. sf->SourceExtension = ext;
  735. }
  736. char * CCONV cmGetFilenameWithoutExtension(const char *name)
  737. {
  738. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  739. char *result = (char *)malloc(sres.size()+1);
  740. strcpy(result,sres.c_str());
  741. return result;
  742. }
  743. char * CCONV cmGetFilenamePath(const char *name)
  744. {
  745. std::string sres = cmSystemTools::GetFilenamePath(name);
  746. char *result = (char *)malloc(sres.size()+1);
  747. strcpy(result,sres.c_str());
  748. return result;
  749. }
  750. char * CCONV cmCapitalized(const char *name)
  751. {
  752. std::string sres = cmSystemTools::Capitalized(name);
  753. char *result = (char *)malloc(sres.size()+1);
  754. strcpy(result,sres.c_str());
  755. return result;
  756. }
  757. void CCONV cmCopyFileIfDifferent(const char *name1, const char *name2)
  758. {
  759. cmSystemTools::CopyFileIfDifferent(name1,name2);
  760. }
  761. void CCONV cmRemoveFile(const char *name)
  762. {
  763. cmSystemTools::RemoveFile(name);
  764. }
  765. void CCONV cmDisplayStatus(void *arg, const char* message)
  766. {
  767. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  768. mf->DisplayStatus(message, -1);
  769. }
  770. void CCONV cmFree(void *data)
  771. {
  772. free(data);
  773. }
  774. void CCONV DefineSourceFileProperty (void *arg, const char *name,
  775. const char *briefDocs,
  776. const char *longDocs,
  777. int chained)
  778. {
  779. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  780. mf->GetState()->DefineProperty(name,cmProperty::SOURCE_FILE,
  781. briefDocs, longDocs,
  782. chained != 0);
  783. }
  784. } // close the extern "C" scope
  785. cmCAPI cmStaticCAPI =
  786. {
  787. cmGetClientData,
  788. cmGetTotalArgumentSize,
  789. cmFreeArguments,
  790. cmSetClientData,
  791. cmSetError,
  792. cmAddCacheDefinition,
  793. cmAddCustomCommand,
  794. cmAddDefineFlag,
  795. cmAddDefinition,
  796. cmAddExecutable,
  797. cmAddLibrary,
  798. cmAddLinkDirectoryForTarget,
  799. cmAddLinkLibraryForTarget,
  800. cmAddUtilityCommand,
  801. cmCommandExists,
  802. cmExecuteCommand,
  803. cmExpandSourceListArguments,
  804. cmExpandVariablesInString,
  805. cmGetCacheMajorVersion,
  806. cmGetCacheMinorVersion,
  807. cmGetCurrentDirectory,
  808. cmGetCurrentOutputDirectory,
  809. cmGetDefinition,
  810. cmGetHomeDirectory,
  811. cmGetHomeOutputDirectory,
  812. cmGetMajorVersion,
  813. cmGetMinorVersion,
  814. cmGetProjectName,
  815. cmGetStartDirectory,
  816. cmGetStartOutputDirectory,
  817. cmIsOn,
  818. cmAddSource,
  819. cmCreateSourceFile,
  820. cmDestroySourceFile,
  821. cmGetSource,
  822. cmSourceFileAddDepend,
  823. cmSourceFileGetProperty,
  824. cmSourceFileGetPropertyAsBool,
  825. cmSourceFileGetSourceName,
  826. cmSourceFileGetFullPath,
  827. cmSourceFileSetName,
  828. cmSourceFileSetName2,
  829. cmSourceFileSetProperty,
  830. cmCapitalized,
  831. cmCopyFileIfDifferent,
  832. cmGetFilenameWithoutExtension,
  833. cmGetFilenamePath,
  834. cmRemoveFile,
  835. cmFree,
  836. cmAddCustomCommandToOutput,
  837. cmAddCustomCommandToTarget,
  838. cmDisplayStatus,
  839. cmCreateNewSourceFile,
  840. DefineSourceFileProperty,
  841. };