cmCPluginAPI.cxx 26 KB

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