cmCPluginAPI.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. /*
  4. this file contains the implementation of the C API to CMake. Generally
  5. these routines just manipulate arguments and then call the associated
  6. methods on the CMake classes. */
  7. #include "cmCPluginAPI.h"
  8. #include <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->SetDepends(depends2);
  272. cc->SetCommandLines(commandLines);
  273. mf->AddCustomCommandToOutput(main_dependency, std::move(cc));
  274. }
  275. static void CCONV cmAddCustomCommandToTarget(void* arg, const char* target,
  276. const char* command, int numArgs,
  277. const char** args,
  278. int commandType)
  279. {
  280. // Get the makefile instance.
  281. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  282. // Construct the command line for the command. Perform an extra
  283. // variable expansion now because the API caller expects it.
  284. cmCustomCommandLine commandLine;
  285. std::string expand = command;
  286. commandLine.push_back(mf->ExpandVariablesInString(expand));
  287. for (int i = 0; i < numArgs; ++i) {
  288. expand = args[i];
  289. commandLine.push_back(mf->ExpandVariablesInString(expand));
  290. }
  291. cmCustomCommandLines commandLines;
  292. commandLines.push_back(commandLine);
  293. // Select the command type.
  294. cmCustomCommandType cctype = cmCustomCommandType::POST_BUILD;
  295. switch (commandType) {
  296. case CM_PRE_BUILD:
  297. cctype = cmCustomCommandType::PRE_BUILD;
  298. break;
  299. case CM_PRE_LINK:
  300. cctype = cmCustomCommandType::PRE_LINK;
  301. break;
  302. case CM_POST_BUILD:
  303. cctype = cmCustomCommandType::POST_BUILD;
  304. break;
  305. }
  306. // Pass the call to the makefile instance.
  307. auto cc = cm::make_unique<cmCustomCommand>();
  308. cc->SetCommandLines(commandLines);
  309. mf->AddCustomCommandToTarget(target, cctype, std::move(cc));
  310. }
  311. static void addLinkLibrary(cmMakefile* mf, std::string const& target,
  312. std::string const& lib, cmTargetLinkLibraryType llt)
  313. {
  314. cmTarget* t = mf->FindLocalNonAliasTarget(target);
  315. if (!t) {
  316. std::ostringstream e;
  317. e << "Attempt to add link library \"" << lib << "\" to target \"" << target
  318. << "\" which is not built in this directory.";
  319. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  320. return;
  321. }
  322. cmTarget* tgt = mf->GetGlobalGenerator()->FindTarget(lib);
  323. if (tgt && (tgt->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  324. (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  325. (tgt->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  326. !tgt->IsExecutableWithExports()) {
  327. std::ostringstream e;
  328. e << "Target \"" << lib << "\" of type "
  329. << cmState::GetTargetTypeName(tgt->GetType())
  330. << " may not be linked into another target. "
  331. << "One may link only to STATIC or SHARED libraries, or "
  332. << "to executables with the ENABLE_EXPORTS property set.";
  333. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  334. }
  335. t->AddLinkLibrary(*mf, lib, llt);
  336. }
  337. static void CCONV cmAddLinkLibraryForTarget(void* arg, const char* tgt,
  338. const char* value, int libtype)
  339. {
  340. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  341. switch (libtype) {
  342. case CM_LIBRARY_GENERAL:
  343. addLinkLibrary(mf, tgt, value, GENERAL_LibraryType);
  344. break;
  345. case CM_LIBRARY_DEBUG:
  346. addLinkLibrary(mf, tgt, value, DEBUG_LibraryType);
  347. break;
  348. case CM_LIBRARY_OPTIMIZED:
  349. addLinkLibrary(mf, tgt, value, OPTIMIZED_LibraryType);
  350. break;
  351. }
  352. }
  353. static void CCONV cmAddLibrary(void* arg, const char* libname, int shared,
  354. int numSrcs, const char** srcs)
  355. {
  356. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  357. std::vector<std::string> srcs2;
  358. int i;
  359. for (i = 0; i < numSrcs; ++i) {
  360. srcs2.emplace_back(srcs[i]);
  361. }
  362. mf->AddLibrary(
  363. libname,
  364. (shared ? cmStateEnums::SHARED_LIBRARY : cmStateEnums::STATIC_LIBRARY),
  365. srcs2);
  366. }
  367. static char CCONV* cmExpandVariablesInString(void* arg, const char* source,
  368. int escapeQuotes, int atOnly)
  369. {
  370. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  371. std::string barf = source;
  372. std::string const& result =
  373. mf->ExpandVariablesInString(barf, escapeQuotes != 0, atOnly != 0);
  374. return strdup(result.c_str());
  375. }
  376. static int CCONV cmExecuteCommand(void* arg, const char* name, int numArgs,
  377. const char** args)
  378. {
  379. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  380. std::vector<cmListFileArgument> lffArgs;
  381. lffArgs.reserve(numArgs);
  382. for (int i = 0; i < numArgs; ++i) {
  383. // Assume all arguments are quoted.
  384. lffArgs.emplace_back(args[i], cmListFileArgument::Quoted, 0);
  385. }
  386. cmListFileFunction lff{ name, 0, std::move(lffArgs) };
  387. cmExecutionStatus status(*mf);
  388. return mf->ExecuteCommand(lff, status);
  389. }
  390. static void CCONV cmExpandSourceListArguments(void* arg, int numArgs,
  391. const char** args, int* resArgc,
  392. char*** resArgv,
  393. unsigned int startArgumentIndex)
  394. {
  395. (void)arg;
  396. (void)startArgumentIndex;
  397. std::vector<std::string> result;
  398. int i;
  399. for (i = 0; i < numArgs; ++i) {
  400. result.emplace_back(args[i]);
  401. }
  402. int resargc = static_cast<int>(result.size());
  403. char** resargv = nullptr;
  404. if (resargc) {
  405. resargv = (char**)malloc(resargc * sizeof(char*));
  406. }
  407. for (i = 0; i < resargc; ++i) {
  408. resargv[i] = strdup(result[i].c_str());
  409. }
  410. *resArgc = resargc;
  411. *resArgv = resargv;
  412. }
  413. static void CCONV cmFreeArguments(int argc, char** argv)
  414. {
  415. int i;
  416. for (i = 0; i < argc; ++i) {
  417. free(argv[i]);
  418. }
  419. free(argv);
  420. }
  421. static int CCONV cmGetTotalArgumentSize(int argc, char** argv)
  422. {
  423. int i;
  424. int result = 0;
  425. for (i = 0; i < argc; ++i) {
  426. if (argv[i]) {
  427. result = result + static_cast<int>(strlen(argv[i]));
  428. }
  429. }
  430. return result;
  431. }
  432. // Source file proxy object to support the old cmSourceFile/cmMakefile
  433. // API for source files.
  434. struct cmCPluginAPISourceFile
  435. {
  436. cmSourceFile* RealSourceFile = nullptr;
  437. std::string SourceName;
  438. std::string SourceExtension;
  439. std::string FullPath;
  440. std::vector<std::string> Depends;
  441. cmPropertyMap Properties;
  442. };
  443. // Keep a map from real cmSourceFile instances stored in a makefile to
  444. // the CPluginAPI proxy source file.
  445. using cmCPluginAPISourceFileMap =
  446. std::map<cmSourceFile*, std::unique_ptr<cmCPluginAPISourceFile>>;
  447. static cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
  448. static void* CCONV cmCreateSourceFile()
  449. {
  450. return new cmCPluginAPISourceFile;
  451. }
  452. static void* CCONV cmCreateNewSourceFile(void*)
  453. {
  454. return new cmCPluginAPISourceFile;
  455. }
  456. static void CCONV cmDestroySourceFile(void* arg)
  457. {
  458. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  459. // Only delete if it was created by cmCreateSourceFile or
  460. // cmCreateNewSourceFile and is therefore not in the map.
  461. if (!sf->RealSourceFile) {
  462. delete sf;
  463. }
  464. }
  465. static void CCONV* cmGetSource(void* arg, const char* name)
  466. {
  467. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  468. if (cmSourceFile* rsf = mf->GetSource(name)) {
  469. // Lookup the proxy source file object for this source.
  470. auto i = cmCPluginAPISourceFiles.find(rsf);
  471. if (i == cmCPluginAPISourceFiles.end()) {
  472. // Create a proxy source file object for this source.
  473. auto sf = cm::make_unique<cmCPluginAPISourceFile>();
  474. sf->RealSourceFile = rsf;
  475. sf->FullPath = rsf->ResolveFullPath();
  476. sf->SourceName =
  477. cmSystemTools::GetFilenameWithoutLastExtension(sf->FullPath);
  478. sf->SourceExtension =
  479. cmSystemTools::GetFilenameLastExtension(sf->FullPath);
  480. // Store the proxy in the map so it can be re-used and deleted later.
  481. i = cmCPluginAPISourceFiles.emplace(rsf, std::move(sf)).first;
  482. }
  483. return i->second.get();
  484. }
  485. return nullptr;
  486. }
  487. static void* CCONV cmAddSource(void* arg, void* arg2)
  488. {
  489. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  490. cmCPluginAPISourceFile* osf = static_cast<cmCPluginAPISourceFile*>(arg2);
  491. if (osf->FullPath.empty()) {
  492. return nullptr;
  493. }
  494. // Create the real cmSourceFile instance and copy over saved information.
  495. cmSourceFile* rsf = mf->GetOrCreateSource(osf->FullPath);
  496. rsf->SetProperties(osf->Properties);
  497. // In case the properties contain the GENERATED property,
  498. // mark the real cmSourceFile as generated.
  499. if (rsf->GetIsGenerated()) {
  500. rsf->MarkAsGenerated();
  501. }
  502. for (std::string const& d : osf->Depends) {
  503. rsf->AddDepend(d);
  504. }
  505. // Create the proxy for the real source file.
  506. auto sf = cm::make_unique<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. auto* value = sf.get();
  513. cmCPluginAPISourceFiles[rsf] = std::move(sf);
  514. return value;
  515. }
  516. static const char* CCONV cmSourceFileGetSourceName(void* arg)
  517. {
  518. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  519. return sf->SourceName.c_str();
  520. }
  521. static const char* CCONV cmSourceFileGetFullPath(void* arg)
  522. {
  523. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  524. return sf->FullPath.c_str();
  525. }
  526. static const char* CCONV cmSourceFileGetProperty(void* arg, const char* prop)
  527. {
  528. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  529. if (cmSourceFile* rsf = sf->RealSourceFile) {
  530. return rsf->GetProperty(prop).GetCStr();
  531. }
  532. if (!strcmp(prop, "LOCATION")) {
  533. return sf->FullPath.c_str();
  534. }
  535. return sf->Properties.GetPropertyValue(prop).GetCStr();
  536. }
  537. static int CCONV cmSourceFileGetPropertyAsBool(void* arg, const char* prop)
  538. {
  539. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  540. if (cmSourceFile* rsf = sf->RealSourceFile) {
  541. return rsf->GetPropertyAsBool(prop) ? 1 : 0;
  542. }
  543. return cmIsOn(cmSourceFileGetProperty(arg, prop)) ? 1 : 0;
  544. }
  545. static void CCONV cmSourceFileSetProperty(void* arg, const char* prop,
  546. const char* value)
  547. {
  548. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  549. if (cmSourceFile* rsf = sf->RealSourceFile) {
  550. rsf->SetProperty(prop, value);
  551. } else if (prop) {
  552. if (!value) {
  553. value = "NOTFOUND";
  554. }
  555. sf->Properties.SetProperty(prop, value);
  556. }
  557. }
  558. static void CCONV cmSourceFileAddDepend(void* arg, const char* depend)
  559. {
  560. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  561. if (cmSourceFile* rsf = sf->RealSourceFile) {
  562. rsf->AddDepend(depend);
  563. } else {
  564. sf->Depends.emplace_back(depend);
  565. }
  566. }
  567. static void CCONV cmSourceFileSetName(void* arg, const char* name,
  568. const char* dir, int numSourceExtensions,
  569. const char** sourceExtensions,
  570. int numHeaderExtensions,
  571. const char** headerExtensions)
  572. {
  573. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  574. if (sf->RealSourceFile) {
  575. // SetName is allowed only on temporary source files created by
  576. // the command for building and passing to AddSource.
  577. return;
  578. }
  579. std::vector<std::string> sourceExts;
  580. std::vector<std::string> headerExts;
  581. int i;
  582. for (i = 0; i < numSourceExtensions; ++i) {
  583. sourceExts.emplace_back(sourceExtensions[i]);
  584. }
  585. for (i = 0; i < numHeaderExtensions; ++i) {
  586. headerExts.emplace_back(headerExtensions[i]);
  587. }
  588. // Save the original name given.
  589. sf->SourceName = name;
  590. // Convert the name to a full path in case the given name is a
  591. // relative path.
  592. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  593. // First try and see whether the listed file can be found
  594. // as is without extensions added on.
  595. std::string hname = pathname;
  596. if (cmSystemTools::FileExists(hname)) {
  597. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  598. if (!sf->SourceName.empty()) {
  599. sf->SourceName += "/";
  600. }
  601. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  602. std::string::size_type pos = hname.rfind('.');
  603. if (pos != std::string::npos) {
  604. sf->SourceExtension = hname.substr(pos + 1, hname.size() - pos);
  605. if (cmSystemTools::FileIsFullPath(name)) {
  606. std::string::size_type pos2 = hname.rfind('/');
  607. if (pos2 != std::string::npos) {
  608. sf->SourceName = hname.substr(pos2 + 1, pos - pos2 - 1);
  609. }
  610. }
  611. }
  612. sf->FullPath = hname;
  613. return;
  614. }
  615. // Next, try the various source extensions
  616. for (std::string const& ext : sourceExts) {
  617. hname = cmStrCat(pathname, '.', ext);
  618. if (cmSystemTools::FileExists(hname)) {
  619. sf->SourceExtension = ext;
  620. sf->FullPath = hname;
  621. return;
  622. }
  623. }
  624. // Finally, try the various header extensions
  625. for (std::string const& ext : headerExts) {
  626. hname = cmStrCat(pathname, '.', ext);
  627. if (cmSystemTools::FileExists(hname)) {
  628. sf->SourceExtension = ext;
  629. sf->FullPath = hname;
  630. return;
  631. }
  632. }
  633. std::ostringstream e;
  634. e << "Cannot find source file \"" << pathname << "\"";
  635. e << "\n\nTried extensions";
  636. for (std::string const& ext : sourceExts) {
  637. e << " ." << ext;
  638. }
  639. for (std::string const& ext : headerExts) {
  640. e << " ." << ext;
  641. }
  642. cmSystemTools::Error(e.str());
  643. }
  644. static void CCONV cmSourceFileSetName2(void* arg, const char* name,
  645. const char* dir, const char* ext,
  646. int headerFileOnly)
  647. {
  648. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  649. if (sf->RealSourceFile) {
  650. // SetName is allowed only on temporary source files created by
  651. // the command for building and passing to AddSource.
  652. return;
  653. }
  654. // Implement the old SetName method code here.
  655. if (headerFileOnly) {
  656. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1");
  657. }
  658. sf->SourceName = name;
  659. std::string fname = sf->SourceName;
  660. if (cmNonempty(ext)) {
  661. fname += ".";
  662. fname += ext;
  663. }
  664. sf->FullPath = cmSystemTools::CollapseFullPath(fname, dir);
  665. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  666. sf->SourceExtension = ext;
  667. }
  668. static char* CCONV cmGetFilenameWithoutExtension(const char* name)
  669. {
  670. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  671. return strdup(sres.c_str());
  672. }
  673. static char* CCONV cmGetFilenamePath(const char* name)
  674. {
  675. std::string sres = cmSystemTools::GetFilenamePath(name);
  676. return strdup(sres.c_str());
  677. }
  678. static char* CCONV cmCapitalized(const char* name)
  679. {
  680. std::string sres = cmSystemTools::Capitalized(name);
  681. return strdup(sres.c_str());
  682. }
  683. static void CCONV cmCopyFileIfDifferent(const char* name1, const char* name2)
  684. {
  685. cmSystemTools::CopyFileIfDifferent(name1, name2);
  686. }
  687. static void CCONV cmRemoveFile(const char* name)
  688. {
  689. cmSystemTools::RemoveFile(name);
  690. }
  691. static void CCONV cmDisplayStatus(void* arg, const char* message)
  692. {
  693. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  694. mf->DisplayStatus(message, -1);
  695. }
  696. static void CCONV cmFree(void* data)
  697. {
  698. free(data);
  699. }
  700. static void CCONV DefineSourceFileProperty(void* arg, const char* name,
  701. const char* briefDocs,
  702. const char* longDocs, int chained)
  703. {
  704. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  705. mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE,
  706. briefDocs ? briefDocs : "",
  707. longDocs ? longDocs : "", chained != 0);
  708. }
  709. } // close the extern "C" scope
  710. static cmCAPI cmStaticCAPI = {
  711. cmGetClientData,
  712. cmGetTotalArgumentSize,
  713. cmFreeArguments,
  714. cmSetClientData,
  715. cmSetError,
  716. cmAddCacheDefinition,
  717. cmAddCustomCommand,
  718. cmAddDefineFlag,
  719. cmAddDefinition,
  720. cmAddExecutable,
  721. cmAddLibrary,
  722. cmAddLinkDirectoryForTarget,
  723. cmAddLinkLibraryForTarget,
  724. cmAddUtilityCommand,
  725. cmCommandExists,
  726. cmExecuteCommand,
  727. cmExpandSourceListArguments,
  728. cmExpandVariablesInString,
  729. cmGetCacheMajorVersion,
  730. cmGetCacheMinorVersion,
  731. cmGetCurrentDirectory,
  732. cmGetCurrentOutputDirectory,
  733. cmGetDefinition,
  734. cmGetHomeDirectory,
  735. cmGetHomeOutputDirectory,
  736. cmGetMajorVersion,
  737. cmGetMinorVersion,
  738. cmGetProjectName,
  739. cmGetStartDirectory,
  740. cmGetStartOutputDirectory,
  741. cmIsOn,
  742. cmAddSource,
  743. cmCreateSourceFile,
  744. cmDestroySourceFile,
  745. cmGetSource,
  746. cmSourceFileAddDepend,
  747. cmSourceFileGetProperty,
  748. cmSourceFileGetPropertyAsBool,
  749. cmSourceFileGetSourceName,
  750. cmSourceFileGetFullPath,
  751. cmSourceFileSetName,
  752. cmSourceFileSetName2,
  753. cmSourceFileSetProperty,
  754. cmCapitalized,
  755. cmCopyFileIfDifferent,
  756. cmGetFilenameWithoutExtension,
  757. cmGetFilenamePath,
  758. cmRemoveFile,
  759. cmFree,
  760. cmAddCustomCommandToOutput,
  761. cmAddCustomCommandToTarget,
  762. cmDisplayStatus,
  763. cmCreateNewSourceFile,
  764. DefineSourceFileProperty,
  765. };