cmCPluginAPI.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. void CCONV* cmGetClientData(void* info)
  20. {
  21. return ((cmLoadedCommandInfo*)info)->ClientData;
  22. }
  23. void CCONV cmSetClientData(void* info, void* cd)
  24. {
  25. ((cmLoadedCommandInfo*)info)->ClientData = cd;
  26. }
  27. void CCONV cmSetError(void* info, const char* err)
  28. {
  29. if (((cmLoadedCommandInfo*)info)->Error) {
  30. free(((cmLoadedCommandInfo*)info)->Error);
  31. }
  32. ((cmLoadedCommandInfo*)info)->Error = strdup(err);
  33. }
  34. unsigned int CCONV cmGetCacheMajorVersion(void* arg)
  35. {
  36. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  37. cmState* state = mf->GetState();
  38. return state->GetCacheMajorVersion();
  39. }
  40. unsigned int CCONV cmGetCacheMinorVersion(void* arg)
  41. {
  42. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  43. cmState* state = mf->GetState();
  44. return state->GetCacheMinorVersion();
  45. }
  46. unsigned int CCONV cmGetMajorVersion(void*)
  47. {
  48. return cmVersion::GetMajorVersion();
  49. }
  50. unsigned int CCONV cmGetMinorVersion(void*)
  51. {
  52. return cmVersion::GetMinorVersion();
  53. }
  54. void CCONV cmAddDefinition(void* arg, const char* name, const char* value)
  55. {
  56. if (value) {
  57. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  58. mf->AddDefinition(name, value);
  59. }
  60. }
  61. /* Add a definition to this makefile and the global cmake cache. */
  62. void CCONV cmAddCacheDefinition(void* arg, const char* name, const char* value,
  63. const char* doc, int type)
  64. {
  65. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  66. switch (type) {
  67. case CM_CACHE_BOOL:
  68. mf->AddCacheDefinition(name, value, doc, cmStateEnums::BOOL);
  69. break;
  70. case CM_CACHE_PATH:
  71. mf->AddCacheDefinition(name, value, doc, cmStateEnums::PATH);
  72. break;
  73. case CM_CACHE_FILEPATH:
  74. mf->AddCacheDefinition(name, value, doc, cmStateEnums::FILEPATH);
  75. break;
  76. case CM_CACHE_STRING:
  77. mf->AddCacheDefinition(name, value, doc, cmStateEnums::STRING);
  78. break;
  79. case CM_CACHE_INTERNAL:
  80. mf->AddCacheDefinition(name, value, doc, cmStateEnums::INTERNAL);
  81. break;
  82. case CM_CACHE_STATIC:
  83. mf->AddCacheDefinition(name, value, doc, cmStateEnums::STATIC);
  84. break;
  85. }
  86. }
  87. const char* CCONV cmGetProjectName(void* arg)
  88. {
  89. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  90. static std::string name;
  91. name = mf->GetStateSnapshot().GetProjectName();
  92. return name.c_str();
  93. }
  94. const char* CCONV cmGetHomeDirectory(void* arg)
  95. {
  96. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  97. return mf->GetHomeDirectory().c_str();
  98. }
  99. const char* CCONV cmGetHomeOutputDirectory(void* arg)
  100. {
  101. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  102. return mf->GetHomeOutputDirectory().c_str();
  103. }
  104. const char* CCONV cmGetStartDirectory(void* arg)
  105. {
  106. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  107. return mf->GetCurrentSourceDirectory().c_str();
  108. }
  109. const char* CCONV cmGetStartOutputDirectory(void* arg)
  110. {
  111. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  112. return mf->GetCurrentBinaryDirectory().c_str();
  113. }
  114. const char* CCONV cmGetCurrentDirectory(void* arg)
  115. {
  116. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  117. return mf->GetCurrentSourceDirectory().c_str();
  118. }
  119. const char* CCONV cmGetCurrentOutputDirectory(void* arg)
  120. {
  121. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  122. return mf->GetCurrentBinaryDirectory().c_str();
  123. }
  124. const char* CCONV cmGetDefinition(void* arg, const char* def)
  125. {
  126. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  127. return cmToCStr(mf->GetDefinition(def));
  128. }
  129. int CCONV cmIsOn(void* arg, const char* name)
  130. {
  131. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  132. return static_cast<int>(mf->IsOn(name));
  133. }
  134. /** Check if a command exists. */
  135. int CCONV cmCommandExists(void* arg, const char* name)
  136. {
  137. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  138. return static_cast<int>(mf->GetState()->GetCommand(name) ? 1 : 0);
  139. }
  140. void CCONV cmAddDefineFlag(void* arg, const char* definition)
  141. {
  142. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  143. mf->AddDefineFlag(definition);
  144. }
  145. void CCONV cmAddLinkDirectoryForTarget(void* arg, const char* tgt,
  146. const char* d)
  147. {
  148. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  149. cmTarget* t = mf->FindLocalNonAliasTarget(tgt);
  150. if (!t) {
  151. cmSystemTools::Error(
  152. "Attempt to add link directories to non-existent target: " +
  153. std::string(tgt) + " for directory " + std::string(d));
  154. return;
  155. }
  156. t->InsertLinkDirectory(d, mf->GetBacktrace());
  157. }
  158. void CCONV cmAddExecutable(void* arg, const char* exename, int numSrcs,
  159. const char** srcs, int win32)
  160. {
  161. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  162. std::vector<std::string> srcs2;
  163. int i;
  164. for (i = 0; i < numSrcs; ++i) {
  165. srcs2.emplace_back(srcs[i]);
  166. }
  167. cmTarget* tg = mf->AddExecutable(exename, srcs2);
  168. if (win32) {
  169. tg->SetProperty("WIN32_EXECUTABLE", "ON");
  170. }
  171. }
  172. void CCONV cmAddUtilityCommand(void* arg, const char* utilityName,
  173. const char* command, const char* arguments,
  174. int all, int numDepends, const char** depends,
  175. int, const char**)
  176. {
  177. // Get the makefile instance. Perform an extra variable expansion
  178. // now because the API caller expects it.
  179. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  180. // Construct the command line for the command.
  181. cmCustomCommandLine commandLine;
  182. std::string expand = command;
  183. commandLine.push_back(mf->ExpandVariablesInString(expand));
  184. if (arguments && arguments[0]) {
  185. // TODO: Parse arguments!
  186. expand = arguments;
  187. commandLine.push_back(mf->ExpandVariablesInString(expand));
  188. }
  189. cmCustomCommandLines commandLines;
  190. commandLines.push_back(commandLine);
  191. // Accumulate the list of dependencies.
  192. std::vector<std::string> depends2;
  193. for (int i = 0; i < numDepends; ++i) {
  194. expand = depends[i];
  195. depends2.push_back(mf->ExpandVariablesInString(expand));
  196. }
  197. // Pass the call to the makefile instance.
  198. std::vector<std::string> no_byproducts;
  199. mf->AddUtilityCommand(utilityName, !all, nullptr, no_byproducts, depends2,
  200. commandLines,
  201. mf->GetPolicyStatus(cmPolicies::CMP0116));
  202. }
  203. void CCONV cmAddCustomCommand(void* arg, const char* source,
  204. const char* command, int numArgs,
  205. const char** args, int numDepends,
  206. const char** depends, int numOutputs,
  207. const char** outputs, const char* target)
  208. {
  209. // Get the makefile instance. Perform an extra variable expansion
  210. // now because the API caller expects it.
  211. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  212. // Construct the command line for the command.
  213. cmCustomCommandLine commandLine;
  214. std::string expand = command;
  215. commandLine.push_back(mf->ExpandVariablesInString(expand));
  216. for (int i = 0; i < numArgs; ++i) {
  217. expand = args[i];
  218. commandLine.push_back(mf->ExpandVariablesInString(expand));
  219. }
  220. cmCustomCommandLines commandLines;
  221. commandLines.push_back(commandLine);
  222. // Accumulate the list of dependencies.
  223. std::vector<std::string> depends2;
  224. for (int i = 0; i < numDepends; ++i) {
  225. expand = depends[i];
  226. depends2.push_back(mf->ExpandVariablesInString(expand));
  227. }
  228. // Accumulate the list of outputs.
  229. std::vector<std::string> outputs2;
  230. for (int i = 0; i < numOutputs; ++i) {
  231. expand = outputs[i];
  232. outputs2.push_back(mf->ExpandVariablesInString(expand));
  233. }
  234. // Pass the call to the makefile instance.
  235. const char* no_comment = nullptr;
  236. mf->AddCustomCommandOldStyle(target, outputs2, depends2, source,
  237. commandLines, no_comment,
  238. mf->GetPolicyStatus(cmPolicies::CMP0116));
  239. }
  240. void CCONV cmAddCustomCommandToOutput(void* arg, const char* output,
  241. const char* command, int numArgs,
  242. const char** args,
  243. const char* main_dependency,
  244. int numDepends, const char** depends)
  245. {
  246. // Get the makefile instance. Perform an extra variable expansion
  247. // now because the API caller expects it.
  248. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  249. // Construct the command line for the command.
  250. cmCustomCommandLine commandLine;
  251. std::string expand = command;
  252. commandLine.push_back(mf->ExpandVariablesInString(expand));
  253. for (int i = 0; i < numArgs; ++i) {
  254. expand = args[i];
  255. commandLine.push_back(mf->ExpandVariablesInString(expand));
  256. }
  257. cmCustomCommandLines commandLines;
  258. commandLines.push_back(commandLine);
  259. // Accumulate the list of dependencies.
  260. std::vector<std::string> depends2;
  261. for (int i = 0; i < numDepends; ++i) {
  262. expand = depends[i];
  263. depends2.push_back(mf->ExpandVariablesInString(expand));
  264. }
  265. // Pass the call to the makefile instance.
  266. const char* no_comment = nullptr;
  267. const char* no_working_dir = nullptr;
  268. mf->AddCustomCommandToOutput(output, depends2, main_dependency, commandLines,
  269. no_comment, no_working_dir,
  270. mf->GetPolicyStatus(cmPolicies::CMP0116));
  271. }
  272. void CCONV cmAddCustomCommandToTarget(void* arg, const char* target,
  273. const char* command, int numArgs,
  274. const char** args, int commandType)
  275. {
  276. // Get the makefile instance.
  277. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  278. // Construct the command line for the command. Perform an extra
  279. // variable expansion now because the API caller expects it.
  280. cmCustomCommandLine commandLine;
  281. std::string expand = command;
  282. commandLine.push_back(mf->ExpandVariablesInString(expand));
  283. for (int i = 0; i < numArgs; ++i) {
  284. expand = args[i];
  285. commandLine.push_back(mf->ExpandVariablesInString(expand));
  286. }
  287. cmCustomCommandLines commandLines;
  288. commandLines.push_back(commandLine);
  289. // Select the command type.
  290. cmCustomCommandType cctype = cmCustomCommandType::POST_BUILD;
  291. switch (commandType) {
  292. case CM_PRE_BUILD:
  293. cctype = cmCustomCommandType::PRE_BUILD;
  294. break;
  295. case CM_PRE_LINK:
  296. cctype = cmCustomCommandType::PRE_LINK;
  297. break;
  298. case CM_POST_BUILD:
  299. cctype = cmCustomCommandType::POST_BUILD;
  300. break;
  301. }
  302. // Pass the call to the makefile instance.
  303. std::vector<std::string> no_byproducts;
  304. std::vector<std::string> no_depends;
  305. const char* no_comment = nullptr;
  306. const char* no_working_dir = nullptr;
  307. mf->AddCustomCommandToTarget(target, no_byproducts, no_depends, commandLines,
  308. cctype, no_comment, no_working_dir,
  309. mf->GetPolicyStatus(cmPolicies::CMP0116));
  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. 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. 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. 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. 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. 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. 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. 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. cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
  448. void* CCONV cmCreateSourceFile(void)
  449. {
  450. return new cmCPluginAPISourceFile;
  451. }
  452. void* CCONV cmCreateNewSourceFile(void*)
  453. {
  454. return new cmCPluginAPISourceFile;
  455. }
  456. 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. 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. 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. const char* CCONV cmSourceFileGetSourceName(void* arg)
  517. {
  518. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  519. return sf->SourceName.c_str();
  520. }
  521. const char* CCONV cmSourceFileGetFullPath(void* arg)
  522. {
  523. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  524. return sf->FullPath.c_str();
  525. }
  526. 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 cmToCStr(rsf->GetProperty(prop));
  531. }
  532. if (!strcmp(prop, "LOCATION")) {
  533. return sf->FullPath.c_str();
  534. }
  535. return cmToCStr(sf->Properties.GetPropertyValue(prop));
  536. }
  537. 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. 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. 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. void CCONV cmSourceFileSetName(void* arg, const char* name, const char* dir,
  568. 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. void CCONV cmSourceFileSetName2(void* arg, const char* name, const char* dir,
  645. const char* ext, int headerFileOnly)
  646. {
  647. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  648. if (sf->RealSourceFile) {
  649. // SetName is allowed only on temporary source files created by
  650. // the command for building and passing to AddSource.
  651. return;
  652. }
  653. // Implement the old SetName method code here.
  654. if (headerFileOnly) {
  655. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1");
  656. }
  657. sf->SourceName = name;
  658. std::string fname = sf->SourceName;
  659. if (cmNonempty(ext)) {
  660. fname += ".";
  661. fname += ext;
  662. }
  663. sf->FullPath = cmSystemTools::CollapseFullPath(fname, dir);
  664. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  665. sf->SourceExtension = ext;
  666. }
  667. char* CCONV cmGetFilenameWithoutExtension(const char* name)
  668. {
  669. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  670. return strdup(sres.c_str());
  671. }
  672. char* CCONV cmGetFilenamePath(const char* name)
  673. {
  674. std::string sres = cmSystemTools::GetFilenamePath(name);
  675. return strdup(sres.c_str());
  676. }
  677. char* CCONV cmCapitalized(const char* name)
  678. {
  679. std::string sres = cmSystemTools::Capitalized(name);
  680. return strdup(sres.c_str());
  681. }
  682. void CCONV cmCopyFileIfDifferent(const char* name1, const char* name2)
  683. {
  684. cmSystemTools::CopyFileIfDifferent(name1, name2);
  685. }
  686. void CCONV cmRemoveFile(const char* name)
  687. {
  688. cmSystemTools::RemoveFile(name);
  689. }
  690. void CCONV cmDisplayStatus(void* arg, const char* message)
  691. {
  692. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  693. mf->DisplayStatus(message, -1);
  694. }
  695. void CCONV cmFree(void* data)
  696. {
  697. free(data);
  698. }
  699. void CCONV DefineSourceFileProperty(void* arg, const char* name,
  700. const char* briefDocs,
  701. const char* longDocs, int chained)
  702. {
  703. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  704. mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE,
  705. briefDocs ? briefDocs : "",
  706. longDocs ? longDocs : "", chained != 0);
  707. }
  708. } // close the extern "C" scope
  709. cmCAPI cmStaticCAPI = {
  710. cmGetClientData,
  711. cmGetTotalArgumentSize,
  712. cmFreeArguments,
  713. cmSetClientData,
  714. cmSetError,
  715. cmAddCacheDefinition,
  716. cmAddCustomCommand,
  717. cmAddDefineFlag,
  718. cmAddDefinition,
  719. cmAddExecutable,
  720. cmAddLibrary,
  721. cmAddLinkDirectoryForTarget,
  722. cmAddLinkLibraryForTarget,
  723. cmAddUtilityCommand,
  724. cmCommandExists,
  725. cmExecuteCommand,
  726. cmExpandSourceListArguments,
  727. cmExpandVariablesInString,
  728. cmGetCacheMajorVersion,
  729. cmGetCacheMinorVersion,
  730. cmGetCurrentDirectory,
  731. cmGetCurrentOutputDirectory,
  732. cmGetDefinition,
  733. cmGetHomeDirectory,
  734. cmGetHomeOutputDirectory,
  735. cmGetMajorVersion,
  736. cmGetMinorVersion,
  737. cmGetProjectName,
  738. cmGetStartDirectory,
  739. cmGetStartOutputDirectory,
  740. cmIsOn,
  741. cmAddSource,
  742. cmCreateSourceFile,
  743. cmDestroySourceFile,
  744. cmGetSource,
  745. cmSourceFileAddDepend,
  746. cmSourceFileGetProperty,
  747. cmSourceFileGetPropertyAsBool,
  748. cmSourceFileGetSourceName,
  749. cmSourceFileGetFullPath,
  750. cmSourceFileSetName,
  751. cmSourceFileSetName2,
  752. cmSourceFileSetProperty,
  753. cmCapitalized,
  754. cmCopyFileIfDifferent,
  755. cmGetFilenameWithoutExtension,
  756. cmGetFilenamePath,
  757. cmRemoveFile,
  758. cmFree,
  759. cmAddCustomCommandToOutput,
  760. cmAddCustomCommandToTarget,
  761. cmDisplayStatus,
  762. cmCreateNewSourceFile,
  763. DefineSourceFileProperty,
  764. };