cmCPluginAPI.cxx 26 KB

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