cmCPluginAPI.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. std::vector<cmListFileArgument> lffArgs;
  377. lffArgs.reserve(numArgs);
  378. for (int i = 0; i < numArgs; ++i) {
  379. // Assume all arguments are quoted.
  380. lffArgs.emplace_back(args[i], cmListFileArgument::Quoted, 0);
  381. }
  382. cmListFileFunction lff{ name, 0, std::move(lffArgs) };
  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. using cmCPluginAPISourceFileMap =
  442. std::map<cmSourceFile*, std::unique_ptr<cmCPluginAPISourceFile>>;
  443. cmCPluginAPISourceFileMap cmCPluginAPISourceFiles;
  444. void* CCONV cmCreateSourceFile(void)
  445. {
  446. return new cmCPluginAPISourceFile;
  447. }
  448. void* CCONV cmCreateNewSourceFile(void*)
  449. {
  450. return new cmCPluginAPISourceFile;
  451. }
  452. void CCONV cmDestroySourceFile(void* arg)
  453. {
  454. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  455. // Only delete if it was created by cmCreateSourceFile or
  456. // cmCreateNewSourceFile and is therefore not in the map.
  457. if (!sf->RealSourceFile) {
  458. delete sf;
  459. }
  460. }
  461. void CCONV* cmGetSource(void* arg, const char* name)
  462. {
  463. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  464. if (cmSourceFile* rsf = mf->GetSource(name)) {
  465. // Lookup the proxy source file object for this source.
  466. auto i = cmCPluginAPISourceFiles.find(rsf);
  467. if (i == cmCPluginAPISourceFiles.end()) {
  468. // Create a proxy source file object for this source.
  469. auto sf = cm::make_unique<cmCPluginAPISourceFile>();
  470. sf->RealSourceFile = rsf;
  471. sf->FullPath = rsf->ResolveFullPath();
  472. sf->SourceName =
  473. cmSystemTools::GetFilenameWithoutLastExtension(sf->FullPath);
  474. sf->SourceExtension =
  475. cmSystemTools::GetFilenameLastExtension(sf->FullPath);
  476. // Store the proxy in the map so it can be re-used and deleted later.
  477. i = cmCPluginAPISourceFiles.emplace(rsf, std::move(sf)).first;
  478. }
  479. return i->second.get();
  480. }
  481. return nullptr;
  482. }
  483. void* CCONV cmAddSource(void* arg, void* arg2)
  484. {
  485. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  486. cmCPluginAPISourceFile* osf = static_cast<cmCPluginAPISourceFile*>(arg2);
  487. if (osf->FullPath.empty()) {
  488. return nullptr;
  489. }
  490. // Create the real cmSourceFile instance and copy over saved information.
  491. cmSourceFile* rsf = mf->GetOrCreateSource(osf->FullPath);
  492. rsf->SetProperties(osf->Properties);
  493. for (std::string const& d : osf->Depends) {
  494. rsf->AddDepend(d);
  495. }
  496. // Create the proxy for the real source file.
  497. auto sf = cm::make_unique<cmCPluginAPISourceFile>();
  498. sf->RealSourceFile = rsf;
  499. sf->FullPath = osf->FullPath;
  500. sf->SourceName = osf->SourceName;
  501. sf->SourceExtension = osf->SourceExtension;
  502. // Store the proxy in the map so it can be re-used and deleted later.
  503. auto value = sf.get();
  504. cmCPluginAPISourceFiles[rsf] = std::move(sf);
  505. return value;
  506. }
  507. const char* CCONV cmSourceFileGetSourceName(void* arg)
  508. {
  509. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  510. return sf->SourceName.c_str();
  511. }
  512. const char* CCONV cmSourceFileGetFullPath(void* arg)
  513. {
  514. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  515. return sf->FullPath.c_str();
  516. }
  517. const char* CCONV cmSourceFileGetProperty(void* arg, const char* prop)
  518. {
  519. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  520. if (cmSourceFile* rsf = sf->RealSourceFile) {
  521. cmProp p = rsf->GetProperty(prop);
  522. return cmToCStr(p);
  523. }
  524. if (!strcmp(prop, "LOCATION")) {
  525. return sf->FullPath.c_str();
  526. }
  527. cmProp retVal = sf->Properties.GetPropertyValue(prop);
  528. return cmToCStr(retVal);
  529. }
  530. int CCONV cmSourceFileGetPropertyAsBool(void* arg, const char* prop)
  531. {
  532. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  533. if (cmSourceFile* rsf = sf->RealSourceFile) {
  534. return rsf->GetPropertyAsBool(prop) ? 1 : 0;
  535. }
  536. return cmIsOn(cmSourceFileGetProperty(arg, prop)) ? 1 : 0;
  537. }
  538. void CCONV cmSourceFileSetProperty(void* arg, const char* prop,
  539. const char* value)
  540. {
  541. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  542. if (cmSourceFile* rsf = sf->RealSourceFile) {
  543. rsf->SetProperty(prop, value);
  544. } else if (prop) {
  545. if (!value) {
  546. value = "NOTFOUND";
  547. }
  548. sf->Properties.SetProperty(prop, value);
  549. }
  550. }
  551. void CCONV cmSourceFileAddDepend(void* arg, const char* depend)
  552. {
  553. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  554. if (cmSourceFile* rsf = sf->RealSourceFile) {
  555. rsf->AddDepend(depend);
  556. } else {
  557. sf->Depends.emplace_back(depend);
  558. }
  559. }
  560. void CCONV cmSourceFileSetName(void* arg, const char* name, const char* dir,
  561. int numSourceExtensions,
  562. const char** sourceExtensions,
  563. int numHeaderExtensions,
  564. const char** headerExtensions)
  565. {
  566. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  567. if (sf->RealSourceFile) {
  568. // SetName is allowed only on temporary source files created by
  569. // the command for building and passing to AddSource.
  570. return;
  571. }
  572. std::vector<std::string> sourceExts;
  573. std::vector<std::string> headerExts;
  574. int i;
  575. for (i = 0; i < numSourceExtensions; ++i) {
  576. sourceExts.emplace_back(sourceExtensions[i]);
  577. }
  578. for (i = 0; i < numHeaderExtensions; ++i) {
  579. headerExts.emplace_back(headerExtensions[i]);
  580. }
  581. // Save the original name given.
  582. sf->SourceName = name;
  583. // Convert the name to a full path in case the given name is a
  584. // relative path.
  585. std::string pathname = cmSystemTools::CollapseFullPath(name, dir);
  586. // First try and see whether the listed file can be found
  587. // as is without extensions added on.
  588. std::string hname = pathname;
  589. if (cmSystemTools::FileExists(hname)) {
  590. sf->SourceName = cmSystemTools::GetFilenamePath(name);
  591. if (!sf->SourceName.empty()) {
  592. sf->SourceName += "/";
  593. }
  594. sf->SourceName += cmSystemTools::GetFilenameWithoutLastExtension(name);
  595. std::string::size_type pos = hname.rfind('.');
  596. if (pos != std::string::npos) {
  597. sf->SourceExtension = hname.substr(pos + 1, hname.size() - pos);
  598. if (cmSystemTools::FileIsFullPath(name)) {
  599. std::string::size_type pos2 = hname.rfind('/');
  600. if (pos2 != std::string::npos) {
  601. sf->SourceName = hname.substr(pos2 + 1, pos - pos2 - 1);
  602. }
  603. }
  604. }
  605. sf->FullPath = hname;
  606. return;
  607. }
  608. // Next, try the various source extensions
  609. for (std::string const& ext : sourceExts) {
  610. hname = cmStrCat(pathname, '.', ext);
  611. if (cmSystemTools::FileExists(hname)) {
  612. sf->SourceExtension = ext;
  613. sf->FullPath = hname;
  614. return;
  615. }
  616. }
  617. // Finally, try the various header extensions
  618. for (std::string const& ext : headerExts) {
  619. hname = cmStrCat(pathname, '.', ext);
  620. if (cmSystemTools::FileExists(hname)) {
  621. sf->SourceExtension = ext;
  622. sf->FullPath = hname;
  623. return;
  624. }
  625. }
  626. std::ostringstream e;
  627. e << "Cannot find source file \"" << pathname << "\"";
  628. e << "\n\nTried extensions";
  629. for (std::string const& ext : sourceExts) {
  630. e << " ." << ext;
  631. }
  632. for (std::string const& ext : headerExts) {
  633. e << " ." << ext;
  634. }
  635. cmSystemTools::Error(e.str());
  636. }
  637. void CCONV cmSourceFileSetName2(void* arg, const char* name, const char* dir,
  638. const char* ext, int headerFileOnly)
  639. {
  640. cmCPluginAPISourceFile* sf = static_cast<cmCPluginAPISourceFile*>(arg);
  641. if (sf->RealSourceFile) {
  642. // SetName is allowed only on temporary source files created by
  643. // the command for building and passing to AddSource.
  644. return;
  645. }
  646. // Implement the old SetName method code here.
  647. if (headerFileOnly) {
  648. sf->Properties.SetProperty("HEADER_FILE_ONLY", "1");
  649. }
  650. sf->SourceName = name;
  651. std::string fname = sf->SourceName;
  652. if (ext && strlen(ext)) {
  653. fname += ".";
  654. fname += ext;
  655. }
  656. sf->FullPath = cmSystemTools::CollapseFullPath(fname, dir);
  657. cmSystemTools::ConvertToUnixSlashes(sf->FullPath);
  658. sf->SourceExtension = ext;
  659. }
  660. char* CCONV cmGetFilenameWithoutExtension(const char* name)
  661. {
  662. std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
  663. return strdup(sres.c_str());
  664. }
  665. char* CCONV cmGetFilenamePath(const char* name)
  666. {
  667. std::string sres = cmSystemTools::GetFilenamePath(name);
  668. return strdup(sres.c_str());
  669. }
  670. char* CCONV cmCapitalized(const char* name)
  671. {
  672. std::string sres = cmSystemTools::Capitalized(name);
  673. return strdup(sres.c_str());
  674. }
  675. void CCONV cmCopyFileIfDifferent(const char* name1, const char* name2)
  676. {
  677. cmSystemTools::CopyFileIfDifferent(name1, name2);
  678. }
  679. void CCONV cmRemoveFile(const char* name)
  680. {
  681. cmSystemTools::RemoveFile(name);
  682. }
  683. void CCONV cmDisplayStatus(void* arg, const char* message)
  684. {
  685. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  686. mf->DisplayStatus(message, -1);
  687. }
  688. void CCONV cmFree(void* data)
  689. {
  690. free(data);
  691. }
  692. void CCONV DefineSourceFileProperty(void* arg, const char* name,
  693. const char* briefDocs,
  694. const char* longDocs, int chained)
  695. {
  696. cmMakefile* mf = static_cast<cmMakefile*>(arg);
  697. mf->GetState()->DefineProperty(name, cmProperty::SOURCE_FILE,
  698. briefDocs ? briefDocs : "",
  699. longDocs ? longDocs : "", chained != 0);
  700. }
  701. } // close the extern "C" scope
  702. cmCAPI cmStaticCAPI = {
  703. cmGetClientData,
  704. cmGetTotalArgumentSize,
  705. cmFreeArguments,
  706. cmSetClientData,
  707. cmSetError,
  708. cmAddCacheDefinition,
  709. cmAddCustomCommand,
  710. cmAddDefineFlag,
  711. cmAddDefinition,
  712. cmAddExecutable,
  713. cmAddLibrary,
  714. cmAddLinkDirectoryForTarget,
  715. cmAddLinkLibraryForTarget,
  716. cmAddUtilityCommand,
  717. cmCommandExists,
  718. cmExecuteCommand,
  719. cmExpandSourceListArguments,
  720. cmExpandVariablesInString,
  721. cmGetCacheMajorVersion,
  722. cmGetCacheMinorVersion,
  723. cmGetCurrentDirectory,
  724. cmGetCurrentOutputDirectory,
  725. cmGetDefinition,
  726. cmGetHomeDirectory,
  727. cmGetHomeOutputDirectory,
  728. cmGetMajorVersion,
  729. cmGetMinorVersion,
  730. cmGetProjectName,
  731. cmGetStartDirectory,
  732. cmGetStartOutputDirectory,
  733. cmIsOn,
  734. cmAddSource,
  735. cmCreateSourceFile,
  736. cmDestroySourceFile,
  737. cmGetSource,
  738. cmSourceFileAddDepend,
  739. cmSourceFileGetProperty,
  740. cmSourceFileGetPropertyAsBool,
  741. cmSourceFileGetSourceName,
  742. cmSourceFileGetFullPath,
  743. cmSourceFileSetName,
  744. cmSourceFileSetName2,
  745. cmSourceFileSetProperty,
  746. cmCapitalized,
  747. cmCopyFileIfDifferent,
  748. cmGetFilenameWithoutExtension,
  749. cmGetFilenamePath,
  750. cmRemoveFile,
  751. cmFree,
  752. cmAddCustomCommandToOutput,
  753. cmAddCustomCommandToTarget,
  754. cmDisplayStatus,
  755. cmCreateNewSourceFile,
  756. DefineSourceFileProperty,
  757. };