cmCPluginAPI.cxx 25 KB

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