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