cmCPluginAPI.cxx 25 KB

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