cmCPluginAPI.cxx 25 KB

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