BuildSystem.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //===- BuildSystem.cpp - Utilities for use by build systems ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements various utilities for use by build systems.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang-c/BuildSystem.h"
  13. #include "CXString.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/Support/CBindingWrapping.h"
  16. #include "llvm/Support/Chrono.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/MemAlloc.h"
  19. #include "llvm/Support/Path.h"
  20. #include "llvm/Support/VirtualFileSystem.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace clang;
  23. using namespace llvm::sys;
  24. unsigned long long clang_getBuildSessionTimestamp(void) {
  25. return llvm::sys::toTimeT(std::chrono::system_clock::now());
  26. }
  27. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter,
  28. CXVirtualFileOverlay)
  29. CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) {
  30. return wrap(new llvm::vfs::YAMLVFSWriter());
  31. }
  32. enum CXErrorCode
  33. clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO,
  34. const char *virtualPath,
  35. const char *realPath) {
  36. if (!VFO || !virtualPath || !realPath)
  37. return CXError_InvalidArguments;
  38. if (!path::is_absolute(virtualPath))
  39. return CXError_InvalidArguments;
  40. if (!path::is_absolute(realPath))
  41. return CXError_InvalidArguments;
  42. for (path::const_iterator
  43. PI = path::begin(virtualPath),
  44. PE = path::end(virtualPath); PI != PE; ++PI) {
  45. StringRef Comp = *PI;
  46. if (Comp == "." || Comp == "..")
  47. return CXError_InvalidArguments;
  48. }
  49. unwrap(VFO)->addFileMapping(virtualPath, realPath);
  50. return CXError_Success;
  51. }
  52. enum CXErrorCode
  53. clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,
  54. int caseSensitive) {
  55. if (!VFO)
  56. return CXError_InvalidArguments;
  57. unwrap(VFO)->setCaseSensitivity(caseSensitive);
  58. return CXError_Success;
  59. }
  60. enum CXErrorCode
  61. clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned,
  62. char **out_buffer_ptr,
  63. unsigned *out_buffer_size) {
  64. if (!VFO || !out_buffer_ptr || !out_buffer_size)
  65. return CXError_InvalidArguments;
  66. llvm::SmallString<256> Buf;
  67. llvm::raw_svector_ostream OS(Buf);
  68. unwrap(VFO)->write(OS);
  69. StringRef Data = OS.str();
  70. *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
  71. *out_buffer_size = Data.size();
  72. memcpy(*out_buffer_ptr, Data.data(), Data.size());
  73. return CXError_Success;
  74. }
  75. void clang_free(void *buffer) {
  76. free(buffer);
  77. }
  78. void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) {
  79. delete unwrap(VFO);
  80. }
  81. struct CXModuleMapDescriptorImpl {
  82. std::string ModuleName;
  83. std::string UmbrellaHeader;
  84. };
  85. CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) {
  86. return new CXModuleMapDescriptorImpl();
  87. }
  88. enum CXErrorCode
  89. clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD,
  90. const char *name) {
  91. if (!MMD || !name)
  92. return CXError_InvalidArguments;
  93. MMD->ModuleName = name;
  94. return CXError_Success;
  95. }
  96. enum CXErrorCode
  97. clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD,
  98. const char *name) {
  99. if (!MMD || !name)
  100. return CXError_InvalidArguments;
  101. MMD->UmbrellaHeader = name;
  102. return CXError_Success;
  103. }
  104. enum CXErrorCode
  105. clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD, unsigned,
  106. char **out_buffer_ptr,
  107. unsigned *out_buffer_size) {
  108. if (!MMD || !out_buffer_ptr || !out_buffer_size)
  109. return CXError_InvalidArguments;
  110. llvm::SmallString<256> Buf;
  111. llvm::raw_svector_ostream OS(Buf);
  112. OS << "framework module " << MMD->ModuleName << " {\n";
  113. OS << " umbrella header \"";
  114. OS.write_escaped(MMD->UmbrellaHeader) << "\"\n";
  115. OS << '\n';
  116. OS << " export *\n";
  117. OS << " module * { export * }\n";
  118. OS << "}\n";
  119. StringRef Data = OS.str();
  120. *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
  121. *out_buffer_size = Data.size();
  122. memcpy(*out_buffer_ptr, Data.data(), Data.size());
  123. return CXError_Success;
  124. }
  125. void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) {
  126. delete MMD;
  127. }