blob: 0d69dcf1725e1448509c2864777e2ccd6f3598f9 [file] [log] [blame]
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +00001//===- BuildSystem.cpp - Utilities for use by build systems ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Gribenkofdd4f302014-02-12 10:40:07 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements various utilities for use by build systems.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang-c/BuildSystem.h"
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000014#include "CXString.h"
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000015#include "llvm/ADT/SmallString.h"
Justin Bogner9c785292014-05-20 21:43:27 +000016#include "llvm/Support/CBindingWrapping.h"
Pavel Labathb66261a2016-11-09 11:19:39 +000017#include "llvm/Support/Chrono.h"
Serge Pavlov52525732018-02-21 02:02:39 +000018#include "llvm/Support/ErrorHandling.h"
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000019#include "llvm/Support/Path.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000020#include "llvm/Support/VirtualFileSystem.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000021#include "llvm/Support/raw_ostream.h"
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000022
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000023using namespace clang;
24using namespace llvm::sys;
25
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000026unsigned long long clang_getBuildSessionTimestamp(void) {
Pavel Labathb66261a2016-11-09 11:19:39 +000027 return llvm::sys::toTimeT(std::chrono::system_clock::now());
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000028}
Dmitri Gribenkofdd4f302014-02-12 10:40:07 +000029
Jonas Devliegherefc514902018-10-10 13:27:25 +000030DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::vfs::YAMLVFSWriter,
Justin Bogner9c785292014-05-20 21:43:27 +000031 CXVirtualFileOverlay)
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000032
33CXVirtualFileOverlay clang_VirtualFileOverlay_create(unsigned) {
Jonas Devliegherefc514902018-10-10 13:27:25 +000034 return wrap(new llvm::vfs::YAMLVFSWriter());
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000035}
36
37enum CXErrorCode
38clang_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 Bogner9c785292014-05-20 21:43:27 +000056 unwrap(VFO)->addFileMapping(virtualPath, realPath);
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000057 return CXError_Success;
58}
59
Argyrios Kyrtzidisa9ab4d42014-03-20 04:51:48 +000060enum CXErrorCode
61clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay VFO,
62 int caseSensitive) {
63 if (!VFO)
64 return CXError_InvalidArguments;
Justin Bogner9c785292014-05-20 21:43:27 +000065 unwrap(VFO)->setCaseSensitivity(caseSensitive);
Argyrios Kyrtzidisa9ab4d42014-03-20 04:51:48 +000066 return CXError_Success;
67}
68
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000069enum CXErrorCode
Argyrios Kyrtzidis74c96c02014-03-03 06:38:52 +000070clang_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 Kyrtzidis0b9682e2014-02-25 03:59:23 +000074 return CXError_InvalidArguments;
75
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000076 llvm::SmallString<256> Buf;
77 llvm::raw_svector_ostream OS(Buf);
Justin Bogner9c785292014-05-20 21:43:27 +000078 unwrap(VFO)->write(OS);
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000079
Argyrios Kyrtzidis74c96c02014-03-03 06:38:52 +000080 StringRef Data = OS.str();
Serge Pavlov52525732018-02-21 02:02:39 +000081 *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
Argyrios Kyrtzidis74c96c02014-03-03 06:38:52 +000082 *out_buffer_size = Data.size();
83 memcpy(*out_buffer_ptr, Data.data(), Data.size());
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000084 return CXError_Success;
85}
86
Yaron Keren45a5bfe2015-07-09 07:53:23 +000087void clang_free(void *buffer) {
88 free(buffer);
89}
90
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000091void clang_VirtualFileOverlay_dispose(CXVirtualFileOverlay VFO) {
Justin Bogner9c785292014-05-20 21:43:27 +000092 delete unwrap(VFO);
Argyrios Kyrtzidis0b9682e2014-02-25 03:59:23 +000093}
Argyrios Kyrtzidisd502a102014-03-03 07:41:45 +000094
95
96struct CXModuleMapDescriptorImpl {
97 std::string ModuleName;
98 std::string UmbrellaHeader;
99};
100
101CXModuleMapDescriptor clang_ModuleMapDescriptor_create(unsigned) {
102 return new CXModuleMapDescriptorImpl();
103}
104
105enum CXErrorCode
106clang_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
115enum CXErrorCode
116clang_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
125enum CXErrorCode
126clang_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 Pavlov52525732018-02-21 02:02:39 +0000143 *out_buffer_ptr = static_cast<char*>(llvm::safe_malloc(Data.size()));
Argyrios Kyrtzidisd502a102014-03-03 07:41:45 +0000144 *out_buffer_size = Data.size();
145 memcpy(*out_buffer_ptr, Data.data(), Data.size());
146 return CXError_Success;
147}
148
149void clang_ModuleMapDescriptor_dispose(CXModuleMapDescriptor MMD) {
150 delete MMD;
151}