cmCPluginAPI.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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, (shared ? true : false), srcs2);
  353. }
  354. char CCONV *cmExpandVariablesInString(void *arg, const char *source,
  355. int escapeQuotes, int atOnly)
  356. {
  357. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  358. std::string barf = source;
  359. std::string result =
  360. mf->ExpandVariablesInString(barf,
  361. (escapeQuotes ? true : false),
  362. (atOnly ? true : false));
  363. char *res = static_cast<char *>(malloc(result.size() + 1));
  364. if (result.size())
  365. {
  366. strcpy(res,result.c_str());
  367. }
  368. res[result.size()] = '\0';
  369. return res;
  370. }
  371. int CCONV cmExecuteCommand(void *arg, const char *name,
  372. int numArgs, const char **args)
  373. {
  374. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  375. cmListFileFunction lff;
  376. lff.Name = name;
  377. for(int i = 0; i < numArgs; ++i)
  378. {
  379. // Assume all arguments are quoted.
  380. lff.Arguments.push_back(cmListFileArgument(args[i], true,
  381. "[CMake-Plugin]", 0));
  382. }
  383. return mf->ExecuteCommand(lff);
  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()
  470. {
  471. return (void*)new cmCPluginAPISourceFile;
  472. }
  473. void * CCONV cmCreateNewSourceFile(void *arg)
  474. {
  475. (void)arg; // no longer needed
  476. return (void*)new cmCPluginAPISourceFile;
  477. }
  478. void CCONV cmDestroySourceFile(void *arg)
  479. {
  480. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  481. // Only delete if it was created by cmCreateSourceFile or
  482. // cmCreateNewSourceFile and is therefore not in the map.
  483. if(!sf->RealSourceFile)
  484. {
  485. delete sf;
  486. }
  487. }
  488. void CCONV *cmGetSource(void *arg, const char *name)
  489. {
  490. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  491. if(cmSourceFile* rsf = mf->GetSource(name))
  492. {
  493. // Lookup the proxy source file object for this source.
  494. cmCPluginAPISourceFileMap::iterator i = cmCPluginAPISourceFiles.find(rsf);
  495. if(i == cmCPluginAPISourceFiles.end())
  496. {
  497. // Create a proxy source file object for this source.
  498. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  499. sf->RealSourceFile = rsf;
  500. sf->FullPath = rsf->GetFullPath();
  501. sf->SourceName =
  502. cmSystemTools::GetFilenameWithoutLastExtension(sf->FullPath.c_str());
  503. sf->SourceExtension =
  504. cmSystemTools::GetFilenameLastExtension(sf->FullPath.c_str());
  505. // Store the proxy in the map so it can be re-used and deleted later.
  506. cmCPluginAPISourceFileMap::value_type entry(rsf, sf);
  507. i = cmCPluginAPISourceFiles.insert(entry).first;
  508. }
  509. return (void *)i->second;
  510. }
  511. else
  512. {
  513. return 0;
  514. }
  515. }
  516. void * CCONV cmAddSource(void *arg, void *arg2)
  517. {
  518. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  519. cmCPluginAPISourceFile* osf = static_cast<cmCPluginAPISourceFile*>(arg2);
  520. if(osf->FullPath.empty())
  521. {
  522. return 0;
  523. }
  524. // Create the real cmSourceFile instance and copy over saved information.
  525. cmSourceFile* rsf = mf->GetOrCreateSource(osf->FullPath.c_str());
  526. rsf->GetProperties() = osf->Properties;
  527. for(std::vector<std::string>::iterator i = osf->Depends.begin();
  528. i != osf->Depends.end(); ++i)
  529. {
  530. rsf->AddDepend(i->c_str());
  531. }
  532. // Create the proxy for the real source file.
  533. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  534. sf->RealSourceFile = rsf;
  535. sf->FullPath = osf->FullPath;
  536. sf->SourceName = osf->SourceName;
  537. sf->SourceExtension = osf->SourceExtension;
  538. // Store the proxy in the map so it can be re-used and deleted later.
  539. cmCPluginAPISourceFiles[rsf] = sf;
  540. return (void *)sf;
  541. }
  542. const char * CCONV cmSourceFileGetSourceName(void *arg)
  543. {
  544. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  545. return sf->SourceName.c_str();
  546. }
  547. const char * CCONV cmSourceFileGetFullPath(void *arg)
  548. {
  549. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  550. return sf->FullPath.c_str();
  551. }
  552. const char * CCONV cmSourceFileGetProperty(void *arg,const char *prop)
  553. {
  554. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  555. if(cmSourceFile* rsf = sf->RealSourceFile)
  556. {
  557. return rsf->GetProperty(prop);
  558. }
  559. else
  560. {
  561. if(!strcmp(prop,"LOCATION"))
  562. {
  563. return sf->FullPath.c_str();
  564. }
  565. bool chain = false;
  566. // Ignore chain because old code will not expect it and it is a
  567. // pain to implement here anyway.
  568. return sf->Properties.GetPropertyValue(prop, cmProperty::SOURCE_FILE,
  569. chain);
  570. }
  571. }
  572. int CCONV cmSourceFileGetPropertyAsBool(void *arg,const char *prop)
  573. {
  574. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  575. if(cmSourceFile* rsf = sf->RealSourceFile)
  576. {
  577. return rsf->GetPropertyAsBool(prop) ? 1:0;
  578. }
  579. else
  580. {
  581. return cmSystemTools::IsOn(cmSourceFileGetProperty(arg, prop))? 1:0;
  582. }
  583. }
  584. void CCONV cmSourceFileSetProperty(void *arg,const char *prop,
  585. const char *value)
  586. {
  587. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  588. if(cmSourceFile* rsf = sf->RealSourceFile)
  589. {
  590. rsf->SetProperty(prop, value);
  591. }
  592. else if(prop)
  593. {
  594. if(!value) { value = "NOTFOUND"; }
  595. sf->Properties.SetProperty(prop, value, cmProperty::SOURCE_FILE);
  596. }
  597. }
  598. void CCONV cmSourceFileAddDepend(void *arg, const char *depend)
  599. {
  600. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  601. if(cmSourceFile* rsf = sf->RealSourceFile)
  602. {
  603. rsf->AddDepend(depend);
  604. }
  605. else
  606. {
  607. sf->Depends.push_back(depend);
  608. }
  609. }
  610. void CCONV cmSourceFileSetName(void *arg, const char* name, const char* dir,
  611. int numSourceExtensions,
  612. const char **sourceExtensions,
  613. int numHeaderExtensions,
  614. const char **headerExtensions)
  615. {
  616. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  617. if(sf->RealSourceFile)
  618. {
  619. // SetName is allowed only on temporary source files created by
  620. // the command for building and passing to AddSource.
  621. return;
  622. }
  623. std::vector<std::string> sourceExts;
  624. std::vector<std::string> headerExts;
  625. int i;
  626. for (i = 0; i < numSourceExtensions; ++i)
  627. {
  628. sourceExts.push_back(sourceExtensions[i]);
  629. }
  630. for (i = 0; i < numHeaderExtensions; ++i)
  631. {
  632. headerExts.push_back(headerExtensions[i]);
  633. }
  634. // Implement the old SetName method code here.
  635. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1",
  636. cmProperty::SOURCE_FILE);
  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. // See if the file is a header file
  667. if(std::find( headerExts.begin(), headerExts.end(),
  668. sf->SourceExtension ) == headerExts.end())
  669. {
  670. sf->Properties.SetProperty("HEADER_FILE_ONLY", "0",
  671. cmProperty::SOURCE_FILE);
  672. }
  673. sf->FullPath = hname;
  674. return;
  675. }
  676. // Next, try the various source extensions
  677. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  678. ext != sourceExts.end(); ++ext )
  679. {
  680. hname = pathname;
  681. hname += ".";
  682. hname += *ext;
  683. if(cmSystemTools::FileExists(hname.c_str()))
  684. {
  685. sf->SourceExtension = *ext;
  686. sf->Properties.SetProperty("HEADER_FILE_ONLY", "0",
  687. cmProperty::SOURCE_FILE);
  688. sf->FullPath = hname;
  689. return;
  690. }
  691. }
  692. // Finally, try the various header extensions
  693. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  694. ext != headerExts.end(); ++ext )
  695. {
  696. hname = pathname;
  697. hname += ".";
  698. hname += *ext;
  699. if(cmSystemTools::FileExists(hname.c_str()))
  700. {
  701. sf->SourceExtension = *ext;
  702. sf->FullPath = hname;
  703. return;
  704. }
  705. }
  706. cmOStringStream e;
  707. e << "Cannot find source file \"" << pathname << "\"";
  708. e << "\n\nTried extensions";
  709. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  710. ext != sourceExts.end(); ++ext )
  711. {
  712. e << " ." << *ext;
  713. }
  714. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  715. ext != headerExts.end(); ++ext )
  716. {
  717. e << " ." << *ext;
  718. }
  719. cmSystemTools::Error(e.str().c_str());
  720. return;
  721. }
  722. void CCONV cmSourceFileSetName2(void *arg, const char* name, const char* dir,
  723. const char *ext, int headerFileOnly)
  724. {
  725. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  726. if(sf->RealSourceFile)
  727. {
  728. // SetName is allowed only on temporary source files created by
  729. // the command for building and passing to AddSource.
  730. return;
  731. }
  732. // Implement the old SetName method code here.
  733. sf->Properties.SetProperty("HEADER_FILE_ONLY",
  734. headerFileOnly? "1" : "0",
  735. cmProperty::SOURCE_FILE);
  736. sf->SourceName = name;
  737. std::string fname = sf->SourceName;
  738. if(ext && strlen(ext))
  739. {
  740. fname += ".";
  741. fname += ext;
  742. }
  743. sf->FullPath = cmSystemTools::CollapseFullPath(fname.c_str(), dir);
  744. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  745. sf->SourceExtension = ext;
  746. }
  747. char * CCONV cmGetFilenameWithoutExtension(const char *name)
  748. {
  749. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  750. char *result = (char *)malloc(sres.size()+1);
  751. strcpy(result,sres.c_str());
  752. return result;
  753. }
  754. char * CCONV cmGetFilenamePath(const char *name)
  755. {
  756. std::string sres = cmSystemTools::GetFilenamePath(name);
  757. char *result = (char *)malloc(sres.size()+1);
  758. strcpy(result,sres.c_str());
  759. return result;
  760. }
  761. char * CCONV cmCapitalized(const char *name)
  762. {
  763. std::string sres = cmSystemTools::Capitalized(name);
  764. char *result = (char *)malloc(sres.size()+1);
  765. strcpy(result,sres.c_str());
  766. return result;
  767. }
  768. void CCONV cmCopyFileIfDifferent(const char *name1, const char *name2)
  769. {
  770. cmSystemTools::CopyFileIfDifferent(name1,name2);
  771. }
  772. void CCONV cmRemoveFile(const char *name)
  773. {
  774. cmSystemTools::RemoveFile(name);
  775. }
  776. void CCONV cmDisplayStatus(void *arg, const char* message)
  777. {
  778. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  779. mf->DisplayStatus(message, -1);
  780. }
  781. void CCONV cmFree(void *data)
  782. {
  783. free(data);
  784. }
  785. void CCONV DefineSourceFileProperty (void *arg, const char *name,
  786. const char *briefDocs,
  787. const char *longDocs,
  788. int chained)
  789. {
  790. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  791. mf->GetCMakeInstance()->DefineProperty(name,cmProperty::SOURCE_FILE,
  792. briefDocs, longDocs,
  793. chained != 0);
  794. }
  795. } // close the extern "C" scope
  796. cmCAPI cmStaticCAPI =
  797. {
  798. cmGetClientData,
  799. cmGetTotalArgumentSize,
  800. cmFreeArguments,
  801. cmSetClientData,
  802. cmSetError,
  803. cmAddCacheDefinition,
  804. cmAddCustomCommand,
  805. cmAddDefineFlag,
  806. cmAddDefinition,
  807. cmAddExecutable,
  808. cmAddLibrary,
  809. cmAddLinkDirectoryForTarget,
  810. cmAddLinkLibraryForTarget,
  811. cmAddUtilityCommand,
  812. cmCommandExists,
  813. cmExecuteCommand,
  814. cmExpandSourceListArguments,
  815. cmExpandVariablesInString,
  816. cmGetCacheMajorVersion,
  817. cmGetCacheMinorVersion,
  818. cmGetCurrentDirectory,
  819. cmGetCurrentOutputDirectory,
  820. cmGetDefinition,
  821. cmGetHomeDirectory,
  822. cmGetHomeOutputDirectory,
  823. cmGetMajorVersion,
  824. cmGetMinorVersion,
  825. cmGetProjectName,
  826. cmGetStartDirectory,
  827. cmGetStartOutputDirectory,
  828. cmIsOn,
  829. cmAddSource,
  830. cmCreateSourceFile,
  831. cmDestroySourceFile,
  832. cmGetSource,
  833. cmSourceFileAddDepend,
  834. cmSourceFileGetProperty,
  835. cmSourceFileGetPropertyAsBool,
  836. cmSourceFileGetSourceName,
  837. cmSourceFileGetFullPath,
  838. cmSourceFileSetName,
  839. cmSourceFileSetName2,
  840. cmSourceFileSetProperty,
  841. cmCapitalized,
  842. cmCopyFileIfDifferent,
  843. cmGetFilenameWithoutExtension,
  844. cmGetFilenamePath,
  845. cmRemoveFile,
  846. cmFree,
  847. cmAddCustomCommandToOutput,
  848. cmAddCustomCommandToTarget,
  849. cmDisplayStatus,
  850. cmCreateNewSourceFile,
  851. DefineSourceFileProperty,
  852. };