1 /* Managing temporary directories and their content within libgccjit.so
2    Copyright (C) 2014-2021 Free Software Foundation, Inc.
3    Contributed by David Malcolm <dmalcolm@redhat.com>.
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11 
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #ifndef JIT_TEMPDIR_H
22 #define JIT_TEMPDIR_H
23 
24 #include "jit-logging.h"
25 
26 namespace gcc {
27 
28 namespace jit {
29 
30 /* A class to keep track of the jit::playback::context's tempdir.
31 
32    The tempdir has the following layout:
33 
34      /tmp/libgccjit-XXXXXX/
35 			 ./fake.c
36 			    (doesn't exist, but the rest of the
37 			     compiler needs a source code filename)
38 
39 			 ./fake.s
40 			      (created by toplev::main)
41 
42 			 ./fake.so
43 			      (created by playback::context::convert_to_dso).
44 
45   It is normally deleted from the filesystem in the playback::context's
46   dtor, unless GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES was set.  */
47 
48 class tempdir : public log_user
49 {
50  public:
51   tempdir (logger *logger, int keep_intermediates);
52   ~tempdir ();
53 
54   bool create ();
55 
get_path()56   const char * get_path () const { return m_path_tempdir; }
get_path_c_file()57   const char * get_path_c_file () const { return m_path_c_file; }
get_path_s_file()58   const char * get_path_s_file () const { return m_path_s_file; }
get_path_so_file()59   const char * get_path_so_file () const { return m_path_so_file; }
60 
61   /* Add PATH to the vec of tempfiles that must be unlinked.
62      Take ownership of the buffer PATH; it will be freed.  */
add_temp_file(char * path)63   void add_temp_file (char *path) { m_tempfiles.safe_push (path); }
64 
65  private:
66   /* Was GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES set?  If so, keep the
67      on-disk tempdir around after this wrapper object goes away.  */
68   int m_keep_intermediates;
69 
70   /* Allocated using xmalloc (by xstrdup).  */
71   char *m_path_template;
72 
73   /* This either aliases m_path_template, or is NULL.  */
74   char *m_path_tempdir;
75 
76   /* The following are allocated using xmalloc.  */
77   char *m_path_c_file;
78   char *m_path_s_file;
79   char *m_path_so_file;
80 
81   /* Other files within the tempdir to be cleaned up:
82      - certain ahead-of-time compilation artifacts (.o and .exe files)
83      - dumpfiles that were requested via gcc_jit_context_enable_dump.  */
84   auto_vec <char *> m_tempfiles;
85 };
86 
87 } // namespace gcc::jit
88 
89 } // namespace gcc
90 
91 #endif /* JIT_TEMPDIR_H */
92