cmCPluginAPI.cxx 26 KB

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