cmCPluginAPI.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. (void)arg; // no longer needed
  478. return (void*)new cmCPluginAPISourceFile;
  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. // Implement the old SetName method code here.
  637. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1",
  638. cmProperty::SOURCE_FILE);
  639. // Save the original name given.
  640. sf->SourceName = name;
  641. // Convert the name to a full path in case the given name is a
  642. // relative path.
  643. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  644. // First try and see whether the listed file can be found
  645. // as is without extensions added on.
  646. std::string hname = pathname;
  647. if(cmSystemTools::FileExists(hname.c_str()))
  648. {
  649. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  650. if ( sf->SourceName.size() > 0 )
  651. {
  652. sf->SourceName += "/";
  653. }
  654. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  655. std::string::size_type pos = hname.rfind('.');
  656. if(pos != std::string::npos)
  657. {
  658. sf->SourceExtension = hname.substr(pos+1, hname.size()-pos);
  659. if ( cmSystemTools::FileIsFullPath(name) )
  660. {
  661. std::string::size_type pos2 = hname.rfind('/');
  662. if(pos2 != std::string::npos)
  663. {
  664. sf->SourceName = hname.substr(pos2+1, pos - pos2-1);
  665. }
  666. }
  667. }
  668. // See if the file is a header file
  669. if(std::find( headerExts.begin(), headerExts.end(),
  670. sf->SourceExtension ) == headerExts.end())
  671. {
  672. sf->Properties.SetProperty("HEADER_FILE_ONLY", "0",
  673. cmProperty::SOURCE_FILE);
  674. }
  675. sf->FullPath = hname;
  676. return;
  677. }
  678. // Next, try the various source extensions
  679. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  680. ext != sourceExts.end(); ++ext )
  681. {
  682. hname = pathname;
  683. hname += ".";
  684. hname += *ext;
  685. if(cmSystemTools::FileExists(hname.c_str()))
  686. {
  687. sf->SourceExtension = *ext;
  688. sf->Properties.SetProperty("HEADER_FILE_ONLY", "0",
  689. cmProperty::SOURCE_FILE);
  690. sf->FullPath = hname;
  691. return;
  692. }
  693. }
  694. // Finally, try the various header extensions
  695. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  696. ext != headerExts.end(); ++ext )
  697. {
  698. hname = pathname;
  699. hname += ".";
  700. hname += *ext;
  701. if(cmSystemTools::FileExists(hname.c_str()))
  702. {
  703. sf->SourceExtension = *ext;
  704. sf->FullPath = hname;
  705. return;
  706. }
  707. }
  708. cmOStringStream e;
  709. e << "Cannot find source file \"" << pathname << "\"";
  710. e << "\n\nTried extensions";
  711. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  712. ext != sourceExts.end(); ++ext )
  713. {
  714. e << " ." << *ext;
  715. }
  716. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  717. ext != headerExts.end(); ++ext )
  718. {
  719. e << " ." << *ext;
  720. }
  721. cmSystemTools::Error(e.str().c_str());
  722. return;
  723. }
  724. void CCONV cmSourceFileSetName2(void *arg, const char* name, const char* dir,
  725. const char *ext, int headerFileOnly)
  726. {
  727. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  728. if(sf->RealSourceFile)
  729. {
  730. // SetName is allowed only on temporary source files created by
  731. // the command for building and passing to AddSource.
  732. return;
  733. }
  734. // Implement the old SetName method code here.
  735. sf->Properties.SetProperty("HEADER_FILE_ONLY",
  736. headerFileOnly? "1" : "0",
  737. cmProperty::SOURCE_FILE);
  738. sf->SourceName = name;
  739. std::string fname = sf->SourceName;
  740. if(ext && strlen(ext))
  741. {
  742. fname += ".";
  743. fname += ext;
  744. }
  745. sf->FullPath = cmSystemTools::CollapseFullPath(fname.c_str(), dir);
  746. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  747. sf->SourceExtension = ext;
  748. }
  749. char * CCONV cmGetFilenameWithoutExtension(const char *name)
  750. {
  751. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  752. char *result = (char *)malloc(sres.size()+1);
  753. strcpy(result,sres.c_str());
  754. return result;
  755. }
  756. char * CCONV cmGetFilenamePath(const char *name)
  757. {
  758. std::string sres = cmSystemTools::GetFilenamePath(name);
  759. char *result = (char *)malloc(sres.size()+1);
  760. strcpy(result,sres.c_str());
  761. return result;
  762. }
  763. char * CCONV cmCapitalized(const char *name)
  764. {
  765. std::string sres = cmSystemTools::Capitalized(name);
  766. char *result = (char *)malloc(sres.size()+1);
  767. strcpy(result,sres.c_str());
  768. return result;
  769. }
  770. void CCONV cmCopyFileIfDifferent(const char *name1, const char *name2)
  771. {
  772. cmSystemTools::CopyFileIfDifferent(name1,name2);
  773. }
  774. void CCONV cmRemoveFile(const char *name)
  775. {
  776. cmSystemTools::RemoveFile(name);
  777. }
  778. void CCONV cmDisplayStatus(void *arg, const char* message)
  779. {
  780. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  781. mf->DisplayStatus(message, -1);
  782. }
  783. void CCONV cmFree(void *data)
  784. {
  785. free(data);
  786. }
  787. void CCONV DefineSourceFileProperty (void *arg, const char *name,
  788. const char *briefDocs,
  789. const char *longDocs,
  790. int chained)
  791. {
  792. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  793. mf->GetCMakeInstance()->DefineProperty(name,cmProperty::SOURCE_FILE,
  794. briefDocs, longDocs,
  795. chained != 0);
  796. }
  797. } // close the extern "C" scope
  798. cmCAPI cmStaticCAPI =
  799. {
  800. cmGetClientData,
  801. cmGetTotalArgumentSize,
  802. cmFreeArguments,
  803. cmSetClientData,
  804. cmSetError,
  805. cmAddCacheDefinition,
  806. cmAddCustomCommand,
  807. cmAddDefineFlag,
  808. cmAddDefinition,
  809. cmAddExecutable,
  810. cmAddLibrary,
  811. cmAddLinkDirectoryForTarget,
  812. cmAddLinkLibraryForTarget,
  813. cmAddUtilityCommand,
  814. cmCommandExists,
  815. cmExecuteCommand,
  816. cmExpandSourceListArguments,
  817. cmExpandVariablesInString,
  818. cmGetCacheMajorVersion,
  819. cmGetCacheMinorVersion,
  820. cmGetCurrentDirectory,
  821. cmGetCurrentOutputDirectory,
  822. cmGetDefinition,
  823. cmGetHomeDirectory,
  824. cmGetHomeOutputDirectory,
  825. cmGetMajorVersion,
  826. cmGetMinorVersion,
  827. cmGetProjectName,
  828. cmGetStartDirectory,
  829. cmGetStartOutputDirectory,
  830. cmIsOn,
  831. cmAddSource,
  832. cmCreateSourceFile,
  833. cmDestroySourceFile,
  834. cmGetSource,
  835. cmSourceFileAddDepend,
  836. cmSourceFileGetProperty,
  837. cmSourceFileGetPropertyAsBool,
  838. cmSourceFileGetSourceName,
  839. cmSourceFileGetFullPath,
  840. cmSourceFileSetName,
  841. cmSourceFileSetName2,
  842. cmSourceFileSetProperty,
  843. cmCapitalized,
  844. cmCopyFileIfDifferent,
  845. cmGetFilenameWithoutExtension,
  846. cmGetFilenamePath,
  847. cmRemoveFile,
  848. cmFree,
  849. cmAddCustomCommandToOutput,
  850. cmAddCustomCommandToTarget,
  851. cmDisplayStatus,
  852. cmCreateNewSourceFile,
  853. DefineSourceFileProperty,
  854. };