1 //===- BuildSystem.cpp - Utilities for use by build systems ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements various utilities for use by build systems.
11 //===----------------------------------------------------------------------===//
13 #include "clang-c/BuildSystem.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/CBindingWrapping.h"
17 #include "llvm/Support/Chrono.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MemAlloc.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/VirtualFileSystem.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang
;
25 using namespace llvm::sys
;
27 unsigned long long clang_getBuildSessionTimestamp(void) {
28 return llvm::sys::toTimeT(std::chrono::system_clock::now());
31 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter
,
34 CXVirtualFileOverlay
clang_VirtualFileOverlay_create(unsigned) {
35 return wrap(new llvm::vfs::YAMLVFSWriter());
39 clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO
,
40 const char *virtualPath
,
41 const char *realPath
) {
42 if (!VFO
|| !virtualPath
|| !realPath
)
43 return CXError_InvalidArguments
;
44 if (!path::is_absolute(virtualPath
))
45 return CXError_InvalidArguments
;
46 if (!path::is_absolute(realPath
))
47 return CXError_InvalidArguments
;
49 for (path::const_iterator
50 PI
= path::begin(virtualPath
),
51 PE
= path::end(virtualPath
); PI
!= PE
; ++PI
) {
53 if (Comp
== "." || Comp
== "..")
54 return CXError_InvalidArguments
;
57 unwrap(VFO
)->addFileMapping(virtualPath
, realPath
);
58 return CXError_Success
;
62 clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO
,
65 return CXError_InvalidArguments
;
66 unwrap(VFO
)->setCaseSensitivity(caseSensitive
);
67 return CXError_Success
;
71 clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO
, unsigned,
72 char **out_buffer_ptr
,
73 unsigned *out_buffer_size
) {
74 if (!VFO
|| !out_buffer_ptr
|| !out_buffer_size
)
75 return CXError_InvalidArguments
;
77 llvm::SmallString
<256> Buf
;
78 llvm::raw_svector_ostream
OS(Buf
);
79 unwrap(VFO
)->write(OS
);
81 StringRef Data
= OS
.str();
82 *out_buffer_ptr
= static_cast<char*>(llvm::safe_malloc(Data
.size()));
83 *out_buffer_size
= Data
.size();
84 memcpy(*out_buffer_ptr
, Data
.data(), Data
.size());
85 return CXError_Success
;
88 void clang_free(void *buffer
) {
92 void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO
) {
97 struct CXModuleMapDescriptorImpl
{
98 std::string ModuleName
;
99 std::string UmbrellaHeader
;
102 CXModuleMapDescriptor
clang_ModuleMapDescriptor_create(unsigned) {
103 return new CXModuleMapDescriptorImpl();
107 clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD
,
110 return CXError_InvalidArguments
;
112 MMD
->ModuleName
= name
;
113 return CXError_Success
;
117 clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD
,
120 return CXError_InvalidArguments
;
122 MMD
->UmbrellaHeader
= name
;
123 return CXError_Success
;
127 clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD
, unsigned,
128 char **out_buffer_ptr
,
129 unsigned *out_buffer_size
) {
130 if (!MMD
|| !out_buffer_ptr
|| !out_buffer_size
)
131 return CXError_InvalidArguments
;
133 llvm::SmallString
<256> Buf
;
134 llvm::raw_svector_ostream
OS(Buf
);
135 OS
<< "framework module " << MMD
->ModuleName
<< " {\n";
136 OS
<< " umbrella header \"";
137 OS
.write_escaped(MMD
->UmbrellaHeader
) << "\"\n";
140 OS
<< " module * { export * }\n";
143 StringRef Data
= OS
.str();
144 *out_buffer_ptr
= static_cast<char*>(llvm::safe_malloc(Data
.size()));
145 *out_buffer_size
= Data
.size();
146 memcpy(*out_buffer_ptr
, Data
.data(), Data
.size());
147 return CXError_Success
;
150 void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD
) {