1 //===- BuildSystem.cpp - Utilities for use by build systems ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements various utilities for use by build systems.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang-c/BuildSystem.h"
15 #include "CXString.h"
16 #include "clang/Basic/VirtualFileSystem.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/Support/CBindingWrapping.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/TimeValue.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 using namespace clang;
24 using namespace llvm::sys;
25
clang_getBuildSessionTimestamp(void)26 unsigned long long clang_getBuildSessionTimestamp(void) {
27 return llvm::sys::TimeValue::now().toEpochTime();
28 }
29
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(clang::vfs::YAMLVFSWriter,CXVirtualFileOverlay)30 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(clang::vfs::YAMLVFSWriter,
31 CXVirtualFileOverlay)
32
33 CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) {
34 return wrap(new clang::vfs::YAMLVFSWriter());
35 }
36
37 enum CXErrorCode
clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay VFO,const char * virtualPath,const char * realPath)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
56 unwrap(VFO)->addFileMapping(virtualPath, realPath);
57 return CXError_Success;
58 }
59
60 enum CXErrorCode
clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,int caseSensitive)61 clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,
62 int caseSensitive) {
63 if (!VFO)
64 return CXError_InvalidArguments;
65 unwrap(VFO)->setCaseSensitivity(caseSensitive);
66 return CXError_Success;
67 }
68
69 enum CXErrorCode
clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO,unsigned,char ** out_buffer_ptr,unsigned * out_buffer_size)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)
74 return CXError_InvalidArguments;
75
76 llvm::SmallString<256> Buf;
77 llvm::raw_svector_ostream OS(Buf);
78 unwrap(VFO)->write(OS);
79
80 StringRef Data = OS.str();
81 *out_buffer_ptr = (char*)malloc(Data.size());
82 *out_buffer_size = Data.size();
83 memcpy(*out_buffer_ptr, Data.data(), Data.size());
84 return CXError_Success;
85 }
86
clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO)87 void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) {
88 delete unwrap(VFO);
89 }
90
91
92 struct CXModuleMapDescriptorImpl {
93 std::string ModuleName;
94 std::string UmbrellaHeader;
95 };
96
clang_ModuleMapDescriptor_create(unsigned)97 CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) {
98 return new CXModuleMapDescriptorImpl();
99 }
100
101 enum CXErrorCode
clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD,const char * name)102 clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD,
103 const char *name) {
104 if (!MMD || !name)
105 return CXError_InvalidArguments;
106
107 MMD->ModuleName = name;
108 return CXError_Success;
109 }
110
111 enum CXErrorCode
clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD,const char * name)112 clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD,
113 const char *name) {
114 if (!MMD || !name)
115 return CXError_InvalidArguments;
116
117 MMD->UmbrellaHeader = name;
118 return CXError_Success;
119 }
120
121 enum CXErrorCode
clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD,unsigned,char ** out_buffer_ptr,unsigned * out_buffer_size)122 clang_ModuleMapDescriptor_writeToBuffer(CXModuleMapDescriptor MMD, unsigned,
123 char **out_buffer_ptr,
124 unsigned *out_buffer_size) {
125 if (!MMD || !out_buffer_ptr || !out_buffer_size)
126 return CXError_InvalidArguments;
127
128 llvm::SmallString<256> Buf;
129 llvm::raw_svector_ostream OS(Buf);
130 OS << "framework module " << MMD->ModuleName << " {\n";
131 OS << " umbrella header \"";
132 OS.write_escaped(MMD->UmbrellaHeader) << "\"\n";
133 OS << '\n';
134 OS << " export *\n";
135 OS << " module * { export * }\n";
136 OS << "}\n";
137
138 StringRef Data = OS.str();
139 *out_buffer_ptr = (char*)malloc(Data.size());
140 *out_buffer_size = Data.size();
141 memcpy(*out_buffer_ptr, Data.data(), Data.size());
142 return CXError_Success;
143 }
144
clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD)145 void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) {
146 delete MMD;
147 }
148