1
0

cmCPluginAPI.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 "cmSourceFile.h"
  20. extern "C"
  21. {
  22. void *cmGetClientData(void *info)
  23. {
  24. return ((cmLoadedCommandInfo *)info)->ClientData;
  25. }
  26. void cmSetClientData(void *info, void *cd)
  27. {
  28. ((cmLoadedCommandInfo *)info)->ClientData = cd;
  29. }
  30. void cmSetError(void *info, const char *err)
  31. {
  32. if (((cmLoadedCommandInfo *)info)->Error)
  33. {
  34. free(((cmLoadedCommandInfo *)info)->Error);
  35. }
  36. ((cmLoadedCommandInfo *)info)->Error = strdup(err);
  37. }
  38. unsigned int cmGetCacheMajorVersion(void *arg)
  39. {
  40. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  41. return mf->GetCacheMajorVersion();
  42. }
  43. unsigned int cmGetCacheMinorVersion(void *arg)
  44. {
  45. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  46. return mf->GetCacheMinorVersion();
  47. }
  48. unsigned int cmGetMajorVersion(void *)
  49. {
  50. return cmMakefile::GetMajorVersion();
  51. }
  52. unsigned int cmGetMinorVersion(void *)
  53. {
  54. return cmMakefile::GetMinorVersion();
  55. }
  56. void cmAddDefinition(void *arg, const char* name, const char* value)
  57. {
  58. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  59. mf->AddDefinition(name,value);
  60. }
  61. /* Add a definition to this makefile and the global cmake cache. */
  62. void cmAddCacheDefinition(void *arg, const char* name, const char* value,
  63. const char* doc,
  64. int type)
  65. {
  66. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  67. switch (type)
  68. {
  69. case CM_CACHE_BOOL:
  70. mf->AddCacheDefinition(name,value,doc,
  71. cmCacheManager::BOOL);
  72. break;
  73. case CM_CACHE_PATH:
  74. mf->AddCacheDefinition(name,value,doc,
  75. cmCacheManager::PATH);
  76. break;
  77. case CM_CACHE_FILEPATH:
  78. mf->AddCacheDefinition(name,value,doc,
  79. cmCacheManager::FILEPATH);
  80. break;
  81. case CM_CACHE_STRING:
  82. mf->AddCacheDefinition(name,value,doc,
  83. cmCacheManager::STRING);
  84. break;
  85. case CM_CACHE_INTERNAL:
  86. mf->AddCacheDefinition(name,value,doc,
  87. cmCacheManager::INTERNAL);
  88. break;
  89. case CM_CACHE_STATIC:
  90. mf->AddCacheDefinition(name,value,doc,
  91. cmCacheManager::STATIC);
  92. break;
  93. }
  94. }
  95. const char* cmGetProjectName(void *arg)
  96. {
  97. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  98. return mf->GetProjectName();
  99. }
  100. const char* cmGetHomeDirectory(void *arg)
  101. {
  102. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  103. return mf->GetHomeDirectory();
  104. }
  105. const char* cmGetHomeOutputDirectory(void *arg)
  106. {
  107. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  108. return mf->GetHomeOutputDirectory();
  109. }
  110. const char* cmGetStartDirectory(void *arg)
  111. {
  112. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  113. return mf->GetStartDirectory();
  114. }
  115. const char* cmGetStartOutputDirectory(void *arg)
  116. {
  117. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  118. return mf->GetStartOutputDirectory();
  119. }
  120. const char* cmGetCurrentDirectory(void *arg)
  121. {
  122. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  123. return mf->GetCurrentDirectory();
  124. }
  125. const char* cmGetCurrentOutputDirectory(void *arg)
  126. {
  127. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  128. return mf->GetCurrentOutputDirectory();
  129. }
  130. const char* cmGetDefinition(void *arg,const char*def)
  131. {
  132. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  133. return mf->GetDefinition(def);
  134. }
  135. int cmIsOn(void *arg, const char* name)
  136. {
  137. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  138. return static_cast<int>(mf->IsOn(name));
  139. }
  140. /** Check if a command exists. */
  141. int cmCommandExists(void *arg, const char* name)
  142. {
  143. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  144. return static_cast<int>(mf->CommandExists(name));
  145. }
  146. void cmAddDefineFlag(void *arg, const char* definition)
  147. {
  148. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  149. mf->AddDefineFlag(definition);
  150. }
  151. void cmAddLinkDirectoryForTarget(void *arg, const char *tgt, const char* d)
  152. {
  153. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  154. mf->AddLinkDirectoryForTarget(tgt,d);
  155. }
  156. void cmAddExecutable(void *arg, const char *exename,
  157. int numSrcs, const char **srcs, int win32)
  158. {
  159. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  160. std::vector<std::string> srcs2;
  161. int i;
  162. for (i = 0; i < numSrcs; ++i)
  163. {
  164. srcs2.push_back(srcs[i]);
  165. }
  166. cmTarget* tg = mf->AddExecutable(exename, srcs2);
  167. if ( win32 )
  168. {
  169. tg->SetProperty("WIN32_EXECUTABLE", "ON");
  170. }
  171. }
  172. void cmAddUtilityCommand(void *arg, const char* utilityName,
  173. const char* command,
  174. const char* arguments,
  175. int all,
  176. int numDepends,
  177. const char **depends,
  178. int numOutputs,
  179. const char **outputs)
  180. {
  181. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  182. std::vector<std::string> depends2;
  183. int i;
  184. for (i = 0; i < numDepends; ++i)
  185. {
  186. depends2.push_back(depends[i]);
  187. }
  188. std::vector<std::string> outputs2;
  189. for (i = 0; i < numOutputs; ++i)
  190. {
  191. outputs2.push_back(outputs[i]);
  192. }
  193. mf->AddUtilityCommand(utilityName,command,arguments, (all ? true : false),
  194. depends2, outputs2);
  195. }
  196. void cmAddCustomCommand(void *arg, const char* source,
  197. const char* command,
  198. int numArgs, const char **args,
  199. int numDepends, const char **depends,
  200. int numOutputs, const char **outputs,
  201. const char *target)
  202. {
  203. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  204. int i;
  205. std::vector<std::string> args2;
  206. for (i = 0; i < numArgs; ++i)
  207. {
  208. args2.push_back(args[i]);
  209. }
  210. std::vector<std::string> depends2;
  211. for (i = 0; i < numDepends; ++i)
  212. {
  213. depends2.push_back(depends[i]);
  214. }
  215. std::vector<std::string> outputs2;
  216. for (i = 0; i < numOutputs; ++i)
  217. {
  218. outputs2.push_back(outputs[i]);
  219. }
  220. mf->AddCustomCommand(source, command, args2, depends2, outputs2, target);
  221. }
  222. void cmAddCustomCommandToOutput(void *arg, const char* output,
  223. const char* command,
  224. int numArgs, const char **args,
  225. const char* main_dependency,
  226. int numDepends, const char **depends)
  227. {
  228. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  229. int i;
  230. std::vector<std::string> args2;
  231. for (i = 0; i < numArgs; ++i)
  232. {
  233. args2.push_back(args[i]);
  234. }
  235. std::vector<std::string> depends2;
  236. for (i = 0; i < numDepends; ++i)
  237. {
  238. depends2.push_back(depends[i]);
  239. }
  240. mf->AddCustomCommandToOutput(output, command, args2, main_dependency,
  241. depends2);
  242. }
  243. void cmAddCustomCommandToTarget(void *arg, const char* target,
  244. const char* command,
  245. int numArgs, const char **args,
  246. int commandType)
  247. {
  248. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  249. int i;
  250. std::vector<std::string> args2;
  251. for (i = 0; i < numArgs; ++i)
  252. {
  253. args2.push_back(args[i]);
  254. }
  255. switch (commandType)
  256. {
  257. case CM_PRE_BUILD:
  258. mf->AddCustomCommandToTarget(target, command, args2,
  259. cmTarget::PRE_BUILD);
  260. break;
  261. case CM_PRE_LINK:
  262. mf->AddCustomCommandToTarget(target, command, args2,
  263. cmTarget::PRE_LINK);
  264. break;
  265. case CM_POST_BUILD:
  266. mf->AddCustomCommandToTarget(target, command, args2,
  267. cmTarget::POST_BUILD);
  268. break;
  269. }
  270. }
  271. void cmAddLinkLibraryForTarget(void *arg, const char *tgt, const char*value,
  272. int libtype)
  273. {
  274. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  275. switch (libtype)
  276. {
  277. case CM_LIBRARY_GENERAL:
  278. mf->AddLinkLibraryForTarget(tgt,value, cmTarget::GENERAL);
  279. break;
  280. case CM_LIBRARY_DEBUG:
  281. mf->AddLinkLibraryForTarget(tgt,value, cmTarget::DEBUG);
  282. break;
  283. case CM_LIBRARY_OPTIMIZED:
  284. mf->AddLinkLibraryForTarget(tgt,value, cmTarget::OPTIMIZED);
  285. break;
  286. }
  287. }
  288. void cmAddLibrary(void *arg, const char *libname, int shared,
  289. int numSrcs, const char **srcs)
  290. {
  291. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  292. std::vector<std::string> srcs2;
  293. int i;
  294. for (i = 0; i < numSrcs; ++i)
  295. {
  296. srcs2.push_back(srcs[i]);
  297. }
  298. mf->AddLibrary(libname, (shared ? true : false), srcs2);
  299. }
  300. char *cmExpandVariablesInString(void *arg, const char *source,
  301. int escapeQuotes, int atOnly)
  302. {
  303. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  304. std::string barf = source;
  305. std::string result =
  306. mf->ExpandVariablesInString(barf,
  307. (escapeQuotes ? true : false),
  308. (atOnly ? true : false));
  309. char *res = static_cast<char *>(malloc(result.size() + 1));
  310. if (result.size())
  311. {
  312. strcpy(res,result.c_str());
  313. }
  314. res[result.size()] = '\0';
  315. return res;
  316. }
  317. int cmExecuteCommand(void *arg, const char *name,
  318. int numArgs, const char **args)
  319. {
  320. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  321. cmListFileFunction lff;
  322. lff.m_Name = name;
  323. for(int i = 0; i < numArgs; ++i)
  324. {
  325. // Assume all arguments are quoted.
  326. lff.m_Arguments.push_back(cmListFileArgument(args[i], true,
  327. "[CMake-Plugin]", 0));
  328. }
  329. return mf->ExecuteCommand(lff);
  330. }
  331. void cmExpandSourceListArguments(void *arg,
  332. int numArgs,
  333. const char **args,
  334. int *resArgc,
  335. char ***resArgv,
  336. unsigned int startArgumentIndex)
  337. {
  338. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  339. std::vector<std::string> result;
  340. std::vector<std::string> args2;
  341. int i;
  342. for (i = 0; i < numArgs; ++i)
  343. {
  344. args2.push_back(args[i]);
  345. }
  346. mf->ExpandSourceListArguments(args2, result, startArgumentIndex);
  347. int resargc = static_cast<int>(result.size());
  348. char **resargv = 0;
  349. if (resargc)
  350. {
  351. resargv = (char **)malloc(resargc*sizeof(char *));
  352. }
  353. for (i = 0; i < resargc; ++i)
  354. {
  355. resargv[i] = strdup(result[i].c_str());
  356. }
  357. *resArgc = resargc;
  358. *resArgv = resargv;
  359. }
  360. void cmFreeArguments(int argc, char **argv)
  361. {
  362. int i;
  363. for (i = 0; i < argc; ++i)
  364. {
  365. free(argv[i]);
  366. }
  367. if (argv)
  368. {
  369. free(argv);
  370. }
  371. }
  372. int cmGetTotalArgumentSize(int argc, char **argv)
  373. {
  374. int i;
  375. int result = 0;
  376. for (i = 0; i < argc; ++i)
  377. {
  378. if (argv[i])
  379. {
  380. result = result + static_cast<int>(strlen(argv[i]));
  381. }
  382. }
  383. return result;
  384. }
  385. void *cmGetSource(void *arg, const char *name)
  386. {
  387. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  388. return (void *)mf->GetSource(name);
  389. }
  390. void * cmAddSource(void *arg, void *arg2)
  391. {
  392. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  393. cmSourceFile *sf = static_cast<cmSourceFile *>(arg2);
  394. return (void *)mf->AddSource(*sf);
  395. }
  396. void * cmCreateSourceFile()
  397. {
  398. return (void *)(new cmSourceFile);
  399. }
  400. void cmDestroySourceFile(void *arg)
  401. {
  402. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  403. delete sf;
  404. }
  405. const char *cmSourceFileGetSourceName(void *arg)
  406. {
  407. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  408. return sf->GetSourceName().c_str();
  409. }
  410. const char *cmSourceFileGetFullPath(void *arg)
  411. {
  412. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  413. return sf->GetFullPath().c_str();
  414. }
  415. const char *cmSourceFileGetProperty(void *arg,const char *prop)
  416. {
  417. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  418. return sf->GetProperty(prop);
  419. }
  420. int cmSourceFileGetPropertyAsBool(void *arg,const char *prop)
  421. {
  422. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  423. return (sf->GetPropertyAsBool(prop) ? 1: 0);
  424. }
  425. void cmSourceFileSetProperty(void *arg,const char *prop, const char *val)
  426. {
  427. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  428. sf->SetProperty(prop,val);
  429. }
  430. void cmSourceFileAddDepend(void *arg, const char *depend)
  431. {
  432. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  433. sf->GetDepends().push_back(depend);
  434. }
  435. void cmSourceFileSetName(void *arg, const char* name, const char* dir,
  436. int numSourceExtensions,
  437. const char **sourceExtensions,
  438. int numHeaderExtensions,
  439. const char **headerExtensions)
  440. {
  441. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  442. std::vector<std::string> srcs;
  443. std::vector<std::string> hdrs;
  444. int i;
  445. for (i = 0; i < numSourceExtensions; ++i)
  446. {
  447. srcs.push_back(sourceExtensions[i]);
  448. }
  449. for (i = 0; i < numHeaderExtensions; ++i)
  450. {
  451. hdrs.push_back(headerExtensions[i]);
  452. }
  453. sf->SetName(name,dir, srcs, hdrs);
  454. }
  455. void cmSourceFileSetName2(void *arg, const char* name, const char* dir,
  456. const char *ext, int headerFileOnly)
  457. {
  458. cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
  459. sf->SetName(name,dir,ext,(headerFileOnly ? true : false));
  460. }
  461. char *cmGetFilenameWithoutExtension(const char *name)
  462. {
  463. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  464. char *result = (char *)malloc(sres.size()+1);
  465. strcpy(result,sres.c_str());
  466. return result;
  467. }
  468. char *cmGetFilenamePath(const char *name)
  469. {
  470. std::string sres = cmSystemTools::GetFilenamePath(name);
  471. char *result = (char *)malloc(sres.size()+1);
  472. strcpy(result,sres.c_str());
  473. return result;
  474. }
  475. char *cmCapitalized(const char *name)
  476. {
  477. std::string sres = cmSystemTools::Capitalized(name);
  478. char *result = (char *)malloc(sres.size()+1);
  479. strcpy(result,sres.c_str());
  480. return result;
  481. }
  482. void cmCopyFileIfDifferent(const char *name1, const char *name2)
  483. {
  484. cmSystemTools::CopyFileIfDifferent(name1,name2);
  485. }
  486. void cmRemoveFile(const char *name)
  487. {
  488. cmSystemTools::RemoveFile(name);
  489. }
  490. void cmDisplayStatus(void *arg, const char* message)
  491. {
  492. cmMakefile *mf = static_cast<cmMakefile *>(arg);
  493. mf->DisplayStatus(message, -1);
  494. }
  495. void cmFree(void *data)
  496. {
  497. free(data);
  498. }
  499. } // close the extern "C" scope
  500. cmCAPI cmStaticCAPI =
  501. {
  502. cmGetClientData,
  503. cmGetTotalArgumentSize,
  504. cmFreeArguments,
  505. cmSetClientData,
  506. cmSetError,
  507. cmAddCacheDefinition,
  508. cmAddCustomCommand,
  509. cmAddDefineFlag,
  510. cmAddDefinition,
  511. cmAddExecutable,
  512. cmAddLibrary,
  513. cmAddLinkDirectoryForTarget,
  514. cmAddLinkLibraryForTarget,
  515. cmAddUtilityCommand,
  516. cmCommandExists,
  517. cmExecuteCommand,
  518. cmExpandSourceListArguments,
  519. cmExpandVariablesInString,
  520. cmGetCacheMajorVersion,
  521. cmGetCacheMinorVersion,
  522. cmGetCurrentDirectory,
  523. cmGetCurrentOutputDirectory,
  524. cmGetDefinition,
  525. cmGetHomeDirectory,
  526. cmGetHomeOutputDirectory,
  527. cmGetMajorVersion,
  528. cmGetMinorVersion,
  529. cmGetProjectName,
  530. cmGetStartDirectory,
  531. cmGetStartOutputDirectory,
  532. cmIsOn,
  533. cmAddSource,
  534. cmCreateSourceFile,
  535. cmDestroySourceFile,
  536. cmGetSource,
  537. cmSourceFileAddDepend,
  538. cmSourceFileGetProperty,
  539. cmSourceFileGetPropertyAsBool,
  540. cmSourceFileGetSourceName,
  541. cmSourceFileGetFullPath,
  542. cmSourceFileSetName,
  543. cmSourceFileSetName2,
  544. cmSourceFileSetProperty,
  545. cmCapitalized,
  546. cmCopyFileIfDifferent,
  547. cmGetFilenameWithoutExtension,
  548. cmGetFilenamePath,
  549. cmRemoveFile,
  550. cmFree,
  551. cmAddCustomCommandToOutput,
  552. cmAddCustomCommandToTarget,
  553. cmDisplayStatus,
  554. };