cmCPluginAPI.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 ? 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. cmProp p = rsf->GetProperty(prop);
  521. return p ? p->c_str() : nullptr;
  522. }
  523. if (!strcmp(prop, "LOCATION")) {
  524. return sf->FullPath.c_str();
  525. }
  526. cmProp retVal = sf->Properties.GetPropertyValue(prop);
  527. return retVal ? retVal->c_str() : nullptr;
  528. }
  529. int CCONV cmSourceFileGetPropertyAsBool(void* arg, const char* prop)
  530. {
  531. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  532. if (cmSourceFile* rsf = sf->RealSourceFile) {
  533. return rsf->GetPropertyAsBool(prop) ? 1 : 0;
  534. }
  535. return cmIsOn(cmSourceFileGetProperty(arg, prop)) ? 1 : 0;
  536. }
  537. void CCONV cmSourceFileSetProperty(void* arg, const char* prop,
  538. const char* value)
  539. {
  540. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  541. if (cmSourceFile* rsf = sf->RealSourceFile) {
  542. rsf->SetProperty(prop, value);
  543. } else if (prop) {
  544. if (!value) {
  545. value = "NOTFOUND";
  546. }
  547. sf->Properties.SetProperty(prop, value);
  548. }
  549. }
  550. void CCONV cmSourceFileAddDepend(void* arg, const char* depend)
  551. {
  552. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  553. if (cmSourceFile* rsf = sf->RealSourceFile) {
  554. rsf->AddDepend(depend);
  555. } else {
  556. sf->Depends.emplace_back(depend);
  557. }
  558. }
  559. void CCONV cmSourceFileSetName(void* arg, const char* name, const char* dir,
  560. int numSourceExtensions,
  561. const char** sourceExtensions,
  562. int numHeaderExtensions,
  563. const char** headerExtensions)
  564. {
  565. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  566. if (sf->RealSourceFile) {
  567. // SetName is allowed only on temporary source files created by
  568. // the command for building and passing to AddSource.
  569. return;
  570. }
  571. std::vector<std::string> sourceExts;
  572. std::vector<std::string> headerExts;
  573. int i;
  574. for (i = 0; i < numSourceExtensions; ++i) {
  575. sourceExts.emplace_back(sourceExtensions[i]);
  576. }
  577. for (i = 0; i < numHeaderExtensions; ++i) {
  578. headerExts.emplace_back(headerExtensions[i]);
  579. }
  580. // Save the original name given.
  581. sf->SourceName = name;
  582. // Convert the name to a full path in case the given name is a
  583. // relative path.
  584. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  585. // First try and see whether the listed file can be found
  586. // as is without extensions added on.
  587. std::string hname = pathname;
  588. if (cmSystemTools::FileExists(hname)) {
  589. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  590. if (!sf->SourceName.empty()) {
  591. sf->SourceName += "/";
  592. }
  593. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  594. std::string::size_type pos = hname.rfind('.');
  595. if (pos != std::string::npos) {
  596. sf->SourceExtension = hname.substr(pos + 1, hname.size() - pos);
  597. if (cmSystemTools::FileIsFullPath(name)) {
  598. std::string::size_type pos2 = hname.rfind('/');
  599. if (pos2 != std::string::npos) {
  600. sf->SourceName = hname.substr(pos2 + 1, pos - pos2 - 1);
  601. }
  602. }
  603. }
  604. sf->FullPath = hname;
  605. return;
  606. }
  607. // Next, try the various source extensions
  608. for (std::string const& ext : sourceExts) {
  609. hname = cmStrCat(pathname, '.', ext);
  610. if (cmSystemTools::FileExists(hname)) {
  611. sf->SourceExtension = ext;
  612. sf->FullPath = hname;
  613. return;
  614. }
  615. }
  616. // Finally, try the various header extensions
  617. for (std::string const& ext : headerExts) {
  618. hname = cmStrCat(pathname, '.', ext);
  619. if (cmSystemTools::FileExists(hname)) {
  620. sf->SourceExtension = ext;
  621. sf->FullPath = hname;
  622. return;
  623. }
  624. }
  625. std::ostringstream e;
  626. e << "Cannot find source file \"" << pathname << "\"";
  627. e << "\n\nTried extensions";
  628. for (std::string const& ext : sourceExts) {
  629. e << " ." << ext;
  630. }
  631. for (std::string const& ext : headerExts) {
  632. e << " ." << ext;
  633. }
  634. cmSystemTools::Error(e.str());
  635. }
  636. void CCONV cmSourceFileSetName2(void* arg, const char* name, const char* dir,
  637. const char* ext, int headerFileOnly)
  638. {
  639. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  640. if (sf->RealSourceFile) {
  641. // SetName is allowed only on temporary source files created by
  642. // the command for building and passing to AddSource.
  643. return;
  644. }
  645. // Implement the old SetName method code here.
  646. if (headerFileOnly) {
  647. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1");
  648. }
  649. sf->SourceName = name;
  650. std::string fname = sf->SourceName;
  651. if (ext && strlen(ext)) {
  652. fname += ".";
  653. fname += ext;
  654. }
  655. sf->FullPath = cmSystemTools::CollapseFullPath(fname, dir);
  656. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  657. sf->SourceExtension = ext;
  658. }
  659. char* CCONV cmGetFilenameWithoutExtension(const char* name)
  660. {
  661. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  662. return strdup(sres.c_str());
  663. }
  664. char* CCONV cmGetFilenamePath(const char* name)
  665. {
  666. std::string sres = cmSystemTools::GetFilenamePath(name);
  667. return strdup(sres.c_str());
  668. }
  669. char* CCONV cmCapitalized(const char* name)
  670. {
  671. std::string sres = cmSystemTools::Capitalized(name);
  672. return strdup(sres.c_str());
  673. }
  674. void CCONV cmCopyFileIfDifferent(const char* name1, const char* name2)
  675. {
  676. cmSystemTools::CopyFileIfDifferent(name1, name2);
  677. }
  678. void CCONV cmRemoveFile(const char* name)
  679. {
  680. cmSystemTools::RemoveFile(name);
  681. }
  682. void CCONV cmDisplayStatus(void* arg, const char* message)
  683. {
  684. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  685. mf->DisplayStatus(message, -1);
  686. }
  687. void CCONV cmFree(void* data)
  688. {
  689. free(data);
  690. }
  691. void CCONV DefineSourceFileProperty(void* arg, const char* name,
  692. const char* briefDocs,
  693. const char* longDocs, int chained)
  694. {
  695. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  696. mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE,
  697. briefDocs ? briefDocs : "",
  698. longDocs ? longDocs : "", chained != 0);
  699. }
  700. } // close the extern "C" scope
  701. cmCAPI cmStaticCAPI = {
  702. cmGetClientData,
  703. cmGetTotalArgumentSize,
  704. cmFreeArguments,
  705. cmSetClientData,
  706. cmSetError,
  707. cmAddCacheDefinition,
  708. cmAddCustomCommand,
  709. cmAddDefineFlag,
  710. cmAddDefinition,
  711. cmAddExecutable,
  712. cmAddLibrary,
  713. cmAddLinkDirectoryForTarget,
  714. cmAddLinkLibraryForTarget,
  715. cmAddUtilityCommand,
  716. cmCommandExists,
  717. cmExecuteCommand,
  718. cmExpandSourceListArguments,
  719. cmExpandVariablesInString,
  720. cmGetCacheMajorVersion,
  721. cmGetCacheMinorVersion,
  722. cmGetCurrentDirectory,
  723. cmGetCurrentOutputDirectory,
  724. cmGetDefinition,
  725. cmGetHomeDirectory,
  726. cmGetHomeOutputDirectory,
  727. cmGetMajorVersion,
  728. cmGetMinorVersion,
  729. cmGetProjectName,
  730. cmGetStartDirectory,
  731. cmGetStartOutputDirectory,
  732. cmIsOn,
  733. cmAddSource,
  734. cmCreateSourceFile,
  735. cmDestroySourceFile,
  736. cmGetSource,
  737. cmSourceFileAddDepend,
  738. cmSourceFileGetProperty,
  739. cmSourceFileGetPropertyAsBool,
  740. cmSourceFileGetSourceName,
  741. cmSourceFileGetFullPath,
  742. cmSourceFileSetName,
  743. cmSourceFileSetName2,
  744. cmSourceFileSetProperty,
  745. cmCapitalized,
  746. cmCopyFileIfDifferent,
  747. cmGetFilenameWithoutExtension,
  748. cmGetFilenamePath,
  749. cmRemoveFile,
  750. cmFree,
  751. cmAddCustomCommandToOutput,
  752. cmAddCustomCommandToTarget,
  753. cmDisplayStatus,
  754. cmCreateNewSourceFile,
  755. DefineSourceFileProperty,
  756. };