cmCPluginAPI.cxx 26 KB

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