cmCPluginAPI.cxx 25 KB

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