cmCPluginAPI.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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(cmListFileArgument(args[i], true,
  380. "[CMake-Plugin]", 0));
  381. }
  382. cmExecutionStatus status;
  383. return mf->ExecuteCommand(lff,status);
  384. }
  385. void CCONV cmExpandSourceListArguments(void *arg,
  386. int numArgs,
  387. const char **args,
  388. int *resArgc,
  389. char ***resArgv,
  390. unsigned int startArgumentIndex)
  391. {
  392. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  393. std::vector<std::string> result;
  394. std::vector<std::string> args2;
  395. int i;
  396. for (i = 0; i < numArgs; ++i)
  397. {
  398. args2.push_back(args[i]);
  399. }
  400. mf->ExpandSourceListArguments(args2, result, startArgumentIndex);
  401. int resargc = static_cast<int>(result.size());
  402. char **resargv = 0;
  403. if (resargc)
  404. {
  405. resargv = (char **)malloc(resargc*sizeof(char *));
  406. }
  407. for (i = 0; i < resargc; ++i)
  408. {
  409. resargv[i] = strdup(result[i].c_str());
  410. }
  411. *resArgc = resargc;
  412. *resArgv = resargv;
  413. }
  414. void CCONV cmFreeArguments(int argc, char **argv)
  415. {
  416. int i;
  417. for (i = 0; i < argc; ++i)
  418. {
  419. free(argv[i]);
  420. }
  421. if (argv)
  422. {
  423. free(argv);
  424. }
  425. }
  426. int CCONV cmGetTotalArgumentSize(int argc, char **argv)
  427. {
  428. int i;
  429. int result = 0;
  430. for (i = 0; i < argc; ++i)
  431. {
  432. if (argv[i])
  433. {
  434. result = result + static_cast<int>(strlen(argv[i]));
  435. }
  436. }
  437. return result;
  438. }
  439. // Source file proxy object to support the old cmSourceFile/cmMakefile
  440. // API for source files.
  441. struct cmCPluginAPISourceFile
  442. {
  443. cmCPluginAPISourceFile(): RealSourceFile(0) {}
  444. cmSourceFile* RealSourceFile;
  445. std::string SourceName;
  446. std::string SourceExtension;
  447. std::string FullPath;
  448. std::vector<std::string> Depends;
  449. cmPropertyMap Properties;
  450. };
  451. // Keep a map from real cmSourceFile instances stored in a makefile to
  452. // the CPluginAPI proxy source file.
  453. class cmCPluginAPISourceFileMap:
  454. public std::map<cmSourceFile*, cmCPluginAPISourceFile*>
  455. {
  456. public:
  457. typedef std::map<cmSourceFile*, cmCPluginAPISourceFile*> derived;
  458. typedef derived::iterator iterator;
  459. typedef derived::value_type value_type;
  460. ~cmCPluginAPISourceFileMap()
  461. {
  462. for(iterator i=this->begin(); i != this->end(); ++i)
  463. {
  464. delete i->second;
  465. }
  466. }
  467. };
  468. cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
  469. void * CCONV cmCreateSourceFile(void)
  470. {
  471. return (void*)new cmCPluginAPISourceFile;
  472. }
  473. void * CCONV cmCreateNewSourceFile(void *arg)
  474. {
  475. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  476. cmCPluginAPISourceFile *sf = new cmCPluginAPISourceFile;
  477. sf->Properties.SetCMakeInstance(mf->GetCMakeInstance());
  478. return (void*)sf;
  479. }
  480. void CCONV cmDestroySourceFile(void *arg)
  481. {
  482. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  483. // Only delete if it was created by cmCreateSourceFile or
  484. // cmCreateNewSourceFile and is therefore not in the map.
  485. if(!sf->RealSourceFile)
  486. {
  487. delete sf;
  488. }
  489. }
  490. void CCONV *cmGetSource(void *arg, const char *name)
  491. {
  492. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  493. if(cmSourceFile* rsf = mf->GetSource(name))
  494. {
  495. // Lookup the proxy source file object for this source.
  496. cmCPluginAPISourceFileMap::iterator i = cmCPluginAPISourceFiles.find(rsf);
  497. if(i == cmCPluginAPISourceFiles.end())
  498. {
  499. // Create a proxy source file object for this source.
  500. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  501. sf->RealSourceFile = rsf;
  502. sf->FullPath = rsf->GetFullPath();
  503. sf->SourceName =
  504. cmSystemTools::GetFilenameWithoutLastExtension(sf->FullPath.c_str());
  505. sf->SourceExtension =
  506. cmSystemTools::GetFilenameLastExtension(sf->FullPath.c_str());
  507. // Store the proxy in the map so it can be re-used and deleted later.
  508. cmCPluginAPISourceFileMap::value_type entry(rsf, sf);
  509. i = cmCPluginAPISourceFiles.insert(entry).first;
  510. }
  511. return (void *)i->second;
  512. }
  513. else
  514. {
  515. return 0;
  516. }
  517. }
  518. void * CCONV cmAddSource(void *arg, void *arg2)
  519. {
  520. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  521. cmCPluginAPISourceFile* osf = static_cast<cmCPluginAPISourceFile*>(arg2);
  522. if(osf->FullPath.empty())
  523. {
  524. return 0;
  525. }
  526. // Create the real cmSourceFile instance and copy over saved information.
  527. cmSourceFile* rsf = mf->GetOrCreateSource(osf->FullPath.c_str());
  528. rsf->GetProperties() = osf->Properties;
  529. for(std::vector<std::string>::iterator i = osf->Depends.begin();
  530. i != osf->Depends.end(); ++i)
  531. {
  532. rsf->AddDepend(i->c_str());
  533. }
  534. // Create the proxy for the real source file.
  535. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  536. sf->RealSourceFile = rsf;
  537. sf->FullPath = osf->FullPath;
  538. sf->SourceName = osf->SourceName;
  539. sf->SourceExtension = osf->SourceExtension;
  540. // Store the proxy in the map so it can be re-used and deleted later.
  541. cmCPluginAPISourceFiles[rsf] = sf;
  542. return (void *)sf;
  543. }
  544. const char * CCONV cmSourceFileGetSourceName(void *arg)
  545. {
  546. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  547. return sf->SourceName.c_str();
  548. }
  549. const char * CCONV cmSourceFileGetFullPath(void *arg)
  550. {
  551. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  552. return sf->FullPath.c_str();
  553. }
  554. const char * CCONV cmSourceFileGetProperty(void *arg,const char *prop)
  555. {
  556. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  557. if(cmSourceFile* rsf = sf->RealSourceFile)
  558. {
  559. return rsf->GetProperty(prop);
  560. }
  561. else
  562. {
  563. if(!strcmp(prop,"LOCATION"))
  564. {
  565. return sf->FullPath.c_str();
  566. }
  567. bool chain = false;
  568. // Ignore chain because old code will not expect it and it is a
  569. // pain to implement here anyway.
  570. return sf->Properties.GetPropertyValue(prop, cmProperty::SOURCE_FILE,
  571. chain);
  572. }
  573. }
  574. int CCONV cmSourceFileGetPropertyAsBool(void *arg,const char *prop)
  575. {
  576. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  577. if(cmSourceFile* rsf = sf->RealSourceFile)
  578. {
  579. return rsf->GetPropertyAsBool(prop) ? 1:0;
  580. }
  581. else
  582. {
  583. return cmSystemTools::IsOn(cmSourceFileGetProperty(arg, prop))? 1:0;
  584. }
  585. }
  586. void CCONV cmSourceFileSetProperty(void *arg,const char *prop,
  587. const char *value)
  588. {
  589. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  590. if(cmSourceFile* rsf = sf->RealSourceFile)
  591. {
  592. rsf->SetProperty(prop, value);
  593. }
  594. else if(prop)
  595. {
  596. if(!value) { value = "NOTFOUND"; }
  597. sf->Properties.SetProperty(prop, value, cmProperty::SOURCE_FILE);
  598. }
  599. }
  600. void CCONV cmSourceFileAddDepend(void *arg, const char *depend)
  601. {
  602. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  603. if(cmSourceFile* rsf = sf->RealSourceFile)
  604. {
  605. rsf->AddDepend(depend);
  606. }
  607. else
  608. {
  609. sf->Depends.push_back(depend);
  610. }
  611. }
  612. void CCONV cmSourceFileSetName(void *arg, const char* name, const char* dir,
  613. int numSourceExtensions,
  614. const char **sourceExtensions,
  615. int numHeaderExtensions,
  616. const char **headerExtensions)
  617. {
  618. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  619. if(sf->RealSourceFile)
  620. {
  621. // SetName is allowed only on temporary source files created by
  622. // the command for building and passing to AddSource.
  623. return;
  624. }
  625. std::vector<std::string> sourceExts;
  626. std::vector<std::string> headerExts;
  627. int i;
  628. for (i = 0; i < numSourceExtensions; ++i)
  629. {
  630. sourceExts.push_back(sourceExtensions[i]);
  631. }
  632. for (i = 0; i < numHeaderExtensions; ++i)
  633. {
  634. headerExts.push_back(headerExtensions[i]);
  635. }
  636. // Save the original name given.
  637. sf->SourceName = name;
  638. // Convert the name to a full path in case the given name is a
  639. // relative path.
  640. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  641. // First try and see whether the listed file can be found
  642. // as is without extensions added on.
  643. std::string hname = pathname;
  644. if(cmSystemTools::FileExists(hname.c_str()))
  645. {
  646. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  647. if ( sf->SourceName.size() > 0 )
  648. {
  649. sf->SourceName += "/";
  650. }
  651. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  652. std::string::size_type pos = hname.rfind('.');
  653. if(pos != std::string::npos)
  654. {
  655. sf->SourceExtension = hname.substr(pos+1, hname.size()-pos);
  656. if ( cmSystemTools::FileIsFullPath(name) )
  657. {
  658. std::string::size_type pos2 = hname.rfind('/');
  659. if(pos2 != std::string::npos)
  660. {
  661. sf->SourceName = hname.substr(pos2+1, pos - pos2-1);
  662. }
  663. }
  664. }
  665. sf->FullPath = hname;
  666. return;
  667. }
  668. // Next, try the various source extensions
  669. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  670. ext != sourceExts.end(); ++ext )
  671. {
  672. hname = pathname;
  673. hname += ".";
  674. hname += *ext;
  675. if(cmSystemTools::FileExists(hname.c_str()))
  676. {
  677. sf->SourceExtension = *ext;
  678. sf->FullPath = hname;
  679. return;
  680. }
  681. }
  682. // Finally, try the various header extensions
  683. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  684. ext != headerExts.end(); ++ext )
  685. {
  686. hname = pathname;
  687. hname += ".";
  688. hname += *ext;
  689. if(cmSystemTools::FileExists(hname.c_str()))
  690. {
  691. sf->SourceExtension = *ext;
  692. sf->FullPath = hname;
  693. return;
  694. }
  695. }
  696. cmOStringStream e;
  697. e << "Cannot find source file \"" << pathname << "\"";
  698. e << "\n\nTried extensions";
  699. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  700. ext != sourceExts.end(); ++ext )
  701. {
  702. e << " ." << *ext;
  703. }
  704. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  705. ext != headerExts.end(); ++ext )
  706. {
  707. e << " ." << *ext;
  708. }
  709. cmSystemTools::Error(e.str().c_str());
  710. return;
  711. }
  712. void CCONV cmSourceFileSetName2(void *arg, const char* name, const char* dir,
  713. const char *ext, int headerFileOnly)
  714. {
  715. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  716. if(sf->RealSourceFile)
  717. {
  718. // SetName is allowed only on temporary source files created by
  719. // the command for building and passing to AddSource.
  720. return;
  721. }
  722. // Implement the old SetName method code here.
  723. if(headerFileOnly)
  724. {
  725. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1",
  726. cmProperty::SOURCE_FILE);
  727. }
  728. sf->SourceName = name;
  729. std::string fname = sf->SourceName;
  730. if(ext && strlen(ext))
  731. {
  732. fname += ".";
  733. fname += ext;
  734. }
  735. sf->FullPath = cmSystemTools::CollapseFullPath(fname.c_str(), dir);
  736. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  737. sf->SourceExtension = ext;
  738. }
  739. char * CCONV cmGetFilenameWithoutExtension(const char *name)
  740. {
  741. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  742. char *result = (char *)malloc(sres.size()+1);
  743. strcpy(result,sres.c_str());
  744. return result;
  745. }
  746. char * CCONV cmGetFilenamePath(const char *name)
  747. {
  748. std::string sres = cmSystemTools::GetFilenamePath(name);
  749. char *result = (char *)malloc(sres.size()+1);
  750. strcpy(result,sres.c_str());
  751. return result;
  752. }
  753. char * CCONV cmCapitalized(const char *name)
  754. {
  755. std::string sres = cmSystemTools::Capitalized(name);
  756. char *result = (char *)malloc(sres.size()+1);
  757. strcpy(result,sres.c_str());
  758. return result;
  759. }
  760. void CCONV cmCopyFileIfDifferent(const char *name1, const char *name2)
  761. {
  762. cmSystemTools::CopyFileIfDifferent(name1,name2);
  763. }
  764. void CCONV cmRemoveFile(const char *name)
  765. {
  766. cmSystemTools::RemoveFile(name);
  767. }
  768. void CCONV cmDisplayStatus(void *arg, const char* message)
  769. {
  770. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  771. mf->DisplayStatus(message, -1);
  772. }
  773. void CCONV cmFree(void *data)
  774. {
  775. free(data);
  776. }
  777. void CCONV DefineSourceFileProperty (void *arg, const char *name,
  778. const char *briefDocs,
  779. const char *longDocs,
  780. int chained)
  781. {
  782. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  783. mf->GetCMakeInstance()->DefineProperty(name,cmProperty::SOURCE_FILE,
  784. briefDocs, longDocs,
  785. chained != 0);
  786. }
  787. } // close the extern "C" scope
  788. cmCAPI cmStaticCAPI =
  789. {
  790. cmGetClientData,
  791. cmGetTotalArgumentSize,
  792. cmFreeArguments,
  793. cmSetClientData,
  794. cmSetError,
  795. cmAddCacheDefinition,
  796. cmAddCustomCommand,
  797. cmAddDefineFlag,
  798. cmAddDefinition,
  799. cmAddExecutable,
  800. cmAddLibrary,
  801. cmAddLinkDirectoryForTarget,
  802. cmAddLinkLibraryForTarget,
  803. cmAddUtilityCommand,
  804. cmCommandExists,
  805. cmExecuteCommand,
  806. cmExpandSourceListArguments,
  807. cmExpandVariablesInString,
  808. cmGetCacheMajorVersion,
  809. cmGetCacheMinorVersion,
  810. cmGetCurrentDirectory,
  811. cmGetCurrentOutputDirectory,
  812. cmGetDefinition,
  813. cmGetHomeDirectory,
  814. cmGetHomeOutputDirectory,
  815. cmGetMajorVersion,
  816. cmGetMinorVersion,
  817. cmGetProjectName,
  818. cmGetStartDirectory,
  819. cmGetStartOutputDirectory,
  820. cmIsOn,
  821. cmAddSource,
  822. cmCreateSourceFile,
  823. cmDestroySourceFile,
  824. cmGetSource,
  825. cmSourceFileAddDepend,
  826. cmSourceFileGetProperty,
  827. cmSourceFileGetPropertyAsBool,
  828. cmSourceFileGetSourceName,
  829. cmSourceFileGetFullPath,
  830. cmSourceFileSetName,
  831. cmSourceFileSetName2,
  832. cmSourceFileSetProperty,
  833. cmCapitalized,
  834. cmCopyFileIfDifferent,
  835. cmGetFilenameWithoutExtension,
  836. cmGetFilenamePath,
  837. cmRemoveFile,
  838. cmFree,
  839. cmAddCustomCommandToOutput,
  840. cmAddCustomCommandToTarget,
  841. cmDisplayStatus,
  842. cmCreateNewSourceFile,
  843. DefineSourceFileProperty,
  844. };