cmCPluginAPI.cxx 26 KB

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