annotate gcc/jit/jit-tempdir.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Managing temporary directories and their content within libgccjit.so
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
8 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
10 any later version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but
kono
parents:
diff changeset
13 WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kono
parents:
diff changeset
15 General Public License for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #include "config.h"
kono
parents:
diff changeset
22 #include "system.h"
kono
parents:
diff changeset
23 #include "coretypes.h"
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #include "jit-tempdir.h"
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 /* Construct a tempdir path template suitable for use by mkdtemp
kono
parents:
diff changeset
29 e.g. "/tmp/libgccjit-XXXXXX", but respecting the rules in
kono
parents:
diff changeset
30 libiberty's choose_tempdir rather than hardcoding "/tmp/".
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 The memory is allocated using malloc and must be freed.
kono
parents:
diff changeset
33 Aborts the process if allocation fails. */
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 static char *
kono
parents:
diff changeset
36 make_tempdir_path_template ()
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 const char *tmpdir_buf;
kono
parents:
diff changeset
39 size_t tmpdir_len;
kono
parents:
diff changeset
40 const char *file_template_buf;
kono
parents:
diff changeset
41 size_t file_template_len;
kono
parents:
diff changeset
42 char *result;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 /* The result of choose_tmpdir is a cached buffer within libiberty, so
kono
parents:
diff changeset
45 we must *not* free it. */
kono
parents:
diff changeset
46 tmpdir_buf = choose_tmpdir ();
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 /* choose_tmpdir aborts on malloc failure. */
kono
parents:
diff changeset
49 gcc_assert (tmpdir_buf);
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 tmpdir_len = strlen (tmpdir_buf);
kono
parents:
diff changeset
52 /* tmpdir_buf should now have a dir separator as the final byte. */
kono
parents:
diff changeset
53 gcc_assert (tmpdir_len > 0);
kono
parents:
diff changeset
54 gcc_assert (tmpdir_buf[tmpdir_len - 1] == DIR_SEPARATOR);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 file_template_buf = "libgccjit-XXXXXX";
kono
parents:
diff changeset
57 file_template_len = strlen (file_template_buf);
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 result = XNEWVEC (char, tmpdir_len + file_template_len + 1);
kono
parents:
diff changeset
60 strcpy (result, tmpdir_buf);
kono
parents:
diff changeset
61 strcpy (result + tmpdir_len, file_template_buf);
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 return result;
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* The constructor for the jit::tempdir object.
kono
parents:
diff changeset
67 The real work is done by the jit::tempdir::create method. */
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 gcc::jit::tempdir::tempdir (logger *logger, int keep_intermediates)
kono
parents:
diff changeset
70 : log_user (logger),
kono
parents:
diff changeset
71 m_keep_intermediates (keep_intermediates),
kono
parents:
diff changeset
72 m_path_template (NULL),
kono
parents:
diff changeset
73 m_path_tempdir (NULL),
kono
parents:
diff changeset
74 m_path_c_file (NULL),
kono
parents:
diff changeset
75 m_path_s_file (NULL),
kono
parents:
diff changeset
76 m_path_so_file (NULL)
kono
parents:
diff changeset
77 {
kono
parents:
diff changeset
78 JIT_LOG_SCOPE (get_logger ());
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 /* Do the real work of creating the on-disk tempdir.
kono
parents:
diff changeset
82 We do this here, rather than in the jit::tempdir constructor
kono
parents:
diff changeset
83 so that we can handle failure without needing exceptions. */
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 bool
kono
parents:
diff changeset
86 gcc::jit::tempdir::create ()
kono
parents:
diff changeset
87 {
kono
parents:
diff changeset
88 JIT_LOG_SCOPE (get_logger ());
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 m_path_template = make_tempdir_path_template ();
kono
parents:
diff changeset
91 if (!m_path_template)
kono
parents:
diff changeset
92 return false;
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 log ("m_path_template: %s", m_path_template);
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 /* Create tempdir using mkdtemp. This is created with 0700 perms and
kono
parents:
diff changeset
97 is unique. Hence no other (non-root) users should have access to
kono
parents:
diff changeset
98 the paths within it. */
kono
parents:
diff changeset
99 m_path_tempdir = mkdtemp (m_path_template);
kono
parents:
diff changeset
100 if (!m_path_tempdir)
kono
parents:
diff changeset
101 return false;
kono
parents:
diff changeset
102 log ("m_path_tempdir: %s", m_path_tempdir);
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 m_path_c_file = concat (m_path_tempdir, "/fake.c", NULL);
kono
parents:
diff changeset
105 m_path_s_file = concat (m_path_tempdir, "/fake.s", NULL);
kono
parents:
diff changeset
106 m_path_so_file = concat (m_path_tempdir, "/fake.so", NULL);
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 /* Success. */
kono
parents:
diff changeset
109 return true;
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 /* The destructor for the jit::tempdir object, which
kono
parents:
diff changeset
113 cleans up the filesystem directory and its contents
kono
parents:
diff changeset
114 (unless keep_intermediates was set). */
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 gcc::jit::tempdir::~tempdir ()
kono
parents:
diff changeset
117 {
kono
parents:
diff changeset
118 JIT_LOG_SCOPE (get_logger ());
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 if (m_keep_intermediates)
kono
parents:
diff changeset
121 fprintf (stderr, "intermediate files written to %s\n", m_path_tempdir);
kono
parents:
diff changeset
122 else
kono
parents:
diff changeset
123 {
kono
parents:
diff changeset
124 /* Clean up .s/.so. */
kono
parents:
diff changeset
125 if (m_path_s_file)
kono
parents:
diff changeset
126 {
kono
parents:
diff changeset
127 log ("unlinking .s file: %s", m_path_s_file);
kono
parents:
diff changeset
128 unlink (m_path_s_file);
kono
parents:
diff changeset
129 }
kono
parents:
diff changeset
130 if (m_path_so_file)
kono
parents:
diff changeset
131 {
kono
parents:
diff changeset
132 log ("unlinking .so file: %s", m_path_so_file);
kono
parents:
diff changeset
133 unlink (m_path_so_file);
kono
parents:
diff changeset
134 }
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 /* Clean up any other tempfiles. */
kono
parents:
diff changeset
137 int i;
kono
parents:
diff changeset
138 char *tempfile;
kono
parents:
diff changeset
139 FOR_EACH_VEC_ELT (m_tempfiles, i, tempfile)
kono
parents:
diff changeset
140 {
kono
parents:
diff changeset
141 log ("unlinking tempfile: %s", tempfile);
kono
parents:
diff changeset
142 unlink (tempfile);
kono
parents:
diff changeset
143 }
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 /* The tempdir should now be empty; remove it. */
kono
parents:
diff changeset
146 if (m_path_tempdir)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 log ("removing tempdir: %s", m_path_tempdir);
kono
parents:
diff changeset
149 rmdir (m_path_tempdir);
kono
parents:
diff changeset
150 }
kono
parents:
diff changeset
151 }
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 free (m_path_template);
kono
parents:
diff changeset
154 /* m_path_tempdir aliases m_path_template, or is NULL, so don't
kono
parents:
diff changeset
155 attempt to free it . */
kono
parents:
diff changeset
156 free (m_path_c_file);
kono
parents:
diff changeset
157 free (m_path_s_file);
kono
parents:
diff changeset
158 free (m_path_so_file);
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 int i;
kono
parents:
diff changeset
161 char *tempfile;
kono
parents:
diff changeset
162 FOR_EACH_VEC_ELT (m_tempfiles, i, tempfile)
kono
parents:
diff changeset
163 free (tempfile);
kono
parents:
diff changeset
164 }