cmCPluginAPI.cxx 25 KB

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