aboutsummaryrefslogtreecommitdiff
path: root/libcody/resolver.cc
diff options
context:
space:
mode:
authorNathan Sidwell <nathan@acm.org>2020-12-14 08:10:27 -0800
committerNathan Sidwell <nathan@acm.org>2020-12-15 07:09:59 -0800
commit362303298ac4c1f93bda87535df2b726481d54bb (patch)
treeb728e42aa7e93c1fd673e75ee0071b86b8ae9c6c /libcody/resolver.cc
parentc5271279d6e86df0d0203c11fc4c3e3c99a14bb7 (diff)
downloadgcc-362303298ac4c1f93bda87535df2b726481d54bb.zip
gcc-362303298ac4c1f93bda87535df2b726481d54bb.tar.gz
gcc-362303298ac4c1f93bda87535df2b726481d54bb.tar.bz2
Add libcody
In order to separate compiler from build system, C++ Modules, as implemented in GCC introduces a communication channel between those two entities. This is implemented by libcody. It is anticipated that other implementations will also implement this protocol, or use libcody to provide it. * Makefile.def: Add libcody. * configure.ac: Add libcody. * Makefile.in: Regenerated. * configure: Regenerated. gcc/ * Makefile.in (CODYINC, CODYLIB, CODYLIB_H): New. Use them. libcody/ * configure.ac: New. * CMakeLists.txt: New. * CODING.md: New. * CONTRIB.md: New. * LICENSE: New. * LICENSE.gcc: New. * Makefile.in: New. * Makesub.in: New. * README.md: New. * buffer.cc: New. * build-aux/config.guess: New. * build-aux/config.sub: New. * build-aux/install-sh: New. * client.cc: New. * cmake/libcody-config-ix.cmake * cody.hh: New. * config.h.in: New. * config.m4: New. * configure: New. * configure.ac: New. * dox.cfg.in: New. * fatal.cc: New. * gdbinit.in: New. * internal.hh: New. * netclient.cc: New. * netserver.cc: New. * packet.cc: New. * resolver.cc: New. * server.cc: New. * tests/01-serialize/connect.cc: New. * tests/01-serialize/decoder.cc: New. * tests/01-serialize/encoder.cc: New. * tests/02-comms/client-1.cc: New. * tests/02-comms/pivot-1.cc: New. * tests/02-comms/server-1.cc: New. * tests/Makesub.in: New. * tests/jouster: New.
Diffstat (limited to 'libcody/resolver.cc')
-rw-r--r--libcody/resolver.cc209
1 files changed, 209 insertions, 0 deletions
diff --git a/libcody/resolver.cc b/libcody/resolver.cc
new file mode 100644
index 0000000..b83880f
--- /dev/null
+++ b/libcody/resolver.cc
@@ -0,0 +1,209 @@
+// CODYlib -*- mode:c++ -*-
+// Copyright (C) 2020 Nathan Sidwell, nathan@acm.org
+// License: Apache v2.0
+
+// Cody
+#include "internal.hh"
+// OS
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#if (defined (__unix__) \
+ || (defined (__Apple__) \
+ && defined (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) \
+ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101000))
+// Autoconf test?
+#define HAVE_FSTATAT 1
+#else
+#define HAVE_FSTATAT 0
+#endif
+
+// Resolver code
+
+#if __windows__
+inline bool IsDirSep (char c)
+{
+ return c == '/' || c == '\\';
+}
+inline bool IsAbsPath (char const *str)
+{
+ // IIRC windows has the concept of per-drive current directories,
+ // which make drive-using paths confusing. Let's not get into that.
+ return IsDirSep (str)
+ || (((str[0] >= 'A' && str[1] <= 'Z')
+ || (str[0] >= 'a' && str[1] <= 'z'))&& str[1] == ':');
+}
+#else
+inline bool IsDirSep (char c)
+{
+ return c == '/';
+}
+inline bool IsAbsPath (char const *str)
+{
+ return IsDirSep (str[0]);
+}
+#endif
+
+constexpr char DIR_SEPARATOR = '/';
+
+constexpr char DOT_REPLACE = ','; // Replace . directories
+constexpr char COLON_REPLACE = '-'; // Replace : (partition char)
+constexpr char const REPO_DIR[] = "cmi.cache";
+
+namespace Cody {
+
+Resolver::~Resolver ()
+{
+}
+
+char const *Resolver::GetCMISuffix ()
+{
+ return "cmi";
+}
+
+std::string Resolver::GetCMIName (std::string const &module)
+{
+ std::string result;
+
+ result.reserve (module.size () + 8);
+ bool is_header = false;
+ bool is_abs = false;
+
+ if (IsAbsPath (module.c_str ()))
+ is_header = is_abs = true;
+ else if (module.front () == '.' && IsDirSep (module.c_str ()[1]))
+ is_header = true;
+
+ if (is_abs)
+ {
+ result.push_back ('.');
+ result.append (module);
+ }
+ else
+ result = std::move (module);
+
+ if (is_header)
+ {
+ if (!is_abs)
+ result[0] = DOT_REPLACE;
+
+ /* Map .. to DOT_REPLACE, DOT_REPLACE. */
+ for (size_t ix = 1; ; ix++)
+ {
+ ix = result.find ('.', ix);
+ if (ix == result.npos)
+ break;
+ if (ix + 2 > result.size ())
+ break;
+ if (result[ix + 1] != '.')
+ continue;
+ if (!IsDirSep (result[ix - 1]))
+ continue;
+ if (!IsDirSep (result[ix + 2]))
+ continue;
+ result[ix] = DOT_REPLACE;
+ result[ix + 1] = DOT_REPLACE;
+ }
+ }
+ else if (COLON_REPLACE != ':')
+ {
+ // There can only be one colon in a module name
+ auto colon = result.find (':');
+ if (colon != result.npos)
+ result[colon] = COLON_REPLACE;
+ }
+
+ if (char const *suffix = GetCMISuffix ())
+ {
+ result.push_back ('.');
+ result.append (suffix);
+ }
+
+ return result;
+}
+
+void Resolver::WaitUntilReady (Server *)
+{
+}
+
+Resolver *Resolver::ConnectRequest (Server *s, unsigned version,
+ std::string &, std::string &)
+{
+ if (version > Version)
+ s->ErrorResponse ("version mismatch");
+ else
+ s->ConnectResponse ("default");
+
+ return this;
+}
+
+int Resolver::ModuleRepoRequest (Server *s)
+{
+ s->PathnameResponse (REPO_DIR);
+ return 0;
+}
+
+// Deprecated resolver functions
+int Resolver::ModuleExportRequest (Server *s, Flags, std::string &module)
+{
+ auto cmi = GetCMIName (module);
+ s->PathnameResponse (cmi);
+ return 0;
+}
+
+int Resolver::ModuleImportRequest (Server *s, Flags, std::string &module)
+{
+ auto cmi = GetCMIName (module);
+ s->PathnameResponse (cmi);
+ return 0;
+}
+
+int Resolver::ModuleCompiledRequest (Server *s, Flags, std::string &)
+{
+ s->OKResponse ();
+ return 0;
+}
+
+int Resolver::IncludeTranslateRequest (Server *s, Flags, std::string &include)
+{
+ bool xlate = false;
+
+ // This is not the most efficient
+ auto cmi = GetCMIName (include);
+ struct stat statbuf;
+
+#if HAVE_FSTATAT
+ int fd_dir = open (REPO_DIR, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
+ if (fd_dir >= 0
+ && fstatat (fd_dir, cmi.c_str (), &statbuf, 0) == 0
+ && S_ISREG (statbuf.st_mode))
+ // Sadly can't easily check if this process has read access,
+ // except by trying to open it.
+ xlate = true;
+ if (fd_dir >= 0)
+ close (fd_dir);
+#else
+ std::string append = REPO_DIR;
+ append.push_back (DIR_SEPARATOR);
+ append.append (cmi);
+ if (stat (append.c_str (), &statbuf) == 0
+ || S_ISREG (statbuf.st_mode))
+ xlate = true;
+#endif
+
+ if (xlate)
+ s->PathnameResponse (cmi);
+ else
+ s->BoolResponse (false);
+
+ return 0;
+}
+
+void Resolver::ErrorResponse (Server *server, std::string &&msg)
+{
+ server->ErrorResponse (msg);
+}
+
+}