blob: 6c1f2c145b0a0559fe9b6f12492c4698a1c0d759 [file] [log] [blame]
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +00001//===- 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"
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000015#include "CXString.h"
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000016#include "llvm/ADT/SmallString.h"
Justin Bogner9c785292014-05-20 21:43:27 +000017#include "llvm/Support/CBindingWrapping.h"
Pavel Labathb66261a2016-11-09 11:19:39 +000018#include "llvm/Support/Chrono.h"
Serge Pavlov52525732018-02-21 02:02:39 +000019#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000020#include "llvm/Support/Path.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000021#include "llvm/Support/VirtualFileSystem.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000022#include "llvm/Support/raw_ostream.h"
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000023
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000024using namespace clang;
25using namespace llvm::sys;
26
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000027unsigned long long clang_getBuildSessionTimestamp(void) {
Pavel Labathb66261a2016-11-09 11:19:39 +000028 return llvm::sys::toTimeT(std::chrono::system_clock::now());
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000029}
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000030
Jonas Devliegherefc514902018-10-10 13:27:25 +000031DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter,
Justin Bogner9c785292014-05-20 21:43:27 +000032 CXVirtualFileOverlay)
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000033
34CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) {
Jonas Devliegherefc514902018-10-10 13:27:25 +000035 return wrap(new llvm::vfs::YAMLVFSWriter());
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000036}
37
38enum CXErrorCode
39clang_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;
48
49 for (path::const_iterator
50 PI = path::begin(virtualPath),
51 PE = path::end(virtualPath); PI != PE; ++PI) {
52 StringRef Comp = *PI;
53 if (Comp == "." || Comp == "..")
54 return CXError_InvalidArguments;
55 }
56
Justin Bogner9c785292014-05-20 21:43:27 +000057 unwrap(VFO)->addFileMapping(virtualPath, realPath);
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000058 return CXError_Success;
59}
60
Argyrios Kyrtzidisa9ab4d42014-03-20 04:51:48 +000061enum CXErrorCode
62clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,
63 int caseSensitive) {
64 if (!VFO)
65 return CXError_InvalidArguments;
Justin Bogner9c785292014-05-20 21:43:27 +000066 unwrap(VFO)->setCaseSensitivity(caseSensitive);
Argyrios Kyrtzidisa9ab4d42014-03-20 04:51:48 +000067 return CXError_Success;
68}
69
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000070enum CXErrorCode
Argyrios Kyrtzidis74c96c02014-03-03 06:38:52 +000071clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned,
72 char **out_buffer_ptr,
73 unsigned *out_buffer_size) {
74 if (!VFO || !out_buffer_ptr || !out_buffer_size)
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000075 return CXError_InvalidArguments;
76
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000077 llvm::SmallString<256> Buf;
78 llvm::raw_svector_ostream OS(Buf);
Justin Bogner9c785292014-05-20 21:43:27 +000079 unwrap(VFO)->write(OS);
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000080
Argyrios Kyrtzidis74c96c02014-03-03 06:38:52 +000081 StringRef Data = OS.str();
Serge Pavlov52525732018-02-21 02:02:39 +000082 *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
Argyrios Kyrtzidis74c96c02014-03-03 06:38:52 +000083 *out_buffer_size = Data.size();
84 memcpy(*out_buffer_ptr, Data.data(), Data.size());
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000085 return CXError_Success;
86}
87
Yaron Keren45a5bfe2015-07-09 07:53:23 +000088void clang_free(void *buffer) {
89 free(buffer);
90}
91
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000092void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) {
Justin Bogner9c785292014-05-20 21:43:27 +000093 delete unwrap(VFO);
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000094}
Argyrios Kyrtzidisd502a102014-03-03 07:41:45 +000095
96
97struct CXModuleMapDescriptorImpl {
98 std::string ModuleName;
99 std::string UmbrellaHeader;
100};
101
102CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) {
103 return new CXModuleMapDescriptorImpl();
104}
105
106enum CXErrorCode
107clang_ModuleMapDescriptor_setFrameworkModuleName(CXModuleMapDescriptor MMD,
108 const char *name) {
109 if (!MMD || !name)
110 return CXError_InvalidArguments;
111
112 MMD->ModuleName = name;
113 return CXError_Success;
114}
115
116enum CXErrorCode
117clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor MMD,
118 const char *name) {
119 if (!MMD || !name)
120 return CXError_InvalidArguments;
121
122 MMD->UmbrellaHeader = name;
123 return CXError_Success;
124}
125
126enum CXErrorCode
127clang_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;
132
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";
138 OS << '\n';
139 OS << " export *\n";
140 OS << " module * { export * }\n";
141 OS << "}\n";
142
143 StringRef Data = OS.str();
Serge Pavlov52525732018-02-21 02:02:39 +0000144 *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
Argyrios Kyrtzidisd502a102014-03-03 07:41:45 +0000145 *out_buffer_size = Data.size();
146 memcpy(*out_buffer_ptr, Data.data(), Data.size());
147 return CXError_Success;
148}
149
150void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) {
151 delete MMD;
152}