cmCPluginAPI.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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().c_str();
  96. }
  97. const char* CCONV cmGetHomeOutputDirectory(void* arg)
  98. {
  99. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  100. return mf->GetHomeOutputDirectory().c_str();
  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, cmMakefile::TargetOrigin::Project,
  197. (all ? false : true), nullptr, depends2, 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(barf, escapeQuotes, atOnly);
  365. return strdup(result.c_str());
  366. }
  367. int CCONV cmExecuteCommand(void* arg, const char* name, int numArgs,
  368. const char** args)
  369. {
  370. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  371. cmListFileFunction lff;
  372. lff.Name = name;
  373. for (int i = 0; i < numArgs; ++i) {
  374. // Assume all arguments are quoted.
  375. lff.Arguments.push_back(
  376. cmListFileArgument(args[i], cmListFileArgument::Quoted, 0));
  377. }
  378. cmExecutionStatus status;
  379. return mf->ExecuteCommand(lff, status);
  380. }
  381. void CCONV cmExpandSourceListArguments(void* arg, int numArgs,
  382. const char** args, int* resArgc,
  383. char*** resArgv,
  384. unsigned int startArgumentIndex)
  385. {
  386. (void)arg;
  387. (void)startArgumentIndex;
  388. std::vector<std::string> result;
  389. int i;
  390. for (i = 0; i < numArgs; ++i) {
  391. result.push_back(args[i]);
  392. }
  393. int resargc = static_cast<int>(result.size());
  394. char** resargv = nullptr;
  395. if (resargc) {
  396. resargv = (char**)malloc(resargc * sizeof(char*));
  397. }
  398. for (i = 0; i < resargc; ++i) {
  399. resargv[i] = strdup(result[i].c_str());
  400. }
  401. *resArgc = resargc;
  402. *resArgv = resargv;
  403. }
  404. void CCONV cmFreeArguments(int argc, char** argv)
  405. {
  406. int i;
  407. for (i = 0; i < argc; ++i) {
  408. free(argv[i]);
  409. }
  410. free(argv);
  411. }
  412. int CCONV cmGetTotalArgumentSize(int argc, char** argv)
  413. {
  414. int i;
  415. int result = 0;
  416. for (i = 0; i < argc; ++i) {
  417. if (argv[i]) {
  418. result = result + static_cast<int>(strlen(argv[i]));
  419. }
  420. }
  421. return result;
  422. }
  423. // Source file proxy object to support the old cmSourceFile/cmMakefile
  424. // API for source files.
  425. struct cmCPluginAPISourceFile
  426. {
  427. cmCPluginAPISourceFile()
  428. : RealSourceFile(nullptr)
  429. {
  430. }
  431. cmSourceFile* RealSourceFile;
  432. std::string SourceName;
  433. std::string SourceExtension;
  434. std::string FullPath;
  435. std::vector<std::string> Depends;
  436. cmPropertyMap Properties;
  437. };
  438. // Keep a map from real cmSourceFile instances stored in a makefile to
  439. // the CPluginAPI proxy source file.
  440. class cmCPluginAPISourceFileMap
  441. : public std::map<cmSourceFile*, cmCPluginAPISourceFile*>
  442. {
  443. public:
  444. typedef std::map<cmSourceFile*, cmCPluginAPISourceFile*> derived;
  445. typedef derived::iterator iterator;
  446. typedef derived::value_type value_type;
  447. ~cmCPluginAPISourceFileMap()
  448. {
  449. for (auto const& i : *this) {
  450. delete i.second;
  451. }
  452. }
  453. };
  454. cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
  455. void* CCONV cmCreateSourceFile(void)
  456. {
  457. return new cmCPluginAPISourceFile;
  458. }
  459. void* CCONV cmCreateNewSourceFile(void*)
  460. {
  461. return new cmCPluginAPISourceFile;
  462. }
  463. void CCONV cmDestroySourceFile(void* arg)
  464. {
  465. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  466. // Only delete if it was created by cmCreateSourceFile or
  467. // cmCreateNewSourceFile and is therefore not in the map.
  468. if (!sf->RealSourceFile) {
  469. delete sf;
  470. }
  471. }
  472. void CCONV* cmGetSource(void* arg, const char* name)
  473. {
  474. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  475. if (cmSourceFile* rsf = mf->GetSource(name)) {
  476. // Lookup the proxy source file object for this source.
  477. cmCPluginAPISourceFileMap::iterator i = cmCPluginAPISourceFiles.find(rsf);
  478. if (i == cmCPluginAPISourceFiles.end()) {
  479. // Create a proxy source file object for this source.
  480. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  481. sf->RealSourceFile = rsf;
  482. sf->FullPath = rsf->GetFullPath();
  483. sf->SourceName =
  484. cmSystemTools::GetFilenameWithoutLastExtension(sf->FullPath);
  485. sf->SourceExtension =
  486. cmSystemTools::GetFilenameLastExtension(sf->FullPath);
  487. // Store the proxy in the map so it can be re-used and deleted later.
  488. cmCPluginAPISourceFileMap::value_type entry(rsf, sf);
  489. i = cmCPluginAPISourceFiles.insert(entry).first;
  490. }
  491. return i->second;
  492. }
  493. return nullptr;
  494. }
  495. void* CCONV cmAddSource(void* arg, void* arg2)
  496. {
  497. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  498. cmCPluginAPISourceFile* osf = static_cast<cmCPluginAPISourceFile*>(arg2);
  499. if (osf->FullPath.empty()) {
  500. return nullptr;
  501. }
  502. // Create the real cmSourceFile instance and copy over saved information.
  503. cmSourceFile* rsf = mf->GetOrCreateSource(osf->FullPath);
  504. rsf->GetProperties() = osf->Properties;
  505. for (std::string const& d : osf->Depends) {
  506. rsf->AddDepend(d);
  507. }
  508. // Create the proxy for the real source file.
  509. cmCPluginAPISourceFile* sf = new cmCPluginAPISourceFile;
  510. sf->RealSourceFile = rsf;
  511. sf->FullPath = osf->FullPath;
  512. sf->SourceName = osf->SourceName;
  513. sf->SourceExtension = osf->SourceExtension;
  514. // Store the proxy in the map so it can be re-used and deleted later.
  515. cmCPluginAPISourceFiles[rsf] = sf;
  516. return sf;
  517. }
  518. const char* CCONV cmSourceFileGetSourceName(void* arg)
  519. {
  520. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  521. return sf->SourceName.c_str();
  522. }
  523. const char* CCONV cmSourceFileGetFullPath(void* arg)
  524. {
  525. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  526. return sf->FullPath.c_str();
  527. }
  528. const char* CCONV cmSourceFileGetProperty(void* arg, const char* prop)
  529. {
  530. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  531. if (cmSourceFile* rsf = sf->RealSourceFile) {
  532. return rsf->GetProperty(prop);
  533. }
  534. if (!strcmp(prop, "LOCATION")) {
  535. return sf->FullPath.c_str();
  536. }
  537. return sf->Properties.GetPropertyValue(prop);
  538. }
  539. int CCONV cmSourceFileGetPropertyAsBool(void* arg, const char* prop)
  540. {
  541. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  542. if (cmSourceFile* rsf = sf->RealSourceFile) {
  543. return rsf->GetPropertyAsBool(prop) ? 1 : 0;
  544. }
  545. return cmSystemTools::IsOn(cmSourceFileGetProperty(arg, prop)) ? 1 : 0;
  546. }
  547. void CCONV cmSourceFileSetProperty(void* arg, const char* prop,
  548. const char* value)
  549. {
  550. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  551. if (cmSourceFile* rsf = sf->RealSourceFile) {
  552. rsf->SetProperty(prop, value);
  553. } else if (prop) {
  554. if (!value) {
  555. value = "NOTFOUND";
  556. }
  557. sf->Properties.SetProperty(prop, value);
  558. }
  559. }
  560. void CCONV cmSourceFileAddDepend(void* arg, const char* depend)
  561. {
  562. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  563. if (cmSourceFile* rsf = sf->RealSourceFile) {
  564. rsf->AddDepend(depend);
  565. } else {
  566. sf->Depends.push_back(depend);
  567. }
  568. }
  569. void CCONV cmSourceFileSetName(void* arg, const char* name, const char* dir,
  570. int numSourceExtensions,
  571. const char** sourceExtensions,
  572. int numHeaderExtensions,
  573. const char** headerExtensions)
  574. {
  575. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  576. if (sf->RealSourceFile) {
  577. // SetName is allowed only on temporary source files created by
  578. // the command for building and passing to AddSource.
  579. return;
  580. }
  581. std::vector<std::string> sourceExts;
  582. std::vector<std::string> headerExts;
  583. int i;
  584. for (i = 0; i < numSourceExtensions; ++i) {
  585. sourceExts.push_back(sourceExtensions[i]);
  586. }
  587. for (i = 0; i < numHeaderExtensions; ++i) {
  588. headerExts.push_back(headerExtensions[i]);
  589. }
  590. // Save the original name given.
  591. sf->SourceName = name;
  592. // Convert the name to a full path in case the given name is a
  593. // relative path.
  594. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  595. // First try and see whether the listed file can be found
  596. // as is without extensions added on.
  597. std::string hname = pathname;
  598. if (cmSystemTools::FileExists(hname.c_str())) {
  599. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  600. if (!sf->SourceName.empty()) {
  601. sf->SourceName += "/";
  602. }
  603. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  604. std::string::size_type pos = hname.rfind('.');
  605. if (pos != std::string::npos) {
  606. sf->SourceExtension = hname.substr(pos + 1, hname.size() - pos);
  607. if (cmSystemTools::FileIsFullPath(name)) {
  608. std::string::size_type pos2 = hname.rfind('/');
  609. if (pos2 != std::string::npos) {
  610. sf->SourceName = hname.substr(pos2 + 1, pos - pos2 - 1);
  611. }
  612. }
  613. }
  614. sf->FullPath = hname;
  615. return;
  616. }
  617. // Next, try the various source extensions
  618. for (std::vector<std::string>::const_iterator ext = sourceExts.begin();
  619. ext != sourceExts.end(); ++ext) {
  620. hname = pathname;
  621. hname += ".";
  622. hname += *ext;
  623. if (cmSystemTools::FileExists(hname.c_str())) {
  624. sf->SourceExtension = *ext;
  625. sf->FullPath = hname;
  626. return;
  627. }
  628. }
  629. // Finally, try the various header extensions
  630. for (std::vector<std::string>::const_iterator ext = headerExts.begin();
  631. ext != headerExts.end(); ++ext) {
  632. hname = pathname;
  633. hname += ".";
  634. hname += *ext;
  635. if (cmSystemTools::FileExists(hname.c_str())) {
  636. sf->SourceExtension = *ext;
  637. sf->FullPath = hname;
  638. return;
  639. }
  640. }
  641. std::ostringstream e;
  642. e << "Cannot find source file \"" << pathname << "\"";
  643. e << "\n\nTried extensions";
  644. for (std::vector<std::string>::const_iterator ext = sourceExts.begin();
  645. ext != sourceExts.end(); ++ext) {
  646. e << " ." << *ext;
  647. }
  648. for (std::vector<std::string>::const_iterator ext = headerExts.begin();
  649. ext != headerExts.end(); ++ext) {
  650. e << " ." << *ext;
  651. }
  652. cmSystemTools::Error(e.str().c_str());
  653. }
  654. void CCONV cmSourceFileSetName2(void* arg, const char* name, const char* dir,
  655. const char* ext, int headerFileOnly)
  656. {
  657. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  658. if (sf->RealSourceFile) {
  659. // SetName is allowed only on temporary source files created by
  660. // the command for building and passing to AddSource.
  661. return;
  662. }
  663. // Implement the old SetName method code here.
  664. if (headerFileOnly) {
  665. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1");
  666. }
  667. sf->SourceName = name;
  668. std::string fname = sf->SourceName;
  669. if (ext && strlen(ext)) {
  670. fname += ".";
  671. fname += ext;
  672. }
  673. sf->FullPath = cmSystemTools::CollapseFullPath(fname, dir);
  674. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  675. sf->SourceExtension = ext;
  676. }
  677. char* CCONV cmGetFilenameWithoutExtension(const char* name)
  678. {
  679. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  680. return strdup(sres.c_str());
  681. }
  682. char* CCONV cmGetFilenamePath(const char* name)
  683. {
  684. std::string sres = cmSystemTools::GetFilenamePath(name);
  685. return strdup(sres.c_str());
  686. }
  687. char* CCONV cmCapitalized(const char* name)
  688. {
  689. std::string sres = cmSystemTools::Capitalized(name);
  690. return strdup(sres.c_str());
  691. }
  692. void CCONV cmCopyFileIfDifferent(const char* name1, const char* name2)
  693. {
  694. cmSystemTools::CopyFileIfDifferent(name1, name2);
  695. }
  696. void CCONV cmRemoveFile(const char* name)
  697. {
  698. cmSystemTools::RemoveFile(name);
  699. }
  700. void CCONV cmDisplayStatus(void* arg, const char* message)
  701. {
  702. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  703. mf->DisplayStatus(message, -1);
  704. }
  705. void CCONV cmFree(void* data)
  706. {
  707. free(data);
  708. }
  709. void CCONV DefineSourceFileProperty(void* arg, const char* name,
  710. const char* briefDocs,
  711. const char* longDocs, int chained)
  712. {
  713. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  714. mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE, briefDocs,
  715. longDocs, chained != 0);
  716. }
  717. } // close the extern "C" scope
  718. cmCAPI cmStaticCAPI = {
  719. cmGetClientData,
  720. cmGetTotalArgumentSize,
  721. cmFreeArguments,
  722. cmSetClientData,
  723. cmSetError,
  724. cmAddCacheDefinition,
  725. cmAddCustomCommand,
  726. cmAddDefineFlag,
  727. cmAddDefinition,
  728. cmAddExecutable,
  729. cmAddLibrary,
  730. cmAddLinkDirectoryForTarget,
  731. cmAddLinkLibraryForTarget,
  732. cmAddUtilityCommand,
  733. cmCommandExists,
  734. cmExecuteCommand,
  735. cmExpandSourceListArguments,
  736. cmExpandVariablesInString,
  737. cmGetCacheMajorVersion,
  738. cmGetCacheMinorVersion,
  739. cmGetCurrentDirectory,
  740. cmGetCurrentOutputDirectory,
  741. cmGetDefinition,
  742. cmGetHomeDirectory,
  743. cmGetHomeOutputDirectory,
  744. cmGetMajorVersion,
  745. cmGetMinorVersion,
  746. cmGetProjectName,
  747. cmGetStartDirectory,
  748. cmGetStartOutputDirectory,
  749. cmIsOn,
  750. cmAddSource,
  751. cmCreateSourceFile,
  752. cmDestroySourceFile,
  753. cmGetSource,
  754. cmSourceFileAddDepend,
  755. cmSourceFileGetProperty,
  756. cmSourceFileGetPropertyAsBool,
  757. cmSourceFileGetSourceName,
  758. cmSourceFileGetFullPath,
  759. cmSourceFileSetName,
  760. cmSourceFileSetName2,
  761. cmSourceFileSetProperty,
  762. cmCapitalized,
  763. cmCopyFileIfDifferent,
  764. cmGetFilenameWithoutExtension,
  765. cmGetFilenamePath,
  766. cmRemoveFile,
  767. cmFree,
  768. cmAddCustomCommandToOutput,
  769. cmAddCustomCommandToTarget,
  770. cmDisplayStatus,
  771. cmCreateNewSourceFile,
  772. DefineSourceFileProperty,
  773. };