cmCPluginAPI.cxx 25 KB

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