cmCPluginAPI.cxx 26 KB

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