Dmitri Gribenko | fdd4f30 | 2014-02-12 10:40:07 +0000 | [diff] [blame] | 1 | //===- BuildSystem.cpp - Utilities for use by build systems ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dmitri Gribenko | fdd4f30 | 2014-02-12 10:40:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements various utilities for use by build systems. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang-c/BuildSystem.h" |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 14 | #include "CXString.h" |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CBindingWrapping.h" |
Pavel Labath | b66261a | 2016-11-09 11:19:39 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Chrono.h" |
Serge Pavlov | 5252573 | 2018-02-21 02:02:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 20 | #include "llvm/Support/VirtualFileSystem.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Dmitri Gribenko | fdd4f30 | 2014-02-12 10:40:07 +0000 | [diff] [blame] | 22 | |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace llvm::sys; |
| 25 | |
Dmitri Gribenko | fdd4f30 | 2014-02-12 10:40:07 +0000 | [diff] [blame] | 26 | unsigned long long clang_getBuildSessionTimestamp(void) { |
Pavel Labath | b66261a | 2016-11-09 11:19:39 +0000 | [diff] [blame] | 27 | return llvm::sys::toTimeT(std::chrono::system_clock::now()); |
Dmitri Gribenko | fdd4f30 | 2014-02-12 10:40:07 +0000 | [diff] [blame] | 28 | } |
Dmitri Gribenko | fdd4f30 | 2014-02-12 10:40:07 +0000 | [diff] [blame] | 29 | |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 30 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter, |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 31 | CXVirtualFileOverlay) |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 32 | |
| 33 | CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) { |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 34 | return wrap(new llvm::vfs::YAMLVFSWriter()); |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | enum CXErrorCode |
| 38 | clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO, |
| 39 | const char *virtualPath, |
| 40 | const char *realPath) { |
| 41 | if (!VFO || !virtualPath || !realPath) |
| 42 | return CXError_InvalidArguments; |
| 43 | if (!path::is_absolute(virtualPath)) |
| 44 | return CXError_InvalidArguments; |
| 45 | if (!path::is_absolute(realPath)) |
| 46 | return CXError_InvalidArguments; |
| 47 | |
| 48 | for (path::const_iterator |
| 49 | PI = path::begin(virtualPath), |
| 50 | PE = path::end(virtualPath); PI != PE; ++PI) { |
| 51 | StringRef Comp = *PI; |
| 52 | if (Comp == "." || Comp == "..") |
| 53 | return CXError_InvalidArguments; |
| 54 | } |
| 55 | |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 56 | unwrap(VFO)->addFileMapping(virtualPath, realPath); |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 57 | return CXError_Success; |
| 58 | } |
| 59 | |
Argyrios Kyrtzidis | a9ab4d4 | 2014-03-20 04:51:48 +0000 | [diff] [blame] | 60 | enum CXErrorCode |
| 61 | clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO, |
| 62 | int caseSensitive) { |
| 63 | if (!VFO) |
| 64 | return CXError_InvalidArguments; |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 65 | unwrap(VFO)->setCaseSensitivity(caseSensitive); |
Argyrios Kyrtzidis | a9ab4d4 | 2014-03-20 04:51:48 +0000 | [diff] [blame] | 66 | return CXError_Success; |
| 67 | } |
| 68 | |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 69 | enum CXErrorCode |
Argyrios Kyrtzidis | 74c96c0 | 2014-03-03 06:38:52 +0000 | [diff] [blame] | 70 | clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned, |
| 71 | char **out_buffer_ptr, |
| 72 | unsigned *out_buffer_size) { |
| 73 | if (!VFO || !out_buffer_ptr || !out_buffer_size) |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 74 | return CXError_InvalidArguments; |
| 75 | |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 76 | llvm::SmallString<256> Buf; |
| 77 | llvm::raw_svector_ostream OS(Buf); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 78 | unwrap(VFO)->write(OS); |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 79 | |
Argyrios Kyrtzidis | 74c96c0 | 2014-03-03 06:38:52 +0000 | [diff] [blame] | 80 | StringRef Data = OS.str(); |
Serge Pavlov | 5252573 | 2018-02-21 02:02:39 +0000 | [diff] [blame] | 81 | *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size())); |
Argyrios Kyrtzidis | 74c96c0 | 2014-03-03 06:38:52 +0000 | [diff] [blame] | 82 | *out_buffer_size = Data.size(); |
| 83 | memcpy(*out_buffer_ptr, Data.data(), Data.size()); |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 84 | return CXError_Success; |
| 85 | } |
| 86 | |
Yaron Keren | 45a5bfe | 2015-07-09 07:53:23 +0000 | [diff] [blame] | 87 | void clang_free(void *buffer) { |
| 88 | free(buffer); |
| 89 | } |
| 90 | |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 91 | void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) { |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 92 | delete unwrap(VFO); |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 93 | } |
Argyrios Kyrtzidis | d502a10 | 2014-03-03 07:41:45 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | struct CXModuleMapDescriptorImpl { |
| 97 | std::string ModuleName; |
| 98 | std::string UmbrellaHeader; |
| 99 | }; |
| 100 | |
| 101 | CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) { |
| 102 | return new CXModuleMapDescriptorImpl(); |
| 103 | } |
| 104 | |
| 105 | enum CXErrorCode |
| 106 | clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD, |
| 107 | const char *name) { |
| 108 | if (!MMD || !name) |
| 109 | return CXError_InvalidArguments; |
| 110 | |
| 111 | MMD->ModuleName = name; |
| 112 | return CXError_Success; |
| 113 | } |
| 114 | |
| 115 | enum CXErrorCode |
| 116 | clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD, |
| 117 | const char *name) { |
| 118 | if (!MMD || !name) |
| 119 | return CXError_InvalidArguments; |
| 120 | |
| 121 | MMD->UmbrellaHeader = name; |
| 122 | return CXError_Success; |
| 123 | } |
| 124 | |
| 125 | enum CXErrorCode |
| 126 | clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD, unsigned, |
| 127 | char **out_buffer_ptr, |
| 128 | unsigned *out_buffer_size) { |
| 129 | if (!MMD || !out_buffer_ptr || !out_buffer_size) |
| 130 | return CXError_InvalidArguments; |
| 131 | |
| 132 | llvm::SmallString<256> Buf; |
| 133 | llvm::raw_svector_ostream OS(Buf); |
| 134 | OS << "framework module " << MMD->ModuleName << " {\n"; |
| 135 | OS << " umbrella header \""; |
| 136 | OS.write_escaped(MMD->UmbrellaHeader) << "\"\n"; |
| 137 | OS << '\n'; |
| 138 | OS << " export *\n"; |
| 139 | OS << " module * { export * }\n"; |
| 140 | OS << "}\n"; |
| 141 | |
| 142 | StringRef Data = OS.str(); |
Serge Pavlov | 5252573 | 2018-02-21 02:02:39 +0000 | [diff] [blame] | 143 | *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size())); |
Argyrios Kyrtzidis | d502a10 | 2014-03-03 07:41:45 +0000 | [diff] [blame] | 144 | *out_buffer_size = Data.size(); |
| 145 | memcpy(*out_buffer_ptr, Data.data(), Data.size()); |
| 146 | return CXError_Success; |
| 147 | } |
| 148 | |
| 149 | void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) { |
| 150 | delete MMD; |
| 151 | } |