diff options
author | David Malcolm <dmalcolm@redhat.com> | 2014-12-09 20:00:07 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2014-12-09 20:00:07 +0000 |
commit | d1e5f2c7e2a800e99509aadb9cdc6d3e76eeb1a9 (patch) | |
tree | 83b8fc521cc13d74c7018f55655bf98202b25e4b /gcc/jit/jit-tempdir.c | |
parent | 661fce82a67307196182f81dd7d627d05aef4816 (diff) | |
download | gcc-d1e5f2c7e2a800e99509aadb9cdc6d3e76eeb1a9.zip gcc-d1e5f2c7e2a800e99509aadb9cdc6d3e76eeb1a9.tar.gz gcc-d1e5f2c7e2a800e99509aadb9cdc6d3e76eeb1a9.tar.bz2 |
Add jit-tempdir.{c|h}
gcc/jit/ChangeLog:
PR jit/64206
* Make-lang.in (jit_OBJS): Add jit/jit-tempdir.o.
* jit-common.h (gcc::jit::tempdir): New forward decl.
* jit-playback.c: Include jit-tempdir.h.
(gcc::jit::playback::context::context): Initialize m_tempdir.
(gcc::jit::playback::context::~context): Move tempdir
cleanup to new file jit-tempdir.c
(make_tempdir_path_template): Move to new file jit-tempdir.c.
(gcc::jit::playback::context::compile): Move tempdir creation
to new tempdir object in new file jit-tempdir.c.
(gcc::jit::playback::context::make_fake_args): Get path from
tempdir object rather than from member data.
(gcc::jit::playback::context::convert_to_dso): Likewise.
(gcc::jit::playback::context::dlopen_built_dso): Likewise.
(gcc::jit::playback::context::dump_generated_code): Likewise.
(gcc::jit::playback::context::get_path_c_file): New function.
(gcc::jit::playback::context::get_path_s_file): New function.
(gcc::jit::playback::context::get_path_so_file): New function.
* jit-playback.h (gcc::jit::playback::context::get_path_c_file):
New function.
(gcc::jit::playback::context::get_path_s_file): New function.
(gcc::jit::playback::context::get_path_so_file): New function.
(gcc::jit::playback::context): Move fields "m_path_template",
"m_path_tempdir", "m_path_c_file", "m_path_s_file",
"m_path_so_file" to new jit::tempdir class; add field "m_tempdir".
* jit-tempdir.c: New file.
* jit-tempdir.h: New file.
From-SVN: r218533
Diffstat (limited to 'gcc/jit/jit-tempdir.c')
-rw-r--r-- | gcc/jit/jit-tempdir.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/gcc/jit/jit-tempdir.c b/gcc/jit/jit-tempdir.c new file mode 100644 index 0000000..f64fbee --- /dev/null +++ b/gcc/jit/jit-tempdir.c @@ -0,0 +1,129 @@ +/* Managing temporary directories and their content within libgccjit.so + Copyright (C) 2014 Free Software Foundation, Inc. + Contributed by David Malcolm <dmalcolm@redhat.com>. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" + +#include "jit-tempdir.h" + + +/* Construct a tempdir path template suitable for use by mkdtemp + e.g. "/tmp/libgccjit-XXXXXX", but respecting the rules in + libiberty's choose_tempdir rather than hardcoding "/tmp/". + + The memory is allocated using malloc and must be freed. + Aborts the process if allocation fails. */ + +static char * +make_tempdir_path_template () +{ + const char *tmpdir_buf; + size_t tmpdir_len; + const char *file_template_buf; + size_t file_template_len; + char *result; + + /* The result of choose_tmpdir is a cached buffer within libiberty, so + we must *not* free it. */ + tmpdir_buf = choose_tmpdir (); + + /* choose_tmpdir aborts on malloc failure. */ + gcc_assert (tmpdir_buf); + + tmpdir_len = strlen (tmpdir_buf); + /* tmpdir_buf should now have a dir separator as the final byte. */ + gcc_assert (tmpdir_len > 0); + gcc_assert (tmpdir_buf[tmpdir_len - 1] == DIR_SEPARATOR); + + file_template_buf = "libgccjit-XXXXXX"; + file_template_len = strlen (file_template_buf); + + result = XNEWVEC (char, tmpdir_len + file_template_len + 1); + strcpy (result, tmpdir_buf); + strcpy (result + tmpdir_len, file_template_buf); + + return result; +} + +/* The constructor for the jit::tempdir object. + The real work is done by the jit::tempdir::create method. */ + +gcc::jit::tempdir::tempdir (int keep_intermediates) + : m_keep_intermediates (keep_intermediates), + m_path_template (NULL), + m_path_tempdir (NULL), + m_path_c_file (NULL), + m_path_s_file (NULL), + m_path_so_file (NULL) +{ +} + +/* Do the real work of creating the on-disk tempdir. + We do this here, rather than in the jit::tempdir constructor + so that we can handle failure without needing exceptions. */ + +bool +gcc::jit::tempdir::create () +{ + m_path_template = make_tempdir_path_template (); + if (!m_path_template) + return false; + + /* Create tempdir using mkdtemp. This is created with 0700 perms and + is unique. Hence no other (non-root) users should have access to + the paths within it. */ + m_path_tempdir = mkdtemp (m_path_template); + if (!m_path_tempdir) + return false; + m_path_c_file = concat (m_path_tempdir, "/fake.c", NULL); + m_path_s_file = concat (m_path_tempdir, "/fake.s", NULL); + m_path_so_file = concat (m_path_tempdir, "/fake.so", NULL); + + /* Success. */ + return true; +} + +/* The destructor for the jit::tempdir object, which + cleans up the filesystem directory and its contents + (unless keep_intermediates was set). */ + +gcc::jit::tempdir::~tempdir () +{ + if (m_keep_intermediates) + fprintf (stderr, "intermediate files written to %s\n", m_path_tempdir); + else + { + /* Clean up .s/.so and tempdir. */ + if (m_path_s_file) + unlink (m_path_s_file); + if (m_path_so_file) + unlink (m_path_so_file); + if (m_path_tempdir) + rmdir (m_path_tempdir); + } + + free (m_path_template); + /* m_path_tempdir aliases m_path_template, or is NULL, so don't + attempt to free it . */ + free (m_path_c_file); + free (m_path_s_file); + free (m_path_so_file); +} |